Skip to content

Commit

Permalink
MetricsTab: const N_DAYS_TO_LOAD = 14
Browse files Browse the repository at this point in the history
From suggestion #1138 (comment)
  • Loading branch information
JGreenlee committed Apr 5, 2024
1 parent da7283e commit 1269aad
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions www/js/metrics/MetricsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import TimelineContext from '../TimelineContext';
import { isoDateRangeToTsRange, isoDatesDifference } from '../diary/timelineHelper';
import { MetricsSummaries } from 'e-mission-common';

// 2 weeks of data is needed in order to compare "past week" vs "previous week"
const N_DAYS_TO_LOAD = 14; // 2 weeks
export const METRIC_LIST = ['duration', 'mean_speed', 'count', 'distance'] as const;

async function fetchMetricsFromServer(
Expand Down Expand Up @@ -77,30 +79,32 @@ const MetricsTab = () => {
return result;
}, [timelineMap]);

// at least 2 weeks of timeline data should be loaded for the user metrics
// at least N_DAYS_TO_LOAD of timeline data should be loaded for the user metrics
useEffect(() => {
if (!appConfig?.server) return;
const dateRangeDays = isoDatesDifference(...dateRange);

// this tab uses the last 2 weeks of data; if we need more, we should fetch it
if (dateRangeDays < 14) {
// this tab uses the last N_DAYS_TO_LOAD of data; if we need more, we should fetch it
if (dateRangeDays < N_DAYS_TO_LOAD) {
if (timelineIsLoading) {
logDebug('MetricsTab: timeline is still loading, not loading more days yet');
} else {
logDebug('MetricsTab: loading more days');
loadMoreDays('past', 14 - dateRangeDays);
loadMoreDays('past', N_DAYS_TO_LOAD - dateRangeDays);
}
} else {
logDebug('MetricsTab: date range >= 14 days, not loading more days');
logDebug(`MetricsTab: date range >= ${N_DAYS_TO_LOAD} days, not loading more days`);
}
}, [dateRange, timelineIsLoading, appConfig?.server]);

// aggregate metrics fetched from the server whenever the date range is set
useEffect(() => {
logDebug('MetricsTab: dateRange updated to ' + JSON.stringify(dateRange));
const dateRangeDays = isoDatesDifference(...dateRange);
if (dateRangeDays < 14) {
logDebug('MetricsTab: date range < 14 days, not loading aggregate metrics yet');
if (dateRangeDays < N_DAYS_TO_LOAD) {
logDebug(
`MetricsTab: date range < ${N_DAYS_TO_LOAD} days, not loading aggregate metrics yet`,
);
} else {
loadMetricsForPopulation('aggregate', dateRange);
}
Expand Down

0 comments on commit 1269aad

Please sign in to comment.