fix(coderd): use stable sorting for insights and improve test coverage (#9250)

Fixes #9213
This commit is contained in:
Mathias Fredriksson
2023-08-24 13:36:40 +03:00
committed by GitHub
parent 970072f61d
commit 6b69abfec7
19 changed files with 1978 additions and 383 deletions
+12 -11
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
@@ -61,18 +62,18 @@ type UserLatencyInsightsRequest struct {
}
func (c *Client) UserLatencyInsights(ctx context.Context, req UserLatencyInsightsRequest) (UserLatencyInsightsResponse, error) {
var qp []string
qp = append(qp, fmt.Sprintf("start_time=%s", req.StartTime.Format(insightsTimeLayout)))
qp = append(qp, fmt.Sprintf("end_time=%s", req.EndTime.Format(insightsTimeLayout)))
qp := url.Values{}
qp.Add("start_time", req.StartTime.Format(insightsTimeLayout))
qp.Add("end_time", req.EndTime.Format(insightsTimeLayout))
if len(req.TemplateIDs) > 0 {
var templateIDs []string
for _, id := range req.TemplateIDs {
templateIDs = append(templateIDs, id.String())
}
qp = append(qp, fmt.Sprintf("template_ids=%s", strings.Join(templateIDs, ",")))
qp.Add("template_ids", strings.Join(templateIDs, ","))
}
reqURL := fmt.Sprintf("/api/v2/insights/user-latency?%s", strings.Join(qp, "&"))
reqURL := fmt.Sprintf("/api/v2/insights/user-latency?%s", qp.Encode())
resp, err := c.Request(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return UserLatencyInsightsResponse{}, xerrors.Errorf("make request: %w", err)
@@ -158,21 +159,21 @@ type TemplateInsightsRequest struct {
}
func (c *Client) TemplateInsights(ctx context.Context, req TemplateInsightsRequest) (TemplateInsightsResponse, error) {
var qp []string
qp = append(qp, fmt.Sprintf("start_time=%s", req.StartTime.Format(insightsTimeLayout)))
qp = append(qp, fmt.Sprintf("end_time=%s", req.EndTime.Format(insightsTimeLayout)))
qp := url.Values{}
qp.Add("start_time", req.StartTime.Format(insightsTimeLayout))
qp.Add("end_time", req.EndTime.Format(insightsTimeLayout))
if len(req.TemplateIDs) > 0 {
var templateIDs []string
for _, id := range req.TemplateIDs {
templateIDs = append(templateIDs, id.String())
}
qp = append(qp, fmt.Sprintf("template_ids=%s", strings.Join(templateIDs, ",")))
qp.Add("template_ids", strings.Join(templateIDs, ","))
}
if req.Interval != "" {
qp = append(qp, fmt.Sprintf("interval=%s", req.Interval))
qp.Add("interval", string(req.Interval))
}
reqURL := fmt.Sprintf("/api/v2/insights/templates?%s", strings.Join(qp, "&"))
reqURL := fmt.Sprintf("/api/v2/insights/templates?%s", qp.Encode())
resp, err := c.Request(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return TemplateInsightsResponse{}, xerrors.Errorf("make request: %w", err)