Commit d3226808 authored by Yoan VALLET's avatar Yoan VALLET
Browse files

Merge branch 'feat/error_code_log' into 'dev'

fix/error code log

See merge request !2
parents bb854c0b f5797ede
{
"COZY_URL": "http://cozy.tools:8080",
"fields": {
"login": "metropole_de_lyon_grdf",
"password": "P-tndq12i+c?"
}
}
\ No newline at end of file
......@@ -7,7 +7,6 @@ const {
log
} = require('cozy-konnector-libs')
// const https = require('https') //optional for ssl issue
const getAccountId = require('./helpers/getAccountId')
const moment = require('moment')
require('moment-timezone')
......@@ -18,9 +17,13 @@ moment.tz.setDefault('Europe/Paris') // set the timezone
const manualExecution =
process.env.COZY_JOB_MANUAL_EXECUTION === 'true' ? true : false
const startDate = manualExecution
? moment().subtract(1, 'year').format('YYYY-MM-DD')
: moment().subtract(3, 'year').format('YYYY-MM-DD')
? moment()
.subtract(1, 'year')
.format('YYYY-MM-DD')
: moment()
.subtract(3, 'year')
.format('YYYY-MM-DD')
const endDate = moment()
.startOf('day')
.subtract(1, 'day')
......@@ -57,8 +60,6 @@ async function start(fields) {
const grdfData = await getData(fields.access_token, id_pce)
if (grdfData) {
// log("debug", "Clean data retrieve by old scraping konnector")
// await cleanOldData()
log('debug', 'Process grdf daily data')
const processedLoadData = await processData(
grdfData,
......@@ -87,7 +88,7 @@ async function start(fields) {
}
}
//Retrieve data from grdf API
// Retrieve data from grdf API
async function getData(token, idPCE) {
log('debug', 'ENTERING GETDATA')
var myHeaders = new Headers()
......@@ -118,8 +119,24 @@ async function getData(token, idPCE) {
return result.match(/.+/g).map(s => {
result = JSON.parse(s)
if (result.statut_restitution !== null) {
log('debug', 'OAUTH OUTDATED : ' + result.statut_restitution.message)
throw errors.USER_ACTION_NEEDED_OAUTH_OUTDATED
log(
'warn',
'USER_ACTION_NEEDED_OAUTH_OUTDATED : ' +
result.statut_restitution.message +
' / Period: ' +
result.periode.date_debut +
'/' +
result.periode.date_fin
)
/**
* Handle no data issue when retreving grdf data.
* 1000008 code stands for "Il n'y a pas de données correspondant à ce PCE sur la période demandée".
* If there is no data, return null data in order to be filtered before saving
*/
if (result.statut_restitution.code !== '1000008') {
throw errors.USER_ACTION_NEEDED_OAUTH_OUTDATED
}
return { energie: null }
}
return result.consommation
})
......@@ -129,54 +146,11 @@ async function getData(token, idPCE) {
throw error
})
const filteredRep = rep.filter(function(el) {
return (
el.energie != null || el.volume_brut != null
)
return el.energie != null || el.volume_brut != null
})
return filteredRep
}
/**
* Get All actual daily data stored in the db
* Return the list of daily data
*/
// async function cleanOldData() {
// if(moment().diff(moment("2020-12-02", "DD/MM/YYYY")) > 0){
// log('debug', 'No cleaning to do')
// return false
// } else {
// log('debug', 'Start cleaning old data')
// const documents = await cozyClient.data.findAll('com.grandlyon.grdf.day')
// if (documents && documents.length > 0) {
// const result = []
// for (const doc of documents) {
// const deleteResult = await cozyClient.data.delete('com.grandlyon.grdf.day', doc)
// result.push(deleteResult)
// }
// log("debug", "NB OF DELETED OLD DAY DATA : " + result.length)
// }
// const documentsMonth = await cozyClient.data.findAll('com.grandlyon.grdf.month')
// if (documentsMonth && documentsMonth.length > 0) {
// const result = []
// for (const doc of documentsMonth) {
// const deleteResult = await cozyClient.data.delete('com.grandlyon.grdf.month', doc)
// result.push(deleteResult)
// }
// log("debug", "NB OF DELETED OLD MONTH DATA : " + result.length)
// }
// const documentsYear = await cozyClient.data.findAll('com.grandlyon.grdf.year')
// if (documentsYear && documentsYear.length > 0) {
// const result = []
// for (const doc of documentsYear) {
// await cozyClient.data.delete('com.grandlyon.grdf.year', doc)
// result.push(deleteResult)
// }
// log("debug", "NB OF DELETED OLD YEAR DATA : " + result.length)
// }
// return true
// }
// }
/**
* Parse data
* Remove existing data from DB using hydrateAndFilter
......@@ -216,7 +190,7 @@ async function formateData(data) {
return data.map(record => {
let date = moment(record.date_debut_consommation, 'YYYY/MM/DD h:mm:ss')
let load =
record.energie && record.energie !== 0
record.energie && record.energie !== 0
? record.energie
: record.volume_brut * record.coeff_calcul.coeff_conversion
return {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment