From f57603cd68b7fd48411bab7a2588c52d137b96c4 Mon Sep 17 00:00:00 2001 From: Abby Wheelis Date: Fri, 15 Sep 2023 13:57:18 -0600 Subject: [PATCH] use custom labels we had figured out that there were some differences https://github.com/e-mission/e-mission-docs/issues/961#issuecomment-1721692442 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 --- www/js/metrics-factory.js | 14 ++++----- www/js/metrics/CarbonFootprintCard.tsx | 7 ++++- www/js/metrics/metricsHelper.ts | 42 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/www/js/metrics-factory.js b/www/js/metrics-factory.js index caeb07ad4..ce813fbaa 100644 --- a/www/js/metrics-factory.js +++ b/www/js/metrics-factory.js @@ -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); } } diff --git a/www/js/metrics/CarbonFootprintCard.tsx b/www/js/metrics/CarbonFootprintCard.tsx index 6e0b3eb81..2ed313f89 100644 --- a/www/js/metrics/CarbonFootprintCard.tsx +++ b/www/js/metrics/CarbonFootprintCard.tsx @@ -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'; @@ -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]) { diff --git a/www/js/metrics/metricsHelper.ts b/www/js/metrics/metricsHelper.ts index 4d8dac427..fd44fc2ab 100644 --- a/www/js/metrics/metricsHelper.ts +++ b/www/js/metrics/metricsHelper.ts @@ -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; +} \ No newline at end of file