|
| 1 | +from datetime import timedelta |
| 2 | + |
1 | 3 | from bolt.admin import (
|
2 | 4 | AdminModelDetailView,
|
3 | 5 | AdminModelListView,
|
|
7 | 9 | from bolt.admin.cards import Card
|
8 | 10 | from bolt.admin.dates import DatetimeRangeAliases
|
9 | 11 | from bolt.http import HttpResponseRedirect
|
| 12 | +from bolt.runtime import settings |
10 | 13 |
|
11 | 14 | from .models import Job, JobRequest, JobResult
|
12 | 15 |
|
13 | 16 |
|
| 17 | +def _td_format(td_object): |
| 18 | + seconds = int(td_object.total_seconds()) |
| 19 | + periods = [ |
| 20 | + ("year", 60 * 60 * 24 * 365), |
| 21 | + ("month", 60 * 60 * 24 * 30), |
| 22 | + ("day", 60 * 60 * 24), |
| 23 | + ("hour", 60 * 60), |
| 24 | + ("minute", 60), |
| 25 | + ("second", 1), |
| 26 | + ] |
| 27 | + |
| 28 | + strings = [] |
| 29 | + for period_name, period_seconds in periods: |
| 30 | + if seconds > period_seconds: |
| 31 | + period_value, seconds = divmod(seconds, period_seconds) |
| 32 | + has_s = "s" if period_value > 1 else "" |
| 33 | + strings.append("%s %s%s" % (period_value, period_name, has_s)) |
| 34 | + |
| 35 | + return ", ".join(strings) |
| 36 | + |
| 37 | + |
14 | 38 | class SuccessfulJobsCard(Card):
|
15 | 39 | title = "Successful Jobs"
|
16 | 40 | text = "View"
|
@@ -45,6 +69,10 @@ class LostJobsCard(Card):
|
45 | 69 | title = "Lost Jobs"
|
46 | 70 | text = "View" # TODO make not required - just an icon?
|
47 | 71 |
|
| 72 | + def get_description(self): |
| 73 | + delta = timedelta(seconds=settings.JOBS_LOST_AFTER) |
| 74 | + return f"Jobs are considered lost after {_td_format(delta)}" |
| 75 | + |
48 | 76 | def get_number(self):
|
49 | 77 | return (
|
50 | 78 | JobResult.objects.lost()
|
@@ -128,6 +156,10 @@ class ListView(AdminModelListView):
|
128 | 156 | allow_global_search = False
|
129 | 157 | default_datetime_range = DatetimeRangeAliases.LAST_7_DAYS
|
130 | 158 |
|
| 159 | + def get_description(self): |
| 160 | + delta = timedelta(seconds=settings.JOBS_CLEARABLE_AFTER) |
| 161 | + return f"Jobs are cleared after {_td_format(delta)}" |
| 162 | + |
131 | 163 | def get_initial_queryset(self):
|
132 | 164 | queryset = super().get_initial_queryset()
|
133 | 165 | if self.filter == "Successful":
|
|
0 commit comments