"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SLOPerformanceDashboard = void 0;
const aws_cdk_lib_1 = require("aws-cdk-lib");
const api_availability_widget_1 = require("./api-availability-widget");
const api_latency_widget_1 = require("./api-latency-widget");
const appsync_availability_widget_1 = require("./appsync-availability-widget");
const appsync_latency_widget_1 = require("./appsync-latency-widget");
const cloudfront_availability_widget_1 = require("./cloudfront-availability-widget");
const cloudfront_latency_widget_1 = require("./cloudfront-latency-widget");
const custom_availability_widget_1 = require("./custom-availability-widget");
const custom_latency_widget_1 = require("./custom-latency-widget");
const elasticsearch_availability_widget_1 = require("./elasticsearch-availability-widget");
const elasticsearch_latency_widget_1 = require("./elasticsearch-latency-widget");
const windows_1 = require("./windows");
/**
 * Creates dashboard showing SLO performance for the last 30 days from a list of slo objects.
 */
class SLOPerformanceDashboard extends aws_cdk_lib_1.aws_cloudwatch.Dashboard {
    constructor(scope, id, props) {
        const widgets = props.slos.reduce((result, slo) => {
            // Create rows with 4 widgets
            let lastRow = result[result.length - 1];
            if (lastRow.length >= 4) {
                lastRow = [];
                result.push(lastRow);
            }
            switch (slo.type) {
                case 'ApiAvailability':
                    const apiAvailabilitySlo = slo;
                    lastRow.push(new api_availability_widget_1.ApiAvailabilityWidget({
                        ...apiAvailabilitySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${apiAvailabilitySlo.title} - Availability`,
                    }));
                    break;
                case 'ApiLatency':
                    const apiLatencySlo = slo;
                    lastRow.push(new api_latency_widget_1.ApiLatencyWidget({
                        ...apiLatencySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${apiLatencySlo.title} - Latency ${apiLatencySlo.latencyThreshold}ms`,
                    }));
                    break;
                case 'AppSyncAvailability':
                    const appSyncAvailabilitySlo = slo;
                    lastRow.push(new appsync_availability_widget_1.AppSyncAvailabilityWidget({
                        ...appSyncAvailabilitySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${appSyncAvailabilitySlo.title} - Availability`,
                    }));
                    break;
                case 'AppSyncLatency':
                    const appSyncLatencySlo = slo;
                    lastRow.push(new appsync_latency_widget_1.AppSyncLatencyWidget({
                        ...appSyncLatencySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${appSyncLatencySlo.title} - Latency ${appSyncLatencySlo.latencyThreshold}ms`,
                    }));
                    break;
                case 'CloudfrontAvailability':
                    const cloudfrontAvailaiblitySlo = slo;
                    lastRow.push(new cloudfront_availability_widget_1.CloudfrontAvailabilityWidget({
                        ...cloudfrontAvailaiblitySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${slo.title} - Availability`,
                    }));
                    break;
                case 'CloudfrontLatency':
                    const cloudfrontLatencySlo = slo;
                    lastRow.push(new cloudfront_latency_widget_1.CloudfrontLatencyWidget({
                        ...cloudfrontLatencySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${cloudfrontLatencySlo.title} - Latency ${cloudfrontLatencySlo.latencyThreshold}ms`,
                    }));
                    break;
                case 'CustomAvailability':
                    const customAvailabilitySlo = slo;
                    lastRow.push(new custom_availability_widget_1.CustomAvailabilityWidget({
                        ...customAvailabilitySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${customAvailabilitySlo.title} - Availability`,
                    }));
                    break;
                case 'CustomLatency':
                    const customLatencySlo = slo;
                    lastRow.push(new custom_latency_widget_1.CustomLatencyWidget({
                        ...customLatencySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${customLatencySlo.title} - Latency ${customLatencySlo.latencyThreshold}ms`,
                    }));
                    break;
                case 'ElasticSearchAvailability':
                    const elasticSearchAvailabilitySlo = slo;
                    lastRow.push(new elasticsearch_availability_widget_1.ElasticSearchAvailabilityWidget({
                        ...elasticSearchAvailabilitySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${elasticSearchAvailabilitySlo.title} - Availability`,
                    }));
                    break;
                case 'ElasticSearchLatency':
                    const esLatencySlo = slo;
                    lastRow.push(new elasticsearch_latency_widget_1.ElasticSearchLatencyWidget({
                        ...esLatencySlo,
                        sloWindow: windows_1.Windows.thirtyDays,
                        title: `${esLatencySlo.title} - Latency ${esLatencySlo.latencyThreshold}ms`,
                    }));
                    break;
                default:
                    throw new Error(`PerformanceDashboard creation encountered an unknown type for slo: ${JSON.stringify(slo)}.`);
            }
            return result;
        }, [[]]);
        const defaultProps = {
            widgets,
            start: '-P360D',
        };
        super(scope, id, { ...defaultProps, ...props });
    }
}
exports.SLOPerformanceDashboard = SLOPerformanceDashboard;
