From 3b09d61d087c5ebffe845b75130e29c5295fc13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20PAILHAREY?= <rpailharey@grandlyon.com> Date: Mon, 30 Oct 2023 16:24:48 +0000 Subject: [PATCH] fix: use first retrieved data to filter aggregates --- .vscode/settings.json | 1 + src/helpers/utils.js | 21 +++++++++++---------- src/index.js | 16 ++++++++-------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3de1abc..6fda245 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -38,6 +38,7 @@ "Derniere", "Detaillees", "enedissgegrandlyon", + "EPGL", "Etage", "etat", "faultstring", diff --git a/src/helpers/utils.js b/src/helpers/utils.js index 1b26566..1e4f4a0 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -66,21 +66,22 @@ function aggregateYearlyLoad(data) { } /** - * Removes the month aggregate that has the same year and month as startDate from the input array if it already exists in database. + * Removes aggregates matching the same year and month as the first data retrieved from EPGL if it is already in database. * * This step is needed to avoid updating the 3 years old month aggregate with incomplete data * - * @param {DateTime} startDate + * @param {number} firstMonth + * @param {number} firstYear * @param {FormattedData[]} yearlyLoads * @returns {Promise<FormattedData[]>} */ -async function filterStartDateMonthlyLoad(startDate, monthlyLoads) { +async function filterFirstMonthlyLoad(firstMonth, firstYear, monthlyLoads) { const monthlyLoadRef = await cozyClient.data.defineIndex( rangeDate.month.doctype, rangeDate.month.keys ) const starDateMonthlyLoad = await cozyClient.data.query(monthlyLoadRef, { - selector: { year: startDate.year, month: startDate.month }, + selector: { year: firstYear, month: firstMonth }, limit: 1, }) @@ -95,21 +96,21 @@ async function filterStartDateMonthlyLoad(startDate, monthlyLoads) { } /** - * Removes the year aggregate that has the same year as startDate from the given array if it already exists in database. + * Removes aggregates matching the same year as the first data retrieved from EPGL if it is already in database. * * This step is needed to avoid updating the 3 years old year aggregate with incomplete data * - * @param {DateTime} startDate + * @param {number} firstYear * @param {FormattedData[]} yearlyLoads * @returns {Promise<FormattedData[]>} */ -async function filterStartDateYearlyLoad(startDate, yearlyLoads) { +async function filterFirstYearlyLoad(firstYear, yearlyLoads) { const yearlyLoadRef = await cozyClient.data.defineIndex( rangeDate.year.doctype, rangeDate.year.keys ) const startDateYearlyLoad = await cozyClient.data.query(yearlyLoadRef, { - selector: { year: startDate.year }, + selector: { year: firstYear }, limit: 1, }) if (!startDateYearlyLoad.length) { @@ -123,6 +124,6 @@ async function filterStartDateYearlyLoad(startDate, yearlyLoads) { module.exports = { aggregateMonthlyLoad, aggregateYearlyLoad, - filterStartDateMonthlyLoad, - filterStartDateYearlyLoad, + filterFirstMonthlyLoad, + filterFirstYearlyLoad, } diff --git a/src/index.js b/src/index.js index 61e4927..6c065ec 100644 --- a/src/index.js +++ b/src/index.js @@ -16,9 +16,9 @@ const { isDev } = require('./helpers/env') const { rangeDate } = require('./types/constants') const { aggregateMonthlyLoad, - filterStartDateMonthlyLoad, + filterFirstMonthlyLoad, aggregateYearlyLoad, - filterStartDateYearlyLoad, + filterFirstYearlyLoad, } = require('./helpers/utils') require('./types/types') @@ -110,12 +110,15 @@ async function start(fields, cozyParameters) { rangeDate.day.doctype, rangeDate.day.keys ) + const { year: firstYear, month: firstMonth } = eglData[0] + log('debug', 'Aggregate EGL yearly load data') const monthlyLoads = aggregateMonthlyLoad(eglData) log('debug', 'Filter first month aggregate if already in database') - const filteredMonthlyLoads = filterStartDateMonthlyLoad( - startDate, + const filteredMonthlyLoads = filterFirstMonthlyLoad( + firstMonth, + firstYear, monthlyLoads ) @@ -130,10 +133,7 @@ async function start(fields, cozyParameters) { const yearlyLoads = aggregateYearlyLoad(monthlyLoads) log('debug', 'Filter first year aggregate if already in database') - const filteredYearlyLoads = filterStartDateYearlyLoad( - startDate, - yearlyLoads - ) + const filteredYearlyLoads = filterFirstYearlyLoad(firstYear, yearlyLoads) log('debug', 'Store aggregated EGL yearly load data') await updateOrCreate( -- GitLab