UNPKG

@golemio/energetics

Version:
55 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FetchConsumptionTask = void 0; const helpers_1 = require("../datasources/helpers"); const OictDataSourceFactory_1 = require("../datasources/OictDataSourceFactory"); const ConsumptionRepository_1 = require("../repositories/ConsumptionRepository"); const FetchConsumptionSchema_1 = require("../schema/FetchConsumptionSchema"); const ConsumptionTransformation_1 = require("../transformations/ConsumptionTransformation"); const helpers_2 = require("@golemio/core/dist/integration-engine/helpers"); const workers_1 = require("@golemio/core/dist/integration-engine/workers"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const luxon_1 = require("@golemio/core/dist/shared/luxon"); class FetchConsumptionTask extends workers_1.AbstractTask { constructor(queuePrefix) { super(queuePrefix); this.queueName = "fetchConsumption"; this.queueTtl = 10 * 60 * 60 * 1000; // 10 hours this.schema = FetchConsumptionSchema_1.FetchConsumptionValidationSchema; this.consumptionRepository = new ConsumptionRepository_1.ConsumptionRepository(); this.consumptionTransformation = new ConsumptionTransformation_1.ConsumptionTransformation(); } async execute(dateParams) { const dataSources = this.getDataSources(dateParams); for (const dataSource of dataSources) { try { const data = await dataSource.getAll(); const transformedData = await this.consumptionTransformation.transform(data); await this.consumptionRepository.saveData(transformedData); } catch (err) { helpers_2.log.error(err); throw new golemio_errors_1.GeneralError("Failed to fetch consumption data", this.constructor.name, err); } } } *getDataSources(dateParams) { const dateFrom = luxon_1.DateTime.fromISO(dateParams.dateFrom).startOf("day"); const dateTo = luxon_1.DateTime.fromISO(dateParams.dateTo).startOf("day"); const intervals = luxon_1.Interval.fromDateTimes(dateFrom, dateTo).splitBy({ days: 5 }); for (const interval of intervals) { const start = interval.start.startOf("day"); const end = interval.end.startOf("day"); yield OictDataSourceFactory_1.OictDataSourceFactory.getDataSource(helpers_1.OictResourceType.Consumption, { dateFrom: start.toISODate(), dateTo: end.toISODate(), }); yield OictDataSourceFactory_1.OictDataSourceFactory.getDataSource(helpers_1.OictResourceType.ConsumptionVisapp, { dateFrom: start.toISODate(), dateTo: end.toISODate(), }); } } } exports.FetchConsumptionTask = FetchConsumptionTask; //# sourceMappingURL=FetchConsumptionTask.js.map