UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 3.48 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MetricSet=void 0,exports.allMetricsGraphJson=allMetricsGraphJson;var drop_empty_object_at_the_end_of_an_array_token_1=()=>{var tmp=require("./drop-empty-object-at-the-end-of-an-array-token");return drop_empty_object_at_the_end_of_an_array_token_1=()=>tmp,tmp},env_tokens_1=()=>{var tmp=require("./env-tokens");return env_tokens_1=()=>tmp,tmp},metric_util_1=()=>{var tmp=require("./metric-util");return metric_util_1=()=>tmp,tmp},object_1=()=>{var tmp=require("./object");return object_1=()=>tmp,tmp},core_1=()=>{var tmp=require("../../../core");return core_1=()=>tmp,tmp};function allMetricsGraphJson(left,right){const mset=new MetricSet;return mset.addTopLevel("left",...left),mset.addTopLevel("right",...right),mset.entries.map(entry=>new(drop_empty_object_at_the_end_of_an_array_token_1()).DropEmptyObjectAtTheEndOfAnArray(metricGraphJson(entry.metric,entry.tag,entry.id)))}function metricGraphJson(metric,yAxis,id){const config=metric.toMetricConfig(),ret=[],options={...config.renderingProperties};(0,metric_util_1().dispatchMetric)(metric,{withStat(stat){ret.push(stat.namespace,stat.metricName);for(const dim of stat.dimensions||[])ret.push(dim.name,dim.value);stat.accountOverride?options.accountId=stat.accountOverride:stat.account&&(options.accountId=(0,env_tokens_1().accountIfDifferentFromStack)(stat.account)),stat.regionOverride?options.region=stat.regionOverride:stat.region&&(options.region=(0,env_tokens_1().regionIfDifferentFromStack)(stat.region)),stat.period&&stat.period.toSeconds()!==300&&(options.period=stat.period.toSeconds()),stat.statistic&&stat.statistic!=="Average"&&(options.stat=stat.statistic)},withExpression(expr){options.expression=expr.expression,expr.searchAccount&&(options.accountId=(0,env_tokens_1().accountIfDifferentFromStack)(expr.searchAccount)),expr.searchRegion&&(options.region=(0,env_tokens_1().regionIfDifferentFromStack)(expr.searchRegion)),expr.period&&expr.period!==300&&(options.period=expr.period)}}),yAxis||(options.visible=!1),yAxis!=="left"&&(options.yAxis=yAxis),id&&(options.id=id),options.visible!==!1&&options.expression&&!options.label&&(options.label=options.label===""?void 0:metric.toString());const renderedOpts=(0,object_1().dropUndefined)(options);return Object.keys(renderedOpts).length!==0&&ret.push(renderedOpts),ret}class MetricSet{constructor(){this.metrics=new Array,this.metricById=new Map,this.metricByKey=new Map}addTopLevel(tag,...metrics){for(const metric of metrics)this.addOne(metric,1,tag)}get entries(){return this.metrics}addOne(metric,level,tag,id){const key=(0,metric_util_1().metricKey)(metric);let existingEntry;if(id&&(existingEntry=this.metricById.get(id),existingEntry&&(0,metric_util_1().metricKey)(existingEntry.metric)!==key))throw new(core_1()).UnscopedValidationError(`Cannot have two different metrics share the same id ('${id}') in one Alarm or Graph. Rename one of them.`);existingEntry||(existingEntry=this.metricByKey.get(key),existingEntry?.id&&id&&(existingEntry=void 0));let entry;existingEntry?entry=existingEntry:(entry={metric,level},this.metrics.push(entry),this.metricByKey.set(key,entry)),!entry.id&&id&&(entry.id=id,this.metricById.set(id,entry)),!entry.tag&&tag&&(entry.tag=tag);const conf=metric.toMetricConfig();if(conf.mathExpression)for(const[subId,subMetric]of Object.entries(conf.mathExpression.usingMetrics))this.addOne(subMetric,level+1,void 0,subId)}}exports.MetricSet=MetricSet;