|
| 1 | +from typing import Any, Dict, Optional |
| 2 | + |
| 3 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper |
| 4 | + |
| 5 | + |
| 6 | +class TimeZone(DictWrapper): |
| 7 | + @property |
| 8 | + def label(self) -> str: |
| 9 | + """The time range label. Either 'UTC' or 'Local'""" |
| 10 | + return self["label"] |
| 11 | + |
| 12 | + @property |
| 13 | + def offset_iso(self) -> str: |
| 14 | + """The time range offset in the format +/-00:00""" |
| 15 | + return self["offsetISO"] |
| 16 | + |
| 17 | + @property |
| 18 | + def offset_in_minutes(self) -> int: |
| 19 | + """The time range offset in minutes""" |
| 20 | + return int(self["offsetInMinutes"]) |
| 21 | + |
| 22 | + |
| 23 | +class TimeRange(DictWrapper): |
| 24 | + @property |
| 25 | + def mode(self) -> str: |
| 26 | + """The time range mode, i.e. 'relative' or 'absolute'""" |
| 27 | + return self["mode"] |
| 28 | + |
| 29 | + @property |
| 30 | + def start(self) -> int: |
| 31 | + """The start time within the time range""" |
| 32 | + return self["start"] |
| 33 | + |
| 34 | + @property |
| 35 | + def end(self) -> int: |
| 36 | + """The end time within the time range""" |
| 37 | + return self["end"] |
| 38 | + |
| 39 | + @property |
| 40 | + def relative_start(self) -> Optional[int]: |
| 41 | + """The relative start time within the time range""" |
| 42 | + return self.get("relativeStart") |
| 43 | + |
| 44 | + @property |
| 45 | + def zoom_start(self) -> Optional[int]: |
| 46 | + """The start time within the zoomed time range""" |
| 47 | + return (self.get("zoom") or {}).get("start") |
| 48 | + |
| 49 | + @property |
| 50 | + def zoom_end(self) -> Optional[int]: |
| 51 | + """The end time within the zoomed time range""" |
| 52 | + return (self.get("zoom") or {}).get("end") |
| 53 | + |
| 54 | + |
| 55 | +class CloudWatchWidgetContext(DictWrapper): |
| 56 | + @property |
| 57 | + def dashboard_name(self) -> str: |
| 58 | + """Get dashboard name, in which the widget is used""" |
| 59 | + return self["dashboardName"] |
| 60 | + |
| 61 | + @property |
| 62 | + def widget_id(self) -> str: |
| 63 | + """Get widget ID""" |
| 64 | + return self["widgetId"] |
| 65 | + |
| 66 | + @property |
| 67 | + def account_id(self) -> str: |
| 68 | + """Get AWS Account ID""" |
| 69 | + return self["accountId"] |
| 70 | + |
| 71 | + @property |
| 72 | + def locale(self) -> str: |
| 73 | + """Get locale language""" |
| 74 | + return self["locale"] |
| 75 | + |
| 76 | + @property |
| 77 | + def timezone(self) -> TimeZone: |
| 78 | + """Timezone information of the dashboard""" |
| 79 | + return TimeZone(self["timezone"]) |
| 80 | + |
| 81 | + @property |
| 82 | + def period(self) -> int: |
| 83 | + """The period shown on the dashboard""" |
| 84 | + return int(self["period"]) |
| 85 | + |
| 86 | + @property |
| 87 | + def is_auto_period(self) -> bool: |
| 88 | + """Whether auto period is enabled""" |
| 89 | + return bool(self["isAutoPeriod"]) |
| 90 | + |
| 91 | + @property |
| 92 | + def time_range(self) -> TimeRange: |
| 93 | + """The widget time range""" |
| 94 | + return TimeRange(self["timeRange"]) |
| 95 | + |
| 96 | + @property |
| 97 | + def theme(self) -> str: |
| 98 | + """The dashboard theme, i.e. 'light' or 'dark'""" |
| 99 | + return self["theme"] |
| 100 | + |
| 101 | + @property |
| 102 | + def link_charts(self) -> bool: |
| 103 | + """The widget is linked to other charts""" |
| 104 | + return bool(self["linkCharts"]) |
| 105 | + |
| 106 | + @property |
| 107 | + def title(self) -> str: |
| 108 | + """Get widget title""" |
| 109 | + return self["title"] |
| 110 | + |
| 111 | + @property |
| 112 | + def params(self) -> Dict[str, Any]: |
| 113 | + """Get widget parameters""" |
| 114 | + return self["params"] |
| 115 | + |
| 116 | + @property |
| 117 | + def forms(self) -> Dict[str, Any]: |
| 118 | + """Get widget form data""" |
| 119 | + return self["forms"]["all"] |
| 120 | + |
| 121 | + @property |
| 122 | + def height(self) -> int: |
| 123 | + """Get widget height""" |
| 124 | + return int(self["height"]) |
| 125 | + |
| 126 | + @property |
| 127 | + def width(self) -> int: |
| 128 | + """Get widget width""" |
| 129 | + return int(self["width"]) |
| 130 | + |
| 131 | + |
| 132 | +class CloudWatchDashboardCustomWidgetEvent(DictWrapper): |
| 133 | + """CloudWatch dashboard custom widget event |
| 134 | +
|
| 135 | + You can use a Lambda function to create a custom widget on a CloudWatch dashboard. |
| 136 | +
|
| 137 | + Documentation: |
| 138 | + ------------- |
| 139 | + - https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/add_custom_widget_dashboard_about.html |
| 140 | + """ |
| 141 | + |
| 142 | + @property |
| 143 | + def describe(self) -> bool: |
| 144 | + """Display widget documentation""" |
| 145 | + return bool(self.get("describe", False)) |
| 146 | + |
| 147 | + @property |
| 148 | + def widget_context(self) -> Optional[CloudWatchWidgetContext]: |
| 149 | + """The widget context""" |
| 150 | + if self.get("widgetContext"): |
| 151 | + return CloudWatchWidgetContext(self["widgetContext"]) |
| 152 | + |
| 153 | + return None |
0 commit comments