-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1098 from Abby-Wheelis/metrics-services-rewrite
📊 🦶 Metrics Services Rewrites
- Loading branch information
Showing
20 changed files
with
815 additions
and
794 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
{ | ||
"MODE": [ | ||
{ | ||
"value": "walk", | ||
"baseMode": "WALKING", | ||
"met_equivalent": "WALKING", | ||
"kgCo2PerKm": 0 | ||
}, | ||
{ | ||
"value": "e-bike", | ||
"baseMode": "E_BIKE", | ||
"met": { | ||
"ALL": { | ||
"range": [0, -1], | ||
"mets": 4.9 | ||
} | ||
}, | ||
"kgCo2PerKm": 0.00728 | ||
}, | ||
{ | ||
"value": "bike", | ||
"baseMode": "BICYCLING", | ||
"met_equivalent": "BICYCLING", | ||
"kgCo2PerKm": 0 | ||
}, | ||
{ | ||
"value": "bikeshare", | ||
"baseMode": "BICYCLING", | ||
"met_equivalent": "BICYCLING", | ||
"kgCo2PerKm": 0 | ||
}, | ||
{ | ||
"value": "scootershare", | ||
"baseMode": "E_SCOOTER", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.00894 | ||
}, | ||
{ | ||
"value": "drove_alone", | ||
"baseMode": "CAR", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.22031 | ||
}, | ||
{ | ||
"value": "shared_ride", | ||
"baseMode": "CAR", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.11015 | ||
}, | ||
{ | ||
"value": "hybrid_drove_alone", | ||
"baseMode": "CAR", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.127 | ||
}, | ||
{ | ||
"value": "hybrid_shared_ride", | ||
"baseMode": "CAR", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.0635 | ||
}, | ||
{ | ||
"value": "e_car_drove_alone", | ||
"baseMode": "E_CAR", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.08216 | ||
}, | ||
{ | ||
"value": "e_car_shared_ride", | ||
"baseMode": "E_CAR", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.04108 | ||
}, | ||
{ | ||
"value": "taxi", | ||
"baseMode": "TAXI", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.30741 | ||
}, | ||
{ | ||
"value": "bus", | ||
"baseMode": "BUS", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.20727 | ||
}, | ||
{ | ||
"value": "train", | ||
"baseMode": "TRAIN", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.12256 | ||
}, | ||
{ | ||
"value": "free_shuttle", | ||
"baseMode": "BUS", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.20727 | ||
}, | ||
{ | ||
"value": "air", | ||
"baseMode": "AIR", | ||
"met_equivalent": "IN_VEHICLE", | ||
"kgCo2PerKm": 0.09975 | ||
}, | ||
{ | ||
"value": "not_a_trip", | ||
"baseMode": "UNKNOWN", | ||
"met_equivalent": "UNKNOWN", | ||
"kgCo2PerKm": 0 | ||
}, | ||
{ | ||
"value": "other", | ||
"baseMode": "OTHER", | ||
"met_equivalent": "UNKNOWN", | ||
"kgCo2PerKm": 0 | ||
} | ||
], | ||
"PURPOSE": [ | ||
{ | ||
"value": "home" | ||
}, | ||
{ | ||
"value": "work" | ||
}, | ||
{ | ||
"value": "at_work" | ||
}, | ||
{ | ||
"value": "school" | ||
}, | ||
{ | ||
"value": "transit_transfer" | ||
}, | ||
{ | ||
"value": "shopping" | ||
}, | ||
{ | ||
"value": "meal" | ||
}, | ||
{ | ||
"value": "pick_drop_person" | ||
}, | ||
{ | ||
"value": "pick_drop_item" | ||
}, | ||
{ | ||
"value": "personal_med" | ||
}, | ||
{ | ||
"value": "access_recreation" | ||
}, | ||
{ | ||
"value": "exercise" | ||
}, | ||
{ | ||
"value": "entertainment" | ||
}, | ||
{ | ||
"value": "religious" | ||
}, | ||
{ | ||
"value": "other" | ||
} | ||
], | ||
"REPLACED_MODE": [ | ||
{ | ||
"value": "no_travel" | ||
}, | ||
{ | ||
"value": "walk" | ||
}, | ||
{ | ||
"value": "bike" | ||
}, | ||
{ | ||
"value": "bikeshare" | ||
}, | ||
{ | ||
"value": "scootershare" | ||
}, | ||
{ | ||
"value": "drove_alone" | ||
}, | ||
{ | ||
"value": "shared_ride" | ||
}, | ||
{ | ||
"value": "e_car_drove_alone" | ||
}, | ||
{ | ||
"value": "e_car_shared_ride" | ||
}, | ||
{ | ||
"value": "taxi" | ||
}, | ||
{ | ||
"value": "bus" | ||
}, | ||
{ | ||
"value": "train" | ||
}, | ||
{ | ||
"value": "free_shuttle" | ||
}, | ||
{ | ||
"value": "other" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { getConfig } from '../js/config/dynamicConfig'; | ||
import { | ||
getCustomFootprint, | ||
getCustomMETs, | ||
initCustomDatasetHelper, | ||
} from '../js/metrics/customMetricsHelper'; | ||
import { mockBEMUserCache } from '../__mocks__/cordovaMocks'; | ||
import { mockLogger } from '../__mocks__/globalMocks'; | ||
import fakeLabels from '../__mocks__/fakeLabels.json'; | ||
|
||
mockBEMUserCache(); | ||
mockLogger(); | ||
|
||
global.fetch = (url: string) => | ||
new Promise((rs, rj) => { | ||
setTimeout(() => | ||
rs({ | ||
text: () => | ||
new Promise((rs, rj) => { | ||
let myJSON = JSON.stringify(fakeLabels); | ||
setTimeout(() => rs(myJSON), 100); | ||
}), | ||
}), | ||
); | ||
}) as any; | ||
|
||
it('gets the custom mets', async () => { | ||
const appConfig = await getConfig(); | ||
await initCustomDatasetHelper(appConfig); | ||
expect(getCustomMETs()).toMatchObject({ | ||
walk: expect.any(Object), | ||
bike: expect.any(Object), | ||
bikeshare: expect.any(Object), | ||
'e-bike': expect.any(Object), | ||
scootershare: expect.any(Object), | ||
drove_alone: expect.any(Object), | ||
}); | ||
}); | ||
|
||
it('gets the custom footprint', async () => { | ||
const appConfig = await getConfig(); | ||
await initCustomDatasetHelper(appConfig); | ||
expect(getCustomFootprint()).toMatchObject({ | ||
walk: expect.any(Number), | ||
bike: expect.any(Number), | ||
bikeshare: expect.any(Number), | ||
'e-bike': expect.any(Number), | ||
scootershare: expect.any(Number), | ||
drove_alone: expect.any(Number), | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { initCustomDatasetHelper } from '../js/metrics/customMetricsHelper'; | ||
import { | ||
clearHighestFootprint, | ||
getFootprintForMetrics, | ||
getHighestFootprint, | ||
getHighestFootprintForDistance, | ||
} from '../js/metrics/footprintHelper'; | ||
import { getConfig } from '../js/config/dynamicConfig'; | ||
import { mockBEMUserCache } from '../__mocks__/cordovaMocks'; | ||
import { mockLogger } from '../__mocks__/globalMocks'; | ||
import fakeLabels from '../__mocks__/fakeLabels.json'; | ||
|
||
mockBEMUserCache(); | ||
mockLogger(); | ||
|
||
global.fetch = (url: string) => | ||
new Promise((rs, rj) => { | ||
setTimeout(() => | ||
rs({ | ||
text: () => | ||
new Promise((rs, rj) => { | ||
let myJSON = JSON.stringify(fakeLabels); | ||
setTimeout(() => rs(myJSON), 100); | ||
}), | ||
}), | ||
); | ||
}) as any; | ||
|
||
beforeEach(() => { | ||
clearHighestFootprint(); | ||
}); | ||
|
||
const custom_metrics = [ | ||
{ key: 'walk', values: 3000 }, | ||
{ key: 'bike', values: 6500 }, | ||
{ key: 'drove_alone', values: 10000 }, | ||
{ key: 'scootershare', values: 25000 }, | ||
{ key: 'unicycle', values: 5000 }, | ||
]; | ||
|
||
it('gets footprint for metrics (custom, fallback 0)', async () => { | ||
const appConfig = await getConfig(); | ||
await initCustomDatasetHelper(appConfig); | ||
expect(getFootprintForMetrics(custom_metrics, 0)).toBe(2.4266); | ||
}); | ||
|
||
it('gets footprint for metrics (custom, fallback 0.1)', async () => { | ||
const appConfig = await getConfig(); | ||
await initCustomDatasetHelper(appConfig); | ||
expect(getFootprintForMetrics(custom_metrics, 0.1)).toBe(2.4266 + 0.5); | ||
}); | ||
|
||
it('gets the highest footprint from the dataset, custom', async () => { | ||
const appConfig = await getConfig(); | ||
await initCustomDatasetHelper(appConfig); | ||
expect(getHighestFootprint()).toBe(0.30741); | ||
}); | ||
|
||
it('gets the highest footprint for distance, custom', async () => { | ||
const appConfig = await getConfig(); | ||
await initCustomDatasetHelper(appConfig); | ||
expect(getHighestFootprintForDistance(12345)).toBe(0.30741 * (12345 / 1000)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getMet } from '../js/metrics/metHelper'; | ||
import { mockBEMUserCache } from '../__mocks__/cordovaMocks'; | ||
import { mockLogger } from '../__mocks__/globalMocks'; | ||
import fakeLabels from '../__mocks__/fakeLabels.json'; | ||
import { getConfig } from '../js/config/dynamicConfig'; | ||
import { initCustomDatasetHelper } from '../js/metrics/customMetricsHelper'; | ||
|
||
mockBEMUserCache(); | ||
mockLogger(); | ||
|
||
global.fetch = (url: string) => | ||
new Promise((rs, rj) => { | ||
setTimeout(() => | ||
rs({ | ||
text: () => | ||
new Promise((rs, rj) => { | ||
let myJSON = JSON.stringify(fakeLabels); | ||
setTimeout(() => rs(myJSON), 100); | ||
}), | ||
}), | ||
); | ||
}) as any; | ||
|
||
it('gets met for mode and speed', () => { | ||
expect(getMet('WALKING', 1.47523, 0)).toBe(4.3); | ||
expect(getMet('BICYCLING', 4.5, 0)).toBe(6.8); | ||
expect(getMet('UNICYCLE', 100, 0)).toBe(0); | ||
expect(getMet('CAR', 25, 1)).toBe(0); | ||
}); | ||
|
||
it('gets custom met for mode and speed', async () => { | ||
const appConfig = await getConfig(); | ||
await initCustomDatasetHelper(appConfig); | ||
expect(getMet('walk', 1.47523, 0)).toBe(4.3); | ||
expect(getMet('bike', 4.5, 0)).toBe(6.8); | ||
expect(getMet('unicycle', 100, 0)).toBe(0); | ||
expect(getMet('drove_alone', 25, 1)).toBe(0); | ||
expect(getMet('e-bike', 6, 1)).toBe(4.9); | ||
expect(getMet('e-bike', 12, 1)).toBe(4.9); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.