Skip to content
Snippets Groups Projects
Commit 9021edaa authored by Hugo's avatar Hugo
Browse files

a

parent 628847ea
No related branches found
No related tags found
1 merge request!4Dev
...@@ -5,38 +5,38 @@ const { ...@@ -5,38 +5,38 @@ const {
addData, addData,
hydrateAndFilter, hydrateAndFilter,
cozyClient cozyClient
} = require("cozy-konnector-libs"); } = require('cozy-konnector-libs')
const rp = require("request-promise"); const rp = require('request-promise')
const moment = require("moment"); const moment = require('moment')
require("moment-timezone"); require('moment-timezone')
moment.locale("fr"); // set the language moment.locale('fr') // set the language
moment.tz.setDefault("Europe/Paris"); // set the timezone moment.tz.setDefault('Europe/Paris') // set the timezone
const startDate = moment() const startDate = moment()
.startOf("year") .startOf('year')
// .subtract(3, 'year') TODO PUT TERNARY EXPRESSION FOR MANUAL LAUNCH // .subtract(3, 'year') TODO PUT TERNARY EXPRESSION FOR MANUAL LAUNCH
.subtract(1, "year") .subtract(1, 'year')
.subtract(1, "day") .subtract(1, 'day')
.format("MM/DD/YYYY"); .format('MM/DD/YYYY')
const endDate = moment().format("MM/DD/YYYY"); const endDate = moment().format('MM/DD/YYYY')
const timeRange = ["day", "month", "year"]; const timeRange = ['day', 'month', 'year']
const rangeDate = { const rangeDate = {
day: { day: {
doctype: "com.grandlyon.egl.day", doctype: 'com.grandlyon.egl.day',
keys: ["year", "month", "day"] keys: ['year', 'month', 'day']
}, },
month: { month: {
doctype: "com.grandlyon.egl.month", doctype: 'com.grandlyon.egl.month',
keys: ["year", "month"] keys: ['year', 'month']
}, },
year: { year: {
doctype: "com.grandlyon.egl.year", doctype: 'com.grandlyon.egl.year',
keys: ["year"] keys: ['year']
} }
}; }
module.exports = new BaseKonnector(start); module.exports = new BaseKonnector(start)
// The start function is run by the BaseKonnector instance only when it got all the account // The start function is run by the BaseKonnector instance only when it got all the account
// information (fields). When you run this connector yourself in "standalone" mode or "dev" mode, // information (fields). When you run this connector yourself in "standalone" mode or "dev" mode,
...@@ -45,82 +45,82 @@ async function start(fields, cozyParameters) { ...@@ -45,82 +45,82 @@ async function start(fields, cozyParameters) {
try { try {
// resetting data for demo only // resetting data for demo only
// await resetData() // await resetData()
const baseUrl = cozyParameters.secret.eglBaseURL; const baseUrl = cozyParameters.secret.eglBaseURL
const apiAuthKey = cozyParameters.secret.eglAPIAuthKey; const apiAuthKey = cozyParameters.secret.eglAPIAuthKey
log("info", "Authenticating ..."); log('info', 'Authenticating ...')
const response = await authenticate( const response = await authenticate(
fields.login, fields.login,
fields.password, fields.password,
baseUrl, baseUrl,
apiAuthKey apiAuthKey
); )
log("info", "Successfully logged in"); log('info', 'Successfully logged in')
Promise.all( Promise.all(
timeRange.map(timeStep => timeRange.map(timeStep =>
processData(timeStep, response, baseUrl, apiAuthKey) processData(timeStep, response, baseUrl, apiAuthKey)
) )
); )
} catch (error) { } catch (error) {
throw new Error(error.message); throw new Error(error.message)
} }
} }
async function processData(timeStep, response, baseUrl, apiAuthKey) { async function processData(timeStep, response, baseUrl, apiAuthKey) {
const doctype = rangeDate[timeStep].doctype; const doctype = rangeDate[timeStep].doctype
log("info", "Getting data TIMESTEP : " + timeStep); log('info', 'Getting data TIMESTEP : ' + timeStep)
const loadProfile = await getData(response, baseUrl, apiAuthKey); const loadProfile = await getData(response, baseUrl, apiAuthKey)
log("info", "Saving data to Cozy"); log('info', 'Saving data to Cozy')
if (doctype === rangeDate.day.doctype) { if (doctype === rangeDate.day.doctype) {
log("info", "Saving daily data" + loadProfile); log('info', 'Saving daily data' + loadProfile)
await storeData(loadProfile, rangeDate.day.doctype, rangeDate.day.keys); await storeData(loadProfile, rangeDate.day.doctype, rangeDate.day.keys)
} else if (doctype === rangeDate.month.doctype) { } else if (doctype === rangeDate.month.doctype) {
await resetInProgressAggregatedData(rangeDate.month.doctype); await resetInProgressAggregatedData(rangeDate.month.doctype)
const monthlyData = processMonthlyAggregation(loadProfile, rangeDate.month); const monthlyData = processMonthlyAggregation(loadProfile, rangeDate.month)
log("info", "Saving monthly data" + monthlyData); log('info', 'Saving monthly data' + monthlyData)
await storeData(monthlyData, rangeDate.month.doctype, rangeDate.month.keys); await storeData(monthlyData, rangeDate.month.doctype, rangeDate.month.keys)
} else if (doctype === rangeDate.year.doctype) { } else if (doctype === rangeDate.year.doctype) {
await resetInProgressAggregatedData(rangeDate.year.doctype); await resetInProgressAggregatedData(rangeDate.year.doctype)
const yearlyData = processYearAggregation( const yearlyData = processYearAggregation(
loadProfile, loadProfile,
rangeDate.year.doctype rangeDate.year.doctype
); )
log("info", "Saving yearly data" + yearlyData); log('info', 'Saving yearly data' + yearlyData)
await storeData(yearlyData, rangeDate.year.doctype, rangeDate.year.keys); await storeData(yearlyData, rangeDate.year.doctype, rangeDate.year.keys)
} else { } else {
throw new Error("Unkonw range type: " + doctype); throw new Error('Unkonw range type: ' + doctype)
} }
} }
async function authenticate(login, password, baseUrl, apiAuthKey) { async function authenticate(login, password, baseUrl, apiAuthKey) {
const authRequest = { const authRequest = {
method: "POST", method: 'POST',
uri: baseUrl + "/connect.aspx", uri: baseUrl + '/connect.aspx',
headers: { headers: {
AuthKey: apiAuthKey, AuthKey: apiAuthKey,
"Content-Type": "application/x-www-form-urlencoded" 'Content-Type': 'application/x-www-form-urlencoded'
}, },
form: { form: {
login: login, login: login,
pass: password pass: password
}, },
json: true json: true
}; }
const response = await rp(authRequest); const response = await rp(authRequest)
if (response.codeRetour === 100) { if (response.codeRetour === 100) {
return response; return response
} else { } else {
throw new Error(errors.LOGIN_FAILED); throw new Error(errors.LOGIN_FAILED)
} }
} }
async function getData(response, baseUrl, apiAuthKey) { async function getData(response, baseUrl, apiAuthKey) {
log("debug", "Start date : " + startDate); log('debug', 'Start date : ' + startDate)
log("debug", "End date : " + endDate); log('debug', 'End date : ' + endDate)
const dataRequest = { const dataRequest = {
method: "POST", method: 'POST',
uri: baseUrl + "/getAllAgregatsByAbonnement.aspx", uri: baseUrl + '/getAllAgregatsByAbonnement.aspx',
headers: { headers: {
AuthKey: apiAuthKey, AuthKey: apiAuthKey,
"Content-Type": "application/x-www-form-urlencoded" 'Content-Type': 'application/x-www-form-urlencoded'
}, },
form: { form: {
token: response.resultatRetour.token, token: response.resultatRetour.token,
...@@ -129,139 +129,139 @@ async function getData(response, baseUrl, apiAuthKey) { ...@@ -129,139 +129,139 @@ async function getData(response, baseUrl, apiAuthKey) {
date_fin: endDate date_fin: endDate
}, },
json: true json: true
}; }
try { try {
const responseEgl = await rp(dataRequest); const responseEgl = await rp(dataRequest)
switch (responseEgl.codeRetour) { switch (responseEgl.codeRetour) {
case 100: case 100:
return format(responseEgl); return format(responseEgl)
case -2: case -2:
throw new Error(errors.LOGIN_FAILED); throw new Error(errors.LOGIN_FAILED)
case -1: case -1:
throw new Error(errors.VENDOR_DOWN); throw new Error(errors.VENDOR_DOWN)
default: default:
throw new Error(errors.UNKNOWN_ERROR); throw new Error(errors.UNKNOWN_ERROR)
} }
} catch (error) { } catch (error) {
throw new Error(errors.VENDOR_DOWN); throw new Error(errors.VENDOR_DOWN)
} }
} }
function format(response) { function format(response) {
const data = response.resultatRetour const data = response.resultatRetour
.slice(1) .slice(1)
.filter(value => value.ValeurIndex); .filter(value => value.ValeurIndex)
const dataLen = data.length; const dataLen = data.length
data.map((value, index) => { data.map((value, index) => {
const time = moment(value.DateReleve, moment.ISO_8601); const time = moment(value.DateReleve, moment.ISO_8601)
if (index + 1 < dataLen) { if (index + 1 < dataLen) {
log( log(
"info", 'info',
"date -> " + 'date -> ' +
value.DateReleve + value.DateReleve +
" SUBSTRACTING : " + ' SUBSTRACTING : ' +
value.ValeurIndex + value.ValeurIndex +
" - " + ' - ' +
response.resultatRetour[index].ValeurIndex + response.resultatRetour[index].ValeurIndex +
"\n" '\n'
); )
return { return {
load: data[index + 1].ValeurIndex - value.ValeurIndex, load: data[index + 1].ValeurIndex - value.ValeurIndex,
year: parseInt(time.format("YYYY")), year: parseInt(time.format('YYYY')),
month: parseInt(time.format("M")), month: parseInt(time.format('M')),
day: parseInt(time.format("D")), day: parseInt(time.format('D')),
hour: 0, hour: 0,
minute: 0, minute: 0,
type: value.TypeAgregat type: value.TypeAgregat
}; }
} else { } else {
return { return {
load: null, load: null,
year: parseInt(time.format("YYYY")), year: parseInt(time.format('YYYY')),
month: parseInt(time.format("M")), month: parseInt(time.format('M')),
day: parseInt(time.format("D")), day: parseInt(time.format('D')),
hour: 0, hour: 0,
minute: 0, minute: 0,
type: value.TypeAgregat type: value.TypeAgregat
}; }
} }
}); })
log("info", "DATALOAD LIST\n"); log('info', 'DATALOAD LIST\n')
data.map(value => { data.map(value => {
log( log(
"info", 'info',
value.month + value.month +
"-" + '-' +
value.day + value.day +
"-" + '-' +
value.year + value.year +
" : " + ' : ' +
value.load + value.load +
"\n" '\n'
); )
}); })
return data; return data
} }
function processYearAggregation(data, doctype) { function processYearAggregation(data, doctype) {
log("info", "Start aggregation for : " + doctype); log('info', 'Start aggregation for : ' + doctype)
const grouped = data.reduce(reduceYearFunction, {}); const grouped = data.reduce(reduceYearFunction, {})
return Object.values(grouped); return Object.values(grouped)
} }
function processMonthlyAggregation(data, range) { function processMonthlyAggregation(data, range) {
log("info", "Start aggregation for : " + range.doctype); log('info', 'Start aggregation for : ' + range.doctype)
// Filter by year // Filter by year
const tmpData = groupBy(data, "year"); const tmpData = groupBy(data, 'year')
const keys = Object.keys(tmpData); const keys = Object.keys(tmpData)
var dataToStore = []; var dataToStore = []
// Monthly aggregation // Monthly aggregation
for (const index in keys) { for (const index in keys) {
// Get daily data of a year // Get daily data of a year
var monthlyData = tmpData[keys[index]]; var monthlyData = tmpData[keys[index]]
// Monthly aggregation // Monthly aggregation
var aggregatedData = monthlyData.reduce(reduceMonthFunction, {}); var aggregatedData = monthlyData.reduce(reduceMonthFunction, {})
// Store it // Store it
dataToStore = dataToStore.concat(Object.values(aggregatedData)); dataToStore = dataToStore.concat(Object.values(aggregatedData))
} }
return dataToStore; return dataToStore
} }
function groupBy(xs, key) { function groupBy(xs, key) {
return xs.reduce(function(rv, x) { return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x); ;(rv[x[key]] = rv[x[key]] || []).push(x)
return rv; return rv
}, {}); }, {})
} }
function reduceYearFunction(acc, x) { function reduceYearFunction(acc, x) {
var id = acc[x.year]; var id = acc[x.year]
if (id) { if (id) {
id.load += x.load; id.load += x.load
} else { } else {
acc[x.year] = x; acc[x.year] = x
} }
return acc; return acc
} }
function reduceMonthFunction(acc, x) { function reduceMonthFunction(acc, x) {
var id = acc[x.month]; var id = acc[x.month]
if (id) { if (id) {
id.load += x.load; id.load += x.load
} else { } else {
acc[x.month] = x; acc[x.month] = x
} }
return acc; return acc
} }
async function storeData(data, doctype, keys) { async function storeData(data, doctype, keys) {
log("debug", "Store into " + doctype); log('debug', 'Store into ' + doctype)
log("debug", keys, "Store into keys "); log('debug', keys, 'Store into keys ')
return hydrateAndFilter(data, doctype, { return hydrateAndFilter(data, doctype, {
keys: keys keys: keys
}).then(filteredDocuments => { }).then(filteredDocuments => {
addData(filteredDocuments, doctype); addData(filteredDocuments, doctype)
}); })
} }
/** /**
...@@ -273,33 +273,33 @@ async function storeData(data, doctype, keys) { ...@@ -273,33 +273,33 @@ async function storeData(data, doctype, keys) {
*/ */
async function resetInProgressAggregatedData(doctype) { async function resetInProgressAggregatedData(doctype) {
// /!\ Warning: cannot use mongo queries because not supported for dev by cozy-konnectors-libs // /!\ Warning: cannot use mongo queries because not supported for dev by cozy-konnectors-libs
log("debug", doctype, "Remove aggregated data for"); log('debug', doctype, 'Remove aggregated data for')
const result = await cozyClient.data.findAll(doctype); const result = await cozyClient.data.findAll(doctype)
if (result.error || result.length <= 0) { if (result.error || result.length <= 0) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.warn("Error while fetching loads, doctype not found "); console.warn('Error while fetching loads, doctype not found ')
} else { } else {
const currentDate = moment(); const currentDate = moment()
// Filter data to remove // Filter data to remove
var filtered = []; var filtered = []
if (doctype === rangeDate.year.doctype) { if (doctype === rangeDate.year.doctype) {
// Yearly case // Yearly case
filtered = result.filter(function(el) { filtered = result.filter(function(el) {
return el.year == currentDate.year(); return el.year == currentDate.year()
}); })
} else { } else {
// Monthly case // Monthly case
filtered = result.filter(function(el) { filtered = result.filter(function(el) {
return ( return (
el.year == currentDate.year() && el.year == currentDate.year() &&
el.month == parseInt(moment().format("M")) el.month == parseInt(moment().format('M'))
); )
}); })
} }
// Remove data // Remove data
for (const doc of filtered) { for (const doc of filtered) {
log("debug", doc, "Removing this entry for " + doctype); log('debug', doc, 'Removing this entry for ' + doctype)
await cozyClient.data.delete(doctype, doc); await cozyClient.data.delete(doctype, doc)
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment