Newer
Older
const {
BaseKonnector,
log,
errors,
addData,
hydrateAndFilter,
cozyClient
const rp = require("request-promise");
const moment = require("moment");
require("moment-timezone");
moment.locale("fr"); // set the language
moment.tz.setDefault("Europe/Paris"); // set the timezone
const manualExecution = process.env.COZY_JOB_MANUAL_EXECUTION === "true" ? true : false
const startDate = manualExecution
? moment().startOf("year").subtract(1, "year").format("MM/DD/YYYY")
: moment().startOf("year").subtract(3, "year").format("MM/DD/YYYY")
const endDate = moment().format("MM/DD/YYYY");
const timeRange = ["day", "month", "year"];
// 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,
// the account information come from ./konnector-dev-config.json file
async function start(fields, cozyParameters) {
try {
// resetting data for demo only
// await resetData()
const baseUrl = cozyParameters.secret.eglBaseURL;
const apiAuthKey = cozyParameters.secret.eglAPIAuthKey;
log("info", "Authenticating ...");
const response = await authenticate(
fields.login,
fields.password,
baseUrl,
apiAuthKey
Promise.all(
timeRange.map(timeStep =>
processData(timeStep, response, baseUrl, apiAuthKey)
)
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");
if (doctype === rangeDate.day.doctype) {
} else if (doctype === rangeDate.month.doctype) {
await resetInProgressAggregatedData(rangeDate.month.doctype);
const monthlyData = processMonthlyAggregation(loadProfile, rangeDate.month);
} else if (doctype === rangeDate.year.doctype) {
const yearlyData = processYearAggregation(
loadProfile,
rangeDate.year.doctype
async function authenticate(login, password, baseUrl, apiAuthKey) {
const authRequest = {
},
form: {
login: login,
pass: password
},
json: true
}
}
async function getData(response, baseUrl, apiAuthKey) {
log("debug", "Start date : " + startDate);
log("debug", "End date : " + endDate);
},
form: {
token: response.resultatRetour.token,
num_abt: response.resultatRetour.num_abt,
date_debut: startDate,
date_fin: endDate
},
json: true
const mapData = data.map((value, index) => {
const time = moment(value.DateReleve, moment.ISO_8601);
if (index + 1 < dataLen) {
return {
load: data[index + 1].ValeurIndex - value.ValeurIndex,
year: parseInt(time.format("YYYY")),
month: parseInt(time.format("M")),
day: parseInt(time.format("D")),
hour: 0,
minute: 0,
type: value.TypeAgregat
year: parseInt(time.format("YYYY")),
month: parseInt(time.format("M")),
day: parseInt(time.format("D")),
function processYearAggregation(data, doctype) {
log("info", "Start aggregation for : " + doctype);
const grouped = data.reduce(reduceYearFunction, {});
return Object.values(grouped);
}
function processMonthlyAggregation(data, range) {
const tmpData = groupBy(data, "year");
const keys = Object.keys(tmpData);
var dataToStore = [];
// Monthly aggregation
for (const index in keys) {
// Get daily data of a year
}
function groupBy(xs, key) {
return xs.reduce(function(rv, x) {
}
function reduceYearFunction(acc, x) {
}
function reduceMonthFunction(acc, x) {
/**
* Save data in the right doctype db and prevent duplicated keys
*/
async function storeData(data, doctype, filterKeys) {
// data.map(v => {
// log("info", "Saving data " + v.load + " for " + v.day + "/" + v.month + "/" + v.year);
// });
const filteredDocuments = await hydrateAndFilter(data, doctype, {
keys: filterKeys
})
return await addData(filteredDocuments, doctype)
/**
* Function handling special case.
* The temporary aggregated data need to be remove in order for the most recent one te be saved.
* { load: 76.712, month: 2020, ... } need to be replace by
* { load: 82.212, month: 2020, ... } after grdf data reprocess
*/
async function resetInProgressAggregatedData(doctype) {
// /!\ Warning: cannot use mongo queries because not supported for dev by cozy-konnectors-libs
log("debug", doctype, "Remove aggregated data for");
const result = await cozyClient.data.findAll(doctype);
if (result.error || result.length <= 0) {
if (doctype === rangeDate.year.doctype) {
// Yearly case
filtered = result.filter(function(el) {
} else {
// Monthly case
filtered = result.filter(function(el) {
return (
el.year == currentDate.year() &&
}
// Remove data
for (const doc of filtered) {
log("debug", doc, "Removing this entry for " + doctype);
await cozyClient.data.delete(doctype, doc);