Commit ff992244 authored by Phil Hughes's avatar Phil Hughes

Merge branch 'mw-pa-scatterplot-start-date' into 'master'

Productivity Analytics: Add getter for scatterplotStartDate

See merge request gitlab-org/gitlab!21538
parents 5f56d89c e2dc993e
......@@ -28,6 +28,7 @@ export default () => {
const initialData = {
mergedAtAfter,
mergedAtBefore,
minDate,
};
let filterManager;
......
......@@ -19,8 +19,7 @@ import { dateFormats } from '../../../../shared/constants';
* }
*
*/
// eslint-disable-next-line import/prefer-default-export
export const getCommonFilterParams = state => chartKey => {
export const getCommonFilterParams = (state, getters) => chartKey => {
const {
groupNamespace,
projectPath,
......@@ -31,10 +30,10 @@ export const getCommonFilterParams = state => chartKey => {
milestoneTitle,
} = 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 =
chartKey && chartKey === chartKeys.scatterplot
? dateFormat(getDateInPast(startDate, scatterPlotAddonQueryDays), dateFormats.isoDate)
? dateFormat(getters.scatterplotStartDate, dateFormats.isoDate)
: dateFormat(startDate, dateFormats.isoDate);
return {
......@@ -47,3 +46,18 @@ export const getCommonFilterParams = state => chartKey => {
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';
export default {
[types.SET_INITIAL_DATA](state, { mergedAtAfter, mergedAtBefore }) {
[types.SET_INITIAL_DATA](state, { mergedAtAfter, mergedAtBefore, minDate }) {
state.startDate = mergedAtAfter;
state.endDate = mergedAtBefore;
state.minDate = minDate;
},
[types.SET_GROUP_NAMESPACE](state, groupNamespace) {
state.groupNamespace = groupNamespace;
......
......@@ -6,4 +6,5 @@ export default () => ({
milestoneTitle: null,
startDate: null,
endDate: null,
minDate: null,
});
......@@ -14,6 +14,7 @@ describe('Productivity analytics filter actions', () => {
const initialData = {
mergedAtAfter: new Date('2019-11-01'),
mergedAtBefore: new Date('2019-12-09'),
minDate: new Date('2019-01-01'),
};
beforeEach(() => {
......
......@@ -4,27 +4,33 @@ import { chartKeys } from 'ee/analytics/productivity_analytics/constants';
describe('Productivity analytics filter getters', () => {
let state;
const currentYear = new Date().getFullYear();
const startDate = new Date(currentYear, 8, 1);
const endDate = new Date(currentYear, 8, 7);
const groupNamespace = 'gitlab-org';
const projectPath = 'gitlab-org/gitlab-test';
const authorUsername = 'root';
const milestoneTitle = 'foo';
const labelName = ['labelxyz'];
beforeEach(() => {
state = createState();
});
describe('getCommonFilterParams', () => {
const startDate = new Date('2019-09-01');
const endDate = new Date('2019-09-07');
beforeEach(() => {
state = {
groupNamespace: 'gitlab-org',
projectPath: 'gitlab-org/gitlab-test',
authorUsername: 'root',
milestoneTitle: 'foo',
labelName: ['labelxyz'],
groupNamespace,
projectPath,
authorUsername,
milestoneTitle,
labelName,
startDate,
endDate,
};
});
/*
describe('when chart is not scatterplot', () => {
it('returns an object with common filter params', () => {
const expected = {
......@@ -42,23 +48,71 @@ describe('Productivity analytics filter getters', () => {
expect(result).toEqual(expected);
});
});
*/
describe('when chart is scatterplot', () => {
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 = {
author_username: 'root',
group_id: 'gitlab-org',
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',
milestone_title: 'foo',
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);
});
});
});
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', () => {
const currentYear = new Date().getFullYear();
const startDate = new Date(currentYear, 8, 1);
const endDate = new Date(currentYear, 8, 7);
const minDate = new Date(currentYear, 0, 1);
beforeEach(() => {
state = getInitialState();
......@@ -22,11 +23,13 @@ describe('Productivity analytics filter mutations', () => {
const initialData = {
mergedAtAfter: startDate,
mergedAtBefore: endDate,
minDate,
};
mutations[types.SET_INITIAL_DATA](state, initialData);
expect(state.startDate).toBe(startDate);
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