Commit e2dc993e authored by Martin Wortschack's avatar Martin Wortschack

Add getter for scatterplotStartDate

- This ensures we won't query the
scatterplot API with a merge_at_after date prior to the minDate
parent 1d0262a9
...@@ -28,6 +28,7 @@ export default () => { ...@@ -28,6 +28,7 @@ export default () => {
const initialData = { const initialData = {
mergedAtAfter, mergedAtAfter,
mergedAtBefore, mergedAtBefore,
minDate,
}; };
let filterManager; let filterManager;
......
...@@ -19,8 +19,7 @@ import { dateFormats } from '../../../../shared/constants'; ...@@ -19,8 +19,7 @@ import { dateFormats } from '../../../../shared/constants';
* } * }
* *
*/ */
// eslint-disable-next-line import/prefer-default-export export const getCommonFilterParams = (state, getters) => chartKey => {
export const getCommonFilterParams = state => chartKey => {
const { const {
groupNamespace, groupNamespace,
projectPath, projectPath,
...@@ -31,10 +30,10 @@ export const getCommonFilterParams = state => chartKey => { ...@@ -31,10 +30,10 @@ export const getCommonFilterParams = state => chartKey => {
milestoneTitle, milestoneTitle,
} = state; } = state;
// for the scatterplot we need to remove 30 days from the state's merged_at_after date // for the scatterplot we need to query the API with a date prior to the selected start date
const mergedAtAfterDate = const mergedAtAfterDate =
chartKey && chartKey === chartKeys.scatterplot chartKey && chartKey === chartKeys.scatterplot
? dateFormat(getDateInPast(startDate, scatterPlotAddonQueryDays), dateFormats.isoDate) ? dateFormat(getters.scatterplotStartDate, dateFormats.isoDate)
: dateFormat(startDate, dateFormats.isoDate); : dateFormat(startDate, dateFormats.isoDate);
return { return {
...@@ -47,3 +46,18 @@ export const getCommonFilterParams = state => chartKey => { ...@@ -47,3 +46,18 @@ export const getCommonFilterParams = state => chartKey => {
merged_at_before: `${dateFormat(endDate, dateFormats.isoDate)}${endOfDayTime}`, merged_at_before: `${dateFormat(endDate, dateFormats.isoDate)}${endOfDayTime}`,
}; };
}; };
/**
* Returns the start date for the scatterplot.
* It computes a dateInPast based on the selected startDate
* and a default number of offset days (offsetDays)
*
* When a minDate exists and the minDate is after the computed dateInPast,
* the minDate is returned. Otherwise the computed dateInPast is returned.
*/
export const scatterplotStartDate = state => {
const { startDate, minDate } = state;
const dateInPast = getDateInPast(startDate, scatterPlotAddonQueryDays);
return minDate && minDate > dateInPast ? minDate : dateInPast;
};
import * as types from './mutation_types'; import * as types from './mutation_types';
export default { export default {
[types.SET_INITIAL_DATA](state, { mergedAtAfter, mergedAtBefore }) { [types.SET_INITIAL_DATA](state, { mergedAtAfter, mergedAtBefore, minDate }) {
state.startDate = mergedAtAfter; state.startDate = mergedAtAfter;
state.endDate = mergedAtBefore; state.endDate = mergedAtBefore;
state.minDate = minDate;
}, },
[types.SET_GROUP_NAMESPACE](state, groupNamespace) { [types.SET_GROUP_NAMESPACE](state, groupNamespace) {
state.groupNamespace = groupNamespace; state.groupNamespace = groupNamespace;
......
...@@ -6,4 +6,5 @@ export default () => ({ ...@@ -6,4 +6,5 @@ export default () => ({
milestoneTitle: null, milestoneTitle: null,
startDate: null, startDate: null,
endDate: null, endDate: null,
minDate: null,
}); });
...@@ -14,6 +14,7 @@ describe('Productivity analytics filter actions', () => { ...@@ -14,6 +14,7 @@ describe('Productivity analytics filter actions', () => {
const initialData = { const initialData = {
mergedAtAfter: new Date('2019-11-01'), mergedAtAfter: new Date('2019-11-01'),
mergedAtBefore: new Date('2019-12-09'), mergedAtBefore: new Date('2019-12-09'),
minDate: new Date('2019-01-01'),
}; };
beforeEach(() => { beforeEach(() => {
......
...@@ -4,27 +4,33 @@ import { chartKeys } from 'ee/analytics/productivity_analytics/constants'; ...@@ -4,27 +4,33 @@ import { chartKeys } from 'ee/analytics/productivity_analytics/constants';
describe('Productivity analytics filter getters', () => { describe('Productivity analytics filter getters', () => {
let state; let state;
const currentYear = new Date().getFullYear(); const groupNamespace = 'gitlab-org';
const startDate = new Date(currentYear, 8, 1); const projectPath = 'gitlab-org/gitlab-test';
const endDate = new Date(currentYear, 8, 7); const authorUsername = 'root';
const milestoneTitle = 'foo';
const labelName = ['labelxyz'];
beforeEach(() => { beforeEach(() => {
state = createState(); state = createState();
}); });
describe('getCommonFilterParams', () => { describe('getCommonFilterParams', () => {
const startDate = new Date('2019-09-01');
const endDate = new Date('2019-09-07');
beforeEach(() => { beforeEach(() => {
state = { state = {
groupNamespace: 'gitlab-org', groupNamespace,
projectPath: 'gitlab-org/gitlab-test', projectPath,
authorUsername: 'root', authorUsername,
milestoneTitle: 'foo', milestoneTitle,
labelName: ['labelxyz'], labelName,
startDate, startDate,
endDate, endDate,
}; };
}); });
/*
describe('when chart is not scatterplot', () => { describe('when chart is not scatterplot', () => {
it('returns an object with common filter params', () => { it('returns an object with common filter params', () => {
const expected = { const expected = {
...@@ -42,23 +48,71 @@ describe('Productivity analytics filter getters', () => { ...@@ -42,23 +48,71 @@ describe('Productivity analytics filter getters', () => {
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
*/
describe('when chart is scatterplot', () => { describe('when chart is scatterplot', () => {
it('returns an object with common filter params and subtracts 30 days from the merged_at_after date', () => { it('returns an object with common filter params and subtracts 30 days from the merged_at_after date', () => {
const mergedAtAfter = '2019-08-02';
const expected = { const expected = {
author_username: 'root', author_username: 'root',
group_id: 'gitlab-org', group_id: 'gitlab-org',
label_name: ['labelxyz'], label_name: ['labelxyz'],
merged_at_after: '2019-08-02T00:00:00Z', merged_at_after: `${mergedAtAfter}T00:00:00Z`,
merged_at_before: '2019-09-07T23:59:59Z', merged_at_before: '2019-09-07T23:59:59Z',
milestone_title: 'foo', milestone_title: 'foo',
project_id: 'gitlab-org/gitlab-test', project_id: 'gitlab-org/gitlab-test',
}; };
const result = getters.getCommonFilterParams(state)(chartKeys.scatterplot); const mockGetters = {
scatterplotStartDate: new Date(mergedAtAfter),
};
const result = getters.getCommonFilterParams(state, mockGetters)(chartKeys.scatterplot);
expect(result).toEqual(expected); expect(result).toEqual(expected);
}); });
}); });
}); });
describe('scatterplotStartDate', () => {
beforeEach(() => {
state = {
groupNamespace,
projectPath,
authorUsername,
milestoneTitle,
labelName,
startDate: new Date('2019-09-01'),
endDate: new Date('2019-09-10'),
};
});
describe('when a minDate exists', () => {
it('returns the minDate when the startDate (minus 30 days) is before to the minDate', () => {
const minDate = new Date('2019-08-15');
state.minDate = minDate;
const result = getters.scatterplotStartDate(state);
expect(result).toBe(minDate);
});
it('returns a computed date when the startDate (minus 30 days) is after to the minDate', () => {
const minDate = new Date('2019-07-01');
state.minDate = minDate;
const result = getters.scatterplotStartDate(state);
expect(result).toEqual(new Date('2019-08-02'));
});
});
describe('when no minDate exists', () => {
it('returns the computed date, i.e., startDate minus 30 days', () => {
const result = getters.scatterplotStartDate(state);
expect(result).toEqual(new Date('2019-08-02'));
});
});
});
}); });
...@@ -12,6 +12,7 @@ describe('Productivity analytics filter mutations', () => { ...@@ -12,6 +12,7 @@ describe('Productivity analytics filter mutations', () => {
const currentYear = new Date().getFullYear(); const currentYear = new Date().getFullYear();
const startDate = new Date(currentYear, 8, 1); const startDate = new Date(currentYear, 8, 1);
const endDate = new Date(currentYear, 8, 7); const endDate = new Date(currentYear, 8, 7);
const minDate = new Date(currentYear, 0, 1);
beforeEach(() => { beforeEach(() => {
state = getInitialState(); state = getInitialState();
...@@ -22,11 +23,13 @@ describe('Productivity analytics filter mutations', () => { ...@@ -22,11 +23,13 @@ describe('Productivity analytics filter mutations', () => {
const initialData = { const initialData = {
mergedAtAfter: startDate, mergedAtAfter: startDate,
mergedAtBefore: endDate, mergedAtBefore: endDate,
minDate,
}; };
mutations[types.SET_INITIAL_DATA](state, initialData); mutations[types.SET_INITIAL_DATA](state, initialData);
expect(state.startDate).toBe(startDate); expect(state.startDate).toBe(startDate);
expect(state.endDate).toBe(endDate); expect(state.endDate).toBe(endDate);
expect(state.minDate).toBe(minDate);
}); });
}); });
......
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