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

chore: jsdoc and format

parent 6210f57c
Branches
No related tags found
1 merge request!30313 aggregation issue
Pipeline #76603 passed
...@@ -47,8 +47,9 @@ const rangeDate = { ...@@ -47,8 +47,9 @@ const rangeDate = {
} }
module.exports = new BaseKonnector(start) module.exports = new BaseKonnector(start)
/** /**
* Sentry * Sentry configuration
*/ */
Sentry.init({ Sentry.init({
dsn: dsn:
...@@ -67,9 +68,14 @@ Sentry.init({ ...@@ -67,9 +68,14 @@ Sentry.init({
], ],
}) })
// 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 start function is run by the BaseKonnector instance only when it receives all the account
// the account information come from ./konnector-dev-config.json file * information (fields). When you run this connector in 'standalone' mode or 'dev' mode,
* the account information comes from the ./konnector-dev-config.json file.
*
* @param {object} fields - The account information fields.
* @param {object} cozyParameters - Cozy platform parameters.
*/
async function start(fields, cozyParameters) { async function start(fields, cozyParameters) {
const transaction = Sentry.startTransaction({ const transaction = Sentry.startTransaction({
op: 'konnector', op: 'konnector',
...@@ -93,8 +99,9 @@ async function start(fields, cozyParameters) { ...@@ -93,8 +99,9 @@ async function start(fields, cozyParameters) {
log('info', 'Successfully logged in') log('info', 'Successfully logged in')
const eglData = await getData(response, baseUrl, apiAuthKey) const eglData = await getData(response, baseUrl, apiAuthKey)
if (eglData) { if (eglData) {
log('debug', 'Process egl daily data') log('debug', 'Process EGL daily data')
const filteredDocuments = await hydrateAndFilter( const filteredDocuments = await hydrateAndFilter(
eglData, eglData,
rangeDate.day.doctype, rangeDate.day.doctype,
...@@ -104,18 +111,18 @@ async function start(fields, cozyParameters) { ...@@ -104,18 +111,18 @@ async function start(fields, cozyParameters) {
) )
await addData(filteredDocuments, rangeDate.day.doctype) await addData(filteredDocuments, rangeDate.day.doctype)
log('debug', 'Aggregate egl monthly load data') log('debug', 'Aggregate EGL monthly load data')
const monthlyLoads = aggregateMonthlyLoad(eglData) const monthlyLoads = aggregateMonthlyLoad(eglData)
log('debug', 'Store aggregated egl monthly load data') log('debug', 'Store aggregated EGL monthly load data')
await updateOrCreate( await updateOrCreate(
monthlyLoads, monthlyLoads,
rangeDate.month.doctype, rangeDate.month.doctype,
rangeDate.month.keys rangeDate.month.keys
) )
log('debug', 'Aggregate egl yearly load data') log('debug', 'Aggregate EGL yearly load data')
const yearlyLoads = aggregateYearlyLoad(monthlyLoads) const yearlyLoads = aggregateYearlyLoad(monthlyLoads)
log('debug', 'Store aggregated egl yearly load data') log('debug', 'Store aggregated EGL yearly load data')
await updateOrCreate( await updateOrCreate(
yearlyLoads, yearlyLoads,
rangeDate.year.doctype, rangeDate.year.doctype,
...@@ -124,6 +131,7 @@ async function start(fields, cozyParameters) { ...@@ -124,6 +131,7 @@ async function start(fields, cozyParameters) {
} else { } else {
log('debug', 'No data found') log('debug', 'No data found')
} }
transaction.setStatus(Tracing.SpanStatus.Ok) transaction.setStatus(Tracing.SpanStatus.Ok)
transaction.finish() transaction.finish()
} catch (error) { } catch (error) {
...@@ -142,6 +150,13 @@ async function start(fields, cozyParameters) { ...@@ -142,6 +150,13 @@ async function start(fields, cozyParameters) {
} }
} }
/**
* Aggregates the load data by month, summing the load of each day for each month.
*
* @param {FormattedData[]} data - The data to aggregate.
*
* @returns {FormattedData[]} - An array of aggregated data by month.
*/
function aggregateMonthlyLoad(data) { function aggregateMonthlyLoad(data) {
const monthlyLoad = {} const monthlyLoad = {}
...@@ -167,11 +182,19 @@ function aggregateMonthlyLoad(data) { ...@@ -167,11 +182,19 @@ function aggregateMonthlyLoad(data) {
return Object.values(monthlyLoad) return Object.values(monthlyLoad)
} }
/**
* Aggregates the load data by year, summing the load of each month for each year.
*
* @param {FormattedData[]} data - The data to aggregate.
*
* @returns {FormattedData[]} - An array of aggregated data by year.
*/
function aggregateYearlyLoad(data) { function aggregateYearlyLoad(data) {
const yearlyLoad = {} const yearlyLoad = {}
for (const entry of data) { for (const entry of data) {
const { year, load } = entry const { year, load } = entry
if (!yearlyLoad[year]) { if (!yearlyLoad[year]) {
yearlyLoad[year] = { yearlyLoad[year] = {
load: load, load: load,
...@@ -260,7 +283,7 @@ async function authenticate(login, password, baseUrl, apiAuthKey) { ...@@ -260,7 +283,7 @@ async function authenticate(login, password, baseUrl, apiAuthKey) {
* @typedef {Object} GetDataResponse * @typedef {Object} GetDataResponse
* @property {number} codeRetour * @property {number} codeRetour
* @property {string} libelleRetour * @property {string} libelleRetour
* @property {Array<Releve>} resultatRetour * @property {Releve[]} resultatRetour
*/ */
/** /**
...@@ -273,12 +296,12 @@ async function authenticate(login, password, baseUrl, apiAuthKey) { ...@@ -273,12 +296,12 @@ async function authenticate(login, password, baseUrl, apiAuthKey) {
/** /**
* Retrieves data from a specified API using the provided response data and API configuration. * Retrieves data from a specified API using the provided response data and API configuration.
* *
* @param {ApiResponse} response - The authentication response containing a valid token. * @param {AuthResponse} response - The authentication response containing a valid token.
* @param {string} baseUrl - The base URL for the data request. * @param {string} baseUrl - The base URL for the data request.
* @param {string} apiAuthKey - The API authentication key. * @param {string} apiAuthKey - The API authentication key.
* *
* @throws {Error} - Throws an error with an error code in case of data retrieval failure. * @throws {Error} - Throws an error with an error code in case of data retrieval failure.
* @returns {Promise<FormattedData>} - A promise that resolves to the retrieved and formatted data. * @returns {Promise<FormattedData[]>} - A promise that resolves to the retrieved and formatted data.
*/ */
async function getData(response, baseUrl, apiAuthKey) { async function getData(response, baseUrl, apiAuthKey) {
const dataRequest = { const dataRequest = {
...@@ -304,6 +327,7 @@ async function getData(response, baseUrl, apiAuthKey) { ...@@ -304,6 +327,7 @@ async function getData(response, baseUrl, apiAuthKey) {
respData.resultatRetour.sort(function(a, b) { respData.resultatRetour.sort(function(a, b) {
return new Date(a.DateReleve) - new Date(b.DateReleve) return new Date(a.DateReleve) - new Date(b.DateReleve)
}) })
switch (respData.codeRetour) { switch (respData.codeRetour) {
case 100: case 100:
return format(respData) return format(respData)
...@@ -337,6 +361,7 @@ async function getData(response, baseUrl, apiAuthKey) { ...@@ -337,6 +361,7 @@ async function getData(response, baseUrl, apiAuthKey) {
end: endDate, end: endDate,
}, },
}) })
if (axios.isAxiosError(error)) { if (axios.isAxiosError(error)) {
throw new Error(errors.VENDOR_DOWN) throw new Error(errors.VENDOR_DOWN)
} }
...@@ -363,14 +388,14 @@ async function getData(response, baseUrl, apiAuthKey) { ...@@ -363,14 +388,14 @@ async function getData(response, baseUrl, apiAuthKey) {
* @param {GetDataResponse} response - The data response to be formatted. * @param {GetDataResponse} response - The data response to be formatted.
* *
* @throws {Error} - Throws an error with an error code in case of data processing failure. * @throws {Error} - Throws an error with an error code in case of data processing failure.
* @returns {Array<FormattedData>} - An array of formatted data objects. * @returns {FormattedData[]} - An array of formatted data objects.
*/ */
function format(response) { function format(response) {
log('info', 'origin response size is: ' + response.resultatRetour.length) log('info', 'origin response size is: ' + response.resultatRetour.length)
// Store first value as reference for index processing // Store first value as reference for index processing
let refValue = response.resultatRetour[0] let refValue = response.resultatRetour[0]
// Create copy of data without first value // Create a copy of data without the first value
const data = response.resultatRetour const data = response.resultatRetour
.slice(1) .slice(1)
.filter(value => value.ValeurIndex) .filter(value => value.ValeurIndex)
...@@ -392,7 +417,7 @@ function format(response) { ...@@ -392,7 +417,7 @@ function format(response) {
throw errors.VENDOR_DOWN throw errors.VENDOR_DOWN
} }
// Change index ref value // Change the index reference value
refValue = value refValue = value
return { return {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment