From: Jérôme Benoit Date: Wed, 13 Mar 2024 16:35:16 +0000 (+0100) Subject: refactor: use more ramdba helpers X-Git-Tag: v1.3.1~47 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c17a8d29b2177a087f7fea1db4ada2b3795a7a31;p=e-mobility-charging-stations-simulator.git refactor: use more ramdba helpers Signed-off-by: Jérôme Benoit --- diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 59d697d1..6363be1c 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -5,6 +5,7 @@ import type { URL } from 'node:url' import { parentPort } from 'node:worker_threads' import { secondsToMilliseconds } from 'date-fns' +import { mean, median } from 'rambda' import { BaseError } from '../exception/index.js' import { @@ -20,7 +21,6 @@ import { type TimestampedData } from '../types/index.js' import { - average, buildPerformanceStatisticsMessage, CircularArray, Configuration, @@ -32,7 +32,6 @@ import { logger, logPrefix, max, - median, min, nthPercentile, stdDeviation @@ -293,8 +292,7 @@ export class PerformanceStatistics { this.statistics.statisticsData.get(entry.name)!.measurementTimeSeries! ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.statistics.statisticsData.get(entry.name)!.avgTimeMeasurement = - average(timeMeasurementValues) + this.statistics.statisticsData.get(entry.name)!.avgTimeMeasurement = mean(timeMeasurementValues) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.medTimeMeasurement = median(timeMeasurementValues) diff --git a/src/utils/StatisticUtils.ts b/src/utils/StatisticUtils.ts index 9ca4398c..4125ab06 100644 --- a/src/utils/StatisticUtils.ts +++ b/src/utils/StatisticUtils.ts @@ -1,37 +1,10 @@ -/** - * Computes the average of the given data set. - * - * @param dataSet - Data set. - * @returns The average of the given data set. - * @internal - */ -export const average = (dataSet: number[]): number => { - if (Array.isArray(dataSet) && dataSet.length === 0) { - return 0 - } else if (Array.isArray(dataSet) && dataSet.length === 1) { - return dataSet[0] - } - return dataSet.reduce((accumulator, nb) => accumulator + nb, 0) / dataSet.length -} +import { mean } from 'rambda' -/** - * Computes the median of the given data set. - * - * @param dataSet - Data set. - * @returns The median of the given data set. - * @internal - */ -export const median = (dataSet: number[]): number => { - if (Array.isArray(dataSet) && dataSet.length === 0) { - return 0 - } else if (Array.isArray(dataSet) && dataSet.length === 1) { - return dataSet[0] - } - const sortedDataSet = dataSet.slice().sort((a, b) => a - b) - return ( - (sortedDataSet[(sortedDataSet.length - 1) >> 1] + sortedDataSet[sortedDataSet.length >> 1]) / 2 - ) -} +export const min = (...args: number[]): number => + args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) + +export const max = (...args: number[]): number => + args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) // TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree export const nthPercentile = (dataSet: number[], percentile: number): number => { @@ -70,10 +43,7 @@ export const nthPercentile = (dataSet: number[], percentile: number): number => * @see https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation * @internal */ -export const stdDeviation = ( - dataSet: number[], - dataSetAverage: number = average(dataSet) -): number => { +export const stdDeviation = (dataSet: number[], dataSetAverage: number = mean(dataSet)): number => { if (Array.isArray(dataSet) && (dataSet.length === 0 || dataSet.length === 1)) { return 0 } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index fc3cf9a9..51ea3a7e 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -346,12 +346,6 @@ export const isArraySorted = (array: T[], compareFn: (a: T, b: T) => number): return true } -export const min = (...args: number[]): number => - args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) - -export const max = (...args: number[]): number => - args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) - export const throwErrorInNextTick = (error: Error): void => { nextTick(() => { throw error diff --git a/src/utils/index.ts b/src/utils/index.ts index 726a69db..549b8347 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -27,7 +27,7 @@ export { buildStoppedMessage, buildUpdatedMessage } from './MessageChannelUtils.js' -export { average, median, nthPercentile, stdDeviation } from './StatisticUtils.js' +export { max, min, nthPercentile, stdDeviation } from './StatisticUtils.js' export { clone, convertToBoolean, @@ -50,8 +50,6 @@ export { isValidDate, JSONStringify, logPrefix, - max, - min, roundTo, secureRandom, sleep, diff --git a/tests/utils/StatisticUtils.test.ts b/tests/utils/StatisticUtils.test.ts index 78af993c..b54cdee8 100644 --- a/tests/utils/StatisticUtils.test.ts +++ b/tests/utils/StatisticUtils.test.ts @@ -2,21 +2,23 @@ import { describe, it } from 'node:test' import { expect } from 'expect' -import { average, median, nthPercentile, stdDeviation } from '../../src/utils/StatisticUtils.js' +import { max, min, nthPercentile, stdDeviation } from '../../src/utils/StatisticUtils.js' await describe('StatisticUtils test suite', async () => { - await it('Verify average()', () => { - expect(average([])).toBe(0) - expect(average([0.08])).toBe(0.08) - expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.1642857142857146) - expect(average([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.8533333333333335) + await it('Verify min()', () => { + expect(min()).toBe(Infinity) + expect(min(0, 1)).toBe(0) + expect(min(1, 0)).toBe(0) + expect(min(0, -1)).toBe(-1) + expect(min(-1, 0)).toBe(-1) }) - await it('Verify median()', () => { - expect(median([])).toBe(0) - expect(median([0.08])).toBe(0.08) - expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(3.05) - expect(median([0.25, 4.75, 3.05, 6.04, 1.01, 2.02])).toBe(2.535) + await it('Verify max()', () => { + expect(max()).toBe(-Infinity) + expect(max(0, 1)).toBe(1) + expect(max(1, 0)).toBe(1) + expect(max(0, -1)).toBe(0) + expect(max(-1, 0)).toBe(0) }) await it('Verify nthPercentile()', () => { diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 630831a8..a8676529 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -25,8 +25,6 @@ import { isNotEmptyString, isObject, isValidDate, - max, - min, roundTo, secureRandom, sleep, @@ -420,20 +418,4 @@ await describe('Utils test suite', async () => { expect(isArraySorted([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false) expect(isArraySorted([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false) }) - - await it('Verify min()', () => { - expect(min()).toBe(Infinity) - expect(min(0, 1)).toBe(0) - expect(min(1, 0)).toBe(0) - expect(min(0, -1)).toBe(-1) - expect(min(-1, 0)).toBe(-1) - }) - - await it('Verify max()', () => { - expect(max()).toBe(-Infinity) - expect(max(0, 1)).toBe(1) - expect(max(1, 0)).toBe(1) - expect(max(0, -1)).toBe(0) - expect(max(-1, 0)).toBe(0) - }) })