diff --git a/webapp/backend/pkg/database/helpers.go b/webapp/backend/pkg/database/helpers.go new file mode 100644 index 0000000..3706d86 --- /dev/null +++ b/webapp/backend/pkg/database/helpers.go @@ -0,0 +1,12 @@ +package database + +import ( + "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements" + "sort" +) + +func sortSmartMeasurementsDesc(smartResults []measurements.Smart) { + sort.SliceStable(smartResults, func(i, j int) bool { + return smartResults[i].Date.After(smartResults[j].Date) + }) +} diff --git a/webapp/backend/pkg/database/helpers_test.go b/webapp/backend/pkg/database/helpers_test.go new file mode 100644 index 0000000..38587b1 --- /dev/null +++ b/webapp/backend/pkg/database/helpers_test.go @@ -0,0 +1,30 @@ +package database + +import ( + "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +func Test_sortSmartMeasurementsDesc_LatestFirst(t *testing.T) { + //setup + timeNow := time.Now() + smartResults := []measurements.Smart{ + { + Date: timeNow.AddDate(0, 0, -2), + }, + { + Date: timeNow, + }, + { + Date: timeNow.AddDate(0, 0, -1), + }, + } + + //test + sortSmartMeasurementsDesc(smartResults) + + //assert + require.Equal(t, smartResults[0].Date, timeNow) +} diff --git a/webapp/backend/pkg/database/scrutiny_repository_device_smart_attributes.go b/webapp/backend/pkg/database/scrutiny_repository_device_smart_attributes.go index 18960cb..96015bb 100644 --- a/webapp/backend/pkg/database/scrutiny_repository_device_smart_attributes.go +++ b/webapp/backend/pkg/database/scrutiny_repository_device_smart_attributes.go @@ -29,6 +29,7 @@ func (sr *scrutinyRepository) SaveSmartAttributes(ctx context.Context, wwn strin return deviceSmartData, sr.saveDatapoint(sr.influxWriteApi, "smart", tags, fields, deviceSmartData.Date, ctx) } +// GetSmartAttributeHistory MUST return in sorted order, where newest entries are at the beginning of the list, and oldest are at the end. func (sr *scrutinyRepository) GetSmartAttributeHistory(ctx context.Context, wwn string, durationKey string, attributes []string) ([]measurements.Smart, error) { // Get SMartResults from InfluxDB @@ -64,6 +65,9 @@ func (sr *scrutinyRepository) GetSmartAttributeHistory(ctx context.Context, wwn return nil, err } + //we have to sort the smartResults again, because the `union` command will return multiple 'tables' and only sort the records in each table. + sortSmartMeasurementsDesc(smartResults) + return smartResults, nil //if err := device.SquashHistory(); err != nil { diff --git a/webapp/frontend/src/app/modules/detail/detail.component.ts b/webapp/frontend/src/app/modules/detail/detail.component.ts index 805ae1c..91e2539 100644 --- a/webapp/frontend/src/app/modules/detail/detail.component.ts +++ b/webapp/frontend/src/app/modules/detail/detail.component.ts @@ -291,7 +291,7 @@ export class DetailComponent implements OnInit, AfterViewInit, OnDestroy { if(smart_results.length == 0){ return smartAttributeDataSource } - var latest_smart_result = smart_results[smart_results.length -1]; + var latest_smart_result = smart_results[0]; let attributes = {} if(this.isScsi()) { this.smartAttributeTableColumns = ['status', 'name', 'value', 'thresh', 'history'];