Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more detailed information to make it easier to filter the data in Grafana for plotting #23

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 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 @@ -57,6 +58,11 @@ def metrics():
attrib["currentValue"] = 1
else:
attrib["currentValue"] = 0
if attrib["name"] == "water":
if attrib["currentValue"] == "dry":
attrib["currentValue"] = 1
else:
attrib["currentValue"] = 0
if attrib["name"] == "power":
if attrib["currentValue"] == "on":
attrib["currentValue"] = 1
Expand All @@ -65,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 @@ -83,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 %}