Skip to content

Commit

Permalink
[INTERNAL] Django upgrade fixes #515
Browse files Browse the repository at this point in the history
In #495 ( 06a2c20 ) we upgraded Django. This means that Django itself doesn't depend on pytz, but we still had a few instances of our code requiring it. Instead of making pytz a dependency of Promgen, we can refactor a few bits to remove the dependency.
  • Loading branch information
kfdm authored May 24, 2024
2 parents 381e6bd + 5e7c7c5 commit 1d1a96e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 7 additions & 3 deletions promgen/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import tempfile
from urllib.parse import urljoin

import pytz
import yaml
from dateutil import parser
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -264,6 +263,9 @@ def silence(*, labels, duration=None, **kwargs):
Post a silence message to Alert Manager
Duration should be sent in a format like 1m 2h 1d etc
"""
# We active a timezone here, because the frontend (browser)
# will be POSTing date times without timezones
timezone.activate(util.setting("timezone", "UTC"))

if duration:
start = timezone.now()
Expand All @@ -278,9 +280,11 @@ def silence(*, labels, duration=None, **kwargs):
kwargs["startsAt"] = start.isoformat()
kwargs["endsAt"] = end.isoformat()
else:
local_timezone = pytz.timezone(util.setting("timezone", "UTC"))
for key in ["startsAt", "endsAt"]:
kwargs[key] = local_timezone.localize(parser.parse(kwargs[key])).isoformat()
dt = parser.parse(kwargs[key])
if timezone.is_naive(dt):
dt = timezone.make_aware(dt)
kwargs[key] = dt.isoformat()

kwargs["matchers"] = [
{
Expand Down
15 changes: 10 additions & 5 deletions promgen/templatetags/promgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import yaml
from django import template
from django.urls import reverse
from django.utils import timezone
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _
from pytz import timezone

from promgen import util

register = template.Library()

Expand Down Expand Up @@ -98,9 +96,16 @@ def pretty_yaml(data):

@register.filter()
def strftime(timestamp, fmt):
tz = util.setting("timezone", "UTC")
"""
Convert a timestamp to a specific format.
Normally we would use the built in django `time` filters, but sometimes we
get a numeric value as a timestamp (from Prometheus results) so we want a
single place where we can convert any type of value to a timestamp.
"""
if isinstance(timestamp, int) or isinstance(timestamp, float):
return timezone(tz).localize(datetime.fromtimestamp(timestamp)).strftime(fmt)
dt = datetime.fromtimestamp(timestamp, timezone.utc)
return timezone.localtime(dt).strftime(fmt)
return timestamp


Expand Down

0 comments on commit 1d1a96e

Please sign in to comment.