From 3dd697755db02c784237c0f08b2a26f7dfea39c2 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Wed, 21 Jul 2021 22:05:39 +0200 Subject: [PATCH] Replace datetime.fromisoformat with dateutil --- Dockerfile | 2 +- requirements.txt | 1 + wmn.py | 23 +++-------------------- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index 544458e..928369f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM docker.io/alpine:latest MAINTAINER Benedikt Ziemons -RUN apk add --no-cache uwsgi-python3 python3 py3-yaml py3-flask py3-matrix-nio +RUN apk add --no-cache uwsgi-python3 python3 py3-yaml py3-flask py3-matrix-nio py3-dateutil # partly from https://hub.docker.com/_/python?tab=description#create-a-dockerfile-in-your-python-app-project WORKDIR /usr/src/wmn diff --git a/requirements.txt b/requirements.txt index 7069f1b..a91adb4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ PyYAML>=5.1,<6 flask[async]>=2.0.0,<3 matrix-nio[e2e]>=0.18.0,<1 +python-dateutil~=2.8.2 diff --git a/wmn.py b/wmn.py index 9448e73..f0d4df0 100644 --- a/wmn.py +++ b/wmn.py @@ -28,6 +28,7 @@ import nio import yaml from flask import Flask, request, abort from werkzeug.datastructures import MultiDict +import dateutil.parser Cfg = Dict[str, Any] ErrorResponse = Tuple[str, int] @@ -40,12 +41,6 @@ application = app # Generally match room alias or id [!#]anything:example.com with unicode support. room_pattern = re.compile(r"^[!#]\w+:[\w\-.]+$") -# older prometheus/alertmanager versions send too many sub-second digits in their timestamp, -# so we get rid of nanoseconds here -promtime_to_isotime_pattern = re.compile( - r"([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(\.[0-9]{6})?(?:[0-9]{3})?(Z|[+-][0-9]{2}:[0-9]{2})" -) - def load_configuration() -> Cfg: with open("config.yml", "r") as ymlfile: @@ -380,16 +375,6 @@ async def process_prometheus_request(): text = severity return color_format_html(_severity_colors.get(severity, "FFFFFF"), text) - def parse_promtime(date_string) -> datetime: - match = promtime_to_isotime_pattern.match(date_string) - if match is None: - # weirdly enough, they switched to ISO primetime - return datetime.fromisoformat(date_string) - grps = list(filter(lambda x: x is not None, match.groups())) - if grps[-1] == "Z": - grps[-1] = "+00:00" - return datetime.fromisoformat("".join(grps)) - def alert_title(status: str, alertname: str, generator_url: str): if alertname: alertname = " alert " + alertname @@ -430,14 +415,12 @@ async def process_prometheus_request(): alert_daterange = [] if "startsAt" in alert and alert["startsAt"] != "0001-01-01T00:00:00Z": alert_start = ( - parse_promtime(alert["startsAt"]) - .strftime("%d. %b %y %H:%M %Z") - .rstrip() + dateutil.parser.isoparse(alert["startsAt"]).strftime("%d. %b %y %H:%M %Z").rstrip() ) alert_daterange.append(f"started at {alert_start}") if "endsAt" in alert and alert["endsAt"] != "0001-01-01T00:00:00Z": alert_end = ( - parse_promtime(alert["endsAt"]).strftime("%d. %b %y %H:%M %Z").rstrip() + dateutil.parser.isoparse(alert["endsAt"]).strftime("%d. %b %y %H:%M %Z").rstrip() ) alert_daterange.append(f"ended at {alert_end}") alert_daterange = ", ".join(alert_daterange)