-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
widget.ts
84 lines (70 loc) · 1.91 KB
/
widget.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
import { IMetric } from './metric-types';
/**
* The width of the grid we're filling
*/
export const GRID_WIDTH = 24;
/**
* A single dashboard widget
*/
export interface IWidget {
/**
* The amount of horizontal grid units the widget will take up
*/
readonly width: number;
/**
* The amount of vertical grid units the widget will take up
*/
readonly height: number;
/**
* Any warnings that are produced as a result of putting together this widget
* @deprecated - use warningsV2
*/
readonly warnings?: string[];
/**
* Any warnings that are produced as a result of putting together this widget
*/
readonly warningsV2?: { [id: string]: string };
/**
* Place the widget at a given position
*/
position(x: number, y: number): void;
/**
* Return the widget JSON for use in the dashboard
*/
toJson(): any[];
}
/**
* A real CloudWatch widget that has its own fixed size and remembers its position
*
* This is in contrast to other widgets which exist for layout purposes.
*/
export abstract class ConcreteWidget implements IWidget {
public readonly width: number;
public readonly height: number;
protected x?: number;
protected y?: number;
public readonly warnings: string[] | undefined = [];
public readonly warningsV2: { [id: string]: string } | undefined = {};
constructor(width: number, height: number) {
this.width = width;
this.height = height;
if (this.width > GRID_WIDTH) {
throw new Error(`Widget is too wide, max ${GRID_WIDTH} units allowed`);
}
}
public position(x: number, y: number): void {
this.x = x;
this.y = y;
}
public abstract toJson(): any[];
/**
* Copy the warnings from the given metric
*/
protected copyMetricWarnings(...ms: IMetric[]) {
ms.forEach(m => {
for (const [id, message] of Object.entries(m.warningsV2 ?? {})) {
this.warningsV2![id] = message;
}
});
}
}