@nodeswork/utils
Advanced tools
Comparing version 0.0.15 to 0.1.0
@@ -21,4 +21,8 @@ export interface MetricsValue<V> { | ||
export interface MetricsData { | ||
timerange: { | ||
start: number; | ||
end: number; | ||
}; | ||
dimensions: Dimensions; | ||
metrics: Metrics; | ||
} |
@@ -7,7 +7,15 @@ import { MetricsData, MetricsDimensions, MetricsValue, Operator } from './def'; | ||
operate<V>(values: MetricsValue<V>[]): MetricsValue<V>; | ||
updateMetricsData(dimensions: MetricsDimensions, name: string, value: MetricsValue<any>, target?: MetricsData): MetricsData; | ||
updateMetricsData<T>(options: UpdateMetricsDataOptions<T>, target?: MetricsData): MetricsData; | ||
projectMetricsData(data: MetricsData, options: MetricsProjectOptions): MetricsData; | ||
mergeMetricsDataByTimeGranularity(datas: MetricsData[], granularityInSecond: number): MetricsData[]; | ||
mergeMetricsDataBy(datas: MetricsData[], func: (data: MetricsData) => string): MetricsData[]; | ||
mergeMetricsData(...datas: (MetricsData | MetricsData[])[]): MetricsData; | ||
private mergeMetricsDataInternal(data, low, high); | ||
} | ||
export interface UpdateMetricsDataOptions<T> { | ||
dimensions?: MetricsDimensions; | ||
name: string; | ||
value: MetricsValue<T>; | ||
ts?: number; | ||
} | ||
export interface MetricsProjectOptions { | ||
@@ -14,0 +22,0 @@ metrics?: string[]; |
@@ -41,6 +41,11 @@ "use strict"; | ||
} | ||
updateMetricsData(dimensions = {}, name, value, target) { | ||
updateMetricsData(options, target) { | ||
const dimensions = options.dimensions || {}; | ||
const dhash = utils_1.hashDimensions(dimensions); | ||
if (target == null) { | ||
target = { | ||
const ts = options.ts || Date.now(), target = { | ||
timerange: { | ||
start: ts, | ||
end: ts, | ||
}, | ||
dimensions: {}, | ||
@@ -51,12 +56,16 @@ metrics: {}, | ||
target.dimensions[dhash] = dimensions; | ||
if (target.metrics[name] == null) { | ||
target.metrics[name] = {}; | ||
if (target.metrics[options.name] == null) { | ||
target.metrics[options.name] = {}; | ||
} | ||
const targetMetrics = target.metrics[name]; | ||
const targetMetrics = target.metrics[options.name]; | ||
if (targetMetrics[dhash] == null) { | ||
targetMetrics[dhash] = value; | ||
targetMetrics[dhash] = options.value; | ||
} | ||
else { | ||
targetMetrics[dhash] = this.operate([targetMetrics[dhash], value]); | ||
targetMetrics[dhash] = this.operate([targetMetrics[dhash], options.value]); | ||
} | ||
if (options.ts) { | ||
target.timerange.start = Math.min(target.timerange.start, options.ts); | ||
target.timerange.end = Math.max(target.timerange.end, options.ts); | ||
} | ||
return target; | ||
@@ -78,2 +87,3 @@ } | ||
return { | ||
timerange: data.timerange, | ||
dimensions: _.pick(data.dimensions, dhashes), | ||
@@ -83,7 +93,15 @@ metrics: _.pick(data.metrics, metricsNames), | ||
} | ||
const result = { dimensions: {}, metrics: {} }; | ||
const result = { | ||
timerange: data.timerange, | ||
dimensions: {}, | ||
metrics: {}, | ||
}; | ||
for (const mName of metricsNames) { | ||
_.each(data.metrics[mName], (metricsValue, dhash) => { | ||
const newDimensions = _.pick(data.dimensions[dhash], dimensionNames); | ||
this.updateMetricsData(newDimensions, mName, metricsValue, result); | ||
this.updateMetricsData({ | ||
dimensions: newDimensions, | ||
name: mName, | ||
value: metricsValue, | ||
}, result); | ||
}); | ||
@@ -93,2 +111,21 @@ } | ||
} | ||
mergeMetricsDataByTimeGranularity(datas, granularityInSecond) { | ||
const granularity = granularityInSecond * 1000; | ||
const func = (data) => { | ||
return (data.timerange.start / granularity).toString(); | ||
}; | ||
const result = this.mergeMetricsDataBy(datas, func); | ||
for (const data of result) { | ||
data.timerange.start = Math.floor(data.timerange.start / granularity) * granularity; | ||
data.timerange.end = Math.floor(data.timerange.start / granularity) * granularity + granularity; | ||
} | ||
return result; | ||
} | ||
mergeMetricsDataBy(datas, func) { | ||
const grouped = _.groupBy(datas, func); | ||
const result = _.map(grouped, (datas) => { | ||
return this.mergeMetricsData(datas); | ||
}); | ||
return result; | ||
} | ||
mergeMetricsData(...datas) { | ||
@@ -125,2 +162,6 @@ const flattenData = _.filter(_.flatten(datas), (x) => x != null); | ||
return { | ||
timerange: { | ||
start: Math.min(left.timerange.start, right.timerange.start), | ||
end: Math.max(left.timerange.end, right.timerange.end), | ||
}, | ||
dimensions: _.extend({}, left.dimensions, right.dimensions), | ||
@@ -127,0 +168,0 @@ metrics, |
{ | ||
"name": "@nodeswork/utils", | ||
"version": "0.0.15", | ||
"version": "0.1.0", | ||
"description": "Utilities used across nodeswork repos.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50967
930