@nodeswork/utils
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -13,2 +13,7 @@ import { MetricsData, MetricsDimensions, MetricsValue, Operator } from './def'; | ||
projectMetricsData(data: MetricsData, options: MetricsProjectOptions): MetricsData; | ||
filterMetricsDatasByValue(data: MetricsData[], filter: MetricsDataValueFilter): MetricsData[]; | ||
getDimensions(data: MetricsData, strict?: boolean): MetricsDimensions[]; | ||
getMetricsNames(data: MetricsData, strict?: boolean): string[]; | ||
filterMetricsDatas(data: MetricsData[], filter: MetricsDataFilter): MetricsData[]; | ||
filterMetricsData(data: MetricsData, filter: MetricsDataFilter): MetricsData; | ||
mergeMetricsDataByTimeGranularity(datas: MetricsData[], granularityInSecond: number): MetricsData[]; | ||
@@ -30,2 +35,13 @@ mergeMetricsDataBy(datas: MetricsData[], func: (data: MetricsData) => string): MetricsData[]; | ||
} | ||
export interface MetricsDataFilter { | ||
(dimension: MetricsDimensions, name: string, value: MetricsValue<any>, data?: MetricsData): boolean; | ||
} | ||
export interface MetricsDataValueFilter { | ||
dimensions: Array<{ | ||
name: string; | ||
selectValues: any[]; | ||
omitValues: any[]; | ||
}>; | ||
metrics: string[]; | ||
} | ||
export declare const operator: MetricsOperator; |
@@ -115,2 +115,63 @@ "use strict"; | ||
} | ||
filterMetricsDatasByValue(data, filter) { | ||
return this.filterMetricsDatas(data, (dimensions, name, value) => { | ||
if (filter.metrics.indexOf(name) === -1) { | ||
return false; | ||
} | ||
for (const dFilter of filter.dimensions) { | ||
if (dFilter.selectValues.length > 0 && | ||
dFilter.selectValues.indexOf(dimensions[dFilter.name]) === -1) { | ||
return false; | ||
} | ||
if (dFilter.omitValues.length > 0 && | ||
dFilter.omitValues.indexOf(dimensions[dFilter.name]) >= 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
} | ||
getDimensions(data, strict = false) { | ||
if (!strict) { | ||
return Object.values(data.dimensions); | ||
} | ||
else { | ||
const metrics = this.getMetricsNames(data); | ||
return _.chain(metrics) | ||
.map((name) => Object.keys(data.metrics[name])) | ||
.flatten() | ||
.uniq() | ||
.map((dhash) => data.dimensions[dhash]) | ||
.value(); | ||
} | ||
} | ||
getMetricsNames(data, strict = false) { | ||
if (!strict) { | ||
return Object.keys(data.metrics); | ||
} | ||
else { | ||
return _.filter(Object.keys(data.metrics), (n) => Object.keys(data.metrics[n]).length > 0); | ||
} | ||
} | ||
filterMetricsDatas(data, filter) { | ||
return _.filter(data, (d) => { | ||
d = this.filterMetricsData(d, filter); | ||
return this.getMetricsNames(d, true).length > 0; | ||
}); | ||
} | ||
filterMetricsData(data, filter) { | ||
const dimensions = {}; | ||
const metrics = _.mapObject(data.metrics, (metrics, name) => { | ||
const result = {}; | ||
_.each(metrics, (value, dhash) => { | ||
const d = data.dimensions[dhash]; | ||
if (filter(d, name, value, data)) { | ||
result[dhash] = value; | ||
dimensions[dhash] = d; | ||
} | ||
}); | ||
return result; | ||
}); | ||
return _.extend({}, data, { dimensions, metrics }); | ||
} | ||
mergeMetricsDataByTimeGranularity(datas, granularityInSecond) { | ||
@@ -117,0 +178,0 @@ const granularity = granularityInSecond * 1000; |
{ | ||
"name": "@nodeswork/utils", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"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
54926
1036