Commit af12a14a authored by Brandon Labuschagne's avatar Brandon Labuschagne Committed by Phil Hughes

Change return type of getDateInPast to Date

The previous return type was a date string.

This approach allows more flexibility when working
with the response.
parent 4495195c
...@@ -553,14 +553,10 @@ export const calculateRemainingMilliseconds = endDate => { ...@@ -553,14 +553,10 @@ export const calculateRemainingMilliseconds = endDate => {
* *
* @param {Date} date the date that we will substract days from * @param {Date} date the date that we will substract days from
* @param {number} daysInPast number of days that are subtracted from a given date * @param {number} daysInPast number of days that are subtracted from a given date
* @returns {String} Date string in ISO format * @returns {Date} Date in past as Date object
*/ */
export const getDateInPast = (date, daysInPast) => { export const getDateInPast = (date, daysInPast) =>
const dateClone = newDate(date); new Date(newDate(date).setDate(date.getDate() - daysInPast));
return new Date(
dateClone.setTime(dateClone.getTime() - daysInPast * 24 * 60 * 60 * 1000),
).toISOString();
};
export const beginOfDayTime = 'T00:00:00Z'; export const beginOfDayTime = 'T00:00:00Z';
export const endOfDayTime = 'T23:59:59Z'; export const endOfDayTime = 'T23:59:59Z';
......
---
title: Change return type of getDateInPast to Date
merge_request: 19081
author:
type: changed
...@@ -119,7 +119,7 @@ export default { ...@@ -119,7 +119,7 @@ export default {
}, },
initDateRange() { initDateRange() {
const endDate = new Date(Date.now()); const endDate = new Date(Date.now());
const startDate = new Date(getDateInPast(endDate, DEFAULT_DAYS_IN_PAST)); const startDate = getDateInPast(endDate, DEFAULT_DAYS_IN_PAST);
this.setDateRange({ skipFetch: true, startDate, endDate }); this.setDateRange({ skipFetch: true, startDate, endDate });
}, },
}, },
......
...@@ -22,7 +22,7 @@ export default () => { ...@@ -22,7 +22,7 @@ export default () => {
const { endpoint, emptyStateSvgPath, noAccessSvgPath } = appContainer.dataset; const { endpoint, emptyStateSvgPath, noAccessSvgPath } = appContainer.dataset;
const now = new Date(Date.now()); const now = new Date(Date.now());
const defaultStartDate = new Date(getDateInPast(now, defaultDaysInPast)); const defaultStartDate = getDateInPast(now, defaultDaysInPast);
const defaultEndDate = now; const defaultEndDate = now;
let filterManager; let filterManager;
......
...@@ -33,7 +33,7 @@ export const fetchChartData = ({ dispatch, getters, state, rootState }, chartKey ...@@ -33,7 +33,7 @@ export const fetchChartData = ({ dispatch, getters, state, rootState }, chartKey
if (chartKey === chartKeys.scatterplot) { if (chartKey === chartKeys.scatterplot) {
const transformedData = transformScatterData( const transformedData = transformScatterData(
data, data,
new Date(getDateInPast(rootState.filters.startDate, scatterPlotAddonQueryDays)), getDateInPast(rootState.filters.startDate, scatterPlotAddonQueryDays),
new Date(rootState.filters.endDate), new Date(rootState.filters.endDate),
); );
......
...@@ -27,10 +27,7 @@ export const getCommonFilterParams = state => chartKey => { ...@@ -27,10 +27,7 @@ export const getCommonFilterParams = state => chartKey => {
// for the scatterplot we need to remove 30 days from the state's merged_at_after date // for the scatterplot we need to remove 30 days from the state's merged_at_after date
const mergedAtAfterDate = const mergedAtAfterDate =
chartKey && chartKey === chartKeys.scatterplot chartKey && chartKey === chartKeys.scatterplot
? dateFormat( ? dateFormat(getDateInPast(startDate, scatterPlotAddonQueryDays), dateFormats.isoDate)
new Date(getDateInPast(new Date(startDate), scatterPlotAddonQueryDays)),
dateFormats.isoDate,
)
: dateFormat(startDate, dateFormats.isoDate); : dateFormat(startDate, dateFormats.isoDate);
return { return {
......
...@@ -163,7 +163,7 @@ export const getMedianLineData = (data, startDate, endDate, daysOffset) => { ...@@ -163,7 +163,7 @@ export const getMedianLineData = (data, startDate, endDate, daysOffset) => {
medianData = transformedData.slice(startIndex, i); medianData = transformedData.slice(startIndex, i);
flattenedData = _.flatten(medianData); flattenedData = _.flatten(medianData);
if (flattenedData.length) { if (flattenedData.length) {
d = getDateInPast(endDate, len - i); d = getDateInPast(endDate, len - i).toISOString();
result.push([dateFormat(d, dateFormats.isoDate), median(flattenedData)]); result.push([dateFormat(d, dateFormats.isoDate), median(flattenedData)]);
} }
} }
......
...@@ -51,7 +51,7 @@ const stageFixtures = defaultStages.reduce((acc, stage) => { ...@@ -51,7 +51,7 @@ const stageFixtures = defaultStages.reduce((acc, stage) => {
}, {}); }, {});
export const endDate = new Date(Date.now()); export const endDate = new Date(Date.now());
export const startDate = new Date(getDateInPast(endDate, DEFAULT_DAYS_IN_PAST)); export const startDate = getDateInPast(endDate, DEFAULT_DAYS_IN_PAST);
export const issueEvents = stageFixtures.issue; export const issueEvents = stageFixtures.issue;
export const planEvents = stageFixtures.plan; export const planEvents = stageFixtures.plan;
......
...@@ -31,8 +31,8 @@ describe('Productivity analytics chart actions', () => { ...@@ -31,8 +31,8 @@ describe('Productivity analytics chart actions', () => {
rootState: { rootState: {
endpoint: `${TEST_HOST}/analytics/productivity_analytics.json`, endpoint: `${TEST_HOST}/analytics/productivity_analytics.json`,
filters: { filters: {
startDate: '2019-09-01', startDate: new Date('2019-09-01'),
endDate: '2091-09-05', endDate: new Date('2091-09-05'),
}, },
}, },
getters: { getters: {
......
...@@ -428,17 +428,19 @@ describe('newDate', () => { ...@@ -428,17 +428,19 @@ describe('newDate', () => {
}); });
describe('getDateInPast', () => { describe('getDateInPast', () => {
const date = new Date(1563235200000); // 2019-07-16T00:00:00.000Z; const date = new Date('2019-07-16T00:00:00.000Z');
const daysInPast = 90; const daysInPast = 90;
it('returns the correct date in the past', () => { it('returns the correct date in the past', () => {
const dateInPast = datetimeUtility.getDateInPast(date, daysInPast); const dateInPast = datetimeUtility.getDateInPast(date, daysInPast);
expect(dateInPast).toBe('2019-04-17T00:00:00.000Z'); const expectedDateInPast = new Date('2019-04-17T00:00:00.000Z');
expect(dateInPast).toStrictEqual(expectedDateInPast);
}); });
it('does not modifiy the original date', () => { it('does not modifiy the original date', () => {
datetimeUtility.getDateInPast(date, daysInPast); datetimeUtility.getDateInPast(date, daysInPast);
expect(date).toStrictEqual(new Date(1563235200000)); expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment