"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerlessApiDashboard = void 0;
const aws_cdk_lib_1 = require("aws-cdk-lib");
const widgets = require("./widgets");
class ServerlessApiDashboard extends aws_cdk_lib_1.aws_cloudwatch.Dashboard {
    /**
     * Creates a dashboard with debug/informational metrics for an API Gateway with
     * Lambda integrations. It will create widgets to track requests, errors and
     * latencies for the API, all of its methods, and any lambda integrations.
     * Assumes that method names and lambda names begin with the api name.
     */
    constructor(scope, id, props) {
        const { headerName, apiName, stage, desc, headerSize, latencyPercentiles } = props;
        const lambdaNames = [apiName];
        const header = new widgets.HeaderWidget({ name: headerName, desc, height: headerSize !== null && headerSize !== void 0 ? headerSize : 3 });
        const requests = new widgets.ApiRequestsWidget({ apiName, stage });
        const methodRequests = new widgets.ApiMethodRequestsWidget({ apiName, stage });
        const errors = new widgets.ApiErrorsWidget({ apiName, stage });
        const methodErrors = new widgets.ApiMethodErrorsWidget({ apiName, stage });
        const latencyWidgets = [];
        if (latencyPercentiles) {
            latencyPercentiles.forEach(latency => {
                const horizontalAnnotations = [
                    { label: latency.thresholdLabel, value: latency.threshold, fill: aws_cdk_lib_1.aws_cloudwatch.Shading.ABOVE },
                ];
                const percentile = latency.percentile * 100;
                latencyWidgets.push(new widgets.ApiLatencyWidget({
                    apiName,
                    stage,
                    percentile,
                    horizontalAnnotations,
                }));
                latencyWidgets.push(new widgets.ApiMethodLatencyWidget({ apiName, stage, percentile }));
            });
        }
        else {
            latencyWidgets.push(new widgets.ApiLatencyWidget({
                apiName,
                stage,
                percentile: 99.7,
            }));
            latencyWidgets.push(new widgets.ApiMethodLatencyWidget({ apiName, stage, percentile: 99.7 }));
        }
        const lambdaSaturation = new widgets.LambdaSaturationWidget({ lambdaNames });
        const lambdaDurations = new widgets.LambdaDurationsWidget({ lambdaNames });
        const defaultProps = {
            widgets: [
                [header],
                [requests, methodRequests, errors, methodErrors],
                latencyWidgets,
                [lambdaSaturation, lambdaDurations],
            ],
        };
        super(scope, id, { ...defaultProps, ...props });
    }
}
exports.ServerlessApiDashboard = ServerlessApiDashboard;
