"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cronRouter = void 0;
const express_1 = require("express");
const crypto_1 = require("crypto");
const pulse_cron_1 = require("../controller/pulse/pulse-cron");
const scheduleJobs_1 = require("../controller/scheduleJobs");
const logger_1 = __importDefault(require("../../logger"));
// import { DataRetentionController } from '../controller/data-retention';
const router = (0, express_1.Router)();
const CORRELATION_HEADER = 'x-correlation-id';
const CRON_CORRELATION_PREFIX = 'generated-';
const newCronCorrelationId = () => {
    return `${CRON_CORRELATION_PREFIX}${(0, crypto_1.randomBytes)(16).toString('hex')}`;
};
const headerOrNa = (req, name) => {
    const value = req.header(name)?.trim();
    return value ? value : 'n/a';
};
const assignCorrelationId = (req, res, next) => {
    if (!res.locals) {
        res.locals = {};
    }
    const incomingCorrelationId = req.header(CORRELATION_HEADER)?.trim();
    const hasIncomingCorrelationId = Boolean(incomingCorrelationId);
    const correlationId = hasIncomingCorrelationId ? incomingCorrelationId : newCronCorrelationId();
    res.locals.correlationId = correlationId;
    // Extract origin tracing info
    const originIp = req.header('x-forwarded-for')?.split(',')[0].trim() ||
        req.header('x-real-ip') ||
        req.socket.remoteAddress ||
        'unknown';
    const originHost = req.header('x-forwarded-host') ||
        req.header('host') ||
        'unknown';
    const originProto = req.header('x-forwarded-proto') ||
        (req.secure ? 'https' : 'http');
    logger_1.default.info(`[cron] request received` +
        ` | path=${req.path}` +
        ` | method=${req.method}` +
        ` | origin=${req.get('origin') ?? 'unknown'}` +
        ` | originIp=${originIp}` +
        ` | originHost=${originHost}` +
        ` | originProto=${originProto}` +
        ` | remoteAddress=${req.socket?.remoteAddress ?? 'unknown'}` +
        ` | remotePort=${req.socket?.remotePort ?? 'unknown'}` +
        ` | userAgent=${req.header('user-agent') ?? 'unknown'}` +
        ` | referer=${req.header('referer') ?? 'none'}` +
        ` | via=${req.header('via') ?? 'none'}` +
        ` | xForwardedFor=${req.header('x-forwarded-for') ?? 'none'}` +
        ` | xForwardedHost=${req.header('x-forwarded-host') ?? 'none'}` +
        ` | xForwardedProto=${req.header('x-forwarded-proto') ?? 'none'}` +
        ` | xAwsLambdaFunctionArn=${headerOrNa(req, 'x-aws-lambda-function-arn')}` +
        ` | xAwsAccountId=${headerOrNa(req, 'x-aws-account-id')}` +
        ` | xAwsRegion=${headerOrNa(req, 'x-aws-region')}` +
        ` | xAwsLambdaFunctionName=${headerOrNa(req, 'x-aws-lambda-function-name')}` +
        ` | xAwsLambdaFunctionVersion=${headerOrNa(req, 'x-aws-lambda-function-version')}` +
        ` | xAwsLambdaMemoryLimitMb=${headerOrNa(req, 'x-aws-lambda-memory-limit-mb')}` +
        ` | xAwsLambdaRequestId=${headerOrNa(req, 'x-aws-lambda-request-id')}` +
        ` | xAwsLambdaLogGroup=${headerOrNa(req, 'x-aws-lambda-log-group')}` +
        ` | xAwsLambdaLogStream=${headerOrNa(req, 'x-aws-lambda-log-stream')}` +
        ` | time=${new Date().toISOString()}` +
        ` | correlationId=${correlationId}` +
        ` | correlationSource=${hasIncomingCorrelationId ? 'incoming-header' : 'generated-cron-api'}`);
    next();
};
router.post('/pulse/evaluate-traces', assignCorrelationId, pulse_cron_1.PulseCronController.evaluateTraces);
router.post('/pulse/metrics-alert', assignCorrelationId, pulse_cron_1.PulseCronController.checkAlerts);
router.post('/pulse/evaluate-sessions', assignCorrelationId, pulse_cron_1.PulseCronController.evaluateSessions);
router.post('/pulse/observation-dashboard-populate', assignCorrelationId, pulse_cron_1.PulseCronController.observationDashboardPopulate);
router.post('/schedule-jobs/run', assignCorrelationId, scheduleJobs_1.runScheduleJobsCron);
/* router.post(
  '/data-retention',
  assignCorrelationId,
  DataRetentionController.dataRetention,
); */
exports.cronRouter = router;
//# sourceMappingURL=cron.js.map