@@ -3,28 +3,42 @@ package database
33import (
44 "context"
55 "fmt"
6+ "strings"
7+ "time"
8+
69 "github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
710 "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
811 influxdb2 "github.com/influxdata/influxdb-client-go/v2"
9- "strings"
10- "time"
1112)
1213
1314////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1415// Temperature Data
1516////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1617func (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)
1820
1921 for ndx , temp := range collectorSmartData .AtaSctTemperatureHistory .Table {
2022 //temp value may be null, we must skip/ignore them. See #393
2123 if temp == 0 {
2224 continue
2325 }
2426
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+ }
2640 smartTemp := measurements.SmartTemperature {
27- Date : time .Unix (collectorSmartData .LocalTime .TimeT - minutesOffset , 0 ),
41+ Date : time .Unix (collectorSmartData .LocalTime .TimeT - ( 60 * minutesOffset ) , 0 ),
2842 Temp : temp ,
2943 }
3044
0 commit comments