diff --git a/models/event_queue.go b/models/event_queue.go new file mode 100644 index 00000000..78d2af4b --- /dev/null +++ b/models/event_queue.go @@ -0,0 +1,78 @@ +package models + +import ( + "time" + + "github.com/flanksource/duty/types" + "github.com/flanksource/postq" + "github.com/google/uuid" +) + +type Event struct { + ID uuid.UUID `gorm:"default:generate_ulid()"` + Name string `json:"name"` + CreatedAt time.Time `json:"created_at"` + Properties types.JSONStringMap `json:"properties"` + Error *string `json:"error,omitempty"` + Attempts int `json:"attempts"` + LastAttempt *time.Time `json:"last_attempt"` + Priority int `json:"priority"` +} + +func (t Event) ToPostQEvent() postq.Event { + return postq.Event{ + ID: t.ID, + Name: t.Name, + Error: t.Error, + Attempts: t.Attempts, + LastAttempt: t.LastAttempt, + Properties: t.Properties, + CreatedAt: t.CreatedAt, + } +} + +// We are using the term `Event` as it represents an event in the +// event_queue table, but the table is named event_queue +// to signify it's usage as a queue +func (Event) TableName() string { + return "event_queue" +} + +type Events []Event + +func (events Events) ToPostQEvents() postq.Events { + var output []postq.Event + for _, event := range events { + output = append(output, event.ToPostQEvent()) + } + + return output +} + +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" +} diff --git a/tests/event_queue_test.go b/tests/event_queue_test.go index 509f7b42..4d43024d 100644 --- a/tests/event_queue_test.go +++ b/tests/event_queue_test.go @@ -1,44 +1,15 @@ package tests import ( - "time" - "github.com/flanksource/commons/logger" + "github.com/flanksource/duty/models" 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 + var summaries []models.EventQueueSummary err := DefaultContext.DB().Find(&summaries).Error Expect(err).ToNot(HaveOccurred()) @@ -46,7 +17,7 @@ var _ = ginkgo.Describe("Event queue views", func() { }) ginkgo.It("should return deleted checks", func() { - var summaries []pushQueueSummary + var summaries []models.PushQueueSummary err := DefaultContext.DB().Find(&summaries).Error Expect(err).ToNot(HaveOccurred())