diff --git a/storage/store/database/database.go b/storage/store/database/database.go index 02b2fce9..940c14c4 100644 --- a/storage/store/database/database.go +++ b/storage/store/database/database.go @@ -521,9 +521,19 @@ func (s *Store) getResultsByServiceID(tx *sql.Tx, serviceID int64, page, pageSiz SELECT service_result_id, success, errors, connected, status, dns_rcode, certificate_expiration, hostname, ip, duration, timestamp FROM service_result WHERE service_id = $1 - ORDER BY timestamp ASC + ORDER BY service_result_id DESC -- Normally, we'd sort by timestamp, but sorting by service_result_id is faster LIMIT $2 OFFSET $3 `, + //` + // SELECT * FROM ( + // SELECT service_result_id, success, errors, connected, status, dns_rcode, certificate_expiration, hostname, ip, duration, timestamp + // FROM service_result + // WHERE service_id = $1 + // ORDER BY service_result_id DESC -- Normally, we'd sort by timestamp, but sorting by service_result_id is faster + // LIMIT $2 OFFSET $3 + // ) + // ORDER BY service_result_id ASC -- Normally, we'd sort by timestamp, but sorting by service_result_id is faster + //`, serviceID, pageSize, (page-1)*pageSize, @@ -540,7 +550,9 @@ func (s *Store) getResultsByServiceID(tx *sql.Tx, serviceID int64, page, pageSiz if len(joinedErrors) != 0 { result.Errors = strings.Split(joinedErrors, arraySeparator) } - results = append(results, result) + //results = append(results, result) + // This is faster than using a subselect + results = append([]*core.Result{result}, results...) idResultMap[id] = result } _ = rows.Close() diff --git a/storage/store/database/database_test.go b/storage/store/database/database_test.go index 583e5cba..6b6ef69d 100644 --- a/storage/store/database/database_test.go +++ b/storage/store/database/database_test.go @@ -242,7 +242,7 @@ func TestStore_GetServiceStatus(t *testing.T) { t.Fatalf("serviceStatus.Results should've had 2 entries") } if serviceStatus.Results[0].Timestamp.After(serviceStatus.Results[1].Timestamp) { - t.Fatalf("The result at index 0 should've been older than the result at index 1") + t.Error("The result at index 0 should've been older than the result at index 1") } if serviceStatus.Uptime.LastHour != 0.5 { t.Errorf("serviceStatus.Uptime.LastHour should've been 0.5") @@ -274,6 +274,36 @@ func TestStore_GetServiceStatusForMissingStatusReturnsNil(t *testing.T) { } } +func TestStore_GetServiceStatusPage1IsHasMoreRecentResultsThanPage2(t *testing.T) { + store, _ := NewStore("sqlite", t.TempDir()+"/TestStore_GetServiceStatusPage1IsHasMoreRecentResultsThanPage2.db") + defer store.Close() + firstResult := testSuccessfulResult + firstResult.Timestamp = timestamp.Add(-time.Minute) + secondResult := testUnsuccessfulResult + secondResult.Timestamp = timestamp + store.Insert(&testService, &firstResult) + store.Insert(&testService, &secondResult) + + serviceStatusPage1 := store.GetServiceStatusByKey(testService.Key(), paging.NewServiceStatusParams().WithResults(1, 1)) + if serviceStatusPage1 == nil { + t.Fatalf("serviceStatusPage1 shouldn't have been nil") + } + if len(serviceStatusPage1.Results) != 1 { + t.Fatalf("serviceStatusPage1 should've had 1 result") + } + serviceStatusPage2 := store.GetServiceStatusByKey(testService.Key(), paging.NewServiceStatusParams().WithResults(2, 1)) + if serviceStatusPage2 == nil { + t.Fatalf("serviceStatusPage2 shouldn't have been nil") + } + if len(serviceStatusPage2.Results) != 1 { + t.Fatalf("serviceStatusPage2 should've had 1 result") + } + // Compare the timestamp of both pages + if !serviceStatusPage1.Results[0].Timestamp.After(serviceStatusPage2.Results[0].Timestamp) { + t.Errorf("The result from the first page should've been more recent than the results from the second page") + } +} + func TestStore_GetServiceStatusByKey(t *testing.T) { store, _ := NewStore("sqlite", t.TempDir()+"/TestStore_GetServiceStatusByKey.db") defer store.Close()