Skip to content

Commit d15c734

Browse files
authored
Add column publication status for cloud (#72)
Added days parameter for flexibility. Removed redundant elif. Handling future date at the beginning. Used diff_days instead of timedelta comparison for clarity. Used dict.update() for cleaner dictionary updates. Used f-strings for better readability.
1 parent a28ae66 commit d15c734

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

monitoring/publishing/templates/cloudsites.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ <h2>Sites publishing cloud accounting records in the last year</h2>
1818
<th class='tableheader'>VMsInLastUpdate</th>
1919
<th class='tableheader'>CloudType</th>
2020
<th class='tableheader'>LastUpdated</th>
21+
<th class='tableheader'>Publication Status</th>
2122
</tr>
2223
{% for site in sites %}
2324
<tr onmouseover="this.className='highlight-row'" onmouseout="this.className='tabletext'" class="tabletext">
2425
<td>{{ site.SiteName }}</td>
2526
<td>{{ site.Vms }}</td>
2627
<td>{{ site.Script }}</td>
2728
<td>{{ site.updated|date:"Y-m-d H:i:s" }}</td>
29+
<td>{{ site.stdout }}</td>
2830
</tr>
2931
{% endfor %}
3032
</table>

monitoring/publishing/views.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,32 @@
2626
GridSiteSyncSubmitHSerializer
2727
)
2828

29+
def update_dict_stdout_and_returncode(single_dict, date, days=7):
30+
today = datetime.today()
31+
32+
# Handle future dates
33+
if date > today:
34+
single_dict.update({
35+
'returncode': 3,
36+
'stdout': "UNKNOWN"
37+
})
38+
return single_dict
39+
40+
diff_days = (today - date).days
41+
formatted_date = date.strftime("%Y-%m-%d")
42+
43+
if diff_days <= days:
44+
status = "OK"
45+
returncode = 0
46+
else:
47+
status = "WARNING"
48+
returncode = 1
2949

30-
def update_dict_stdout_and_returncode(single_dict, date):
31-
diff = datetime.today() - date
32-
date = date.strftime("%Y-%m-%d")
50+
single_dict.update({
51+
'returncode': returncode,
52+
'stdout': f"{status} [ last published {diff_days} days ago: {formatted_date} ]"
53+
})
3354

34-
if diff <= timedelta(days=7):
35-
single_dict['returncode'] = 0
36-
single_dict['stdout'] = "OK [ last published %s days ago: %s ]" % (diff.days, date)
37-
elif diff > timedelta(days=7):
38-
single_dict['returncode'] = 1
39-
single_dict['stdout'] = "WARNING [ last published %s days ago: %s ]" % (diff.days, date)
40-
else:
41-
single_dict['returncode'] = 3
42-
single_dict['stdout'] = "UNKNOWN"
4355
return single_dict
4456

4557

@@ -203,11 +215,18 @@ def list(self, request):
203215
if last_fetched is not None:
204216
print(last_fetched.replace(tzinfo=None), datetime.today() - timedelta(hours=1, seconds=20))
205217

218+
final_response = []
206219
response = super(CloudSiteViewSet, self).list(request)
220+
221+
for single_dict in response.data:
222+
date = single_dict.get('updated').replace(tzinfo=None)
223+
single_dict = update_dict_stdout_and_returncode(single_dict, date)
224+
final_response.append(single_dict)
225+
207226
# Wrap data in a dict so that it can display in template.
208227
if type(request.accepted_renderer) is TemplateHTMLRenderer:
209228
response.data = {
210-
'sites': response.data,
229+
'sites': final_response,
211230
'last_fetched': last_fetched
212231
}
213232
return response

0 commit comments

Comments
 (0)