From 431bd2448e9654c7edcec3534bbeff19e10c0235 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 3 Jan 2024 09:43:03 +0545 Subject: [PATCH] feat: push_queue_summary & event_queue_summary --- tests/event_queue_test.go | 55 +++++++++++++++++++++++++++++++++++++++ views/007_events.sql | 25 ++++++++++++++---- 2 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 tests/event_queue_test.go diff --git a/tests/event_queue_test.go b/tests/event_queue_test.go new file mode 100644 index 00000000..509f7b42 --- /dev/null +++ b/tests/event_queue_test.go @@ -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)) + }) +}) diff --git a/views/007_events.sql b/views/007_events.sql index 2d0b1783..a678a727 100644 --- a/views/007_events.sql +++ b/views/007_events.sql @@ -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,