initial naïve Prometheus implementation (#446)

This commit is contained in:
Igor Chubin 2020-06-09 23:11:11 +02:00
parent f215cdf68d
commit a573ae27eb
2 changed files with 46 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import pytz
from constants import WWO_CODE, WEATHER_SYMBOL, WIND_DIRECTION, WEATHER_SYMBOL_WIDTH_VTE
from weather_data import get_weather_data
from . import v2
from . import prometheus
PRECONFIGURED_FORMAT = {
'1': u'%c %t',
@ -336,6 +337,8 @@ def format_weather_data(query, parsed_query, data):
if format_line == "j1":
return render_json(data['data'])
if format_line == "p1":
return prometheus.render_prometheus(data['data'])
if format_line[:2] == "v2":
return v2.main(query, parsed_query, data)

43
lib/view/prometheus.py Normal file
View File

@ -0,0 +1,43 @@
"""
Rendering weather data in the Prometheus format.
"""
EXPORTED_FIELDS = [
"FeelsLikeC", "FeelsLikeF", "cloudcover", "humidity",
"precipMM", "pressure", "temp_C", "temp_F", "uvIndex",
"visibility", "winddirDegree", "windspeedKmph",
"windspeedMiles",
]
def _render_current(data):
output = []
current_condition = data["current_condition"][0]
for field in EXPORTED_FIELDS:
try:
output.append("%s(forecast=\"0h\") %s" % (field, current_condition[field]))
except IndexError:
pass
try:
weather_desc = current_condition["weatherDesc"][0]["value"]
output.append("weatherDesc{forecast=\"0h\", description=\"%s\"} 1" % weather_desc)
except IndexError:
pass
try:
winddir16point = current_condition["winddir16Point"]
output.append("winddir16Point{forecast=\"0h\", description=\"%s\"} 1" % winddir16point)
except IndexError:
pass
return "\n".join(output)+"\n"
def render_prometheus(data):
"""
Convert `data` into Prometheus format
and return it as sting.
"""
return _render_current(data)