/
failed_events.go
132 lines (111 loc) · 3.6 KB
/
failed_events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package repository
import (
"github.com/caos/zitadel/internal/domain"
"strings"
"github.com/caos/zitadel/internal/errors"
view_model "github.com/caos/zitadel/internal/view/model"
"github.com/jinzhu/gorm"
)
const (
errViewNameKey = "view_name"
errFailedSeqKey = "failed_sequence"
)
type FailedEvent struct {
ViewName string `gorm:"column:view_name;primary_key"`
FailedSequence uint64 `gorm:"column:failed_sequence;primary_key"`
FailureCount uint64 `gorm:"column:failure_count"`
ErrMsg string `gorm:"column:err_msg"`
}
type FailedEventSearchQuery struct {
Key FailedEventSearchKey
Method domain.SearchMethod
Value interface{}
}
func (req FailedEventSearchQuery) GetKey() ColumnKey {
return failedEventSearchKey(req.Key)
}
func (req FailedEventSearchQuery) GetMethod() domain.SearchMethod {
return req.Method
}
func (req FailedEventSearchQuery) GetValue() interface{} {
return req.Value
}
type FailedEventSearchKey int32
const (
FailedEventKeyUndefined FailedEventSearchKey = iota
FailedEventKeyViewName
FailedEventKeyFailedSequence
)
type failedEventSearchKey FailedEventSearchKey
func (key failedEventSearchKey) ToColumnName() string {
switch FailedEventSearchKey(key) {
case FailedEventKeyViewName:
return "view_name"
case FailedEventKeyFailedSequence:
return "failed_sequence"
default:
return ""
}
}
func FailedEventFromModel(failedEvent *view_model.FailedEvent) *FailedEvent {
return &FailedEvent{
ViewName: failedEvent.Database + "." + failedEvent.ViewName,
FailureCount: failedEvent.FailureCount,
FailedSequence: failedEvent.FailedSequence,
ErrMsg: failedEvent.ErrMsg,
}
}
func FailedEventToModel(failedEvent *FailedEvent) *view_model.FailedEvent {
dbView := strings.Split(failedEvent.ViewName, ".")
return &view_model.FailedEvent{
Database: dbView[0],
ViewName: dbView[1],
FailureCount: failedEvent.FailureCount,
FailedSequence: failedEvent.FailedSequence,
ErrMsg: failedEvent.ErrMsg,
}
}
func SaveFailedEvent(db *gorm.DB, table string, failedEvent *FailedEvent) error {
save := PrepareSave(table)
err := save(db, failedEvent)
if err != nil {
return errors.ThrowInternal(err, "VIEW-4F8us", "unable to updated failed events")
}
return nil
}
func RemoveFailedEvent(db *gorm.DB, table string, failedEvent *FailedEvent) error {
delete := PrepareDeleteByKeys(table,
Key{Key: failedEventSearchKey(FailedEventKeyViewName), Value: failedEvent.ViewName},
Key{Key: failedEventSearchKey(FailedEventKeyFailedSequence), Value: failedEvent.FailedSequence},
)
return delete(db)
}
func LatestFailedEvent(db *gorm.DB, table, viewName string, sequence uint64) (*FailedEvent, error) {
failedEvent := new(FailedEvent)
queries := []SearchQuery{
FailedEventSearchQuery{Key: FailedEventKeyViewName, Method: domain.SearchMethodEqualsIgnoreCase, Value: viewName},
FailedEventSearchQuery{Key: FailedEventKeyFailedSequence, Method: domain.SearchMethodEquals, Value: sequence},
}
query := PrepareGetByQuery(table, queries...)
err := query(db, failedEvent)
if err == nil && failedEvent.ViewName != "" {
return failedEvent, nil
}
if errors.IsNotFound(err) {
return &FailedEvent{
ViewName: viewName,
FailedSequence: sequence,
FailureCount: 0,
}, nil
}
return nil, errors.ThrowInternalf(err, "VIEW-9LyCB", "unable to get failed events of %s", viewName)
}
func AllFailedEvents(db *gorm.DB, table string) ([]*FailedEvent, error) {
failedEvents := make([]*FailedEvent, 0)
query := PrepareSearchQuery(table, GeneralSearchRequest{})
_, err := query(db, &failedEvents)
if err != nil {
return nil, err
}
return failedEvents, nil
}