UNPKG

@golemio/energetics

Version:
70 lines 4.09 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); const UnimonitorCemApiHelper_1 = require("../../shared/helpers/UnimonitorCemApiHelper"); const FetchXDaysDataValidationSchema_1 = require("../schema/FetchXDaysDataValidationSchema"); const _sch_1 = require("../../../schema-definitions"); const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken"); const AbstractTask_1 = require("@golemio/core/dist/integration-engine/workers/AbstractTask"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const luxon_1 = require("@golemio/core/dist/shared/luxon"); const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe"); const ModuleContainer_1 = require("../ioc/ModuleContainer"); const FetchDateFactory_1 = require("./FetchDateFactory"); let FetchXDaysDataTask = class FetchXDaysDataTask extends AbstractTask_1.AbstractTask { constructor(config, fetchDataFactory) { super(_sch_1.EnergeticsSchema.name.toLowerCase()); this.config = config; this.fetchDataFactory = fetchDataFactory; this.queueName = "fetchVpalacXDaysData"; this.schema = FetchXDaysDataValidationSchema_1.FetchXDaysDataValidationSchema; this.queueTtl = 24 * 60 * 60 * 1000; // 24 hours const { url, config: cemConfig } = this.getProviderConfig(); this.unimonitorCemApi = new UnimonitorCemApiHelper_1.UnimonitorCemApiHelper(url, cemConfig); } async execute(msg) { const authCookie = await this.unimonitorCemApi.createSession(); try { const targetDays = msg.targetDays; const currentDate = luxon_1.DateTime.now().startOf("day"); const dateFrom = currentDate.minus({ days: targetDays }); const intervals = luxon_1.Interval.fromDateTimes(dateFrom, currentDate).splitBy({ days: 1 }); for (const interval of intervals) { const from = interval.start.startOf("day").toFormat(UnimonitorCemApiHelper_1.UnimonitorCemApiHelper.API_DATE_FORMAT); const to = interval.end.startOf("day").toFormat(UnimonitorCemApiHelper_1.UnimonitorCemApiHelper.API_DATE_FORMAT); await this.fetchDataFactory.fetchAndSaveData(from, to, authCookie); } } catch (err) { throw new golemio_errors_1.GeneralError(`Error while executing FetchXDaysDataTask.`, this.constructor.name, err); } finally { await this.unimonitorCemApi.terminateSession(authCookie); } } getProviderConfig() { return { url: this.config.getValue("module.UnimonitorCemApiEnergetics.url"), config: this.config.getValue("module.UnimonitorCemApiEnergetics"), }; } }; FetchXDaysDataTask = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.SimpleConfig)), __param(1, (0, tsyringe_1.inject)(ModuleContainer_1.VpalaceContainerToken.FetchDateFactory)), __metadata("design:paramtypes", [Object, FetchDateFactory_1.FetchDateFactory]) ], FetchXDaysDataTask); exports.default = FetchXDaysDataTask; //# sourceMappingURL=FetchXDaysDataTask.js.map