diff --git a/index.js b/index.js index 716ba6057adbc0751769b026a9439b2448c98782..9948df045f6dc85acfee12388051583881e916a1 100644 --- a/index.js +++ b/index.js @@ -141,27 +141,27 @@ async function start(fields, cozyParameters) { // const apiAuthKey = fields.eglAPIAuthKey const baseUrl = cozyParameters.secret.eglBaseURL const apiAuthKey = cozyParameters.secret.eglAPIAuthKey - log('info', 'Authenticating ...') + log('debug', 'Authenticating ...') const response = await authenticate( fields.login, fields.password, baseUrl, apiAuthKey ) - log('info', 'Successfully logged in') + log('debug', 'Successfully logged in') const eglData = await getData(response, baseUrl, apiAuthKey) if (eglData) { - log('info', 'Process egl daily data') + log('debug', 'Process egl daily data') const processedLoadData = await processData( eglData, rangeDate.day.doctype, rangeDate.day.keys ) - log('info', 'Agregate egl load data for month and year') + log('debug', 'Agregate egl load data for month and year') await agregateMonthAndYearData(processedLoadData) } else { - log('info', 'No data found') + log('debug', 'No data found') } } catch (error) { throw new Error(error.message) @@ -176,14 +176,14 @@ async function start(fields, cozyParameters) { */ async function processData(data, doctype, filterKeys) { // const formatedData = await formateData(data) - log('info', 'processData - data formated') + log('debug', 'processData - data formated') // Remove data for existing days into the DB console.log(data) const filteredData = await hydrateAndFilter(data, doctype, { keys: filterKeys }) console.log(filteredData) - log('info', 'processData - data filtered') + log('debug', 'processData - data filtered') // Store new day data await storeData(filteredData, doctype, filterKeys) return filteredData @@ -229,12 +229,12 @@ async function agregateMonthAndYearData(data) { * Return an Array of agregated data */ async function buildAgregatedData(data, doctype) { - log('info', 'entering buildAgregatedData') + log('debug', 'entering buildAgregatedData') let agregatedData = [] for (let [key, value] of Object.entries(data)) { const data = await buildDataFromKey(doctype, key, value) const oldValue = await resetInProgressAggregatedData(data, doctype) - log('info', 'Dataload + oldvalue is ' + data.load + ' + ' + oldValue) + log('debug', 'Dataload + oldvalue is ' + data.load + ' + ' + oldValue) data.load += oldValue agregatedData.push(data) } @@ -244,13 +244,13 @@ async function buildAgregatedData(data, doctype) { // async function processData(timeStep, response, baseUrl, apiAuthKey) { // const doctype = rangeDate[timeStep].doctype; // const loadProfile = await getData(response, baseUrl, apiAuthKey); -// log("info", "Saving data to Cozy"); +// log("debug", "Saving data to Cozy"); // if (doctype === rangeDate.day.doctype) { // await storeData(loadProfile, rangeDate.day.doctype, rangeDate.day.keys); // } else if (doctype === rangeDate.month.doctype) { // await resetInProgressAggregatedData(rangeDate.month.doctype); // const monthlyData = processMonthlyAggregation(loadProfile, rangeDate.month); -// log("info", "Saving monthly data"); +// log("debug", "Saving monthly data"); // await storeData(monthlyData, rangeDate.month.doctype, rangeDate.month.keys); // } else if (doctype === rangeDate.year.doctype) { // await resetInProgressAggregatedData(rangeDate.year.doctype); @@ -258,7 +258,7 @@ async function buildAgregatedData(data, doctype) { // loadProfile, // rangeDate.year.doctype // ); -// log("info", "Saving yearly data"); +// log("debug", "Saving yearly data"); // await storeData(yearlyData, rangeDate.year.doctype, rangeDate.year.keys); // } else { // throw new Error("Unkonw range type: " + doctype); @@ -288,8 +288,8 @@ async function authenticate(login, password, baseUrl, apiAuthKey) { } async function getData(response, baseUrl, apiAuthKey) { - log('info', 'Start date : ' + startDate) - log('info', 'End date : ' + endDate) + log('debug', 'Start date : ' + startDate) + log('debug', 'End date : ' + endDate) const dataRequest = { method: 'POST', uri: baseUrl + '/getAllAgregatsByAbonnement.aspx', @@ -323,21 +323,21 @@ async function getData(response, baseUrl, apiAuthKey) { } function format(response) { - log('info', 'origin response size is : ' + response.resultatRetour.length) - log('info', 'Raw value: ' ) + log('debug', 'origin response size is : ' + response.resultatRetour.length) + log('debug', 'Raw value: ' ) console.log(response.resultatRetour) const data = response.resultatRetour .slice(1) .filter(value => value.ValeurIndex) - log('info', 'Filterd value: ') + log('debug', 'Filterd value: ') console.log(data) const dataLen = data.length - log('info', 'filtered size is : ' + dataLen) + log('debug', 'filtered size is : ' + dataLen) const mapData = data.map((value, index) => { const time = moment(value.DateReleve, moment.ISO_8601) if (index !== 0 && index < dataLen) { - log('info', 'index: ' + value.ValeurIndex) - log('info', 'index precedent: ' + data[index - 1].ValeurIndex) + log('debug', 'index: ' + value.ValeurIndex) + log('debug', 'index precedent: ' + data[index - 1].ValeurIndex) return { load: value.ValeurIndex - data[index - 1].ValeurIndex, year: parseInt(time.format('YYYY')), @@ -364,13 +364,13 @@ function format(response) { } // function processYearAggregation(data, doctype) { -// log("info", "Start aggregation for : " + doctype); +// log("debug", "Start aggregation for : " + doctype); // const grouped = data.reduce(reduceYearFunction, {}); // return Object.values(grouped); // } // function processMonthlyAggregation(data, range) { -// log("info", "Start aggregation for : " + range.doctype); +// log("debug", "Start aggregation for : " + range.doctype); // // Filter by year // const tmpData = groupBy(data, "year"); // const keys = Object.keys(tmpData); @@ -418,10 +418,10 @@ function format(response) { * Save data in the right doctype db and prevent duplicated keys */ async function storeData(data, doctype, filterKeys) { - log('info', 'Store into ' + doctype) - log('info', 'Store into keys : ' + filterKeys) + log('debug', 'Store into ' + doctype) + log('debug', 'Store into keys : ' + filterKeys) data.map(v => { - log("info", "Saving data " + v.load + " for " + v.day + "/" + v.month + "/" + v.year); + log("debug", "Saving data " + v.load + " for " + v.day + "/" + v.month + "/" + v.year); }); const filteredDocuments = await hydrateAndFilter(data, doctype, { keys: filterKeys @@ -474,7 +474,7 @@ async function buildDataFromKey(doctype, key, value) { */ async function resetInProgressAggregatedData(data, doctype) { // /!\ Warning: cannot use mongo queries because not supported for dev by cozy-konnectors-libs - log('info', 'Remove aggregated data for ' + doctype) + log('debug', 'Remove aggregated data for ' + doctype) const result = await cozyClient.data.findAll(doctype) if (result && result.length > 0) { // Filter data to remove @@ -504,7 +504,7 @@ async function resetInProgressAggregatedData(data, doctype) { let sum = 0.0 for (const doc of filtered) { sum += doc.load - log('info', 'Removing this entry for ' + doc.load) + log('debug', 'Removing this entry for ' + doc.load) await cozyClient.data.delete(doctype, doc) } return sum