Skip to content
Snippets Groups Projects
index.js 8.77 MiB
Newer Older
/******/ (() => { // webpackBootstrap
/******/ 	var __webpack_modules__ = ([
Romain CREY's avatar
Romain CREY committed
/* 0 */
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
Romain CREY's avatar
Romain CREY committed

const {
  BaseKonnector,
  log,
  errors,
  addData,
  hydrateAndFilter,
  cozyClient,
} = __webpack_require__(1)
const fetch = __webpack_require__(469)
const moment = __webpack_require__(1374)
__webpack_require__(1511)
moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone

const Sentry = __webpack_require__(1514)
const Tracing = __webpack_require__(1596) // Needed for tracking performance in Sentry
const { version } = __webpack_require__(1633)
const { isDev } = __webpack_require__(1634)

const manualExecution =
  process.env.COZY_JOB_MANUAL_EXECUTION === 'true' ? true : false

const startDate = manualExecution
      .subtract(1, 'year')
      .format('MM/DD/YYYY')
      .subtract(3, 'year')
      .format('MM/DD/YYYY')
const endDate = moment().format('MM/DD/YYYY')
const rangeDate = {
  day: {
    doctype: 'com.grandlyon.egl.day',
    keys: ['year', 'month', 'day'],
    doctype: 'com.grandlyon.egl.month',
    keys: ['year', 'month'],
    doctype: 'com.grandlyon.egl.year',
    keys: ['year'],
  },
}

module.exports = new BaseKonnector(start)
/**
 * Sentry
 */
Sentry.init({
  dsn:
    'https://3f97baf46c2b44c2bd9e0c371abe3e05@grandlyon.errors.cozycloud.cc/2',
Romain CREY's avatar
Romain CREY committed

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,
  release: version,
  environment: isDev() ? 'development' : 'production',
  debug: isDev(),
  integrations: [
    // enable HTTP calls tracing
    new Sentry.Integrations.Http({ tracing: true }),
  ],
})
Romain CREY's avatar
Romain CREY committed

// 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,
Romain CREY's avatar
Romain CREY committed
// the account information come from ./konnector-dev-config.json file
async function start(fields, cozyParameters) {
  const transaction = Sentry.startTransaction({
    op: 'konnector',
    name: 'EGL Konnector',
  })
  transaction.startChild({ op: 'Konnector starting' })

Romain CREY's avatar
Romain CREY committed
  try {
    // Local debug data
    const baseUrl = fields.eglBaseURL
    const apiAuthKey = fields.eglAPIAuthKey
    // const baseUrl = cozyParameters.secret.eglBaseURL
    // const apiAuthKey = cozyParameters.secret.eglAPIAuthKey
    log('info', 'Authenticating ...')
Romain CREY's avatar
Romain CREY committed
    const response = await authenticate(
      fields.login,
      fields.password,
      baseUrl,
      apiAuthKey
    )
    log('info', 'Successfully logged in')
    const eglData = await getData(response, baseUrl, apiAuthKey)
    if (eglData) {
      log('debug', 'Process egl daily data')
      const processedLoadData = await processData(
        eglData,
        rangeDate.day.doctype,
        rangeDate.day.keys
      )
      log('debug', 'Aggregate egl load data for month and year')
      await aggregateMonthAndYearData(processedLoadData)
    } else {
    transaction.setStatus(Tracing.SpanStatus.Ok)
    transaction.finish()
Romain CREY's avatar
Romain CREY committed
  } catch (error) {
    log('error', error)
    Sentry.captureException(error)
    transaction.setStatus(Tracing.SpanStatus.Aborted)
    transaction.finish()
    await Sentry.flush()
    throw error
Romain CREY's avatar
Romain CREY committed
  }
}
/**
 * Parse data
 * Remove existing data from DB using hydrateAndFilter
 * Store filtered data
 * Return the list of filtered data
 */
async function processData(data, doctype, filterKeys) {
  log('debug', 'processData - data formatted')
  // Remove data for existing days into the DB
  const filteredData = await hydrateAndFilter(data, doctype, {
    keys: filterKeys,
  })
  log('debug', 'processData - data filtered')
  // Store new day data
  await storeData(filteredData, doctype, filterKeys)
  return filteredData
 * Aggregate data from daily data to monthly and yearly data
async function aggregateMonthAndYearData(data) {
  // Sum year and month values into object with year or year-month as keys
  if (data && data.length !== 0) {
    let monthData = {}
    let yearData = {}

    data.forEach(element => {
      const monthDataKey = element.year + '-' + element.month
      if (monthDataKey in monthData) {
        monthData[monthDataKey] += element.load
      } else {
        monthData[monthDataKey] = element.load
      }

      const yearDataKey = element.year
      if (yearDataKey in yearData) {
        yearData[yearDataKey] += element.load
      } else {
        yearData[yearDataKey] = element.load
      }
    })
    // Aggregation for Month data
    const aggregatedMonthData = await buildAggregatedData(
      monthData,
      'com.grandlyon.egl.month'
    )
    await storeData(aggregatedMonthData, 'com.grandlyon.egl.month', [
      'year',
      'month',
    ])
    // Aggregation for Year data
    const aggregatedYearData = await buildAggregatedData(
      yearData,
      'com.grandlyon.egl.year'
    )
    await storeData(aggregatedYearData, 'com.grandlyon.egl.year', ['year'])
  }
}

/**
 * Retrieve and remove old data for a specific doctype
 * Return an Array of aggregated data
async function buildAggregatedData(data, doctype) {
  log('info', 'entering buildAggregatedData')
  let aggregatedData = []
  for (let [key, value] of Object.entries(data)) {
    const data = await buildDataFromKey(doctype, key, value)
    const oldValue = await resetInProgressAggregatedData(data, doctype)
    log('info', 'Data load + old value is ' + data.load + ' + ' + oldValue)
    data.load += oldValue
    aggregatedData.push(data)
Loading
Loading full blame...