From dabfc55c8be1d793e355c7023f3345533ee16be3 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Fri, 12 Jun 2020 19:12:08 +0200 Subject: [PATCH] prometheus format code clean up (#446) --- lib/fields.py | 105 ++++++++++++++++++++++++++ lib/view/prometheus.py | 168 +++++++++-------------------------------- 2 files changed, 141 insertions(+), 132 deletions(-) create mode 100644 lib/fields.py diff --git a/lib/fields.py b/lib/fields.py new file mode 100644 index 0000000..a1ccc0d --- /dev/null +++ b/lib/fields.py @@ -0,0 +1,105 @@ +""" +Human readable description of the available data fields +describing current weather, weather forecast, and astronomical data +""" + +DESCRIPTION = { + # current condition fields + "FeelsLikeC": ( + "Feels Like Temperature in Celsius", + "temperature_feels_like_celsius"), + "FeelsLikeF": ( + "Feels Like Temperature in Fahrenheit", + "temperature_feels_like_fahrenheit"), + "cloudcover": ( + "Cloud Coverage in Percent", + "cloudcover_percentage"), + "humidity": ( + "Humidity in Percent", + "humidity_percentage"), + "precipMM": ( + "Precipitation (Rainfall) in mm", + "precipitation_mm"), + "pressure": ( + "Air pressure in hPa", + "pressure_hpa"), + "temp_C": ( + "Temperature in Celsius", + "temperature_celsius"), + "temp_F": ( + "Temperature in Fahrenheit", + "temperature_fahrenheit"), + "uvIndex": ( + "Ultaviolet Radiation Index", + "uv_index"), + "visibility": ( + "Visible Distance in Kilometres", + "visibility"), + "weatherCode": ( + "Code to describe Weather Condition", + "weather_code"), + "winddirDegree": ( + "Wind Direction in Degree", + "winddir_degree"), + "windspeedKmph": ( + "Wind Speed in Kilometres per Hour", + "windspeed_kmph"), + "windspeedMiles": ( + "Wind Speed in Miles per Hour", + "windspeed_mph"), + "observation_time": ( + "Minutes since start of the day the observation happened", + "observation_time"), + + # fields with `description` + "weatherDesc": ( + "Weather Description", + "weather_desc"), + "winddir16Point": ( + "Wind Direction on a 16-wind compass rose", + "winddir_16_point"), + + # forecast fields + "maxtempC": ( + "Maximum Temperature in Celsius", + "temperature_celsius_maximum"), + "maxtempF": ( + "Maximum Temperature in Fahrenheit", + "temperature_fahrenheit_maximum"), + "mintempC": ( + "Minimum Temperature in Celsius", + "temperature_celsius_minimum"), + "mintempF": ( + "Minimum Temperature in Fahrenheit", + "temperature_fahrenheit_minimum"), + "sunHour":( + "Hours of sunlight", + "sun_hour"), + "totalSnow_cm":( + "Total snowfall in cm", + "snowfall_cm"), + + # astronomy fields + "moon_illumination": ( + "Percentage of the moon illuminated", + "astronomy_moon_illumination"), + + # astronomy fields with description + "moon_phase": ( + "Phase of the moon", + "astronomy_moon_phase"), + + # astronomy fields with time + "moonrise": ( + "Minutes since start of the day untill the moon appears above the horizon", + "astronomy_moonrise_min"), + "moonset": ( + "Minutes since start of the day untill the moon disappears below the horizon", + "astronomy_moonset_min"), + "sunrise": ( + "Minutes since start of the day untill the sun appears below the horizon", + "astronomy_sunrise_min"), + "sunset": ( + "Minutes since start of the day untill the moon disappears below the horizon", + "astronomy_sunset_min"), + } diff --git a/lib/view/prometheus.py b/lib/view/prometheus.py index 869cab4..84a0eb9 100644 --- a/lib/view/prometheus.py +++ b/lib/view/prometheus.py @@ -5,151 +5,51 @@ Rendering weather data in the Prometheus format. from datetime import datetime +from fields import DESCRIPTION -EXPORTED_FIELDS = { - "FeelsLikeC":("Feels Like Temperature in Celsius", "temperature_feels_like_celsius"), - "FeelsLikeF":("Feels Like Temperature in Fahrenheit", "temperature_feels_like_fahrenheit"), - "cloudcover":("Cloud Coverage in Percent", "cloudcover_percentage"), - "humidity":("Humidity in Percent", "humidity_percentage"), - "precipMM":("Precipitation (Rainfall) in mm", "precipitation_mm"), - "pressure":("Air pressure in hPa", "pressure_hpa"), - "temp_C":("Temperature in Celsius", "temperature_celsius"), - "temp_F":("Temperature in Fahrenheit", "temperature_fahrenheit"), - "uvIndex":("Ultaviolet Radiation Index", "uv_index"), - "visibility":("Visible Distance in Kilometres", "visibility"), - "weatherCode":("Code to describe Weather Condition", "weather_code"), - "winddirDegree":("Wind Direction in Degree", "winddir_degree"), - "windspeedKmph":("Wind Speed in Kilometres per Hour", "windspeed_kmph"), - "windspeedMiles":("Wind Speed in Miles per Hour", "windspeed_mph"), - } +def _render_current(data, for_day="current"): + "Converts data into prometheus style format" -EXPORTED_FIELDS_DESC = { - "weatherDesc":("Weather Description", "weather_desc"), - "winddir16Point":("Wind Direction on a 16-wind compass rose", "winddir_16_point"), - } - -EXPORTED_FIELDS_CONV = { - "observation_time":("Minutes since start of the day the observation happened", "obversation_time") - } - -EXPORTED_FIELDS_WEATHER = { - "maxtempC":("Maximum Temperature in Celsius", "temperature_celsius_maximum"), - "maxtempF":("Maximum Temperature in Fahrenheit", "temperature_fahrenheit_maximum"), - "mintempC":("Minimum Temperature in Celsius", "temperature_celsius_minimum"), - "mintempF":("Minimum Temperature in Fahrenheit", "temperature_fahrenheit_minimum"), - "sunHour":("Hours of sunlight", "sun_hour"), - "totalSnow_cm":("Total snowfall in cm", "snowfall_cm"), - "uvIndex":("Ultaviolet Radiation Index", "uv_index"), - } - -EXPORTED_FIELDS_WEATHER_ASTRONOMY = { - "moon_illumination":("Percentage of the moon illuminated", "astronomoy_moon_illumination"), - } - -EXPORTED_FIELDS_WEATHER_ASTRONOMY_DESC = { - "moon_phase": ("Phase of the moon", "astronomoy_moon_phase"), - } - -EXPORTED_FIELDS_WEATHER_ASTRONOMY_CONV = { - "moonrise":("Minutes since start of the day untill the moon appears above the horizon", "astronomoy_moonrise_min"), - "moonset":("Minutes since start of the day untill the moon disappears below the horizon", "astronomoy_moonset_min"), - "sunrise":("Minutes since start of the day untill the sun appears below the horizon", "astronomoy_sunrise_min"), - "sunset":("Minutes since start of the day untill the moon disappears below the horizon", "astronomoy_sunset_min"), + output = [] - } + for field_name, val in DESCRIPTION.items(): -def _render_current(data): - """ - Converts data into prometheus style format - """ + help, name = val - output = [] - current_condition = data["current_condition"][0] - for field in EXPORTED_FIELDS: try: - output.append("# HELP %s %s\n%s{forecast=\"current\"} %s" % - (EXPORTED_FIELDS[field][1], - EXPORTED_FIELDS[field][0], - EXPORTED_FIELDS[field][1], - current_condition[field])) - except IndexError: - pass + value = data[field_name] + if field_name == "weatherDesc": + value = value[0]["value"] + except (IndexError, KeyError): + try: + value = data["astronomy"][0][field_name] + if value.endswith(" AM") or value.endswith(" PM"): + value = _convert_time_to_minutes(value) + except (IndexError, KeyError, ValueError): + continue - for field in EXPORTED_FIELDS_CONV: try: - output.append("# HELP %s %s\n%s{forecast=\"current\"} %s" % - (EXPORTED_FIELDS[field][1], - EXPORTED_FIELDS[field][0], - EXPORTED_FIELDS[field][1], - current_condition[field])) - except IndexError: - pass + if name == "observation_time": + value = _convert_time_to_minutes(value) + except ValueError: + continue - for field in EXPORTED_FIELDS_DESC: + description = "" try: - output.append("# HELP %s %s\n%s{forecast=\"current\", description=\"%s\"} 1" % - (EXPORTED_FIELDS_DESC[field][1], - EXPORTED_FIELDS_DESC[field][0], - EXPORTED_FIELDS_DESC[field][1], - convert_time_to_minutes(current_condition[field]))) - except IndexError: - pass + float(value) + except ValueError: + description = f", description=\"{value}\"" + value = "1" - weather = data["weather"] - i = 0 - for day in weather: - for field in EXPORTED_FIELDS_WEATHER: - try: - output.append("# HELP %s %s\n%s{forecast=\"%id\"} %s" % - (EXPORTED_FIELDS_WEATHER[field][1], - EXPORTED_FIELDS_WEATHER[field][0], - i, - EXPORTED_FIELDS_WEATHER[field][1], - day[field])) - except IndexError: - pass - - for field in EXPORTED_FIELDS_WEATHER_ASTRONOMY: - try: - output.append("# HELP %s %s\n%s{forecast=\"%dd\"} %s" % - (EXPORTED_FIELDS_WEATHER[field][1], - EXPORTED_FIELDS_WEATHER[field][0], - EXPORTED_FIELDS_WEATHER[field][1], - i, - day["astronomy"][field])) - except IndexError: - pass - - for field in EXPORTED_FIELDS_WEATHER_ASTRONOMY_CONV: - try: - output.append("# HELP %s %s\n%s{forecast=\"%dd\"} %s" % - (EXPORTED_FIELDS_WEATHER[field][1], - EXPORTED_FIELDS_WEATHER[field][0], - EXPORTED_FIELDS_WEATHER[field][1], - i, - convert_time_to_minutes(day["astronomy"][field]))) - except IndexError: - pass - - for field in EXPORTED_FIELDS_WEATHER_ASTRONOMY_DESC: - try: - output.append("# HELP %s %s\n%s{forecast=\"%dd\", description=\"%s\"} 1" % - (EXPORTED_FIELDS_WEATHER[field][1], - EXPORTED_FIELDS_WEATHER[field][0], - EXPORTED_FIELDS_WEATHER[field][1], - i, - day["astronomy"][field])) - except IndexError: - pass - i = i+1 + output.append(f"# HELP {name} {help}\n" + f"{name}{{forecast=\"{for_day}\"{description}}} {value}") return "\n".join(output)+"\n" -def convert_time_to_minutes(time_str): - """ - Convert time from midnight to minutes - """ - return int((datetime.strptime(time_str, "%I:%M %p") - datetime.strptime("12:00 AM", "%I:%M %p")).total_seconds()/60) +def _convert_time_to_minutes(time_str): + "Convert time from midnight to minutes" + return int((datetime.strptime(time_str, "%I:%M %p") + - datetime.strptime("12:00 AM", "%I:%M %p")).total_seconds())//60 def render_prometheus(data): """ @@ -157,4 +57,8 @@ def render_prometheus(data): and return it as string. """ - return _render_current(data) + answer = _render_current(data["current_condition"][0]) + for i in range(3): + data2 = data["weather"][i] + answer += _render_current(data2, for_day="%sd" % i) + return answer