2020-06-25 06:01:13 +00:00
|
|
|
package repository
|
2020-04-14 16:20:20 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-28 07:42:21 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-04-14 16:20:20 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
view_model "github.com/zitadel/zitadel/internal/view/model"
|
2020-04-14 16:20:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FailedEvent struct {
|
2020-05-11 10:16:29 +00:00
|
|
|
ViewName string `gorm:"column:view_name;primary_key"`
|
2020-06-11 12:59:57 +00:00
|
|
|
FailedSequence uint64 `gorm:"column:failed_sequence;primary_key"`
|
|
|
|
FailureCount uint64 `gorm:"column:failure_count"`
|
|
|
|
ErrMsg string `gorm:"column:err_msg"`
|
2022-04-19 06:26:12 +00:00
|
|
|
InstanceID string `gorm:"column:instance_id"`
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FailedEventSearchQuery struct {
|
|
|
|
Key FailedEventSearchKey
|
2021-03-01 07:48:50 +00:00
|
|
|
Method domain.SearchMethod
|
2020-04-14 16:20:20 +00:00
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req FailedEventSearchQuery) GetKey() ColumnKey {
|
|
|
|
return failedEventSearchKey(req.Key)
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:48:50 +00:00
|
|
|
func (req FailedEventSearchQuery) GetMethod() domain.SearchMethod {
|
2020-04-14 16:20:20 +00:00
|
|
|
return req.Method
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req FailedEventSearchQuery) GetValue() interface{} {
|
|
|
|
return req.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
type FailedEventSearchKey int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
FailedEventKeyUndefined FailedEventSearchKey = iota
|
|
|
|
FailedEventKeyViewName
|
|
|
|
FailedEventKeyFailedSequence
|
2022-04-19 06:26:12 +00:00
|
|
|
FailedEventKeyInstanceID
|
2020-04-14 16:20:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type failedEventSearchKey FailedEventSearchKey
|
|
|
|
|
|
|
|
func (key failedEventSearchKey) ToColumnName() string {
|
|
|
|
switch FailedEventSearchKey(key) {
|
2020-06-23 12:47:47 +00:00
|
|
|
case FailedEventKeyViewName:
|
2020-04-14 16:20:20 +00:00
|
|
|
return "view_name"
|
2020-06-23 12:47:47 +00:00
|
|
|
case FailedEventKeyFailedSequence:
|
2020-04-14 16:20:20 +00:00
|
|
|
return "failed_sequence"
|
2022-04-19 06:26:12 +00:00
|
|
|
case FailedEventKeyInstanceID:
|
|
|
|
return "instance_id"
|
2020-04-14 16:20:20 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 16:20:20 +00:00
|
|
|
func SaveFailedEvent(db *gorm.DB, table string, failedEvent *FailedEvent) error {
|
|
|
|
save := PrepareSave(table)
|
|
|
|
err := save(db, failedEvent)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-12-02 07:50:59 +00:00
|
|
|
return errors.ThrowInternal(err, "VIEW-4F8us", "unable to updated failed events")
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
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},
|
2022-04-19 06:26:12 +00:00
|
|
|
Key{Key: failedEventSearchKey(FailedEventKeyInstanceID), Value: failedEvent.InstanceID},
|
2020-06-25 06:01:13 +00:00
|
|
|
)
|
|
|
|
return delete(db)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func LatestFailedEvent(db *gorm.DB, table, viewName, instanceID string, sequence uint64) (*FailedEvent, error) {
|
2020-04-14 16:20:20 +00:00
|
|
|
failedEvent := new(FailedEvent)
|
|
|
|
queries := []SearchQuery{
|
2021-03-01 07:48:50 +00:00
|
|
|
FailedEventSearchQuery{Key: FailedEventKeyViewName, Method: domain.SearchMethodEqualsIgnoreCase, Value: viewName},
|
|
|
|
FailedEventSearchQuery{Key: FailedEventKeyFailedSequence, Method: domain.SearchMethodEquals, Value: sequence},
|
2022-04-19 06:26:12 +00:00
|
|
|
FailedEventSearchQuery{Key: FailedEventKeyInstanceID, Method: domain.SearchMethodEquals, Value: instanceID},
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
query := PrepareGetByQuery(table, queries...)
|
2020-05-11 10:16:29 +00:00
|
|
|
err := query(db, failedEvent)
|
2020-04-14 16:20:20 +00:00
|
|
|
|
2020-05-20 12:28:08 +00:00
|
|
|
if err == nil && failedEvent.ViewName != "" {
|
2020-04-14 16:20:20 +00:00
|
|
|
return failedEvent, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
if errors.IsNotFound(err) {
|
2020-06-11 11:27:25 +00:00
|
|
|
return &FailedEvent{
|
|
|
|
ViewName: viewName,
|
|
|
|
FailedSequence: sequence,
|
|
|
|
FailureCount: 0,
|
|
|
|
}, nil
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
return nil, errors.ThrowInternalf(err, "VIEW-9LyCB", "unable to get failed events of %s", viewName)
|
|
|
|
|
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|