Skip to content

Commit

Permalink
Merge pull request #1098 from Abby-Wheelis/metrics-services-rewrite
Browse files Browse the repository at this point in the history
📊 🦶 Metrics Services Rewrites
  • Loading branch information
shankari authored Dec 4, 2023
2 parents 9a321b7 + 1006165 commit c1f6dc3
Show file tree
Hide file tree
Showing 20 changed files with 815 additions and 794 deletions.
208 changes: 208 additions & 0 deletions www/__mocks__/fakeLabels.json
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"
}
]
}
51 changes: 51 additions & 0 deletions www/__tests__/customMetricsHelper.test.ts
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),
});
});
63 changes: 63 additions & 0 deletions www/__tests__/footprintHelper.test.ts
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));
});
40 changes: 40 additions & 0 deletions www/__tests__/metHelper.test.ts
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);
});
2 changes: 0 additions & 2 deletions www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ import './js/i18n-utils.js';
import './js/main.js';
import './js/diary.js';
import './js/diary/services.js';
import './js/metrics-factory.js';
import './js/metrics-mappings.js';
import './js/plugin/logger.ts';
2 changes: 2 additions & 0 deletions www/js/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { initPushNotify } from './splash/pushNotifySettings';
import { initStoreDeviceSettings } from './splash/storeDeviceSettings';
import { initRemoteNotifyHandler } from './splash/remoteNotifyHandler';
import { withErrorBoundary } from './plugin/ErrorBoundary';
import { initCustomDatasetHelper } from './metrics/customMetricsHelper';

const defaultRoutes = (t) => [
{
Expand Down Expand Up @@ -77,6 +78,7 @@ const App = () => {
initPushNotify();
initStoreDeviceSettings();
initRemoteNotifyHandler();
initCustomDatasetHelper(appConfig);
}, [appConfig]);

const appContextValue = {
Expand Down
Loading

0 comments on commit c1f6dc3

Please sign in to comment.