-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the Dashboard tab configurable, and implement new components #1142
Changes from 55 commits
946758a
d3034eb
5130653
16edaf2
b17ffb7
21adb20
84f8a51
b95c268
57d2298
18e99bb
2281c7e
c38c353
1f7a692
58cfb5f
8150e95
ed52e0f
d68354e
d4b3f51
abec657
5ab984c
5916fab
ab6fece
c9628e0
32336ea
1d99f59
c7fbc25
5a8759e
269faed
cc4d134
7e1c536
7510bf1
2e02fb7
f28bc23
18089bf
0642f06
e4ba6d0
6ff253f
34a99da
f71280f
637d496
66bbc62
6ef2adf
bbfbfa1
96549b3
114d15e
cbefbbd
c803973
b3e80df
663f86c
90b631c
a3570d6
084224e
8cf5fb0
f55faf1
abf2f3f
66464ca
a963381
3f08b6c
1bd1f48
e55e5ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,53 @@ | ||
import { DateTime } from 'luxon'; | ||
import { | ||
calculatePercentChange, | ||
formatDate, | ||
formatDateRangeOfDays, | ||
getLabelsForDay, | ||
getUniqueLabelsForDays, | ||
secondsToHours, | ||
secondsToMinutes, | ||
segmentDaysByWeeks, | ||
metricToValue, | ||
tsForDayOfMetricData, | ||
valueForFieldOnDay, | ||
generateSummaryFromData, | ||
isCustomLabels, | ||
isAllCustom, | ||
isOnFoot, | ||
} from '../js/metrics/metricsHelper'; | ||
import { | ||
DayOfClientMetricData, | ||
DayOfMetricData, | ||
DayOfServerMetricData, | ||
} from '../js/metrics/metricsTypes'; | ||
import { DayOfMetricData } from '../js/metrics/metricsTypes'; | ||
|
||
describe('metricsHelper', () => { | ||
describe('getUniqueLabelsForDays', () => { | ||
const days1 = [ | ||
{ label_a: 1, label_b: 2 }, | ||
{ label_c: 1, label_d: 3 }, | ||
] as any as DayOfServerMetricData[]; | ||
it("should return unique labels for days with 'label_*'", () => { | ||
{ mode_confirm_a: 1, mode_confirm_b: 2 }, | ||
{ mode_confirm_b: 1, mode_confirm_c: 3 }, | ||
{ mode_confirm_c: 1, mode_confirm_d: 3 }, | ||
] as any as DayOfMetricData[]; | ||
it("should return unique labels for days with 'mode_confirm_*'", () => { | ||
expect(getUniqueLabelsForDays(days1)).toEqual(['a', 'b', 'c', 'd']); | ||
}); | ||
|
||
const days2 = [ | ||
{ mode_a: 1, mode_b: 2 }, | ||
{ mode_c: 1, mode_d: 3 }, | ||
] as any as DayOfClientMetricData[]; | ||
it("should return unique labels for days with 'mode_*'", () => { | ||
expect(getUniqueLabelsForDays(days2)).toEqual(['a', 'b', 'c', 'd']); | ||
}); | ||
}); | ||
|
||
describe('getLabelsForDay', () => { | ||
const day1 = { label_a: 1, label_b: 2 } as any as DayOfServerMetricData; | ||
it("should return labels for a day with 'label_*'", () => { | ||
const day1 = { mode_confirm_a: 1, mode_confirm_b: 2 } as any as DayOfMetricData; | ||
it("should return labels for a day with 'mode_confirm_*'", () => { | ||
expect(getLabelsForDay(day1)).toEqual(['a', 'b']); | ||
}); | ||
}); | ||
|
||
const day2 = { mode_a: 1, mode_b: 2 } as any as DayOfClientMetricData; | ||
it("should return labels for a day with 'mode_*'", () => { | ||
expect(getLabelsForDay(day2)).toEqual(['a', 'b']); | ||
describe('secondsToMinutes', () => { | ||
it("should convert from seconds to minutes properly", () => { | ||
expect(secondsToMinutes(360)).toEqual(6); | ||
}); | ||
}); | ||
|
||
// secondsToMinutes | ||
|
||
// secondsToHours | ||
describe('secondsToHours', () => { | ||
it("should convert from seconds to hours properly", () => { | ||
expect(secondsToHours(3600)).toEqual(1); | ||
}); | ||
}); | ||
|
||
describe('segmentDaysByWeeks', () => { | ||
const days1 = [ | ||
|
@@ -55,7 +57,7 @@ describe('metricsHelper', () => { | |
{ date: '2021-01-08' }, | ||
{ date: '2021-01-09' }, | ||
{ date: '2021-01-10' }, | ||
] as any as DayOfClientMetricData[]; | ||
] as any as DayOfMetricData[]; | ||
|
||
it("should segment days with 'date' into weeks", () => { | ||
expect(segmentDaysByWeeks(days1, '2021-01-10')).toEqual([ | ||
|
@@ -70,59 +72,168 @@ describe('metricsHelper', () => { | |
[{ date: '2021-01-01' }, { date: '2021-01-02' }], | ||
]); | ||
}); | ||
|
||
const days2 = [ | ||
{ fmt_time: '2021-01-01T00:00:00Z' }, | ||
{ fmt_time: '2021-01-02T00:00:00Z' }, | ||
{ fmt_time: '2021-01-04T00:00:00Z' }, | ||
{ fmt_time: '2021-01-08T00:00:00Z' }, | ||
{ fmt_time: '2021-01-09T00:00:00Z' }, | ||
{ fmt_time: '2021-01-10T00:00:00Z' }, | ||
] as any as DayOfServerMetricData[]; | ||
it("should segment days with 'fmt_time' into weeks", () => { | ||
expect(segmentDaysByWeeks(days2, '2021-01-10')).toEqual([ | ||
// most recent week | ||
[ | ||
{ fmt_time: '2021-01-04T00:00:00Z' }, | ||
{ fmt_time: '2021-01-08T00:00:00Z' }, | ||
{ fmt_time: '2021-01-09T00:00:00Z' }, | ||
{ fmt_time: '2021-01-10T00:00:00Z' }, | ||
], | ||
// prior week | ||
[{ fmt_time: '2021-01-01T00:00:00Z' }, { fmt_time: '2021-01-02T00:00:00Z' }], | ||
]); | ||
}); | ||
Comment on lines
-73
to
-94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, please fix this test that was removed to get the tests to passs |
||
}); | ||
|
||
describe('formatDate', () => { | ||
const day1 = { date: '2021-01-01' } as any as DayOfClientMetricData; | ||
const day1 = { date: '2021-01-01' } as any as DayOfMetricData; | ||
it('should format date', () => { | ||
expect(formatDate(day1)).toEqual('1/1'); | ||
}); | ||
|
||
const day2 = { fmt_time: '2021-01-01T00:00:00Z' } as any as DayOfServerMetricData; | ||
it('should format date', () => { | ||
expect(formatDate(day2)).toEqual('1/1'); | ||
}); | ||
}); | ||
|
||
describe('formatDateRangeOfDays', () => { | ||
const days1 = [ | ||
{ date: '2021-01-01' }, | ||
{ date: '2021-01-02' }, | ||
{ date: '2021-01-04' }, | ||
] as any as DayOfClientMetricData[]; | ||
] as any as DayOfMetricData[]; | ||
it('should format date range for days with date', () => { | ||
expect(formatDateRangeOfDays(days1)).toEqual('1/1 - 1/4'); | ||
}); | ||
}); | ||
|
||
describe('metricToValue', () => { | ||
const metric = { | ||
walking: 10, | ||
nUsers: 5, | ||
}; | ||
it('returns correct value for user population', () => { | ||
const result = metricToValue('user', metric, 'walking'); | ||
expect(result).toBe(10); | ||
}); | ||
|
||
it('returns correct value for aggregate population', () => { | ||
const result = metricToValue('aggregate', metric, 'walking'); | ||
expect(result).toBe(2); | ||
}); | ||
}); | ||
|
||
describe('isOnFoot', () => { | ||
it('returns true for on foot mode', () => { | ||
const result = isOnFoot('WALKING'); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false for non on foot mode', () => { | ||
const result = isOnFoot('DRIVING'); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('calculatePercentChange', () => { | ||
it('calculates percent change correctly for low and high values', () => { | ||
const pastWeekRange = { low: 10, high: 30 }; | ||
const previousWeekRange = { low: 5, high: 10 }; | ||
const result = calculatePercentChange(pastWeekRange, previousWeekRange); | ||
expect(result.low).toBe(100); | ||
expect(result.high).toBe(200); | ||
}); | ||
}); | ||
|
||
describe('tsForDayOfMetricData', () => { | ||
const mockDay = { | ||
date: '2024-05-28T12:00:00Z', | ||
nUsers: 10, | ||
}; | ||
let _datesTsCache; | ||
beforeEach(() => { | ||
_datesTsCache = {}; | ||
}); | ||
|
||
it('calculates timestamp for a given day', () => { | ||
const expectedTimestamp = DateTime.fromISO(mockDay.date).toSeconds(); | ||
const result = tsForDayOfMetricData(mockDay); | ||
expect(result).toBe(expectedTimestamp); | ||
}); | ||
|
||
it('caches the timestamp for subsequent calls with the same day', () => { | ||
const firstResult = tsForDayOfMetricData(mockDay); | ||
const secondResult = tsForDayOfMetricData(mockDay); | ||
expect(secondResult).toBe(firstResult); | ||
}); | ||
}); | ||
|
||
describe('valueForFieldOnDay', () => { | ||
const mockDay = { | ||
date: '2024-05-28T12:00:00Z', | ||
nUsers: 10, | ||
field_key: 'example_value' | ||
}; | ||
|
||
it('returns the value for a specified field and key', () => { | ||
const result = valueForFieldOnDay(mockDay, 'field', 'key'); | ||
expect(result).toBe('example_value'); | ||
}); | ||
}); | ||
|
||
const days2 = [ | ||
{ fmt_time: '2021-01-01T00:00:00Z' }, | ||
{ fmt_time: '2021-01-02T00:00:00Z' }, | ||
{ fmt_time: '2021-01-04T00:00:00Z' }, | ||
] as any as DayOfServerMetricData[]; | ||
it('should format date range for days with fmt_time', () => { | ||
expect(formatDateRangeOfDays(days2)).toEqual('1/1 - 1/4'); | ||
describe('generateSummaryFromData', () => { | ||
const modeMap = [ | ||
{ key: 'mode1', values: [['value1', 10], ['value2', 20]] }, | ||
{ key: 'mode2', values: [['value3', 30], ['value4', 40]] }, | ||
]; | ||
it('returns summary with sum for non-speed metric', () => { | ||
const metric = 'some_metric'; | ||
const expectedResult = [ | ||
{ key: 'mode1', values: 30 }, | ||
{ key: 'mode2', values: 70 }, | ||
]; | ||
const result = generateSummaryFromData(modeMap, metric); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it('returns summary with average for speed metric', () => { | ||
const metric = 'mean_speed'; | ||
const expectedResult = [ | ||
{ key: 'mode1', values: 15 }, | ||
{ key: 'mode2', values: 35 }, | ||
]; | ||
const result = generateSummaryFromData(modeMap, metric); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('isCustomLabels', () => { | ||
const modeMap = [ | ||
{ key: 'label_mode1', values: [['value1', 10], ['value2', 20]] }, | ||
{ key: 'label_mode2', values: [['value3', 30], ['value4', 40]] }, | ||
]; | ||
|
||
it('returns true for all custom labels', () => { | ||
const result = isCustomLabels(modeMap); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns true for all sensed labels', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for sensed labels, we expect |
||
const result = isCustomLabels(modeMap); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false for mixed custom and sensed labels', () => { | ||
const result = isCustomLabels(modeMap); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isAllCustom', () => { | ||
it('returns true when all keys are custom', () => { | ||
const isSensedKeys = [false, false, false]; | ||
const isCustomKeys = [true, true, true]; | ||
const result = isAllCustom(isSensedKeys, isCustomKeys); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false when all keys are sensed', () => { | ||
const isSensedKeys = [true, true, true]; | ||
const isCustomKeys = [false, false, false]; | ||
const result = isAllCustom(isSensedKeys, isCustomKeys); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('returns undefined for mixed custom and sensed keys', () => { | ||
const isSensedKeys = [true, false, true]; | ||
const isCustomKeys = [false, true, false]; | ||
const result = isAllCustom(isSensedKeys, isCustomKeys); | ||
expect(result).toBe(undefined); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jiji14 please replace this with a test that actually have duplicate labels (see my comment in #1138 (comment))