mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 03:57:32 +00:00
feat: save last occurrence of failed events and fix instance filtering (#4710)
* fix: filter failed events and current sequence correctly * fix failed events sorting column * feat: save last occurrence of failed event * fix failedEvents query and update sql statements * change sql statement to only create index * fix linting * fix linting * Update internal/query/failed_events.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * update job name on test-docs to match the one from test-code Co-authored-by: Silvan <silvan.reusser@gmail.com>
This commit is contained in:
@@ -42,6 +42,10 @@ type CurrentSequencesSearchQueries struct {
|
||||
Queries []SearchQuery
|
||||
}
|
||||
|
||||
func NewCurrentSequencesInstanceIDSearchQuery(instanceID string) (SearchQuery, error) {
|
||||
return NewTextQuery(CurrentSequenceColInstanceID, instanceID, TextEquals)
|
||||
}
|
||||
|
||||
func (q *CurrentSequencesSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
||||
query = q.SearchRequest.toQuery(query)
|
||||
for _, q := range q.Queries {
|
||||
|
@@ -3,7 +3,7 @@ package query
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
errs "errors"
|
||||
"time"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
|
||||
@@ -15,6 +15,7 @@ const (
|
||||
failedEventsColumnProjectionName = "projection_name"
|
||||
failedEventsColumnFailedSequence = "failed_sequence"
|
||||
failedEventsColumnFailureCount = "failure_count"
|
||||
failedEventsColumnLastFailed = "last_failed"
|
||||
failedEventsColumnError = "error"
|
||||
failedEventsColumnInstanceID = "instance_id"
|
||||
)
|
||||
@@ -36,10 +37,18 @@ var (
|
||||
name: failedEventsColumnFailureCount,
|
||||
table: failedEventsTable,
|
||||
}
|
||||
FailedEventsColumnLastFailed = Column{
|
||||
name: failedEventsColumnLastFailed,
|
||||
table: failedEventsTable,
|
||||
}
|
||||
FailedEventsColumnError = Column{
|
||||
name: failedEventsColumnError,
|
||||
table: failedEventsTable,
|
||||
}
|
||||
FailedEventsColumnInstanceID = Column{
|
||||
name: failedEventsColumnInstanceID,
|
||||
table: failedEventsTable,
|
||||
}
|
||||
)
|
||||
|
||||
type FailedEvents struct {
|
||||
@@ -52,6 +61,7 @@ type FailedEvent struct {
|
||||
FailedSequence uint64
|
||||
FailureCount uint64
|
||||
Error string
|
||||
LastFailed time.Time
|
||||
}
|
||||
|
||||
type FailedEventSearchQueries struct {
|
||||
@@ -73,26 +83,27 @@ func (q *Queries) SearchFailedEvents(ctx context.Context, queries *FailedEventSe
|
||||
return scan(rows)
|
||||
}
|
||||
|
||||
func (q *Queries) RemoveFailedEvent(ctx context.Context, projectionName string, sequence uint64) (err error) {
|
||||
func (q *Queries) RemoveFailedEvent(ctx context.Context, projectionName, instanceID string, sequence uint64) (err error) {
|
||||
stmt, args, err := sq.Delete(projection.FailedEventsTable).
|
||||
Where(sq.Eq{
|
||||
failedEventsColumnProjectionName: projectionName,
|
||||
failedEventsColumnFailedSequence: sequence,
|
||||
failedEventsColumnInstanceID: instanceID,
|
||||
}).
|
||||
PlaceholderFormat(sq.Dollar).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return errors.ThrowInternal(err, "QUERY-DGgh3", "Errors.RemoveFailed")
|
||||
}
|
||||
_, err = q.client.Exec(stmt, args...)
|
||||
_, err = q.client.ExecContext(ctx, stmt, args...)
|
||||
if err != nil {
|
||||
return errors.ThrowInternal(err, "QUERY-0kbFF", "Errors.RemoveFailed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewFailedEventProjectionNameSearchQuery(method TextComparison, value string) (SearchQuery, error) {
|
||||
return NewTextQuery(FailedEventsColumnProjectionName, value, method)
|
||||
func NewFailedEventInstanceIDSearchQuery(instanceID string) (SearchQuery, error) {
|
||||
return NewTextQuery(FailedEventsColumnInstanceID, instanceID, TextEquals)
|
||||
}
|
||||
|
||||
func (r *ProjectSearchQueries) AppendProjectionNameQuery(projectionName string) error {
|
||||
@@ -112,36 +123,12 @@ func (q *FailedEventSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuil
|
||||
return query
|
||||
}
|
||||
|
||||
func prepareFailedEventQuery(instanceIDs ...string) (sq.SelectBuilder, func(*sql.Row) (*FailedEvent, error)) {
|
||||
return sq.Select(
|
||||
FailedEventsColumnProjectionName.identifier(),
|
||||
FailedEventsColumnFailedSequence.identifier(),
|
||||
FailedEventsColumnFailureCount.identifier(),
|
||||
FailedEventsColumnError.identifier()).
|
||||
From(failedEventsTable.identifier()).PlaceholderFormat(sq.Dollar),
|
||||
func(row *sql.Row) (*FailedEvent, error) {
|
||||
p := new(FailedEvent)
|
||||
err := row.Scan(
|
||||
&p.ProjectionName,
|
||||
&p.FailedSequence,
|
||||
&p.FailureCount,
|
||||
&p.Error,
|
||||
)
|
||||
if err != nil {
|
||||
if errs.Is(err, sql.ErrNoRows) {
|
||||
return nil, errors.ThrowNotFound(err, "QUERY-5N00f", "Errors.FailedEvents.NotFound")
|
||||
}
|
||||
return nil, errors.ThrowInternal(err, "QUERY-0oJf3", "Errors.Internal")
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
func prepareFailedEventsQuery() (sq.SelectBuilder, func(*sql.Rows) (*FailedEvents, error)) {
|
||||
return sq.Select(
|
||||
FailedEventsColumnProjectionName.identifier(),
|
||||
FailedEventsColumnFailedSequence.identifier(),
|
||||
FailedEventsColumnFailureCount.identifier(),
|
||||
FailedEventsColumnLastFailed.identifier(),
|
||||
FailedEventsColumnError.identifier(),
|
||||
countColumn.identifier()).
|
||||
From(failedEventsTable.identifier()).PlaceholderFormat(sq.Dollar),
|
||||
@@ -150,16 +137,19 @@ func prepareFailedEventsQuery() (sq.SelectBuilder, func(*sql.Rows) (*FailedEvent
|
||||
var count uint64
|
||||
for rows.Next() {
|
||||
failedEvent := new(FailedEvent)
|
||||
var lastFailed sql.NullTime
|
||||
err := rows.Scan(
|
||||
&failedEvent.ProjectionName,
|
||||
&failedEvent.FailedSequence,
|
||||
&failedEvent.FailureCount,
|
||||
&lastFailed,
|
||||
&failedEvent.Error,
|
||||
&count,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
failedEvent.LastFailed = lastFailed.Time
|
||||
failedEvents = append(failedEvents, failedEvent)
|
||||
}
|
||||
|
||||
|
@@ -28,6 +28,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
|
||||
` projections.failed_events.failed_sequence,`+
|
||||
` projections.failed_events.failure_count,`+
|
||||
` projections.failed_events.last_failed,`+
|
||||
` projections.failed_events.error,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.failed_events`),
|
||||
@@ -45,6 +46,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
|
||||
` projections.failed_events.failed_sequence,`+
|
||||
` projections.failed_events.failure_count,`+
|
||||
` projections.failed_events.last_failed,`+
|
||||
` projections.failed_events.error,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.failed_events`),
|
||||
@@ -52,6 +54,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
"projection_name",
|
||||
"failed_sequence",
|
||||
"failure_count",
|
||||
"last_failed",
|
||||
"error",
|
||||
"count",
|
||||
},
|
||||
@@ -60,6 +63,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
"projection-name",
|
||||
uint64(20211108),
|
||||
uint64(2),
|
||||
testNow,
|
||||
"error",
|
||||
},
|
||||
},
|
||||
@@ -74,6 +78,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
ProjectionName: "projection-name",
|
||||
FailedSequence: 20211108,
|
||||
FailureCount: 2,
|
||||
LastFailed: testNow,
|
||||
Error: "error",
|
||||
},
|
||||
},
|
||||
@@ -87,6 +92,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
|
||||
` projections.failed_events.failed_sequence,`+
|
||||
` projections.failed_events.failure_count,`+
|
||||
` projections.failed_events.last_failed,`+
|
||||
` projections.failed_events.error,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.failed_events`),
|
||||
@@ -94,6 +100,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
"projection_name",
|
||||
"failed_sequence",
|
||||
"failure_count",
|
||||
"last_failed",
|
||||
"error",
|
||||
"count",
|
||||
},
|
||||
@@ -102,12 +109,14 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
"projection-name",
|
||||
uint64(20211108),
|
||||
2,
|
||||
testNow,
|
||||
"error",
|
||||
},
|
||||
{
|
||||
"projection-name-2",
|
||||
uint64(20211108),
|
||||
2,
|
||||
nil,
|
||||
"error",
|
||||
},
|
||||
},
|
||||
@@ -122,6 +131,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
ProjectionName: "projection-name",
|
||||
FailedSequence: 20211108,
|
||||
FailureCount: 2,
|
||||
LastFailed: testNow,
|
||||
Error: "error",
|
||||
},
|
||||
{
|
||||
@@ -141,6 +151,7 @@ func Test_FailedEventsPrepares(t *testing.T) {
|
||||
regexp.QuoteMeta(`SELECT projections.failed_events.projection_name,`+
|
||||
` projections.failed_events.failed_sequence,`+
|
||||
` projections.failed_events.failure_count,`+
|
||||
` projections.failed_events.last_failed,`+
|
||||
` projections.failed_events.error,`+
|
||||
` COUNT(*) OVER ()`+
|
||||
` FROM projections.failed_events`),
|
||||
|
Reference in New Issue
Block a user