# Imports
import boto3
import datetime
import json
import os
import time
import uuid
from l4ecwcw import *
from dashboards_definition import *
# Initialization
l4e_client = boto3.client('lookoutequipment')
cw_client = boto3.client('cloudwatch')
account_id = boto3.client('sts').get_caller_identity()['Account']
session = boto3.session.Session()
current_region = session.region_name
all_dashboards = None
# Entry point
def create_model_dashboard(event, context):
"""
Entry point of the list models custom widgets. This function build
an HTML table with all the Amazon Lookout for Equipment models found in
this account. The user can also create a dedicated dashboard for each
model.
Returns:
html (string): an HTML formatted string with the table to be displayed
"""
# If a model action is requested, we perform it
# before displaying the dashboard:
if 'dashboard_name' in event:
process_dashboard_actions(event)
# Get all the existing dashboard once and for all:
global all_dashboards
all_dashboards = build_dashboard_list()
# Get time extent to show model from:
widget_context = event['widgetContext']
start = widget_context['timeRange']['start']
end = widget_context['timeRange']['end']
start = datetime.datetime.fromtimestamp(start/1000, datetime.timezone.utc)
end = datetime.datetime.fromtimestamp(end/1000, datetime.timezone.utc)
# Get all the models in this account and display then in an HTML table:
list_models = l4e_client.list_models()['ModelSummaries']
# We only keep models created in the timeframe selected by the user:
filtered_list_models = []
for m in list_models:
created = m['CreatedAt']
if (created >= start) and (created <= end):
filtered_list_models.append(m)
html = generate_html_table(filtered_list_models)
return html
def process_dashboard_actions(event):
"""
Creates a dedicated dashboard for either a model,
an asset or a fleet of assets
"""
dashboard_name = event['dashboard_name']
dashboard_type = event['dashboard_type']
entity_name = event['entity_name']
# Get the right dashboard definition
# depending on the asset we want to focus on:
dashboard_body = eval(
f'get_{dashboard_type}_dashboard_body("{entity_name}")'
)
# Create the new CloudWatch dashboard:
cw_client.put_dashboard(
DashboardName=dashboard_name,
DashboardBody=json.dumps(dashboard_body)
)
create_synthetics(dashboard_name)
def create_synthetics(dashboard_name):
client = boto3.client('synthetics')
canary_name = 'modeleval-' + str(uuid.uuid4()).replace('-', '')[:11]
version = os.getenv('VERSION')
syn_source_bucket = os.getenv('SYN_SOURCE_BUCKET')
syn_source_prefix = 'cloudwatch-dashboard-source-code'
syn_source_code = f'{syn_source_prefix}/{version}/synthetics/canary-dashboard.zip'
syn_execution_role = os.getenv('SYN_EXECUTION_ROLE')
artifacts_s3_path = os.getenv('SYN_ARTIFACT_S3_PATH') + dashboard_name + '/'
stack_id = os.getenv('Stack')
response = client.create_canary(
Name=canary_name,
Code={
'S3Bucket': syn_source_bucket,
'S3Key': syn_source_code,
'Handler': 'dashboard-snapshot.handler'
},
ArtifactS3Location=artifacts_s3_path,
ExecutionRoleArn=syn_execution_role,
Schedule={
'Expression': 'rate(0 minute)',
'DurationInSeconds': 0
},
RunConfig={
'TimeoutInSeconds': 180,
'MemoryInMB': 1024,
'ActiveTracing': False,
'EnvironmentVariables': {
'DASHBOARD': dashboard_name,
'STACK_ID': stack_id,
'VIEWPORT_HEIGHT': '1850',
'DASHBOARD_TYPE': 'ModelEvaluation'
}
},
SuccessRetentionPeriodInDays=31,
FailureRetentionPeriodInDays=31,
RuntimeVersion='syn-python-selenium-1.0'
)
status = 'CREATING'
while status != 'READY':
response = client.get_canary(Name=canary_name)
status = response['Canary']['Status']['State']
time.sleep(1)
response = client.start_canary(Name=canary_name)
def generate_html_table(list_models):
header = (
'
Dataset | ' 'Model | ' 'Training status | ' 'Model dashboard | ' '
---|