Skip to content

Commit

Permalink
Merge pull request #23 from mboisson/detailed_info
Browse files Browse the repository at this point in the history
Adding more detailed information to make it easier to filter the data in Grafana for plotting
  • Loading branch information
proffalken authored Oct 25, 2023
2 parents b4850e7 + d9348fb commit 7312515
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import requests
import time
import re

from flask import render_template, Flask, make_response, Response

Expand Down Expand Up @@ -70,13 +71,20 @@ def metrics():
else:
attrib["currentValue"] = attrib["currentValue"]

# Sanitise the device name as it will appear in the label
device_name = device_details['label'].lower().replace(' ','_').replace('-','_')
# Sanitise the metric name
metric_name = attrib['name'].lower().replace(' ','_').replace('-','_')
# Sanitize to allow Prometheus Ingestion
device_name = sanitize(device_details['name'])
device_label = sanitize(device_details['label'])
device_human_label = device_details['label']
device_type = sanitize(device_details['type'])
device_id = sanitize(device_details['id'])
metric_name = sanitize(attrib['name'])
# Create the dict that holds the data
device_attributes.append({
"device_name": f"{device_name}",
"device_label": f"{device_label}",
"device_human_label": f"{device_human_label}",
"device_type": f"{device_type}",
"device_id": f"{device_id}",
"metric_name": f"{metric_name}",
"metric_value": f"{attrib['currentValue']}",
"metric_timestamp": time.time()})
Expand All @@ -88,5 +96,5 @@ def metrics():
response.mimetype = "text/plain"
return response



def sanitize(inputValue):
return re.sub('[^a-z0-9]+', '_', inputValue.lower())
2 changes: 1 addition & 1 deletion templates/base.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% for device in device_details %}
{{ device["metric_name"] }}{device_name="{{device['device_name']}}"} {{ device["metric_value"] }}
{{ device["metric_name"] }}{device_name="{{device['device_name']}}",device_human_label="{{device['device_human_label']}}",device_label="{{device['device_label']}}",device_type="{{device['device_type']}}",device_id="{{device['device_id']}}"} {{ device["metric_value"] }}
{% endfor %}

0 comments on commit 7312515

Please sign in to comment.