"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectAndSaveLogsTask = void 0;
const Di_1 = require("../../../../ioc/Di");
const ModuleContainerToken_1 = require("../../../../ioc/ModuleContainerToken");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const queueprocessors_1 = require("@golemio/core/dist/integration-engine/queueprocessors");
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const PresetLogProviderEnum_1 = require("../datasources/helpers/PresetLogProviderEnum");
const LogQueryManager_1 = require("../helpers/LogQueryManager");
const LogCollectionSchema_1 = require("../schema/LogCollectionSchema");
class CollectAndSaveLogsTask extends workers_1.AbstractTask {
    constructor(queuePrefix) {
        super(queuePrefix);
        this.queueName = "collectAndSaveLogs";
        this.queueTtl = 59 * 1000; // 59 seconds
        this.schema = LogCollectionSchema_1.LogCollectionValidationSchema;
        this.dataSourceFactory = Di_1.PidContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.PresetLogDataSourceFactory);
        this.logTransformation = Di_1.PidContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.PresetLogTransformation);
        this.logRepository = Di_1.PidContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.PresetLogRepository);
        this.logFilter = Di_1.PidContainer.resolve(ModuleContainerToken_1.ModuleContainerToken.PresetLogFilter);
        this.logger = Di_1.PidContainer.resolve(CoreToken_1.CoreToken.Logger);
    }
    async execute(data) {
        const currentTimestampInSeconds = Math.round(new Date().getTime() / 1000);
        const dataSource = this.dataSourceFactory.getDataSource(PresetLogProviderEnum_1.PresetLogProvider.GrafanaLoki, {
            start: currentTimestampInSeconds - data.targetMinutes * 60,
            end: currentTimestampInSeconds,
            query: LogQueryManager_1.LogQueryManager.getCompletedPresetRequestLogsQuery(),
        });
        let queryResult = [];
        try {
            queryResult = await dataSource.getAll();
        }
        catch (err) {
            if (err instanceof golemio_errors_1.AbstractGolemioError) {
                throw err;
            }
            throw new golemio_errors_1.DatasourceError("Error while fetching preset logs", this.constructor.name, err);
        }
        const validLogEntries = this.logFilter.getValidLogEntries(queryResult);
        if (validLogEntries.length > 0) {
            const presetLogDtos = await this.logTransformation.transform(queryResult);
            this.logger.info(`${this.constructor.name}: ${presetLogDtos.length} log entries collected`);
            await this.logRepository.saveData(presetLogDtos);
        }
        else {
            this.logger.warn(`${this.constructor.name}: no valid log entries collected`);
        }
        await queueprocessors_1.QueueManager.sendMessageToExchange(this.queuePrefix, "processAndSendLogs", {});
    }
}
exports.CollectAndSaveLogsTask = CollectAndSaveLogsTask;
//# sourceMappingURL=CollectAndSaveLogsTask.js.map