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);
};
import { LIMIT_PER_PROJECT } from "../../../../Types/Database/LimitMax";
import OneUptimeDate from "../../../../Types/Date";
import { CheckOn, EvaluateOverTimeType, } from "../../../../Types/Monitor/CriteriaFilter";
import MonitorMetricTypeUtil from "../../../../Utils/Monitor/MonitorMetricType";
import MetricService from "../../../Services/MetricService";
import CaptureSpan from "../../Telemetry/CaptureSpan";
import InBetween from "../../../../Types/BaseDatabase/InBetween";
export default class EvaluateOverTime {
    static async getValueOverTime(data) {
        // get values over time
        const lastMinutesDate = OneUptimeDate.getSomeMinutesAgo(data.evaluateOverTimeOptions.timeValueInMinutes);
        // TODO: Query over miscData
        const now = OneUptimeDate.getCurrentDate();
        const query = {
            projectId: data.projectId,
            time: new InBetween(lastMinutesDate, now),
            primaryEntityId: data.monitorId,
            name: MonitorMetricTypeUtil.getMonitorMeticTypeByCheckOn(data.metricType),
        };
        if (data.miscData) {
            query.attributes = data.miscData;
        }
        const monitorMetricsItems = await MetricService.findBy({
            query: query,
            limit: LIMIT_PER_PROJECT,
            skip: 0,
            props: {
                isRoot: true,
            },
            select: {
                value: true,
            },
        });
        const values = monitorMetricsItems
            .map((item) => {
            if (data.metricType === CheckOn.IsOnline ||
                data.metricType === CheckOn.IsRequestTimeout) {
                return item.value === 1;
            }
            return item.value;
        })
            .filter((value) => {
            return value !== undefined;
        });
        if (data.evaluateOverTimeOptions.evaluateOverTimeType ===
            EvaluateOverTimeType.AnyValue ||
            data.evaluateOverTimeOptions.evaluateOverTimeType ===
                EvaluateOverTimeType.AllValues) {
            // if its any or all then return the values. Otherwise compute the value based on the type
            return values;
        }
        return this.getValueByEvaluationType({
            values: values,
            evaluateOverTimeType: data.evaluateOverTimeOptions.evaluateOverTimeType,
        });
    }
    static getValueByEvaluationType(data) {
        switch (data.evaluateOverTimeType) {
            case EvaluateOverTimeType.Average:
                return this.getAverage(data.values);
            case EvaluateOverTimeType.MaximumValue:
                return this.getMax(data.values);
            case EvaluateOverTimeType.MunimumValue:
                return this.getMin(data.values);
            case EvaluateOverTimeType.Sum:
                return this.getSum(data.values);
            default:
                return 0;
        }
    }
    static getSum(values) {
        // get sum of all values
        return values.reduce((a, b) => {
            return a + b;
        }, 0);
    }
    static getMin(values) {
        // get min value
        return Math.min(...values);
    }
    static getMax(values) {
        // get max value
        return Math.max(...values);
    }
    static getAverage(values) {
        // get average value
        return this.getSum(values) / values.length;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], EvaluateOverTime, "getValueOverTime", null);
//# sourceMappingURL=EvaluateOverTime.js.map