diff --git a/webapp/backend/pkg/database/helpers.go b/webapp/backend/pkg/database/helpers.go index 3706d86e..eea7db8a 100644 --- a/webapp/backend/pkg/database/helpers.go +++ b/webapp/backend/pkg/database/helpers.go @@ -1,8 +1,10 @@ package database import ( - "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements" "sort" + "syscall" + + "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements" ) func sortSmartMeasurementsDesc(smartResults []measurements.Smart) { @@ -10,3 +12,11 @@ func sortSmartMeasurementsDesc(smartResults []measurements.Smart) { return smartResults[i].Date.After(smartResults[j].Date) }) } + +func getUptimeSeconds() (int64, error) { + sysinfo := &syscall.Sysinfo_t{} + if err := syscall.Sysinfo(sysinfo); err != nil { + return 0, err + } + return sysinfo.Uptime, nil +} \ No newline at end of file diff --git a/webapp/backend/pkg/database/scrutiny_repository_temperature.go b/webapp/backend/pkg/database/scrutiny_repository_temperature.go index 9edec87e..6e2b9e62 100644 --- a/webapp/backend/pkg/database/scrutiny_repository_temperature.go +++ b/webapp/backend/pkg/database/scrutiny_repository_temperature.go @@ -3,18 +3,20 @@ package database import ( "context" "fmt" + "strings" + "time" + "github.com/analogj/scrutiny/webapp/backend/pkg/models/collector" "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements" influxdb2 "github.com/influxdata/influxdb-client-go/v2" - "strings" - "time" ) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Temperature Data //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// func (sr *scrutinyRepository) SaveSmartTemperature(ctx context.Context, wwn string, deviceProtocol string, collectorSmartData collector.SmartInfo) error { - if len(collectorSmartData.AtaSctTemperatureHistory.Table) > 0 { + uptimeSeconds, err := getUptimeSeconds() + if len(collectorSmartData.AtaSctTemperatureHistory.Table) > 0 && err == nil { //If cannot get uptime, fallback to default behavior (don't parse history) for ndx, temp := range collectorSmartData.AtaSctTemperatureHistory.Table { //temp value may be null, we must skip/ignore them. See #393 @@ -22,9 +24,21 @@ func (sr *scrutinyRepository) SaveSmartTemperature(ctx context.Context, wwn stri continue } - minutesOffset := collectorSmartData.AtaSctTemperatureHistory.LoggingIntervalMinutes * int64(ndx) * 60 + index := collectorSmartData.AtaSctTemperatureHistory.Index + size := collectorSmartData.AtaSctTemperatureHistory.Size + dt := collectorSmartData.AtaSctTemperatureHistory.LoggingIntervalMinutes + var minutesOffset int64 + if ndx <= index { + minutesOffset = dt * int64(index-ndx) + } else { + minutesOffset = dt * int64(size+index-ndx) + } + + if (minutesOffset * 60 > uptimeSeconds) { + continue // skip values before boot + } smartTemp := measurements.SmartTemperature{ - Date: time.Unix(collectorSmartData.LocalTime.TimeT-minutesOffset, 0), + Date: time.Unix(collectorSmartData.LocalTime.TimeT-(60*minutesOffset), 0), Temp: temp, }