Newer
Older
Bucknell, Mary S.
committed
const memoize = require('fast-memoize');
Bucknell, Mary S.
committed
const { createSelector } = require('reselect');
Bucknell, Mary S.
committed
/*
* Selectors that return properties from the state
*/
export const getVariables = state => state.series.variables ? state.series.variables : null;
export const getMethods = state => state.series.methods ? state.series.methods : null;
export const getQueryInfo = state => state.series.queryInfo || {};
export const getCurrentVariableID = state => state.timeSeriesState.currentVariableID;
Bucknell, Mary S.
committed
export const getCurrentDateRange = state => state.timeSeriesState.currentDateRange;
Bucknell, Mary S.
committed
export const getLoadingTsKeys = state => state.timeSeriesState.loadingTSKeys;
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
/*
* Selectors the return derived data from the state
*/
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
/*
* @return {Object} Variable details for the currently selected variable or null.
Bucknell, Mary S.
committed
*/
Bucknell, Mary S.
committed
export const getCurrentVariable = createSelector(
getVariables,
getCurrentVariableID,
Bucknell, Mary S.
committed
(variables, variableID) => {
Bucknell, Mary S.
committed
return variableID && variables && variables[variableID] ? variables[variableID] : null;
Bucknell, Mary S.
committed
}
);
Bucknell, Mary S.
committed
/*
* @return {String} or null - The parameter code of the currently selected variable or null if no variable selected
*/
export const getCurrentParmCd = createSelector(
getCurrentVariable,
Bucknell, Mary S.
committed
(currentVar) => {
return currentVar && currentVar.variableCode ? currentVar.variableCode.value : null;
}
Bucknell, Mary S.
committed
);
Bucknell, Mary S.
committed
/*
* @param {String} - Time series key: current, compre or median
Bucknell, Mary S.
committed
* @param {String} or null period = date range of interest as an ISO-8601 duration. If null the currentDateRange is used
* @param {String} or null parmCD - if null the parmCd of the current variable is used.
* @return {String} or null - Return the the request key for the request object
* selected variable.
Bucknell, Mary S.
committed
*/
Bucknell, Mary S.
committed
export const getTsRequestKey = memoize((tsKey, period, parmCd) => createSelector(
getCurrentDateRange,
getCurrentParmCd,
(dateRange, currentParmCd) => {
let result = `${tsKey}`;
if (tsKey !== 'median') {
const periodToUse = period ? period : dateRange;
result += `:${periodToUse}`;
if (periodToUse !== 'P7D') {
const parmCdToUse = parmCd ? parmCd : currentParmCd;
result += `:${parmCdToUse}`;
}
Bucknell, Mary S.
committed
}
Bucknell, Mary S.
committed
return result;
})
);
Bucknell, Mary S.
committed
/*
* @param {String} tsKey - current, compare, or median
Bucknell, Mary S.
committed
* @param {String} or null period - date range of interest specified as an ISO-8601 duration. If null, P7D is assumed
Bucknell, Mary S.
committed
* @param {String} or null parmCD - Only need to specify if period is something other than P7D or null
Bucknell, Mary S.
committed
* @return {Boolean} - True if the time series with key, period, and parmCd has already been requested
*
*/
Bucknell, Mary S.
committed
export const hasTimeSeries = memoize((tsKey, period, parmCd) => createSelector(
getTsRequestKey(tsKey, period, parmCd),
state => state.series,
(tsRequestKey, series) => {
return Boolean(series && series.requests && series.requests[tsRequestKey]);
Bucknell, Mary S.
committed
}));
Bucknell, Mary S.
committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* @param {String} tsKey - current, compare, or median
* @param {String} or null period - date range of interest specified as an ISO-8601 duration. If null, P7D is assumed
* @param {String} or null parmCD - Only need to specify if period is something other than P7D or null
* @return {Object} with start and end {Date} properties that contain the range of the data requested or null
* if the store does not contain a query for the tsKey request
* */
export const getRequestTimeRange = memoize((tsKey, period, parmCd) => createSelector(
getQueryInfo,
getTsRequestKey(tsKey, period, parmCd),
(queryInfos, tsRequestKey) => {
const notes = queryInfos[tsRequestKey] && queryInfos[tsRequestKey].notes ? queryInfos[tsRequestKey].notes : null;
if (!notes) {
return null;
}
let result;
// If this is a period-based query (eg, P7D), use the request time
// as the end date.
if (notes['filter:timeRange'].mode === 'PERIOD') {
const start = new Date(notes.requestDT);
start.setDate(notes.requestDT.getDate() - notes['filter:timeRange'].periodDays);
result = {
start: start,
end: notes.requestDT
};
} else {
result = {
start: notes['filter:timeRange'].interval.start,
end: notes['filter:timeRange'].interval.end
};
}
return result;
}
));
Bucknell, Mary S.
committed
export const isLoadingTS = memoize((tsKey, period, parmCd) => createSelector(
Bucknell, Mary S.
committed
getTsRequestKey(tsKey, period, parmCd),
(loadingTSKeys, tsRequestKey) => loadingTSKeys.includes(tsRequestKey)