Examples
How to create the HTML template for a report
The HTML code for the template must be in HTML format and include Jinja2 templating tags. Below is an example of an HTML template that can be used to generate a report for an asset:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Asset Report</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
</style>
</head>
<body>
<h1>Report for Asset: {{ asset_name }}</h1>
<p>Date: {{ report_date }}</p>
<p>Generated by: {{ user_name }}</p>
<h2>Asset Details</h2>
<table>
<tr>
<th>Attribute</th>
<th>Value</th>
</tr>
<tr>
<td>Asset ID</td>
<td>{{ asset_id }}</td>
</tr>
</table>
<h2>Performance Metrics</h2>
<table>
<thead>
<tr>
<th>Metric</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for metric, value in performance_metrics.items() %}
<tr>
<td>{{ metric }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
How to create the template in a Python notebook
To create the notebook, the following configuration is necessary in the first cell:
#Environment
asset_id = "$ASSET_ID" # The ID of the asset for which the report is being generated
start_ts = "$FROM_DAY" # Start date of the report
end_ts = "$TO_DAY" # End date of the report
base_url = "$BASEURL" # Base URL of the API
token = "$TOKEN" # API Key token for authentication
Explanation:
- The above variables are placeholders that must be replaced with actual values when running locally.
- For local testing, you can manually assign these values to test the code.
Here’s how you can set local values to run the notebook:
#NoExecute
token = "fake_token"
asset_id = 'fake_asset_id'
start_ts = "2024-09-12"
end_ts = "2024-09-16"
base_url = 'https://mycircutor.com'
This cell is commented as #NoExecute
to ensure it doesn't run in production but can be used for local tests.
Generating Output Variables for HTML
To generate a file that will contain the necessary variables in HTML format, you can use the following code:
output = {
'report_date': "value",
'asset_name' : "value",
}
with open("output.json", "w") as f:
f.write(json.dumps(output))
Rendering the HTML Template Locally
To generate an HTML file locally and check if the templates are correct, add the following cell. This is recommended only for local testing:
#NoExecute
from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader
jinja2_env = Environment(loader=FileSystemLoader("."), autoescape=select_autoescape())
template = jinja2_env.get_template("template.html")
html = template.render(output)
with open("sample.html", "w") as f:
f.write(html)
Making API Calls for Data Retrieval
Here is an example of how to make an API call to fetch the necessary data for the report:
def request_metric_v3(variables, aggregation, bucket):
url_request = "{base_url}/api/v2/assets/{id}/metricsv3/aggregate/{aggregation}/{bucket}?vars={variables}&from={start_ts}T00:00:00&to={end_ts}T00:00:00".format(
base_url=base_url, id=asset_id, aggregation=aggregation, bucket=bucket,
variables=variables, start_ts=start_ts, end_ts=end_ts)
response_metrics = requests.get(url_request, headers={'X-API-KEY': token})
df_flicker_severity = request_metric_v3("PLT_AN,PLT_BN,PLT_CN", "avg", "hour")
def metric_metric_raw_v3(variables):
url_request = "{base_url}/api/v2/assets/{id}/metricsv3/raw?vars={variables}&from={start_ts}T00:00:00&to={end_ts}T00:00:00".format(
base_url=base_url, id=asset_id, variables=variables, start_ts=start_ts, end_ts=end_ts)
response_metrics = requests.get(url_request, headers={'X-API-KEY': token})
df_frequency, is_data_accepted = metric_metric_raw_v3("FREQ")
Clarifications:
- request_metric_v3: This function retrieves aggregated data (e.g., average values).
- metric_metric_raw_v3: This function fetches raw data (e.g., individual data points).
- Both functions use the API key (token) and the base URL to authenticate the requests.
How to upload the template in a project
To create a report, upload a ZIP file containing both the HTML and the notebook to the platform. Go to the specific project, navigate to the "Templates" section, and select "Upload New Version" to submit it. You can also update existing templates in this same section: choose the template you want to update and follow the process to upload a new version.
How to generate a report for an asset
To generate a report, go to the "Reports" section of an edition. Then, select the type of report you wish to generate, choose the relevant asset, and set the date ranges.