-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathgraph.ts
1182 lines (1042 loc) · 30.2 KB
/
graph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { IAlarm } from './alarm-base';
import { IMetric } from './metric-types';
import { allMetricsGraphJson } from './private/rendering';
import { ConcreteWidget } from './widget';
import * as cdk from '../../core';
/**
* Basic properties for widgets that display metrics
*/
export interface MetricWidgetProps {
/**
* Title for the graph
*
* @default - None
*/
readonly title?: string;
/**
* The region the metrics of this graph should be taken from
*
* @default - Current region
*/
readonly region?: string;
/**
* Width of the widget, in a grid of 24 units wide
*
* @default 6
*/
readonly width?: number;
/**
* Height of the widget
*
* @default - 6 for Alarm and Graph widgets.
* 3 for single value widgets where most recent value of a metric is displayed.
*/
readonly height?: number;
}
/**
* Properties for a Y-Axis
*/
export interface YAxisProps {
/**
* The min value
*
* @default 0
*/
readonly min?: number;
/**
* The max value
*
* @default - No maximum value
*/
readonly max?: number;
/**
* The label
*
* @default - No label
*/
readonly label?: string;
/**
* Whether to show units
*
* @default true
*/
readonly showUnits?: boolean;
}
/**
* Properties for an AlarmWidget
*/
export interface AlarmWidgetProps extends MetricWidgetProps {
/**
* The alarm to show
*/
readonly alarm: IAlarm;
/**
* Left Y axis
*
* @default - No minimum or maximum values for the left Y-axis
*/
readonly leftYAxis?: YAxisProps;
}
/**
* Display the metric associated with an alarm, including the alarm line
*/
export class AlarmWidget extends ConcreteWidget {
private readonly props: AlarmWidgetProps;
constructor(props: AlarmWidgetProps) {
super(props.width || 6, props.height || 6);
this.props = props;
}
public toJson(): any[] {
return [{
type: 'metric',
width: this.width,
height: this.height,
x: this.x,
y: this.y,
properties: {
view: 'timeSeries',
title: this.props.title,
region: this.props.region || cdk.Aws.REGION,
annotations: {
alarms: [this.props.alarm.alarmArn],
},
yAxis: {
left: this.props.leftYAxis ?? undefined,
},
},
}];
}
}
/**
* Types of view
*/
export enum GraphWidgetView {
/**
* Display as a line graph.
*/
TIME_SERIES = 'timeSeries',
/**
* Display as a bar graph.
*/
BAR = 'bar',
/**
* Display as a pie graph.
*/
PIE = 'pie',
}
/**
* Properties for a GaugeWidget
*/
export interface GaugeWidgetProps extends MetricWidgetProps {
/**
* Metrics to display on left Y axis
*
* @default - No metrics
*/
readonly metrics?: IMetric[];
/**
* Annotations for the left Y axis
*
* @default - No annotations
*/
readonly annotations?: HorizontalAnnotation[];
/**
* Left Y axis
*
* @default - None
*/
readonly leftYAxis?: YAxisProps;
/**
* Position of the legend
*
* @default - bottom
*/
readonly legendPosition?: LegendPosition;
/**
* Whether the graph should show live data
*
* @default false
*/
readonly liveData?: boolean;
/**
* Whether to show the value from the entire time range. Only applicable for Bar and Pie charts.
*
* If false, values will be from the most recent period of your chosen time range;
* if true, shows the value from the entire time range.
*
* @default false
*/
readonly setPeriodToTimeRange?: boolean;
/**
* The default period for all metrics in this widget.
* The period is the length of time represented by one data point on the graph.
* This default can be overridden within each metric definition.
*
* @default cdk.Duration.seconds(300)
*/
readonly period?: cdk.Duration;
/**
* The default statistic to be displayed for each metric.
* This default can be overridden within the definition of each individual metric
*
* @default - The statistic for each metric is used
*/
readonly statistic?: string;
/**
* The start of the time range to use for each widget independently from those of the dashboard.
* You can specify start without specifying end to specify a relative time range that ends with the current time.
* In this case, the value of start must begin with -P, and you can use M, H, D, W and M as abbreviations for
* minutes, hours, days, weeks and months. For example, -PT8H shows the last 8 hours and -P3M shows the last three months.
* You can also use start along with an end field, to specify an absolute time range.
* When specifying an absolute time range, use the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the start time will be the default time range.
*/
readonly start?: string;
/**
* The end of the time range to use for each widget independently from those of the dashboard.
* If you specify a value for end, you must also specify a value for start.
* Specify an absolute time in the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the end date will be the current time.
*/
readonly end?: string;
}
/**
* A dashboard gauge widget that displays metrics
*/
export class GaugeWidget extends ConcreteWidget {
private readonly props: GaugeWidgetProps;
private readonly metrics: IMetric[];
constructor(props: GaugeWidgetProps) {
super(props.width || 6, props.height || 6);
this.props = props;
this.metrics = props.metrics ?? [];
this.copyMetricWarnings(...this.metrics);
if (props.end !== undefined && props.start === undefined) {
throw new cdk.UnscopedValidationError('If you specify a value for end, you must also specify a value for start.');
}
}
/**
* Add another metric to the left Y axis of the GaugeWidget
*
* @param metric the metric to add
*/
public addMetric(metric: IMetric) {
this.metrics.push(metric);
this.copyMetricWarnings(metric);
}
public toJson(): any[] {
const metrics = allMetricsGraphJson(this.metrics, []);
const leftYAxis = {
...this.props.leftYAxis,
min: this.props.leftYAxis?.min ?? 0,
max: this.props.leftYAxis?.max ?? 100,
};
return [{
type: 'metric',
width: this.width,
height: this.height,
x: this.x,
y: this.y,
properties: {
view: 'gauge',
title: this.props.title,
region: this.props.region || cdk.Aws.REGION,
metrics: metrics.length > 0 ? metrics : undefined,
annotations: (this.props.annotations ?? []).length > 0 ? { horizontal: this.props.annotations } : undefined,
yAxis: {
left: leftYAxis ?? undefined,
},
legend: this.props.legendPosition !== undefined ? { position: this.props.legendPosition } : undefined,
liveData: this.props.liveData,
setPeriodToTimeRange: this.props.setPeriodToTimeRange,
period: this.props.period?.toSeconds(),
stat: this.props.statistic,
start: this.props.start,
end: this.props.end,
},
}];
}
}
/**
* Properties for a GraphWidget
*/
export interface GraphWidgetProps extends MetricWidgetProps {
/**
* Metrics to display on left Y axis
*
* @default - No metrics
*/
readonly left?: IMetric[];
/**
* Metrics to display on right Y axis
*
* @default - No metrics
*/
readonly right?: IMetric[];
/**
* Annotations for the left Y axis
*
* @default - No annotations
*/
readonly leftAnnotations?: HorizontalAnnotation[];
/**
* Annotations for the right Y axis
*
* @default - No annotations
*/
readonly rightAnnotations?: HorizontalAnnotation[];
/**
* Annotations for the X axis
*
* @default - No annotations
*/
readonly verticalAnnotations?: VerticalAnnotation[];
/**
* Whether the graph should be shown as stacked lines
*
* @default false
*/
readonly stacked?: boolean;
/**
* Left Y axis
*
* @default - None
*/
readonly leftYAxis?: YAxisProps;
/**
* Right Y axis
*
* @default - None
*/
readonly rightYAxis?: YAxisProps;
/**
* Position of the legend
*
* @default - bottom
*/
readonly legendPosition?: LegendPosition;
/**
* Whether the graph should show live data
*
* @default false
*/
readonly liveData?: boolean;
/**
* Display this metric
*
* @default TimeSeries
*/
readonly view?: GraphWidgetView;
/**
* Whether to show the value from the entire time range. Only applicable for Bar and Pie charts.
*
* If false, values will be from the most recent period of your chosen time range;
* if true, shows the value from the entire time range.
*
* @default false
*/
readonly setPeriodToTimeRange?: boolean;
/**
* The default period for all metrics in this widget.
* The period is the length of time represented by one data point on the graph.
* This default can be overridden within each metric definition.
*
* @default cdk.Duration.seconds(300)
*/
readonly period?: cdk.Duration;
/**
* The default statistic to be displayed for each metric.
* This default can be overridden within the definition of each individual metric
*
* @default - The statistic for each metric is used
*/
readonly statistic?: string;
/**
* The start of the time range to use for each widget independently from those of the dashboard.
* You can specify start without specifying end to specify a relative time range that ends with the current time.
* In this case, the value of start must begin with -P, and you can use M, H, D, W and M as abbreviations for
* minutes, hours, days, weeks and months. For example, -PT8H shows the last 8 hours and -P3M shows the last three months.
* You can also use start along with an end field, to specify an absolute time range.
* When specifying an absolute time range, use the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the start time will be the default time range.
*/
readonly start?: string;
/**
* The end of the time range to use for each widget independently from those of the dashboard.
* If you specify a value for end, you must also specify a value for start.
* Specify an absolute time in the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the end date will be the current time.
*/
readonly end?: string;
}
/**
* A dashboard widget that displays metrics
*/
export class GraphWidget extends ConcreteWidget {
private static readonly ISO8601_REGEX = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?(Z)?$/;
private static isIso8601(date: string): boolean {
return this.ISO8601_REGEX.test(date);
}
private readonly props: GraphWidgetProps;
private readonly leftMetrics: IMetric[];
private readonly rightMetrics: IMetric[];
constructor(props: GraphWidgetProps) {
super(props.width || 6, props.height || 6);
props.verticalAnnotations?.forEach(annotation => {
const date = annotation.date;
if (!GraphWidget.isIso8601(date)) {
throw new cdk.UnscopedValidationError(`Given date ${date} is not in ISO 8601 format`);
}
});
this.props = props;
this.leftMetrics = props.left ?? [];
this.rightMetrics = props.right ?? [];
this.copyMetricWarnings(...this.leftMetrics, ...this.rightMetrics);
if (props.end !== undefined && props.start === undefined) {
throw new cdk.UnscopedValidationError('If you specify a value for end, you must also specify a value for start.');
}
}
/**
* Add another metric to the left Y axis of the GraphWidget
*
* @param metric the metric to add
*/
public addLeftMetric(metric: IMetric) {
this.leftMetrics.push(metric);
this.copyMetricWarnings(metric);
}
/**
* Add another metric to the right Y axis of the GraphWidget
*
* @param metric the metric to add
*/
public addRightMetric(metric: IMetric) {
this.rightMetrics.push(metric);
this.copyMetricWarnings(metric);
}
public toJson(): any[] {
const horizontalAnnotations = [
...(this.props.leftAnnotations || []).map(mapAnnotation('left')),
...(this.props.rightAnnotations || []).map(mapAnnotation('right')),
];
const verticalAnnotations = (this.props.verticalAnnotations || []).map(({ date, ...rest }) => ({
value: date,
...rest,
}));
const annotations = horizontalAnnotations.length > 0 || verticalAnnotations.length > 0 ? ({
horizontal: horizontalAnnotations.length > 0 ? horizontalAnnotations : undefined,
vertical: verticalAnnotations.length > 0 ? verticalAnnotations : undefined,
}) : undefined;
const metrics = allMetricsGraphJson(this.leftMetrics, this.rightMetrics);
return [{
type: 'metric',
width: this.width,
height: this.height,
x: this.x,
y: this.y,
properties: {
view: this.props.view ?? GraphWidgetView.TIME_SERIES,
title: this.props.title,
region: this.props.region || cdk.Aws.REGION,
stacked: this.props.stacked,
metrics: metrics.length > 0 ? metrics : undefined,
annotations,
yAxis: {
left: this.props.leftYAxis ?? undefined,
right: this.props.rightYAxis ?? undefined,
},
legend: this.props.legendPosition !== undefined ? { position: this.props.legendPosition } : undefined,
liveData: this.props.liveData,
setPeriodToTimeRange: this.props.setPeriodToTimeRange,
period: this.props.period?.toSeconds(),
stat: this.props.statistic,
start: this.props.start,
end: this.props.end,
},
}];
}
}
/**
* Layout for TableWidget
*/
export enum TableLayout {
/**
* Data points are laid out in columns
*/
HORIZONTAL = 'horizontal',
/**
* Data points are laid out in rows
*/
VERTICAL = 'vertical',
}
/**
* Standard table summary columns
*/
export enum TableSummaryColumn {
/**
* Minimum of all data points
*/
MINIMUM = 'MIN',
/**
* Maximum of all data points
*/
MAXIMUM = 'MAX',
/**
* Sum of all data points
*/
SUM = 'SUM',
/**
* Average of all data points
*/
AVERAGE = 'AVG',
}
/**
* Properties for TableWidget's summary columns
*/
export interface TableSummaryProps {
/**
* Summary columns
*
* @default - No summary columns will be shown
*/
readonly columns?: TableSummaryColumn[];
/**
* Make the summary columns sticky, so that they remain in view while scrolling
*
* @default - false
*/
readonly sticky?: boolean;
/**
* Prevent the columns of datapoints from being displayed, so that only the label and summary columns are displayed
*
* @default - false
*/
readonly hideNonSummaryColumns?: boolean;
}
/**
* Thresholds for highlighting cells in TableWidget
*/
export class TableThreshold {
/**
* A threshold for highlighting and coloring cells above the specified value
*
* @param value lower bound of threshold range
* @param color cell color for values within threshold range
*/
public static above(value: number, color?: string): TableThreshold {
return new TableThreshold(value, undefined, color, Shading.ABOVE);
}
/**
* A threshold for highlighting and coloring cells below the specified value
*
* @param value upper bound of threshold range
* @param color cell color for values within threshold range
*/
public static below(value: number, color?: string): TableThreshold {
return new TableThreshold(value, undefined, color, Shading.BELOW);
}
/**
* A threshold for highlighting and coloring cells within the specified values
*
* @param lowerBound lower bound of threshold range
* @param upperBound upper bound of threshold range
* @param color cell color for values within threshold range
*/
public static between(lowerBound: number, upperBound: number, color?: string): TableThreshold {
return new TableThreshold(lowerBound, upperBound, color);
}
private readonly lowerBound: number;
private readonly upperBound?: number;
private readonly color?: string;
private readonly comparator?: Shading;
private constructor(lowerBound: number, upperBound?: number, color?: string, comparator?: Shading) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.color = color;
this.comparator = comparator;
}
public toJson(): any {
if (this.upperBound) {
return [
{ value: this.lowerBound, color: this.color },
{ value: this.upperBound },
];
}
return { value: this.lowerBound, color: this.color, fill: this.comparator };
}
}
/**
* Properties for a TableWidget
*/
export interface TableWidgetProps extends MetricWidgetProps {
/**
* Table layout
*
* @default - TableLayout.HORIZONTAL
*/
readonly layout?: TableLayout;
/**
* Properties for displaying summary columns
*
* @default - no summary columns are shown
*/
readonly summary?: TableSummaryProps;
/**
* Thresholds for highlighting table cells
*
* @default - No thresholds
*/
readonly thresholds?: TableThreshold[];
/**
* Show the metrics units in the label column
*
* @default - false
*/
readonly showUnitsInLabel?: boolean;
/**
* Metrics to display in the table
*
* @default - No metrics
*/
readonly metrics?: IMetric[];
/**
* Whether the graph should show live data
*
* @default false
*/
readonly liveData?: boolean;
/**
* Whether to show as many digits as can fit, before rounding.
*
* @default false
*/
readonly fullPrecision?: boolean;
/**
* Whether to show the value from the entire time range. Only applicable for Bar and Pie charts.
*
* If false, values will be from the most recent period of your chosen time range;
* if true, shows the value from the entire time range.
*
* @default false
*/
readonly setPeriodToTimeRange?: boolean;
/**
* The default period for all metrics in this widget.
* The period is the length of time represented by one data point on the graph.
* This default can be overridden within each metric definition.
*
* @default cdk.Duration.seconds(300)
*/
readonly period?: cdk.Duration;
/**
* The default statistic to be displayed for each metric.
* This default can be overridden within the definition of each individual metric
*
* @default - The statistic for each metric is used
*/
readonly statistic?: string;
/**
* The start of the time range to use for each widget independently from those of the dashboard.
* You can specify start without specifying end to specify a relative time range that ends with the current time.
* In this case, the value of start must begin with -P, and you can use M, H, D, W and M as abbreviations for
* minutes, hours, days, weeks and months. For example, -PT8H shows the last 8 hours and -P3M shows the last three months.
* You can also use start along with an end field, to specify an absolute time range.
* When specifying an absolute time range, use the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the start time will be the default time range.
*/
readonly start?: string;
/**
* The end of the time range to use for each widget independently from those of the dashboard.
* If you specify a value for end, you must also specify a value for start.
* Specify an absolute time in the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the end date will be the current time.
*/
readonly end?: string;
}
/**
* A dashboard widget that displays metrics
*/
export class TableWidget extends ConcreteWidget {
private readonly metrics: IMetric[];
constructor(private readonly props: TableWidgetProps) {
super(props.width || 6, props.height || 6);
this.props = props;
this.metrics = props.metrics ?? [];
this.copyMetricWarnings(...this.metrics);
if (props.end !== undefined && props.start === undefined) {
throw new cdk.UnscopedValidationError('If you specify a value for end, you must also specify a value for start.');
}
}
/**
* Add another metric
*
* @param metric the metric to add
*/
public addMetric(metric: IMetric) {
this.metrics.push(metric);
this.copyMetricWarnings(metric);
}
public toJson(): any[] {
const horizontalAnnotations = (this.props.thresholds ?? []).map(threshold => threshold.toJson());
const annotations = horizontalAnnotations.length > 0 ? ({
horizontal: horizontalAnnotations,
}) : undefined;
const metrics = allMetricsGraphJson(this.metrics, []);
return [{
type: 'metric',
width: this.width,
height: this.height,
x: this.x,
y: this.y,
properties: {
title: this.props.title,
view: 'table',
table: {
layout: this.props.layout ?? TableLayout.HORIZONTAL,
showTimeSeriesData: !(this.props.summary?.hideNonSummaryColumns ?? false),
stickySummary: this.props.summary?.sticky ?? false,
summaryColumns: this.props.summary?.columns ?? [],
},
region: this.props.region || cdk.Aws.REGION,
metrics: metrics.length > 0 ? metrics : undefined,
annotations,
yAxis: {
left: this.props.showUnitsInLabel ? {
showUnits: true,
} : undefined,
},
liveData: this.props.liveData,
singleValueFullPrecision: this.props.fullPrecision,
setPeriodToTimeRange: this.props.setPeriodToTimeRange,
period: this.props.period?.toSeconds(),
stat: this.props.statistic,
start: this.props.start,
end: this.props.end,
},
}];
}
}
/**
* Properties for a SingleValueWidget
*/
export interface SingleValueWidgetProps extends MetricWidgetProps {
/**
* Metrics to display
*/
readonly metrics: IMetric[];
/**
* The default period for all metrics in this widget.
* The period is the length of time represented by one data point on the graph.
* This default can be overridden within each metric definition.
*
* @default cdk.Duration.seconds(300)
*/
readonly period?: cdk.Duration;
/**
* Whether to show the value from the entire time range.
*
* @default false
*/
readonly setPeriodToTimeRange?: boolean;
/**
* Whether to show as many digits as can fit, before rounding.
*
* @default false
*/
readonly fullPrecision?: boolean;
/**
* Whether to show a graph below the value illustrating the value for the whole time range.
* Cannot be used in combination with `setPeriodToTimeRange`
*
* @default false
*/
readonly sparkline?: boolean;
/**
* The start of the time range to use for each widget independently from those of the dashboard.
* You can specify start without specifying end to specify a relative time range that ends with the current time.
* In this case, the value of start must begin with -P, and you can use M, H, D, W and M as abbreviations for
* minutes, hours, days, weeks and months. For example, -PT8H shows the last 8 hours and -P3M shows the last three months.
* You can also use start along with an end field, to specify an absolute time range.
* When specifying an absolute time range, use the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the start time will be the default time range.
*/
readonly start?: string;
/**
* The end of the time range to use for each widget independently from those of the dashboard.
* If you specify a value for end, you must also specify a value for start.
* Specify an absolute time in the ISO 8601 format. For example, 2018-12-17T06:00:00.000Z.
*
* @default When the dashboard loads, the end date will be the current time.
*/
readonly end?: string;
}
/**
* A dashboard widget that displays the most recent value for every metric
*/
export class SingleValueWidget extends ConcreteWidget {
private readonly props: SingleValueWidgetProps;
constructor(props: SingleValueWidgetProps) {
super(props.width || 6, props.height || 3);
this.props = props;
this.copyMetricWarnings(...props.metrics);
if (props.setPeriodToTimeRange && props.sparkline) {
throw new cdk.UnscopedValidationError('You cannot use setPeriodToTimeRange with sparkline');
}
if (props.end !== undefined && props.start === undefined) {
throw new cdk.UnscopedValidationError('If you specify a value for end, you must also specify a value for start.');
}
}
public toJson(): any[] {
return [{
type: 'metric',
width: this.width,
height: this.height,
x: this.x,
y: this.y,
properties: {
view: 'singleValue',
title: this.props.title,
region: this.props.region || cdk.Aws.REGION,
sparkline: this.props.sparkline,
metrics: allMetricsGraphJson(this.props.metrics, []),
setPeriodToTimeRange: this.props.setPeriodToTimeRange,
singleValueFullPrecision: this.props.fullPrecision,
period: this.props.period?.toSeconds(),
start: this.props.start,
end: this.props.end,
},
}];
}
}
/**
* The properties for a CustomWidget
*/
export interface CustomWidgetProps {
/**
* The Arn of the AWS Lambda function that returns HTML or JSON that will be displayed in the widget
*/
readonly functionArn: string;
/**
* Width of the widget, in a grid of 24 units wide
*
* @default 6
*/
readonly width?: number;
/**
* Height of the widget
*
* @default - 6 for Alarm and Graph widgets.
* 3 for single value widgets where most recent value of a metric is displayed.
*/
readonly height?: number;
/**
* The title of the widget
*/
readonly title: string;
/**
* Update the widget on refresh
*
* @default true
*/
readonly updateOnRefresh?: boolean;
/**
* Update the widget on resize
*
* @default true
*/
readonly updateOnResize?: boolean;
/**
* Update the widget on time range change
*
* @default true
*/
readonly updateOnTimeRangeChange?: boolean;
/**
* Parameters passed to the lambda function
*
* @default - no parameters are passed to the lambda function
*/
readonly params?: any;
}
/**
* A CustomWidget shows the result of a AWS lambda function
*/
export class CustomWidget extends ConcreteWidget {
private readonly props: CustomWidgetProps;
public constructor(props: CustomWidgetProps) {
super(props.width ?? 6, props.height ?? 6);
this.props = props;
}
public toJson(): any[] {
return [{
type: 'custom',
width: this.width,
height: this.height,
x: this.x,
y: this.y,
properties: {
endpoint: this.props.functionArn,
params: this.props.params,
title: this.props.title,
updateOn: {
refresh: this.props.updateOnRefresh ?? true,