Skip to content

Commit

Permalink
remove country-specific
Browse files Browse the repository at this point in the history
since we've changed the way fallback labels are handled, everything should be custom now. This means that we're not using the carbon values from the set country, but rather from the labels
  • Loading branch information
Abby Wheelis committed Nov 9, 2023
1 parent 1a734f7 commit 1d7bf5b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 191 deletions.
4 changes: 2 additions & 2 deletions www/__tests__/footprintHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const metrics = [
{ key: 'Unicycle', values: 5000 },
];

it('gets footprint for metrics (not custom, fallback 0', () => {
it('gets footprint for metrics (not custom, fallback 0)', () => {
expect(getFootprintForMetrics(metrics, 0)).toBe(10.534493474207583);
});

it('gets footprint for metrics (not custom, fallback 0.1', () => {
it('gets footprint for metrics (not custom, fallback 0.1)', () => {
expect(getFootprintForMetrics(metrics, 0.1)).toBe(10.534493474207583 + 0.5);
});

Expand Down
123 changes: 0 additions & 123 deletions www/js/metrics/CarbonDatasets.ts

This file was deleted.

74 changes: 15 additions & 59 deletions www/js/metrics/CustomMetricsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,35 @@
import angular from 'angular';
import { getLabelOptions } from '../survey/multilabel/confirmHelper';
import { getConfig } from '../config/dynamicConfig';
import { storageGet, storageSet } from '../plugin/storage';
import { displayError, displayErrorMsg, logDebug } from '../plugin/logger';
import { standardMETs } from './metDataset';
import { carbonDatasets } from './carbonDatasets';

const CARBON_DATASET_KEY = 'carbon_dataset_locale';
const defaultCarbonDatasetCode = 'US';
import { fallbackCarbon } from './carbonDatasetFallback';

let _customMETs;
let _customPerKmFootprint;
let _range_limited_motorized;
let _inputParams;
let _currentCarbonDatasetCode = defaultCarbonDatasetCode;

// we need to call the method from within a promise in initialize()
// and using this.setCurrentCarbonDatasetLocale doesn't seem to work
const setCurrentCarbonDatasetLocale = function (localeCode) {
for (var code in carbonDatasets) {
if (code == localeCode) {
_currentCarbonDatasetCode = localeCode;
break;
}
}
};

const loadCarbonDatasetLocale = function () {
return storageGet(CARBON_DATASET_KEY).then(function (localeCode) {
logDebug('loadCarbonDatasetLocale() obtained value from storage [' + localeCode + ']');
if (!localeCode) {
localeCode = defaultCarbonDatasetCode;
logDebug('loadCarbonDatasetLocale() no value in storage, using [' + localeCode + '] instead');
}
setCurrentCarbonDatasetLocale(localeCode);
});
};

export const saveCurrentCarbonDatasetLocale = function (localeCode) {
setCurrentCarbonDatasetLocale(localeCode);
storageSet(CARBON_DATASET_KEY, _currentCarbonDatasetCode);
logDebug(
'saveCurrentCarbonDatasetLocale() saved value [' + _currentCarbonDatasetCode + '] to storage',
);
};

export const getCarbonDatasetOptions = function () {
var options = [];
for (var code in carbonDatasets) {
options.push({
text: code, //carbonDatasets[code].regionName,
value: code,
});
}
return options;
};

export const getCurrentCarbonDatasetCode = function () {
return _currentCarbonDatasetCode;
};

export const getCurrentCarbonDatasetFootprint = function () {
return carbonDatasets[_currentCarbonDatasetCode].footprintData;
};

export const getCustomMETs = function () {
console.log('Getting custom METs', _customMETs);
logDebug('Getting custom METs' + JSON.stringify(_customMETs));
return _customMETs;
};

export const getCustomFootprint = function () {
console.log('Getting custom footprint', _customPerKmFootprint);
logDebug('Getting custom footprint' + JSON.stringify(_customPerKmFootprint));
return _customPerKmFootprint;
};

export const getRangeLimitedMotorixe = function () {
logDebug('Getting range limited motorized' + JSON.stringify(_range_limited_motorized));
return _range_limited_motorized;
};

export const getFallbackFootprint = function () {
console.log('getting fallback carbon');
return fallbackCarbon.footprintData;
};

const populateCustomMETs = function () {
let modeOptions = _inputParams['MODE'];
let modeMETEntries = modeOptions.map((opt) => {
Expand Down Expand Up @@ -103,7 +59,7 @@ const populateCustomMETs = function () {
}
});
_customMETs = Object.fromEntries(modeMETEntries.filter((e) => angular.isDefined(e)));
console.log('After populating, custom METs = ', _customMETs);
logDebug('After populating, custom METs = ' + JSON.stringify(_customMETs));
};

const populateCustomFootprints = function () {
Expand All @@ -128,7 +84,7 @@ const populateCustomFootprints = function () {
})
.filter((modeCO2) => angular.isDefined(modeCO2));
_customPerKmFootprint = Object.fromEntries(modeCO2PerKm);
console.log('After populating, custom perKmFootprint', _customPerKmFootprint);
logDebug('After populating, custom perKmFootprint' + JSON.stringify(_customPerKmFootprint));
};

export const initCustomDatasetHelper = async function (newConfig) {
Expand Down
14 changes: 14 additions & 0 deletions www/js/metrics/carbonDatasetFallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const fallbackCarbon = {
regionName: 'United States',
footprintData: {
WALKING: 0,
BICYCLING: 0,
CAR: 267 / 1609,
BUS: 278 / 1609,
LIGHT_RAIL: 120 / 1609,
SUBWAY: 74 / 1609,
TRAM: 90 / 1609,
TRAIN: 92 / 1609,
AIR_OR_HSR: 217 / 1609,
},
};
12 changes: 5 additions & 7 deletions www/js/metrics/footprintHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCustomFootprint } from './CustomMetricsHelper';
import { getCurrentCarbonDatasetFootprint } from './CustomMetricsHelper';
import { displayErrorMsg } from '../plugin/logger';
import { getCustomFootprint, getFallbackFootprint } from './CustomMetricsHelper';

var highestFootprint = 0;
let useCustom = false;
Expand All @@ -16,14 +16,12 @@ const getFootprint = function () {
if (useCustom == true) {
return getCustomFootprint();
} else {
return getCurrentCarbonDatasetFootprint();
//TODO: check through configs and ensure they all have custom lables
displayErrorMsg('Error in Footprint Calculatons', 'issue with data or default labels');
return getFallbackFootprint();
}
};

const readableFormat = function (v) {
return v > 999 ? Math.round(v / 1000) + 'k kg CO₂' : Math.round(v) + ' kg CO₂';
};

export const getFootprintForMetrics = function (userMetrics, defaultIfMissing = 0) {
var footprint = getFootprint();
var result = 0;
Expand Down

0 comments on commit 1d7bf5b

Please sign in to comment.