1
|
from django.db import connection
|
2
|
|
3
|
from passerelle.base.models import ResourceStatus
|
4
|
|
5
|
last_down = None
|
6
|
downtimes = []
|
7
|
|
8
|
for r in ResourceStatus.objects.order_by('resource_pk', 'resource_type_id', 'start_timestamp'):
|
9
|
if r.status == 'down':
|
10
|
if last_down:
|
11
|
if (last_down.resource_type, last_down.resource_pk) == (r.resource_type, r.resource_pk):
|
12
|
continue
|
13
|
last_down = r
|
14
|
else:
|
15
|
if last_down:
|
16
|
if (last_down.resource_type, last_down.resource_pk) == (r.resource_type, r.resource_pk):
|
17
|
downtimes.append((r.resource_type, r.resource_pk, last_down.start_timestamp, r.start_timestamp - last_down.start_timestamp))
|
18
|
last_down = None
|
19
|
|
20
|
if downtimes:
|
21
|
print connection.tenant.domain_url
|
22
|
resource = None
|
23
|
grouped = {}
|
24
|
for rt, rpk, timestamp, duration in downtimes:
|
25
|
resource = rt.model_class().objects.get(pk=rpk)
|
26
|
grouped.setdefault(resource, []).append((timestamp, duration))
|
27
|
|
28
|
for resource in grouped:
|
29
|
print resource, 'downtimes :'
|
30
|
for timestamp, duration in grouped[resource]:
|
31
|
hours = duration.total_seconds() // 3600
|
32
|
minutes = duration.total_seconds() // 60
|
33
|
if hours > 1:
|
34
|
human = '%d heures' % hours
|
35
|
elif minutes > 1:
|
36
|
human = '%d minutes' % minutes
|
37
|
else:
|
38
|
human = '%d secondes' % duration.total_seconds()
|
39
|
print timestamp, 'pendant', human
|