Commit bf8c7c58 authored by Martin Wortschack's avatar Martin Wortschack Committed by Filipa Lacerda

Add initDateArray utility method

Creates a two-dimensional array of length=n
where n is the difference in days between a given
start and end date plus 1
parent 6b9ef7ca
import { getDateInPast } from '~/lib/utils/datetime_utility'; import { getDayDifference, getDateInPast } from '~/lib/utils/datetime_utility';
import { median } from '~/lib/utils/number_utils'; import { median } from '~/lib/utils/number_utils';
/** /**
...@@ -29,6 +29,21 @@ export const getMilestonesEndpoint = (namespacePath, projectPathWithNamespace) = ...@@ -29,6 +29,21 @@ export const getMilestonesEndpoint = (namespacePath, projectPathWithNamespace) =
return `/groups/${namespacePath}/-/milestones`; return `/groups/${namespacePath}/-/milestones`;
}; };
/**
* Computes the day difference 'days' between a given start and end date
* and creates an array of length 'days'.
* For each day in the array it initializes an empty array.
*
* E.g. initDateArray(new Date('2019-01-01'), new Date('2019-01-03'))
* the following data structure gets generated: [ [], [], [] ]
* @param {Date} startDate - The start date
* @param {Date} endDate - The end date
*/
export const initDateArray = (startDate, endDate) => {
const days = getDayDifference(startDate, endDate);
return Array.from({ length: days + 1 }, () => []);
};
/** /**
* Transforms a given data object into an array * Transforms a given data object into an array
* which will be used as series data for the scatterplot chart. * which will be used as series data for the scatterplot chart.
......
import { import {
getLabelsEndpoint, getLabelsEndpoint,
getMilestonesEndpoint, getMilestonesEndpoint,
initDateArray,
getScatterPlotData, getScatterPlotData,
getMedianLineData, getMedianLineData,
} from 'ee/analytics/productivity_analytics/utils'; } from 'ee/analytics/productivity_analytics/utils';
...@@ -35,6 +36,15 @@ describe('Productivity Analytics utils', () => { ...@@ -35,6 +36,15 @@ describe('Productivity Analytics utils', () => {
}); });
}); });
describe('initDateArray', () => {
it('creates a two-dimensional array with 3 empty arrays for startDate=2019-09-01 and endDate=2019-09-03', () => {
const startDate = new Date('2019-09-01');
const endDate = new Date('2019-09-03');
expect(initDateArray(startDate, endDate)).toEqual([[], [], []]);
});
});
describe('getScatterPlotData', () => { describe('getScatterPlotData', () => {
it('filters out data before given "dateInPast", transforms the data and sorts by date ascending', () => { it('filters out data before given "dateInPast", transforms the data and sorts by date ascending', () => {
const dateInPast = new Date(2019, 7, 9); // '2019-08-09T22:00:00.000Z'; const dateInPast = new Date(2019, 7, 9); // '2019-08-09T22: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