Skip to content

Commit

Permalink
Fix #494
Browse files Browse the repository at this point in the history
(not tested yet)
  • Loading branch information
mcarbonne committed Jan 2, 2024
1 parent 5bbd4c3 commit bca8876
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
12 changes: 11 additions & 1 deletion webapp/backend/pkg/database/helpers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
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) {
sort.SliceStable(smartResults, func(i, j int) bool {
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
}
24 changes: 19 additions & 5 deletions webapp/backend/pkg/database/scrutiny_repository_temperature.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,42 @@ 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
if temp == 0 {
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,
}

Expand Down

0 comments on commit bca8876

Please sign in to comment.