Skip to content

Commit

Permalink
use custom labels
Browse files Browse the repository at this point in the history
we had figured out that there were some differences e-mission/e-mission-docs#961 (comment)

Eventually, we realized this was because the new dashboard was not using the custom labels.

This commit adds the methods that check to see if the labels are custom or sensed to `metricsHelper`, checks for custom labels and indicates the need for custom footprint mappings in `CarbonFootprintCard` and the finally reverts back to "rich modes" rather than "base modes" in `metrics-factory` so we use the custom labels
  • Loading branch information
Abby Wheelis committed Sep 15, 2023
1 parent 67111b8 commit f57603c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
14 changes: 7 additions & 7 deletions www/js/metrics-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ angular.module('emission.main.metrics.factory',
var footprint = fh.getFootprint();
var result = 0;
for (var i in userMetrics) {
const baseMode = getBaseModeByValue(userMetrics[i].key, labelOptions).name;
if (baseMode == 'ON_FOOT') {
baseMode = 'WALKING';
var mode = userMetrics[i].key;
if (mode == 'ON_FOOT') {
mode = 'WALKING';
}

if (baseMode in footprint) {
result += footprint[baseMode] * mtokm(userMetrics[i].values);
} else if (baseMode == 'IN_VEHICLE') {
if (mode in footprint) {
result += footprint[mode] * mtokm(userMetrics[i].values);
} else if (mode == 'IN_VEHICLE') {
result += ((footprint['CAR'] + footprint['BUS'] + footprint["LIGHT_RAIL"] + footprint['TRAIN'] + footprint['TRAM'] + footprint['SUBWAY']) / 6) * mtokm(userMetrics[i].values);
} else {
console.warn('WARNING FootprintHelper.getFootprintFromMetrics() was requested for an unknown mode: ' + baseMode + " metrics JSON: " + JSON.stringify(userMetrics));
console.warn('WARNING FootprintHelper.getFootprintFromMetrics() was requested for an unknown mode: ' + mode + " metrics JSON: " + JSON.stringify(userMetrics));
result += defaultIfMissing * mtokm(userMetrics[i].values);
}
}
Expand Down
7 changes: 6 additions & 1 deletion www/js/metrics/CarbonFootprintCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { View } from 'react-native';
import { Card, Text, useTheme} from 'react-native-paper';
import { MetricsData } from './metricsTypes';
import { cardStyles } from './MetricsTab';
import { formatDateRangeOfDays, parseDataFromMetrics, generateSummaryFromData, calculatePercentChange, segmentDaysByWeeks } from './metricsHelper';
import { formatDateRangeOfDays, parseDataFromMetrics, generateSummaryFromData, calculatePercentChange, segmentDaysByWeeks, isCustomLabels } from './metricsHelper';
import { useTranslation } from 'react-i18next';
import BarChart from '../components/BarChart';
import { getAngularService } from '../angular-react-helper';
Expand Down Expand Up @@ -39,6 +39,11 @@ const CarbonFootprintCard = ({ userMetrics, aggMetrics }: Props) => {
//setting up data to be displayed
let graphRecords = [];

//set custon dataset, if the labels are custom
if(isCustomLabels(userThisWeekModeMap)){
FootprintHelper.setUseCustomFootprint();
}

//calculate low-high and format range for prev week, if exists
let userPrevWeek;
if(userLastWeekSummaryMap[0]) {
Expand Down
42 changes: 42 additions & 0 deletions www/js/metrics/metricsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,45 @@ export function generateSummaryFromData(modeMap, metric) {

return summaryMap;
}

/*
* We use the results to determine whether these results are from custom
* labels or from the automatically sensed labels. Automatically sensedV
* labels are in all caps, custom labels are prefixed by label, but have had
* the label_prefix stripped out before this. Results should have either all
* sensed labels or all custom labels.
*/
export const isCustomLabels = function(modeMap) {
const isSensed = (mode) => mode == mode.toUpperCase();
const isCustom = (mode) => mode == mode.toLowerCase();
const metricSummaryChecksCustom = [];
const metricSummaryChecksSensed = [];

const distanceKeys = modeMap.map((e) => e.key);
const isSensedKeys = distanceKeys.map(isSensed);
const isCustomKeys = distanceKeys.map(isCustom);
console.log("Checking metric keys", distanceKeys, " sensed ", isSensedKeys,
" custom ", isCustomKeys);
const isAllCustomForMetric = isAllCustom(isSensedKeys, isCustomKeys);
metricSummaryChecksSensed.push(!isAllCustomForMetric);
metricSummaryChecksCustom.push(isAllCustomForMetric);

console.log("overall custom/not results for each metric = ", metricSummaryChecksCustom);
return isAllCustom(metricSummaryChecksSensed, metricSummaryChecksCustom);
}

const isAllCustom = function(isSensedKeys, isCustomKeys) {
const allSensed = isSensedKeys.reduce((a, b) => a && b, true);
const anySensed = isSensedKeys.reduce((a, b) => a || b, false);
const allCustom = isCustomKeys.reduce((a, b) => a && b, true);
const anyCustom = isCustomKeys.reduce((a, b) => a || b, false);
if ((allSensed && !anyCustom)) {
return false; // sensed, not custom
}
if ((!anySensed && allCustom)) {
return true; // custom, not sensed; false implies that the other option is true
}
// Logger.displayError("Mixed entries that combine sensed and custom labels",
// "Please report to your program admin");
return undefined;
}

0 comments on commit f57603c

Please sign in to comment.