join all snapshots logs in one for every uuid

This commit is contained in:
Cayo Puigdefabregas 2022-05-19 13:48:52 +02:00
parent cea143dcbe
commit 744b14f286
2 changed files with 37 additions and 12 deletions

View File

@ -519,13 +519,42 @@ class SnapshotListView(GenericMixin):
def dispatch_request(self):
self.get_context()
self.context['page_title'] = "Snapshots Logs"
snapshots_log = SnapshotsLog.query.filter(
SnapshotsLog.owner == current_user
).order_by(SnapshotsLog.created.desc())
self.context['snapshots_log'] = snapshots_log
self.context['snapshots_log'] = self.get_snapshots_log()
return flask.render_template(self.template_name, **self.context)
def get_snapshots_log(self):
snapshots_log = SnapshotsLog.query.filter(
SnapshotsLog.owner == g.user
).order_by(SnapshotsLog.created.desc())
logs = {}
for snap in snapshots_log:
if snap.snapshot_uuid not in logs:
logs[snap.snapshot_uuid] = {
'sid': snap.sid,
'snapshot_uuid': snap.snapshot_uuid,
'version': snap.version,
'device': snap.snapshot.device.devicehub_id
if snap.snapshot
else '',
'status': snap.get_status(),
'severity': snap.severity,
'created': snap.created,
}
continue
if snap.created > logs[snap.snapshot_uuid]['created']:
logs[snap.snapshot_uuid]['created'] = snap.created
if snap.severity > logs[snap.snapshot_uuid]['severity']:
logs[snap.snapshot_uuid]['severity'] = snap.severity
logs[snap.snapshot_uuid]['status'] = snap.get_status()
result = sorted(logs.values(), key=lambda d: d['created'])
result.reverse()
return result
devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add'))
devices.add_url_rule('/action/trade/add/', view_func=NewTradeView.as_view('trade_add'))

View File

@ -30,7 +30,6 @@
<th scope="col">Version</th>
<th scope="col">Device</th>
<th scope="col">Status</th>
<th scope="col">Description</th>
<th scope="col" data-type="date" data-format="DD-MM-YYYY">Time</th>
</tr>
</thead>
@ -47,17 +46,14 @@
{{ snap.version }}
</td>
<td>
{% if snap.snapshot %}
<a href="{{ url_for('inventory.device_details', id=snap.snapshot.device.devicehub_id)}}">
{{ snap.snapshot.device.devicehub_id }}
{% if snap.device %}
<a href="{{ url_for('inventory.device_details', id=snap.device) }}">
{{ snap.device }}
</a>
{% endif %}
</td>
<td>
{{ snap.get_status() }}
</td>
<td>
{{ snap.description }}
{{ snap.status }}
</td>
<td>{{ snap.created.strftime('%H:%M %d-%m-%Y') }}</td>
</tr>