forked from GithubBackups/healthchecks
In monthly report, show months in ascending order. Cleanup.
This commit is contained in:
parent
cb2e763e98
commit
b7320b1b69
@ -256,17 +256,16 @@ class Check(models.Model):
|
|||||||
def monthkey(dt):
|
def monthkey(dt):
|
||||||
return dt.year, dt.month
|
return dt.year, dt.month
|
||||||
|
|
||||||
|
# Datetimes of the first days of months we're interested in. Ascending order.
|
||||||
|
boundaries = month_boundaries(months=months)
|
||||||
|
|
||||||
# Will accumulate totals here.
|
# Will accumulate totals here.
|
||||||
# (year, month) -> [datetime, downtime_in_secs, number_of_outages]
|
# (year, month) -> [datetime, total_downtime, number_of_outages]
|
||||||
totals = {}
|
totals = {monthkey(b): [b, td(), 0] for b in boundaries}
|
||||||
# Will collect flips and month boundaries here
|
|
||||||
events = []
|
|
||||||
|
|
||||||
for boundary in month_boundaries(months=months):
|
# A list of flips and month boundaries
|
||||||
totals[monthkey(boundary)] = [boundary, 0, 0]
|
events = [(b, "---") for b in boundaries]
|
||||||
events.append((boundary, "---"))
|
for flip in self.flip_set.filter(created__gt=min(boundaries)):
|
||||||
|
|
||||||
for flip in self.flip_set.filter(created__gt=boundary):
|
|
||||||
events.append((flip.created, flip.old_status))
|
events.append((flip.created, flip.old_status))
|
||||||
|
|
||||||
# Iterate through flips and month boundaries in reverse order,
|
# Iterate through flips and month boundaries in reverse order,
|
||||||
@ -275,16 +274,14 @@ class Check(models.Model):
|
|||||||
for prev_dt, prev_status in sorted(events, reverse=True):
|
for prev_dt, prev_status in sorted(events, reverse=True):
|
||||||
if status == "down":
|
if status == "down":
|
||||||
delta = dt - prev_dt
|
delta = dt - prev_dt
|
||||||
totals[monthkey(prev_dt)][1] += int(delta.total_seconds())
|
totals[monthkey(prev_dt)][1] += delta
|
||||||
totals[monthkey(prev_dt)][2] += 1
|
totals[monthkey(prev_dt)][2] += 1
|
||||||
|
|
||||||
dt = prev_dt
|
dt = prev_dt
|
||||||
if prev_status != "---":
|
if prev_status != "---":
|
||||||
status = prev_status
|
status = prev_status
|
||||||
|
|
||||||
flattened = list(totals.values())
|
return sorted(totals.values())
|
||||||
flattened.sort(reverse=True)
|
|
||||||
return flattened
|
|
||||||
|
|
||||||
|
|
||||||
class Ping(models.Model):
|
class Ping(models.Model):
|
||||||
|
@ -170,8 +170,8 @@ class CheckModelTestCase(BaseTestCase):
|
|||||||
check = Check.objects.create(project=self.project)
|
check = Check.objects.create(project=self.project)
|
||||||
r = check.outages_by_month(10)
|
r = check.outages_by_month(10)
|
||||||
self.assertEqual(len(r), 10)
|
self.assertEqual(len(r), 10)
|
||||||
for dt, secs, outages in r:
|
for dt, downtime, outages in r:
|
||||||
self.assertEqual(secs, 0)
|
self.assertEqual(downtime.total_seconds(), 0)
|
||||||
self.assertEqual(outages, 0)
|
self.assertEqual(outages, 0)
|
||||||
|
|
||||||
def test_outages_by_month_handles_currently_down_check(self):
|
def test_outages_by_month_handles_currently_down_check(self):
|
||||||
@ -179,7 +179,7 @@ class CheckModelTestCase(BaseTestCase):
|
|||||||
|
|
||||||
r = check.outages_by_month(10)
|
r = check.outages_by_month(10)
|
||||||
self.assertEqual(len(r), 10)
|
self.assertEqual(len(r), 10)
|
||||||
for dt, secs, outages in r:
|
for dt, downtime, outages in r:
|
||||||
self.assertEqual(outages, 1)
|
self.assertEqual(outages, 1)
|
||||||
|
|
||||||
@patch("hc.api.models.timezone.now")
|
@patch("hc.api.models.timezone.now")
|
||||||
@ -195,12 +195,12 @@ class CheckModelTestCase(BaseTestCase):
|
|||||||
|
|
||||||
r = check.outages_by_month(10)
|
r = check.outages_by_month(10)
|
||||||
self.assertEqual(len(r), 10)
|
self.assertEqual(len(r), 10)
|
||||||
for dt, secs, outages in r:
|
for dt, downtime, outages in r:
|
||||||
if dt.month == 7:
|
if dt.month == 7:
|
||||||
self.assertEqual(secs, 86400)
|
self.assertEqual(downtime.total_seconds(), 86400)
|
||||||
self.assertEqual(outages, 1)
|
self.assertEqual(outages, 1)
|
||||||
else:
|
else:
|
||||||
self.assertEqual(secs, 0)
|
self.assertEqual(downtime.total_seconds(), 0)
|
||||||
self.assertEqual(outages, 0)
|
self.assertEqual(outages, 0)
|
||||||
|
|
||||||
@patch("hc.api.models.timezone.now")
|
@patch("hc.api.models.timezone.now")
|
||||||
@ -216,14 +216,14 @@ class CheckModelTestCase(BaseTestCase):
|
|||||||
|
|
||||||
r = check.outages_by_month(10)
|
r = check.outages_by_month(10)
|
||||||
self.assertEqual(len(r), 10)
|
self.assertEqual(len(r), 10)
|
||||||
for dt, secs, outages in r:
|
for dt, downtime, outages in r:
|
||||||
if dt.month == 7:
|
if dt.month == 7:
|
||||||
self.assertEqual(outages, 1)
|
self.assertEqual(outages, 1)
|
||||||
elif dt.month == 6:
|
elif dt.month == 6:
|
||||||
self.assertEqual(secs, 30 * 86400)
|
self.assertEqual(downtime.total_seconds(), 30 * 86400)
|
||||||
self.assertEqual(outages, 1)
|
self.assertEqual(outages, 1)
|
||||||
elif dt.month == 5:
|
elif dt.month == 5:
|
||||||
self.assertEqual(outages, 1)
|
self.assertEqual(outages, 1)
|
||||||
else:
|
else:
|
||||||
self.assertEqual(secs, 0)
|
self.assertEqual(downtime.total_seconds(), 0)
|
||||||
self.assertEqual(outages, 0)
|
self.assertEqual(outages, 0)
|
||||||
|
@ -36,9 +36,6 @@ def format_duration(td):
|
|||||||
|
|
||||||
|
|
||||||
def format_hms(td):
|
def format_hms(td):
|
||||||
if isinstance(td, int):
|
|
||||||
total_seconds = td
|
|
||||||
else:
|
|
||||||
total_seconds = int(td.total_seconds())
|
total_seconds = int(td.total_seconds())
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
@ -57,7 +54,8 @@ def format_hms(td):
|
|||||||
return " ".join(result)
|
return " ".join(result)
|
||||||
|
|
||||||
|
|
||||||
def format_approx_duration(v):
|
def format_approx_duration(td):
|
||||||
|
v = td.total_seconds()
|
||||||
for unit in (DAY, HOUR, MINUTE, SECOND):
|
for unit in (DAY, HOUR, MINUTE, SECOND):
|
||||||
if v >= unit.nsecs:
|
if v >= unit.nsecs:
|
||||||
vv = v // unit.nsecs
|
vv = v // unit.nsecs
|
||||||
@ -75,7 +73,7 @@ def month_boundaries(months=2):
|
|||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
y, m = now.year, now.month
|
y, m = now.year, now.month
|
||||||
for x in range(0, months):
|
for x in range(0, months):
|
||||||
result.append(dt(y, m, 1, tzinfo=timezone.utc))
|
result.insert(0, dt(y, m, 1, tzinfo=timezone.utc))
|
||||||
|
|
||||||
m -= 1
|
m -= 1
|
||||||
if m == 0:
|
if m == 0:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user