From 1c5e70dc897d20a46d8f18470580d537776d6815 Mon Sep 17 00:00:00 2001 From: Armen Zambrano G Date: Mon, 7 Jan 2019 15:42:40 -0500 Subject: [PATCH] Bucket counting of bugs per week instead of per day --- package.json | 1 + src/utils/bugzilla/generateChartJsData.js | 26 ++++++++++---------- src/utils/toDayOfWeek.js | 15 ++++++++++++ test/utils/toDayOfWeek.test.js | 29 +++++++++++++++++++++++ yarn.lock | 2 +- 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/utils/toDayOfWeek.js create mode 100644 test/utils/toDayOfWeek.test.js diff --git a/package.json b/package.json index 5979b26..a6b9077 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "@material-ui/icons": "^3.0.1", "@mozilla-frontend-infra/components": "^2.0.0", "chart.js": "^2.7.3", + "moment": "^2.23.0", "prop-types": "^15", "query-string": "^6.2.0", "react": "^16", diff --git a/src/utils/bugzilla/generateChartJsData.js b/src/utils/bugzilla/generateChartJsData.js index 091d60d..79d9708 100644 --- a/src/utils/bugzilla/generateChartJsData.js +++ b/src/utils/bugzilla/generateChartJsData.js @@ -1,26 +1,22 @@ import queryBugzilla from './queryBugzilla'; import generateDatasetStyle from '../chartJs/generateDatasetStyle'; +import toDayOfWeek from '../toDayOfWeek'; import COLORS from '../chartJs/colors'; -const newDate = (datetime, startDate) => { - const onlyDate = datetime.substring(0, 10); - return startDate && (onlyDate < startDate) ? startDate : onlyDate; -}; - /* eslint-disable camelcase */ -// Count bugs created on each day +// Count bugs created/closed each week // startDate allow us to group bugs older than such date -const bugsPerDay = (bugs, startDate) => ( +const bugsGroupedByWeek = (bugs, startDate) => ( bugs.reduce((result, { creation_time, cf_last_resolved }) => { const newResult = Object.assign({}, result); - const createdDate = newDate(creation_time, startDate); + const createdDate = toDayOfWeek(creation_time, startDate); if (!newResult[createdDate]) { newResult[createdDate] = 0; } newResult[createdDate] += 1; if (cf_last_resolved) { - const resolvedDate = newDate(cf_last_resolved, startDate); + const resolvedDate = toDayOfWeek(cf_last_resolved, startDate); if (!newResult[resolvedDate]) { newResult[resolvedDate] = 0; } @@ -32,14 +28,16 @@ const bugsPerDay = (bugs, startDate) => ( ); /* eslint-enable camelcase */ +const sortDates = (a, b) => new Date(a) - new Date(b); + const bugsByCreationDate = (bugs, startDate) => { - // Count bugs created on each day - const byCreationDate = bugsPerDay(bugs, startDate); + // Count bugs created on each week + const byCreationDate = bugsGroupedByWeek(bugs, startDate); let count = 0; let lastDataPoint; const accumulatedCount = Object.keys(byCreationDate) - .sort().reduce((result, date) => { + .sort(sortDates).reduce((result, date) => { count += byCreationDate[date]; // Read more here http://momentjs.com/guides/#/warnings/js-date/ lastDataPoint = { x: new Date(date), y: count }; @@ -48,9 +46,9 @@ const bugsByCreationDate = (bugs, startDate) => { }, []); // This guarantees that the line goes all the way to the end of the graph - const today = new Date(); + const today = toDayOfWeek(); if (lastDataPoint.x !== today) { - accumulatedCount.push({ x: today, y: count }); + accumulatedCount.push({ x: new Date(today), y: count }); } return accumulatedCount; diff --git a/src/utils/toDayOfWeek.js b/src/utils/toDayOfWeek.js new file mode 100644 index 0000000..06cdd0e --- /dev/null +++ b/src/utils/toDayOfWeek.js @@ -0,0 +1,15 @@ +import moment from 'moment'; + +// By default it changes the day to Friday +// We're interested to know the state of bucket by the end of the week +const toDayOfWeek = (dt = new Date(), dayOfWeek = 5) => { + let increment = 0; + // isoWeekDay represents Sunday as 7 instead of 0 + const day = moment(dt).isoWeekday(); + if (day > dayOfWeek) { + increment = 7; + } + return moment(dt).add(increment, 'days').isoWeekday(dayOfWeek).format('YYYY-MM-DD'); +}; + +export default toDayOfWeek; diff --git a/test/utils/toDayOfWeek.test.js b/test/utils/toDayOfWeek.test.js new file mode 100644 index 0000000..f75e633 --- /dev/null +++ b/test/utils/toDayOfWeek.test.js @@ -0,0 +1,29 @@ +import toDayOfWeek from '../../src/utils/toDayOfWeek'; + +it('Monday to Friday', () => { + const newDate = toDayOfWeek('2018-12-31'); + expect(newDate).toBe('2019-01-04'); +}); + +it('Friday to Friday', () => { + const newDate = toDayOfWeek('2019-01-04'); + expect(newDate).toBe('2019-01-04'); +}); + +it('Saturday to next week Friday', () => { + const newDate = toDayOfWeek('2018-12-29'); + expect(newDate).toBe('2019-01-04'); +}); + +it('Sunday to Friday', () => { + const newDate = toDayOfWeek('2018-12-30'); + expect(newDate).toBe('2019-01-04'); +}); + +it('Current - To Friday of current week', () => { + const newDate = toDayOfWeek(); + const myTempDate = new Date(); + const distance = 5 - myTempDate.getDay(); // 5 represents Friday + myTempDate.setDate(myTempDate.getDate() + distance); + expect(newDate).toBe((myTempDate).toISOString().split('T')[0]); +}); diff --git a/yarn.lock b/yarn.lock index de2ceb0..a22a6d8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5982,7 +5982,7 @@ mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0: dependencies: minimist "0.0.8" -moment@^2.10.2: +moment@^2.10.2, moment@^2.23.0: version "2.23.0" resolved "https://registry.yarnpkg.com/moment/-/moment-2.23.0.tgz#759ea491ac97d54bac5ad776996e2a58cc1bc225" integrity sha512-3IE39bHVqFbWWaPOMHZF98Q9c3LDKGTmypMiTM2QygGXXElkFWIH7GxfmlwmY2vwa+wmNsoYZmG2iusf1ZjJoA==