diff --git a/src/index.js b/src/index.js index 3532f780e7a20613f00751d9471e02995aa3a58e..02de2c6f526c604d305b8ff647677732c54969c5 100644 --- a/src/index.js +++ b/src/index.js @@ -24,6 +24,7 @@ const startDailyDate = manualExecution const startDailyDateString = startDailyDate.format('YYYY-MM-DD') const startLoadDate = moment().subtract(7, 'day') const startLoadDateString = startLoadDate.format('YYYY-MM-DD') +const checkHistoryDate = moment().subtract(8, 'day') const endDate = moment() const endDateString = endDate.format('YYYY-MM-DD') const baseUrl = 'https://gw.prd.api.enedis.fr' @@ -227,6 +228,31 @@ async function checkConsentForLoadCurve( } } +/** + * Function checking if the history is loaded + */ +async function isHistoryLoaded(doctype) { + log('debug', doctype, 'Retrieve data') + const result = await cozyClient.data.findAll(doctype) + if (result && result.length > 0) { + const filtered = result.filter(function(el) { + const elDate = moment({ + year: el.year, + month: el.month, + day: el.day, + minute: el.minute, + }) + return elDate.isBefore(checkHistoryDate) + }) + if (filtered.length > 0) { + return true + } else { + return false + } + } + return false +} + /** * Launch process to handle load data */ @@ -245,13 +271,13 @@ async function launchLoadDataProcess( ) if (fetchedLoadData && fetchedLoadData.length > 0) { log('info', 'Process enedis load data') - const processedLoadData = await processData( - fetchedLoadData, - 'com.grandlyon.enedis.minute', - ['year', 'month', 'day', 'hour', 'minute'] - ) - log('info', 'Agregate enedis load data for hour') - await agregateHourlyData(processedLoadData) + await processData(fetchedLoadData, 'com.grandlyon.enedis.minute', [ + 'year', + 'month', + 'day', + 'hour', + 'minute', + ]) } else { log('info', 'No consent or data for load curve') } @@ -334,40 +360,6 @@ async function agregateMonthAndYearData(data) { } } -/** - * Agregate data from load data (every 30 min) to Hourly data - */ -async function agregateHourlyData(data) { - // Sum year and month values into object with year or year-month as keys - if (data && data.length > 0) { - let hourData = {} - data.forEach(element => { - let key = - element.year + - '-' + - element.month + - '-' + - element.day + - '-' + - element.hour - key in hourData - ? (hourData[key] += element.load) - : (hourData[key] = element.load) - }) - // Agregation for Month data - const agregatedMonthData = await buildAgregatedData( - hourData, - 'com.grandlyon.enedis.hour' - ) - await storeData(agregatedMonthData, 'com.grandlyon.enedis.hour', [ - 'year', - 'month', - 'day', - 'hour', - ]) - } -} - /** * Save data in the right doctype db and prevent duplicated keys */ @@ -458,29 +450,6 @@ async function buildDataFromKey(doctype, key, value) { } } -/** - * Function checking if the history is loaded - */ -async function isHistoryLoaded(doctype) { - log('debug', doctype, 'Retrieve data') - const result = await cozyClient.data.findAll(doctype) - if (result && result.length > 0) { - const filtered = result.filter(function(el) { - return ( - el.year <= startLoadDate.year && - el.month <= startLoadDate.month && - el.day <= startLoadDate.day - ) - }) - if (filtered.length > 0) { - return false - } else { - return true - } - } - return false -} - /** * Function handling special case. * The temporary aggregated data need to be remove in order for the most recent one te be saved.