From 2483280e6adbef16013afefd85a0740f792e1227 Mon Sep 17 00:00:00 2001 From: oliveryh Date: Tue, 29 Mar 2022 21:44:32 +0100 Subject: [PATCH 1/2] :bug: bugfix(stats): show last 12 months on month view (fix #139) --- client/src/common/utils.js | 36 ++++++++++++++++++++++++++++++++- client/src/common/utils.test.js | 33 +++++++++++++++++++++++++++--- client/src/views/Stats.vue | 10 +++------ 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/client/src/common/utils.js b/client/src/common/utils.js index 00c0aaf..9fef221 100644 --- a/client/src/common/utils.js +++ b/client/src/common/utils.js @@ -17,6 +17,17 @@ const getWeekNumber = d => { return [year, weekNo] } +const monthSpread = (endDate, numMonths) => { + const endDateObj = new Date(endDate) + const endMonth = endDateObj.getMonth() + 1 + const endYear = endDateObj.getYear() + 1900 + const startMonth = (12 % (endMonth - numMonths)) + 1 + const startYear = endYear - Math.floor(numMonths / 12) + const endMonthString = `${endYear}-${String(endMonth).padStart(2, '0')}` + const startMonthString = `${startYear}-${String(startMonth).padStart(2, '0')}` + return monthSpreadSequential(startMonthString, endMonthString) +} + const weekSpread = (endDate, numWeeks) => { if (endDate != null) { const endDateObj = new Date(endDate) @@ -56,4 +67,27 @@ const weekSpreadSequential = (startYearWeek, endYearWeek) => { }, []) } -module.exports = { weekSpread, weekSpreadSequential } +const monthSpreadSequential = (startYearMonth, endYearMonth) => { + const startYear = Number(startYearMonth.slice(0, 4)) + const startMonth = Number(startYearMonth.slice(5, 7)) + const endYear = Number(endYearMonth.slice(0, 4)) + const endMonth = Number(endYearMonth.slice(5, 7)) + const years = + startYear != endYear + ? [...Array(endYear - startYear + 1).keys()].map(i => endYear + i - 1) + : [startYear] + return years.reduce((monthList, year) => { + const numMonthsInYear = 12 + let monthsInYear = Array.from({ length: numMonthsInYear }, (_, ac) => ac + 1) + if (year == startYear) { + monthsInYear = monthsInYear.filter(a => a >= startMonth) + } + if (year == endYear) { + monthsInYear = monthsInYear.filter(a => a <= endMonth) + } + monthsInYear = monthsInYear.map(a => `${year}-${String(a).padStart(2, '0')}`) + return [...monthList, ...monthsInYear] + }, []) +} + +module.exports = { monthSpread, monthSpreadSequential, weekSpread, weekSpreadSequential } diff --git a/client/src/common/utils.test.js b/client/src/common/utils.test.js index f17dd5f..07f9de5 100644 --- a/client/src/common/utils.test.js +++ b/client/src/common/utils.test.js @@ -1,4 +1,4 @@ -import { weekSpread, weekSpreadSequential } from './utils' +import { monthSpread, monthSpreadSequential, weekSpread, weekSpreadSequential } from './utils' describe('utils', () => { describe('weekSpread', () => { @@ -23,14 +23,41 @@ describe('utils', () => { ]) }) }) - describe('weekSpreadSequential', () => { + describe('monthSpread', () => { it('basic case', () => { - expect(weekSpreadSequential('2021-01', '2021-03')).toEqual([ + expect(monthSpread('2021-05-15', 12)).toEqual([ + '2020-06', + '2020-07', + '2020-08', + '2020-09', + '2020-10', + '2020-11', + '2020-12', '2021-01', '2021-02', '2021-03', + '2021-04', + '2021-05', + ]) + }) + }) + describe('monthSpreadSequential', () => { + it('basic case', () => { + expect(monthSpreadSequential('2021-01', '2021-03')).toEqual(['2021-01', '2021-02', '2021-03']) + }) + it('across years', () => { + expect(monthSpreadSequential('2021-11', '2022-02')).toEqual([ + '2021-11', + '2021-12', + '2022-01', + '2022-02', ]) }) + }) + describe('weekSpreadSequential', () => { + it('basic case', () => { + expect(weekSpreadSequential('2021-01', '2021-03')).toEqual(['2021-01', '2021-02', '2021-03']) + }) it('across years', () => { expect(weekSpreadSequential('2020-52', '2021-02')).toEqual([ '2020-52', diff --git a/client/src/views/Stats.vue b/client/src/views/Stats.vue index ce9ac55..a61ab7f 100644 --- a/client/src/views/Stats.vue +++ b/client/src/views/Stats.vue @@ -229,7 +229,7 @@ let generateQuery = (graphType, level) => { } statFilter['entryDate'] = { greaterThanOrEqualTo: '2021-01-01', - lessThan: '2022-01-01', + lessThan: '2023-01-01', } } groupBy.push(`${level.toUpperCase()}_DESCRIPTION`) @@ -397,18 +397,14 @@ export default { this.$apollo.queries.statsPieCategory.refetch() }, weekSpread: utils.weekSpread, - monthSpread(year) { - return [...Array(12).keys()].map( - (j) => `${year}-${String(j + 1).padStart(2, '0')}`, - ) - }, + monthSpread: utils.monthSpread, getSpread() { if (this.period == 'daily') { return this.dateSpread(this.settings.startDate) } else if (this.period == 'weekly') { return this.weekSpread(this.settings.startDate, this.numWeeks) } else if (this.period == 'monthly') { - return this.monthSpread(2021) + return this.monthSpread(this.settings.startDate, 12) } }, }, From 373a013604ee364bb7ca3cb0193091ca7e26ed09 Mon Sep 17 00:00:00 2001 From: oliveryh Date: Tue, 29 Mar 2022 21:48:01 +0100 Subject: [PATCH 2/2] :wrench: chore: bump package version --- client/package-lock.json | 2 +- client/package.json | 2 +- server-postgraphile/package-lock.json | 2 +- server-postgraphile/package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index 0664fdc..74ada38 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -1,6 +1,6 @@ { "name": "clear-habits-client", - "version": "0.7.1", + "version": "0.7.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/client/package.json b/client/package.json index 552e5af..d9dd054 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "clear-habits-client", - "version": "0.7.1", + "version": "0.7.2", "private": true, "scripts": { "serve": "vue-cli-service serve", diff --git a/server-postgraphile/package-lock.json b/server-postgraphile/package-lock.json index 6d13c02..e3f0a18 100644 --- a/server-postgraphile/package-lock.json +++ b/server-postgraphile/package-lock.json @@ -1,6 +1,6 @@ { "name": "clear-habits-server", - "version": "0.6.0", + "version": "0.7.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/server-postgraphile/package.json b/server-postgraphile/package.json index c28ffe9..95587ff 100644 --- a/server-postgraphile/package.json +++ b/server-postgraphile/package.json @@ -1,6 +1,6 @@ { "name": "clear-habits-server", - "version": "0.7.0", + "version": "0.7.2", "description": "", "main": "index.js", "scripts": {