Skip to content

Commit

Permalink
Use absolute time for graphite time format
Browse files Browse the repository at this point in the history
  • Loading branch information
stveit committed Mar 9, 2023
1 parent db33cb2 commit 92ae92b
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions python/nav/metrics/thresholds.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@
Alerting is outside of the scope of this module.
"""
from datetime import timedelta
from datetime import timedelta, datetime
from functools import partial
import logging
import re

from nav.metrics.data import get_metric_average
try:
from zoneinfo import ZoneInfo
except ImportError:
from backports.zoneinfo import ZoneInfo

from django.conf import settings

from nav.metrics.data import get_metric_average, GRAPHITE_TIME_FORMAT
from nav.metrics.graphs import get_metric_meta, extract_series_name


Expand Down Expand Up @@ -135,7 +142,7 @@ def get_values(self):
"""
Retrieves actual values from Graphite based on the evaluators target.
"""
start = "-{0}".format(interval_to_graphite(self.period))
start = interval_to_graphite_start(self.period)
averages = get_metric_average(
self.target, start=start, end='now', ignore_unknown=True
)
Expand Down Expand Up @@ -230,18 +237,13 @@ class InvalidExpressionError(Exception):
pass


def interval_to_graphite(delta):
def interval_to_graphite_start(delta):
"""Converts a timedelta to a usable Graphite time specification.
:type delta: datetime.timedelta
"""
secs = delta.total_seconds()
if secs % YEAR == 0:
return "{0}year".format(int(secs / YEAR))
elif secs % DAY == 0:
return "{0}day".format(int(secs / DAY))
elif secs % MINUTE == 0:
return "{0}min".format(int(secs / MINUTE))
else:
return "{0}s".format(int(secs))
now = datetime.now(tz=ZoneInfo(settings.TIME_ZONE))
start = now - delta
graphite_start = start.strftime(GRAPHITE_TIME_FORMAT)
return graphite_start

0 comments on commit 92ae92b

Please sign in to comment.