Skip to content

Commit 2493a19

Browse files
authored
Merge pull request nicolargo#2925 from nicolargo/standardize-history-display-timestamp
chg: alerts - standardise history display timings
2 parents 74a8b0b + 16d5610 commit 2493a19

File tree

3 files changed

+97
-88
lines changed

3 files changed

+97
-88
lines changed
Lines changed: 92 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,111 @@
11
<template>
2-
<div class="plugin">
3-
<section id="alerts">
2+
<div class="plugin">
3+
<section id="alerts">
44
<span class="title" v-if="hasAlerts">
55
Warning or critical alerts (last {{ countAlerts }} entries)
66
</span>
7-
<span class="title" v-else>No warning or critical alert detected</span>
8-
</section>
9-
<section id="alert">
10-
<div class="table">
11-
<div class="table-row" v-for="(alert, alertId) in alerts" :key="alertId">
12-
<div class="table-cell text-left">
13-
{{ formatDate(alert.begin) }}
14-
{{ alert.tz }}
15-
({{ alert.ongoing ? 'ongoing' : alert.duration }}) -
16-
<span v-show="!alert.ongoing"> {{ alert.state }} on </span>
17-
<span :class="alert.state.toLowerCase()">
7+
<span class="title" v-else>No warning or critical alert detected</span>
8+
</section>
9+
<section id="alert">
10+
<div class="table">
11+
<div class="table-row" v-for="(alert, alertId) in alerts" :key="alertId">
12+
<div class="table-cell text-left">
13+
{{ formatDate(alert.begin) }}
14+
({{ alert.ongoing ? 'ongoing' : alert.duration }}) -
15+
<span v-show="!alert.ongoing"> {{ alert.state }} on </span>
16+
<span :class="alert.state.toLowerCase()">
1817
{{ alert.type }}
19-
</span>
20-
({{ $filters.number(alert.max, 1) }})
21-
{{ alert.top }}
22-
</div>
23-
</div>
24-
</div>
25-
</section>
26-
</div>
18+
</span>
19+
({{ $filters.number(alert.max, 1) }})
20+
{{ alert.top }}
21+
</div>
22+
</div>
23+
</div>
24+
</section>
25+
</div>
2726
</template>
2827

2928
<script>
30-
import { padStart } from 'lodash';
31-
import { GlancesFavico } from '../services.js';
29+
import {padStart} from 'lodash';
30+
import {GlancesFavico} from '../services.js';
3231
3332
export default {
34-
props: {
35-
data: {
36-
type: Object
37-
}
33+
props: {
34+
data: {
35+
type: Object
36+
}
37+
},
38+
computed: {
39+
stats() {
40+
return this.data.stats['alert'];
3841
},
39-
computed: {
40-
stats() {
41-
return this.data.stats['alert'];
42-
},
43-
alerts() {
44-
return (this.stats || []).map((alertalertStats) => {
45-
const alert = {};
46-
var tzoffset = new Date().getTimezoneOffset();
47-
alert.state = alertalertStats.state;
48-
alert.type = alertalertStats.type;
49-
alert.begin = alertalertStats.begin * 1000 - tzoffset * 60 * 1000;
50-
alert.end = alertalertStats.end * 1000 - tzoffset * 60 * 1000;
51-
alert.ongoing = alertalertStats.end == -1;
52-
alert.min = alertalertStats.min;
53-
alert.avg = alertalertStats.avg;
54-
alert.max = alertalertStats.max;
55-
alert.top = alertalertStats.top.join(', ');
56-
57-
if (!alert.ongoing) {
58-
const duration = alert.end - alert.begin;
59-
const seconds = parseInt((duration / 1000) % 60),
60-
minutes = parseInt((duration / (1000 * 60)) % 60),
61-
hours = parseInt((duration / (1000 * 60 * 60)) % 24);
42+
alerts() {
43+
return (this.stats || []).map((alertStats) => {
44+
const alert = {};
45+
alert.state = alertStats.state;
46+
alert.type = alertStats.type;
47+
alert.begin = alertStats.begin * 1000;
48+
alert.end = alertStats.end * 1000;
49+
alert.ongoing = alertStats.end == -1;
50+
alert.min = alertStats.min;
51+
alert.avg = alertStats.avg;
52+
alert.max = alertStats.max;
53+
alert.top = alertStats.top.join(', ');
6254
63-
alert.duration =
64-
padStart(hours, 2, '0') +
65-
':' +
66-
padStart(minutes, 2, '0') +
67-
':' +
68-
padStart(seconds, 2, '0');
69-
}
55+
if (!alert.ongoing) {
56+
const duration = alert.end - alert.begin;
57+
const seconds = parseInt((duration / 1000) % 60),
58+
minutes = parseInt((duration / (1000 * 60)) % 60),
59+
hours = parseInt((duration / (1000 * 60 * 60)) % 24);
7060
71-
return alert;
72-
});
73-
},
74-
hasAlerts() {
75-
return this.countAlerts > 0;
76-
},
77-
countAlerts() {
78-
return this.alerts.length;
79-
},
80-
hasOngoingAlerts() {
81-
return this.countOngoingAlerts > 0;
82-
},
83-
countOngoingAlerts() {
84-
return this.alerts.filter(({ ongoing }) => ongoing).length;
61+
alert.duration = padStart(hours, 2, '0') +
62+
':' + padStart(minutes, 2, '0') +
63+
':' + padStart(seconds, 2, '0');
8564
}
65+
66+
return alert;
67+
});
8668
},
87-
watch: {
88-
countOngoingAlerts() {
89-
if (this.countOngoingAlerts) {
90-
GlancesFavico.badge(this.countOngoingAlerts);
91-
} else {
92-
GlancesFavico.reset();
93-
}
94-
}
69+
hasAlerts() {
70+
return this.countAlerts > 0;
9571
},
96-
methods: {
97-
formatDate(date) {
98-
return new Date(date).toLocaleString();
99-
}
72+
countAlerts() {
73+
return this.alerts.length;
74+
},
75+
hasOngoingAlerts() {
76+
return this.countOngoingAlerts > 0;
77+
},
78+
countOngoingAlerts() {
79+
return this.alerts.filter(({ongoing}) => ongoing).length;
80+
}
81+
},
82+
watch: {
83+
countOngoingAlerts() {
84+
if (this.countOngoingAlerts) {
85+
GlancesFavico.badge(this.countOngoingAlerts);
86+
} else {
87+
GlancesFavico.reset();
88+
}
89+
}
90+
},
91+
methods: {
92+
formatDate(timestamp) {
93+
const tzOffset = new Date().getTimezoneOffset();
94+
const hours = Math.trunc(Math.abs(tzOffset) / 60);
95+
const minutes = Math.abs(tzOffset % 60);
96+
97+
let tzString = tzOffset <= 0 ? '+' : '-';
98+
tzString += String(hours).padStart(2, '0') + String(minutes).padStart(2, '0');
99+
100+
const date = new Date(timestamp);
101+
return String(date.getFullYear()) +
102+
'-' + String(date.getMonth()).padStart(2, '0') +
103+
'-' + String(date.getDate()).padStart(2, '0') +
104+
' ' + String(date.getHours()).padStart(2, '0') +
105+
':' + String(date.getMinutes()).padStart(2, '0') +
106+
':' + String(date.getSeconds()).padStart(2, '0') +
107+
'(' + tzString + ')';
100108
}
109+
}
101110
};
102111
</script>

0 commit comments

Comments
 (0)