Skip to content

Commit

Permalink
feat: push_queue_summary & event_queue_summary
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 3, 2024
1 parent fff4f4d commit 431bd24
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
55 changes: 55 additions & 0 deletions tests/event_queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package tests

import (
"time"

"github.com/flanksource/commons/logger"
ginkgo "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

type eventQueueSummary struct {
Name string `json:"name"`
Pending int64 `json:"pending"`
Failed int64 `json:"failed"`
AvgAttempts int64 `json:"average_attempts"`
FirstFailure *time.Time `json:"first_failure,omitempty"`
LastFailure *time.Time `json:"last_failure,omitempty"`
MostCommonErr string `json:"most_common_error,omitempty"`
}

func (t *eventQueueSummary) TableName() string {
return "event_queue_summary"
}

type pushQueueSummary struct {
Table string `json:"table"`
Pending int64 `json:"pending"`
Failed int64 `json:"failed"`
AvgAttempts int64 `json:"average_attempts"`
FirstFailure *time.Time `json:"first_failure,omitempty"`
LastFailure *time.Time `json:"last_failure,omitempty"`
MostCommonErr string `json:"most_common_error,omitempty"`
}

func (t *pushQueueSummary) TableName() string {
return "push_queue_summary"
}

var _ = ginkgo.Describe("Event queue views", func() {
ginkgo.It("should query event queue views", func() {
var summaries []eventQueueSummary
err := DefaultContext.DB().Find(&summaries).Error
Expect(err).ToNot(HaveOccurred())

logger.Infof("eventQueueSummary (%d)", len(summaries))
})

ginkgo.It("should return deleted checks", func() {
var summaries []pushQueueSummary
err := DefaultContext.DB().Find(&summaries).Error
Expect(err).ToNot(HaveOccurred())

logger.Infof("pushQueueSummary (%d)", len(summaries))
})
})
25 changes: 20 additions & 5 deletions views/007_events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,36 @@ AFTER UPDATE ON components
FOR EACH ROW
EXECUTE PROCEDURE insert_component_status_updates_in_event_queue();

CREATE OR REPLACE VIEW failed_push_queue AS
CREATE OR REPLACE VIEW push_queue_summary AS
SELECT
properties ->> 'table' AS "table",
COUNT(id) AS error_count,
COUNT(id) AS pending,
COUNT(CASE WHEN error IS NOT NULL THEN 1 END) AS failed,
ROUND(AVG(attempts)::numeric, 2) AS average_attempts,
MIN(created_at) AS first_failure,
MAX(last_attempt) AS latest_failure,
MIN(CASE WHEN error IS NOT NULL THEN created_at END) AS first_failure,
MAX(last_attempt) AS last_failure,
mode() WITHIN GROUP (ORDER BY error) AS most_common_error
FROM
event_queue
WHERE
error IS NOT NULL AND attempts > 0
name = 'push_queue.create'
GROUP BY
"table";

CREATE OR REPLACE VIEW event_queue_summary AS
SELECT
name,
COUNT(id) AS pending,
COUNT(CASE WHEN error IS NOT NULL THEN 1 END) AS failed,
ROUND(AVG(attempts)::numeric, 2) AS average_attempts,
MIN(CASE WHEN error IS NOT NULL THEN created_at END) AS first_failure,
MAX(last_attempt) AS last_failure,
mode() WITHIN GROUP (ORDER BY error) AS most_common_error
FROM
event_queue
GROUP BY
name;

CREATE OR REPLACE VIEW failed_events AS
SELECT
name,
Expand Down

0 comments on commit 431bd24

Please sign in to comment.