Skip to content

Commit

Permalink
feat: add canary namespace to check summary view and refactor check
Browse files Browse the repository at this point in the history
summary query
  • Loading branch information
adityathebe authored and moshloop committed Nov 9, 2023
1 parent 925f510 commit 866ab84
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion check_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CheckSummary(ctx DBContext, checkID string) (*models.CheckSummary, error) {
}

// deprecated use query.CheckSummary
func QueryCheckSummary(ctx gocontext.Context, dbpool *pgxpool.Pool, opts ...query.CheckSummaryOptions) (models.Checks, error) {
func QueryCheckSummary(ctx gocontext.Context, dbpool *pgxpool.Pool, opts ...query.CheckSummaryOptions) ([]models.CheckSummary, error) {
return query.CheckSummary(context.NewContext(ctx).WithDB(nil, dbpool), opts...)
}

Expand Down
21 changes: 11 additions & 10 deletions models/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,20 @@ func (CheckStatusAggregate1d) TableName() string {
type CheckSummary struct {
ID uuid.UUID `json:"id"`
CanaryID uuid.UUID `json:"canary_id"`
Uptime types.Uptime `json:"uptime"`
Latency types.Latency `json:"latency"`
CanaryName string `json:"canary_name"`
CanaryNamespace string `json:"canary_namespace"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
Labels types.JSONStringMap `json:"labels"`
LastTransitionTime *time.Time `json:"last_transition_time,omitempty"`
Type string `json:"type"`
Icon string `json:"icon"`
Latency types.Latency `json:"latency,omitempty"`
Name string `json:"name"`
Status string `json:"status"`
Description string `json:"description"`
Namespace string `json:"namespace"`
CanaryName string `json:"canary_name"`
Labels types.JSONStringMap `json:"labels"`
Severity string `json:"severity"`
Owner string `json:"owner"`
Owner string `json:"owner,omitempty"`
Severity string `json:"severity,omitempty"`
Status string `json:"status"`
Type string `json:"type"`
Uptime types.Uptime `json:"uptime,omitempty"`
LastRuntime *time.Time `json:"last_runtime,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Expand Down
39 changes: 14 additions & 25 deletions query/check_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/flanksource/duty/models"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"golang.org/x/exp/slices"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -51,7 +50,7 @@ func CheckSummaryByID(ctx context.Context, checkID string) (*models.CheckSummary
return &checkSummary, nil
}

func CheckSummary(ctx context.Context, opts ...CheckSummaryOptions) (models.Checks, error) {
func CheckSummary(ctx context.Context, opts ...CheckSummaryOptions) ([]models.CheckSummary, error) {
opt := CheckSummaryOptions{
Timeout: DefaultQueryTimeout,
}
Expand All @@ -71,7 +70,15 @@ func CheckSummary(ctx context.Context, opts ...CheckSummaryOptions) (models.Chec
defer cancel()
}

query := `SELECT json_agg(result) FROM check_summary AS result WHERE deleted_at is null`
selectField := "result"
switch opt.SortBy {
case CheckSummarySortByName:
selectField += " ORDER BY name"
case "-" + CheckSummarySortByName:
selectField += " ORDER BY name DESC"
}

query := fmt.Sprintf(`SELECT json_agg(%s) FROM check_summary AS result WHERE deleted_at is null`, selectField)

var args = pgx.NamedArgs{}
if opt.DeleteFrom != nil {
Expand All @@ -89,36 +96,18 @@ func CheckSummary(ctx context.Context, opts ...CheckSummaryOptions) (models.Chec
}
defer rows.Close()

var results models.Checks
var results []models.CheckSummary
for rows.Next() {
var checks models.Checks
var summmaries []models.CheckSummary
if rows.RawValues()[0] == nil {
continue
}

if err := json.Unmarshal(rows.RawValues()[0], &checks); err != nil {
if err := json.Unmarshal(rows.RawValues()[0], &summmaries); err != nil {
return nil, fmt.Errorf("failed to unmarshal components:%v for %s", err, rows.RawValues()[0])
}
results = append(results, checks...)
}

if len(opts) > 0 && opts[0].SortBy != "" {
slice := []*models.Check(results)
slices.SortFunc(slice, func(a, b *models.Check) int {
var _a, _b string
if opts[0].SortBy == CheckSummarySortByName {
_a = a.Name
_b = b.Name
}
if _a > _b {
return 1
}
if _a == _b {
return 0
}
return -1
})
return models.Checks(slice), nil
results = append(results, summmaries...)
}

return results, nil
Expand Down
3 changes: 1 addition & 2 deletions schema/checks.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ table "checks" {
type = text
}
column "namespace" {
null = false
null = true
type = text
default = "" # Only for backward compatibility
}
column "description" {
null = true
Expand Down
1 change: 1 addition & 0 deletions tests/check_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var _ = ginkgo.Describe("Check summary", ginkgo.Ordered, func() {

matcher.MatchFixture("fixtures/expectations/check_status_summary.json", result, `del(.[].uptime.last_pass) | del(.[].uptime.last_fail) | del(.[].created_at) | del(.[].updated_at) | del(.[].agent_id)`)
})

ginkgo.It("should return deleted checks", func() {
err := query.RefreshCheckStatusSummary(testutils.DefaultContext)
Expect(err).ToNot(HaveOccurred())
Expand Down
12 changes: 8 additions & 4 deletions tests/fixtures/expectations/check_status_summary.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"rolling1h": 0
},
"name": "cart-api-health-check",
"namespace": "cart",
"namespace": "",
"canary_namespace": "cart",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-01T21:47:47.647714+02:00",
Expand All @@ -34,7 +35,8 @@
"rolling1h": 0
},
"name": "logistics-api-health-check",
"namespace": "logistics",
"namespace": "",
"canary_namespace": "logistics",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-01T21:47:47.646435+02:00",
Expand All @@ -60,7 +62,8 @@
"rolling1h": 0
},
"name": "logistics-api-home-check",
"namespace": "logistics",
"namespace": "",
"canary_namespace": "logistics",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-01T21:47:47.64714+02:00",
Expand All @@ -85,7 +88,8 @@
"rolling1h": 0
},
"name": "logistics-db-check",
"namespace": "logistics",
"namespace": "",
"canary_namespace": "logistics",
"status": "unhealthy",
"type": "postgres",
"updated_at": "2023-11-01T21:47:47.647428+02:00",
Expand Down
18 changes: 12 additions & 6 deletions tests/fixtures/expectations/check_status_summary_deleted.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"rolling1h": 0
},
"name": "cart-api-health-check",
"namespace": "cart",
"namespace": "",
"canary_namespace": "cart",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-02T01:39:54.624837+02:00",
Expand All @@ -35,7 +36,8 @@
"rolling1h": 0
},
"name": "cart-deleted",
"namespace": "cart",
"namespace": "",
"canary_namespace": "cart",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-02T01:39:54.622771+02:00",
Expand All @@ -61,7 +63,8 @@
"rolling1h": 0
},
"name": "cart-deleted-old",
"namespace": "cart",
"namespace": "",
"canary_namespace": "cart",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-02T01:39:54.623593+02:00",
Expand All @@ -86,7 +89,8 @@
"rolling1h": 0
},
"name": "logistics-api-health-check",
"namespace": "logistics",
"namespace": "",
"canary_namespace": "logistics",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-02T01:39:54.62394+02:00",
Expand All @@ -112,7 +116,8 @@
"rolling1h": 0
},
"name": "logistics-api-home-check",
"namespace": "logistics",
"namespace": "",
"canary_namespace": "logistics",
"status": "healthy",
"type": "http",
"updated_at": "2023-11-02T01:39:54.624241+02:00",
Expand All @@ -137,7 +142,8 @@
"rolling1h": 0
},
"name": "logistics-db-check",
"namespace": "logistics",
"namespace": "",
"canary_namespace": "logistics",
"status": "unhealthy",
"type": "postgres",
"updated_at": "2023-11-02T01:39:54.624539+02:00",
Expand Down
3 changes: 2 additions & 1 deletion views/013_check_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ CREATE OR REPLACE VIEW check_summary AS
checks.name,
checks.status,
checks.description,
canaries.namespace,
checks.namespace,
canaries.namespace as canary_namespace,
canaries.name as canary_name,
canaries.labels || checks.labels as labels,
checks.severity,
Expand Down

0 comments on commit 866ab84

Please sign in to comment.