Fix result ordering issue
This commit is contained in:
		| @ -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 | 			SELECT service_result_id, success, errors, connected, status, dns_rcode, certificate_expiration, hostname, ip, duration, timestamp | ||||||
| 			FROM service_result | 			FROM service_result | ||||||
| 			WHERE service_id = $1 | 			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 | 			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, | 		serviceID, | ||||||
| 		pageSize, | 		pageSize, | ||||||
| 		(page-1)*pageSize, | 		(page-1)*pageSize, | ||||||
| @ -540,7 +550,9 @@ func (s *Store) getResultsByServiceID(tx *sql.Tx, serviceID int64, page, pageSiz | |||||||
| 		if len(joinedErrors) != 0 { | 		if len(joinedErrors) != 0 { | ||||||
| 			result.Errors = strings.Split(joinedErrors, arraySeparator) | 			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 | 		idResultMap[id] = result | ||||||
| 	} | 	} | ||||||
| 	_ = rows.Close() | 	_ = rows.Close() | ||||||
|  | |||||||
| @ -242,7 +242,7 @@ func TestStore_GetServiceStatus(t *testing.T) { | |||||||
| 		t.Fatalf("serviceStatus.Results should've had 2 entries") | 		t.Fatalf("serviceStatus.Results should've had 2 entries") | ||||||
| 	} | 	} | ||||||
| 	if serviceStatus.Results[0].Timestamp.After(serviceStatus.Results[1].Timestamp) { | 	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 { | 	if serviceStatus.Uptime.LastHour != 0.5 { | ||||||
| 		t.Errorf("serviceStatus.Uptime.LastHour should've been 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) { | func TestStore_GetServiceStatusByKey(t *testing.T) { | ||||||
| 	store, _ := NewStore("sqlite", t.TempDir()+"/TestStore_GetServiceStatusByKey.db") | 	store, _ := NewStore("sqlite", t.TempDir()+"/TestStore_GetServiceStatusByKey.db") | ||||||
| 	defer store.Close() | 	defer store.Close() | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user