better previous indicator and funnel

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-08-20 23:27:04 +02:00
parent a6b3d341c1
commit 96326ad193
8 changed files with 231 additions and 213 deletions

View File

@@ -0,0 +1,39 @@
import { isNil } from 'ramda';
import type { PreviousValue } from '@openpanel/validation';
import { round } from './math';
export function getPreviousMetric(
current: number,
previous: number | null | undefined
): PreviousValue {
if (isNil(previous)) {
return undefined;
}
const diff = round(
((current > previous
? current / previous
: current < previous
? previous / current
: 0) -
1) *
100,
1
);
return {
diff:
Number.isNaN(diff) || !Number.isFinite(diff) || current === previous
? null
: diff,
state:
current > previous
? 'positive'
: current < previous
? 'negative'
: 'neutral',
value: previous,
};
}