"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AvailabilityWidget = void 0;
const aws_cdk_lib_1 = require("aws-cdk-lib");
const types_1 = require("./types");
/**
 * Creates a dashboard widget for an Availability SLO
 */
class AvailabilityWidget extends aws_cdk_lib_1.aws_cloudwatch.GraphWidget {
    constructor(props) {
        const sloBudget = 1 - props.sloThreshold;
        const leftYAxis = {
            // By default, scale the left axis to the SLO threshold
            // Should put it at 0.95, .995, etc, based on order of magnitude of the SLO threshold
            min: props.sloThreshold - (1 - props.sloThreshold) * 4,
            max: 1,
        };
        let leftAnnotations = [{ label: 'SLO', value: props.sloThreshold, fill: aws_cdk_lib_1.aws_cloudwatch.Shading.BELOW, color: types_1.Colors.red }];
        if (props.showBurnRateThreshold === true) {
            // Calculate the SLO alarm threshold in "error rate space".
            let alarmThreshold = 1 - props.sloWindow.burnRateThreshold * sloBudget;
            alarmThreshold = Math.max(0, Math.min(alarmThreshold, 1));
            const roundedPercent = Math.round(props.sloWindow.percent * 100) / 100;
            const alarmLabel = `${roundedPercent}% of Budget in ${props.sloWindow.alertWindow.toHumanString()}`;
            // Scale the left axis according to the burn rate threshold instead of the slo threshold
            leftYAxis.min = alarmThreshold - (1 - alarmThreshold) * 0.2;
            leftAnnotations = [
                { label: alarmLabel, value: +alarmThreshold.toFixed(4), fill: aws_cdk_lib_1.aws_cloudwatch.Shading.BELOW, color: types_1.Colors.red },
                { label: 'SLO', value: props.sloThreshold, fill: aws_cdk_lib_1.aws_cloudwatch.Shading.NONE, color: types_1.Colors.orange },
            ];
        }
        let title = props.title;
        if (props.addPeriodToTitle === true) {
            title = `${props.title} - ${props.sloWindow.alertWindow.toHumanString()}`;
        }
        const myProps = {
            title,
            left: [props.availability],
            leftAnnotations,
            leftYAxis,
        };
        super({ ...props, ...myProps });
    }
}
exports.AvailabilityWidget = AvailabilityWidget;
