Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(dashboard): previous-period change measures drops correctly

Carl-Gerhard Lindesvärd6f9513de9151
diff --git a/apps/start/src/utils/previous-metric.test.ts b/apps/start/src/utils/previous-metric.test.ts
index 76a37f716..96af1464e 100644
--- a/apps/start/src/utils/previous-metric.test.ts
+++ b/apps/start/src/utils/previous-metric.test.ts
@@ -19,16 +19,16 @@ describe('getPreviousMetric', () => {
});
});
- test('reports a drop as negative, with the ratio the other way round', () => {
+ test('reports a drop as negative, relative to the previous value', () => {
expect(getPreviousMetric(50, 100)).toEqual({
- diff: 100,
+ diff: 50,
state: 'negative',
value: 100,
});
- expect(getPreviousMetric(3, 7)).toEqual({
- diff: 133.3,
+ expect(getPreviousMetric(4, 10)).toEqual({
+ diff: 60,
state: 'negative',
- value: 7,
+ value: 10,
});
});
@@ -40,14 +40,17 @@ describe('getPreviousMetric', () => {
});
});
- test('a zero on either side keeps the direction but drops the diff', () => {
+ test('a previous value of zero keeps the direction but drops the diff', () => {
expect(getPreviousMetric(5, 0)).toEqual({
diff: null,
state: 'positive',
value: 0,
});
+ });
+
+ test('a drop to zero is a full drop', () => {
expect(getPreviousMetric(0, 5)).toEqual({
- diff: null,
+ diff: 100,
state: 'negative',
value: 5,
});
diff --git a/apps/start/src/utils/previous-metric.ts b/apps/start/src/utils/previous-metric.ts
index 17647bae9..db3085345 100644
--- a/apps/start/src/utils/previous-metric.ts
+++ b/apps/start/src/utils/previous-metric.ts
@@ -19,13 +19,7 @@ export function getPreviousMetric(
}
const diff = round(
- ((current > previous
- ? current / previous
- : current < previous
- ? previous / current
- : 0) -
- 1) *
- 100,
+ (Math.abs(current - previous) / Math.abs(previous)) * 100,
1
);
diff --git a/packages/core/src/modules/report/src/previous-metric.ts b/packages/core/src/modules/report/src/previous-metric.ts
index aaeba2997..271a27afa 100644
--- a/packages/core/src/modules/report/src/previous-metric.ts
+++ b/packages/core/src/modules/report/src/previous-metric.ts
@@ -19,13 +19,7 @@ export function getPreviousMetric(
}
const diff = round(
- ((current > previous
- ? current / previous
- : current < previous
- ? previous / current
- : 0) -
- 1) *
- PERCENT,
+ (Math.abs(current - previous) / Math.abs(previous)) * PERCENT,
DIFF_DECIMALS
);