Skip to content

Commit bca8876

Browse files
committed
Fix #494
(not tested yet)
1 parent 5bbd4c3 commit bca8876

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
package database
22

33
import (
4-
"github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
54
"sort"
5+
"syscall"
6+
7+
"github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
68
)
79

810
func sortSmartMeasurementsDesc(smartResults []measurements.Smart) {
911
sort.SliceStable(smartResults, func(i, j int) bool {
1012
return smartResults[i].Date.After(smartResults[j].Date)
1113
})
1214
}
15+
16+
func getUptimeSeconds() (int64, error) {
17+
sysinfo := &syscall.Sysinfo_t{}
18+
if err := syscall.Sysinfo(sysinfo); err != nil {
19+
return 0, err
20+
}
21+
return sysinfo.Uptime, nil
22+
}

webapp/backend/pkg/database/scrutiny_repository_temperature.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,42 @@ package database
33
import (
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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1617
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)
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

Comments
 (0)