Skip to content

Commit

Permalink
chore: make event queue related structs public
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jan 4, 2024
1 parent aa19bf6 commit 8b0b5df
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 32 deletions.
78 changes: 78 additions & 0 deletions models/event_queue.go
Original file line number Diff line number Diff line change
@@ -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"
}
35 changes: 3 additions & 32 deletions tests/event_queue_test.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,23 @@
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())

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

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

Expand Down

0 comments on commit 8b0b5df

Please sign in to comment.