|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""This module contains code containing wrapper classes for dashboard widgets in CloudWatch. |
| 14 | +
|
| 15 | +These classes assist with creating dashboards in Python3 and then using boto3 CloudWatch client |
| 16 | +to publish the generated dashboards. To be used to aid dashboard creation in ClarifyModelMonitor |
| 17 | +and ModelMonitor. |
| 18 | +""" |
| 19 | + |
| 20 | +import json |
| 21 | + |
| 22 | +class DashboardWidgetProperties: |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + view=None, |
| 26 | + stacked=None, |
| 27 | + metrics=None, |
| 28 | + region=None, |
| 29 | + period=None, |
| 30 | + title=None, |
| 31 | + markdown=None, |
| 32 | + ): |
| 33 | + self.view = view |
| 34 | + self.stacked = stacked |
| 35 | + self.metrics = metrics |
| 36 | + self.region = region |
| 37 | + self.period = period |
| 38 | + self.title = title |
| 39 | + self.markdown = markdown |
| 40 | + |
| 41 | + def to_dict(self): |
| 42 | + widget_properties_dict = {} |
| 43 | + if self.view is not None: |
| 44 | + widget_properties_dict["view"] = self.view |
| 45 | + if self.period is not None: |
| 46 | + widget_properties_dict["period"] = self.period |
| 47 | + if self.markdown is not None: |
| 48 | + widget_properties_dict["markdown"] = self.markdown |
| 49 | + if self.stacked is not None: |
| 50 | + widget_properties_dict["stacked"] = self.stacked |
| 51 | + if self.region is not None: |
| 52 | + widget_properties_dict["region"] = self.region |
| 53 | + if self.metrics is not None: |
| 54 | + widget_properties_dict["metrics"] = self.metrics |
| 55 | + if self.title is not None: |
| 56 | + widget_properties_dict["title"] = self.title |
| 57 | + return widget_properties_dict |
| 58 | + |
| 59 | + def to_json(self): |
| 60 | + json.dumps(self.to_dict(), indent=4) |
| 61 | + |
| 62 | + |
| 63 | +class DashboardWidget: |
| 64 | + def __init__(self, height, width, widget_type, properties=None): |
| 65 | + self.height = height |
| 66 | + self.width = width |
| 67 | + self.type = widget_type |
| 68 | + self.properties = ( |
| 69 | + properties |
| 70 | + if properties |
| 71 | + else DashboardWidgetProperties(None, False, [], None, None, None) |
| 72 | + ) |
| 73 | + |
| 74 | + def to_dict(self): |
| 75 | + return { |
| 76 | + "height": self.height, |
| 77 | + "width": self.width, |
| 78 | + "type": self.type, |
| 79 | + "properties": self.properties.to_dict(), |
| 80 | + } |
| 81 | + |
| 82 | + def to_json(self): |
| 83 | + return json.dumps(self.to_dict(), indent=4) |
0 commit comments