Skip to content
Snippets Groups Projects
Commit ef02d5b7 authored by Rémi PAILHAREY's avatar Rémi PAILHAREY :fork_knife_plate:
Browse files

Merge branch '313-aggregation-issue-fix' into 'dev'

313 aggregation issue fix

See merge request !31
parents acfa29b0 3b09d61d
Branches
No related tags found
2 merge requests!33Merge dev into master,!31313 aggregation issue fix
Pipeline #79297 passed
......@@ -38,6 +38,7 @@
"Derniere",
"Detaillees",
"enedissgegrandlyon",
"EPGL",
"Etage",
"etat",
"faultstring",
......
......@@ -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,
}
......@@ -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(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment