@@ -3,28 +3,42 @@ package database
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
7
+ "time"
8
+
6
9
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
7
10
"github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
8
11
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
9
- "strings"
10
- "time"
11
12
)
12
13
13
14
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
14
15
// Temperature Data
15
16
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16
17
func (sr * scrutinyRepository ) SaveSmartTemperature (ctx context.Context , wwn string , deviceProtocol string , collectorSmartData collector.SmartInfo ) error {
17
- if len (collectorSmartData .AtaSctTemperatureHistory .Table ) > 0 {
18
+ uptimeSeconds , err := getUptimeSeconds ()
19
+ if len (collectorSmartData .AtaSctTemperatureHistory .Table ) > 0 && err == nil { //If cannot get uptime, fallback to default behavior (don't parse history)
18
20
19
21
for ndx , temp := range collectorSmartData .AtaSctTemperatureHistory .Table {
20
22
//temp value may be null, we must skip/ignore them. See #393
21
23
if temp == 0 {
22
24
continue
23
25
}
24
26
25
- minutesOffset := collectorSmartData .AtaSctTemperatureHistory .LoggingIntervalMinutes * int64 (ndx ) * 60
27
+ index := collectorSmartData .AtaSctTemperatureHistory .Index
28
+ size := collectorSmartData .AtaSctTemperatureHistory .Size
29
+ dt := collectorSmartData .AtaSctTemperatureHistory .LoggingIntervalMinutes
30
+ var minutesOffset int64
31
+ if ndx <= index {
32
+ minutesOffset = dt * int64 (index - ndx )
33
+ } else {
34
+ minutesOffset = dt * int64 (size + index - ndx )
35
+ }
36
+
37
+ if (minutesOffset * 60 > uptimeSeconds ) {
38
+ continue // skip values before boot
39
+ }
26
40
smartTemp := measurements.SmartTemperature {
27
- Date : time .Unix (collectorSmartData .LocalTime .TimeT - minutesOffset , 0 ),
41
+ Date : time .Unix (collectorSmartData .LocalTime .TimeT - ( 60 * minutesOffset ) , 0 ),
28
42
Temp : temp ,
29
43
}
30
44
0 commit comments