diff --git a/src/handlers/metricTypes/stats.ts b/src/handlers/metricTypes/stats.ts index bd392c5..7e235f2 100644 --- a/src/handlers/metricTypes/stats.ts +++ b/src/handlers/metricTypes/stats.ts @@ -1,5 +1,5 @@ import { MetricSchema, Stats } from "../../types"; -import { calcMedian } from "../../utils"; +import { calcMedian, calculateAverage } from "../../utils"; export class GraphiteStats implements Stats { private count = 0; @@ -11,13 +11,9 @@ export class GraphiteStats implements Stats { public readonly tags?: Record ) {} - private calculateAverage(values: number[]): number { - return values.reduce((a, b) => a + b, 0) / values.length; - } - get(): MetricSchema[] { const median = calcMedian(this.values); - const avg = this.calculateAverage(this.values); + const avg = calculateAverage(this.values); const time = Date.now(); diff --git a/src/utils.ts b/src/utils.ts index 6b83fdc..acf4ed5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,3 +8,9 @@ export const calcMedian = (arr: number[]): number | undefined => { const mid = Math.floor(s.length / 2); return s.length % 2 === 0 ? (s[mid - 1] + s[mid]) / 2 : s[mid]; }; + +export const calculateAverage = (arr: number[]): number => { + return arr.length + ? arr.reduce((a, b) => a + b, 0) / arr.length + : 0; +};