Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: split data model object for api.Anomaly (#830)

Yilong Li6c73065272ee
diff --git a/api/anomaly.go b/api/anomaly.go
index 9543a9654f..78b67fcb39 100644
--- a/api/anomaly.go
+++ b/api/anomaly.go
@@ -87,6 +87,52 @@ type AnomalyDatabaseSchemaDriftPayload struct {
Actual string `json:"actual,omitempty"`
}
+// AnomalyRaw is the store model for an Anomaly.
+// Fields have exactly the same meanings as Anomaly.
+type AnomalyRaw struct {
+ ID int
+
+ // Standard fields
+ CreatorID int
+ CreatedTs int64
+ UpdaterID int
+ UpdatedTs int64
+
+ // Related fields
+ InstanceID int
+ DatabaseID *int
+
+ // Domain specific fields
+ Type AnomalyType
+ // Calculated field derived from type
+ Severity AnomalySeverity
+ Payload string
+}
+
+// ToAnomaly creates an instance of Anomaly based on the AnomalyRaw.
+// This is intended to be called when we need to compose an Anomaly relationship.
+func (raw *AnomalyRaw) ToAnomaly() *Anomaly {
+ return &Anomaly{
+ ID: raw.ID,
+
+ // Standard fields
+ CreatorID: raw.CreatorID,
+ CreatedTs: raw.CreatedTs,
+ UpdaterID: raw.UpdaterID,
+ UpdatedTs: raw.UpdatedTs,
+
+ // Related fields
+ InstanceID: raw.InstanceID,
+ DatabaseID: raw.DatabaseID,
+
+ // Domain specific fields
+ Type: raw.Type,
+ // Calculated field derived from type
+ Severity: raw.Severity,
+ Payload: raw.Payload,
+ }
+}
+
// Anomaly is the API message for an anomaly.
type Anomaly struct {
ID int `jsonapi:"primary,anomaly"`
@@ -156,7 +202,7 @@ type AnomalyArchive struct {
// AnomalyService is the service for anomaly.
type AnomalyService interface {
// UpsertActiveAnomaly would update the existing active anomaly if both database id and type match, otherwise create a new one.
- UpsertActiveAnomaly(ctx context.Context, upsert *AnomalyUpsert) (*Anomaly, error)
- FindAnomalyList(ctx context.Context, find *AnomalyFind) ([]*Anomaly, error)
+ UpsertActiveAnomaly(ctx context.Context, upsert *AnomalyUpsert) (*AnomalyRaw, error)
+ FindAnomalyList(ctx context.Context, find *AnomalyFind) ([]*AnomalyRaw, error)
ArchiveAnomaly(ctx context.Context, archive *AnomalyArchive) error
}
diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.go
index 308af2bb0b..90cbb7825d 100644
--- a/server/anomaly_scanner.go
+++ b/server/anomaly_scanner.go
@@ -181,13 +181,12 @@ func (s *AnomalyScanner) checkInstanceAnomaly(ctx context.Context, instance *api
zap.String("type", string(api.AnomalyInstanceConnection)),
zap.Error(err))
} else {
- _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
+ if _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
CreatorID: api.SystemBotID,
InstanceID: instance.ID,
Type: api.AnomalyInstanceConnection,
Payload: string(payload),
- })
- if err != nil {
+ }); err != nil {
s.l.Error("Failed to create anomaly",
zap.String("instance", instance.Name),
zap.String("type", string(api.AnomalyInstanceConnection)),
@@ -219,12 +218,11 @@ func (s *AnomalyScanner) checkInstanceAnomaly(ctx context.Context, instance *api
zap.Error(err))
} else {
if setup {
- _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
+ if _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
CreatorID: api.SystemBotID,
InstanceID: instance.ID,
Type: api.AnomalyInstanceMigrationSchema,
- })
- if err != nil {
+ }); err != nil {
s.l.Error("Failed to create anomaly",
zap.String("instance", instance.Name),
zap.String("type", string(api.AnomalyInstanceMigrationSchema)),
@@ -262,14 +260,13 @@ func (s *AnomalyScanner) checkDatabaseAnomaly(ctx context.Context, instance *api
zap.String("type", string(api.AnomalyDatabaseConnection)),
zap.Error(err))
} else {
- _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
+ if _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
CreatorID: api.SystemBotID,
InstanceID: instance.ID,
DatabaseID: &database.ID,
Type: api.AnomalyDatabaseConnection,
Payload: string(payload),
- })
- if err != nil {
+ }); err != nil {
s.l.Error("Failed to create anomaly",
zap.String("instance", instance.Name),
zap.String("database", database.Name),
@@ -352,14 +349,13 @@ func (s *AnomalyScanner) checkDatabaseAnomaly(ctx context.Context, instance *api
zap.String("type", string(api.AnomalyDatabaseSchemaDrift)),
zap.Error(err))
} else {
- _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
+ if _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
CreatorID: api.SystemBotID,
InstanceID: instance.ID,
DatabaseID: &database.ID,
Type: api.AnomalyDatabaseSchemaDrift,
Payload: string(payload),
- })
- if err != nil {
+ }); err != nil {
s.l.Error("Failed to create anomaly",
zap.String("instance", instance.Name),
zap.String("database", database.Name),
@@ -437,14 +433,13 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.I
zap.String("type", string(api.AnomalyDatabaseBackupPolicyViolation)),
zap.Error(err))
} else {
- _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
+ if _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
CreatorID: api.SystemBotID,
InstanceID: instance.ID,
DatabaseID: &database.ID,
Type: api.AnomalyDatabaseBackupPolicyViolation,
Payload: string(payload),
- })
- if err != nil {
+ }); err != nil {
s.l.Error("Failed to create anomaly",
zap.String("instance", instance.Name),
zap.String("database", database.Name),
@@ -521,14 +516,13 @@ func (s *AnomalyScanner) checkBackupAnomaly(ctx context.Context, instance *api.I
zap.String("type", string(api.AnomalyDatabaseBackupMissing)),
zap.Error(err))
} else {
- _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
+ if _, err = s.server.AnomalyService.UpsertActiveAnomaly(ctx, &api.AnomalyUpsert{
CreatorID: api.SystemBotID,
InstanceID: instance.ID,
DatabaseID: &database.ID,
Type: api.AnomalyDatabaseBackupMissing,
Payload: string(payload),
- })
- if err != nil {
+ }); err != nil {
s.l.Error("Failed to create anomaly",
zap.String("instance", instance.Name),
zap.String("database", database.Name),
diff --git a/server/database.go b/server/database.go
index deec673db0..cc3b2d5928 100644
--- a/server/database.go
+++ b/server/database.go
@@ -977,13 +977,18 @@ func (s *Server) composeDatabaseRelationship(ctx context.Context, database *api.
database.DataSourceList = []*api.DataSource{}
rowStatus := api.Normal
- anomalyList, err := s.AnomalyService.FindAnomalyList(ctx, &api.AnomalyFind{
+ anomalyListRaw, err := s.AnomalyService.FindAnomalyList(ctx, &api.AnomalyFind{
RowStatus: &rowStatus,
DatabaseID: &database.ID,
})
if err != nil {
return err
}
+ var anomalyList []*api.Anomaly
+ for _, anomalyRaw := range anomalyListRaw {
+ anomalyList = append(anomalyList, anomalyRaw.ToAnomaly())
+ }
+ // TODO(dragonly): implement composeAnomalyRelationship
database.AnomalyList = anomalyList
for _, anomaly := range database.AnomalyList {
anomaly.Creator, err = s.composePrincipalByID(ctx, anomaly.CreatorID)
diff --git a/server/instance.go b/server/instance.go
index c84620c47d..a1af33ca40 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -482,7 +482,7 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
}
rowStatus := api.Normal
- anomalyList, err := s.AnomalyService.FindAnomalyList(ctx, &api.AnomalyFind{
+ anomalyListRaw, err := s.AnomalyService.FindAnomalyList(ctx, &api.AnomalyFind{
RowStatus: &rowStatus,
InstanceID: &instance.ID,
InstanceOnly: true,
@@ -490,6 +490,11 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
if err != nil {
return err
}
+ var anomalyList []*api.Anomaly
+ for _, anomalyRaw := range anomalyListRaw {
+ anomalyList = append(anomalyList, anomalyRaw.ToAnomaly())
+ }
+ // TODO(dragonly): implement composeAnomalyRelationship
instance.AnomalyList = anomalyList
for _, anomaly := range instance.AnomalyList {
if anomaly.Creator, err = s.composePrincipalByID(ctx, anomaly.CreatorID); err != nil {
diff --git a/store/anomaly.go b/store/anomaly.go
index bf018b1377..df2e9d98e8 100644
--- a/store/anomaly.go
+++ b/store/anomaly.go
@@ -29,7 +29,7 @@ func NewAnomalyService(logger *zap.Logger, db *DB) *AnomalyService {
// UpsertActiveAnomaly would update the existing active anomaly if both database id and type match, otherwise create a new one.
// Do not use ON CONFLICT (upsert syntax) as it will consume autoincrement id. Functional wise, this is fine, but
// from the UX perspective, it's not great, since user will see large id gaps.
-func (s *AnomalyService) UpsertActiveAnomaly(ctx context.Context, upsert *api.AnomalyUpsert) (*api.Anomaly, error) {
+func (s *AnomalyService) UpsertActiveAnomaly(ctx context.Context, upsert *api.AnomalyUpsert) (*api.AnomalyRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -48,9 +48,9 @@ func (s *AnomalyService) UpsertActiveAnomaly(ctx context.Context, upsert *api.An
return nil, err
}
- var anomaly *api.Anomaly
+ var anomalyRaw *api.AnomalyRaw
if len(list) == 0 {
- anomaly, err = createAnomaly(ctx, tx.PTx, upsert)
+ anomalyRaw, err = createAnomaly(ctx, tx.PTx, upsert)
if err != nil {
return nil, err
}
@@ -61,7 +61,7 @@ func (s *AnomalyService) UpsertActiveAnomaly(ctx context.Context, upsert *api.An
UpdaterID: upsert.CreatorID,
Payload: upsert.Payload,
}
- anomaly, err = patchAnomaly(ctx, tx.PTx, patch)
+ anomalyRaw, err = patchAnomaly(ctx, tx.PTx, patch)
if err != nil {
return nil, err
}
@@ -73,11 +73,11 @@ func (s *AnomalyService) UpsertActiveAnomaly(ctx context.Context, upsert *api.An
return nil, FormatError(err)
}
- return anomaly, nil
+ return anomalyRaw, nil
}
// FindAnomalyList retrieves a list of anomalies based on the find condition.
-func (s *AnomalyService) FindAnomalyList(ctx context.Context, find *api.AnomalyFind) ([]*api.Anomaly, error) {
+func (s *AnomalyService) FindAnomalyList(ctx context.Context, find *api.AnomalyFind) ([]*api.AnomalyRaw, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
@@ -113,7 +113,7 @@ func (s *AnomalyService) ArchiveAnomaly(ctx context.Context, archive *api.Anomal
}
// createAnomaly creates a new anomaly.
-func createAnomaly(ctx context.Context, tx *sql.Tx, upsert *api.AnomalyUpsert) (*api.Anomaly, error) {
+func createAnomaly(ctx context.Context, tx *sql.Tx, upsert *api.AnomalyUpsert) (*api.AnomalyRaw, error) {
// Inserts row into database.
if upsert.Payload == "" {
upsert.Payload = "{}"
@@ -144,31 +144,31 @@ func createAnomaly(ctx context.Context, tx *sql.Tx, upsert *api.AnomalyUpsert) (
defer row.Close()
row.Next()
- var anomaly api.Anomaly
+ var anomalyRaw api.AnomalyRaw
databaseID := sql.NullInt32{}
if err := row.Scan(
- &anomaly.ID,
- &anomaly.CreatorID,
- &anomaly.CreatedTs,
- &anomaly.UpdaterID,
- &anomaly.UpdatedTs,
- &anomaly.InstanceID,
+ &anomalyRaw.ID,
+ &anomalyRaw.CreatorID,
+ &anomalyRaw.CreatedTs,
+ &anomalyRaw.UpdaterID,
+ &anomalyRaw.UpdatedTs,
+ &anomalyRaw.InstanceID,
&databaseID,
- &anomaly.Type,
- &anomaly.Payload,
+ &anomalyRaw.Type,
+ &anomalyRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
if databaseID.Valid {
value := int(databaseID.Int32)
- anomaly.DatabaseID = &value
+ anomalyRaw.DatabaseID = &value
}
- anomaly.Severity = api.AnomalySeverityFromType(anomaly.Type)
+ anomalyRaw.Severity = api.AnomalySeverityFromType(anomalyRaw.Type)
- return nil, err
+ return &anomalyRaw, err
}
-func findAnomalyList(ctx context.Context, tx *sql.Tx, find *api.AnomalyFind) ([]*api.Anomaly, error) {
+func findAnomalyList(ctx context.Context, tx *sql.Tx, find *api.AnomalyFind) ([]*api.AnomalyRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.InstanceID; v != nil {
@@ -210,37 +210,37 @@ func findAnomalyList(ctx context.Context, tx *sql.Tx, find *api.AnomalyFind) ([]
}
defer rows.Close()
- // Iterate over result set and deserialize rows into anomalyList.
- var anomalyList []*api.Anomaly
+ // Iterate over result set and deserialize rows into anomalyRawList.
+ var anomalyRawList []*api.AnomalyRaw
for rows.Next() {
- var anomaly api.Anomaly
+ var anomalyRaw api.AnomalyRaw
databaseID := sql.NullInt32{}
if err := rows.Scan(
- &anomaly.ID,
- &anomaly.CreatorID,
- &anomaly.CreatedTs,
- &anomaly.UpdaterID,
- &anomaly.UpdatedTs,
- &anomaly.InstanceID,
+ &anomalyRaw.ID,
+ &anomalyRaw.CreatorID,
+ &anomalyRaw.CreatedTs,
+ &anomalyRaw.UpdaterID,
+ &anomalyRaw.UpdatedTs,
+ &anomalyRaw.InstanceID,
&databaseID,
- &anomaly.Type,
- &anomaly.Payload,
+ &anomalyRaw.Type,
+ &anomalyRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
if databaseID.Valid {
value := int(databaseID.Int32)
- anomaly.DatabaseID = &value
+ anomalyRaw.DatabaseID = &value
}
- anomaly.Severity = api.AnomalySeverityFromType(anomaly.Type)
+ anomalyRaw.Severity = api.AnomalySeverityFromType(anomalyRaw.Type)
- anomalyList = append(anomalyList, &anomaly)
+ anomalyRawList = append(anomalyRawList, &anomalyRaw)
}
if err := rows.Err(); err != nil {
return nil, FormatError(err)
}
- return anomalyList, nil
+ return anomalyRawList, nil
}
type anomalyPatch struct {
@@ -254,7 +254,7 @@ type anomalyPatch struct {
}
// patchAnomaly patches an anomaly
-func patchAnomaly(ctx context.Context, tx *sql.Tx, patch *anomalyPatch) (*api.Anomaly, error) {
+func patchAnomaly(ctx context.Context, tx *sql.Tx, patch *anomalyPatch) (*api.AnomalyRaw, error) {
// Build UPDATE clause.
if patch.Payload == "" {
patch.Payload = "{}"
@@ -283,28 +283,28 @@ func patchAnomaly(ctx context.Context, tx *sql.Tx, patch *anomalyPatch) (*api.An
defer row.Close()
row.Next()
- var anomaly api.Anomaly
+ var anomalyRaw api.AnomalyRaw
databaseID := sql.NullInt32{}
if err := row.Scan(
- &anomaly.ID,
- &anomaly.CreatorID,
- &anomaly.CreatedTs,
- &anomaly.UpdaterID,
- &anomaly.UpdatedTs,
- &anomaly.InstanceID,
+ &anomalyRaw.ID,
+ &anomalyRaw.CreatorID,
+ &anomalyRaw.CreatedTs,
+ &anomalyRaw.UpdaterID,
+ &anomalyRaw.UpdatedTs,
+ &anomalyRaw.InstanceID,
&databaseID,
- &anomaly.Type,
- &anomaly.Payload,
+ &anomalyRaw.Type,
+ &anomalyRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
if databaseID.Valid {
value := int(databaseID.Int32)
- anomaly.DatabaseID = &value
+ anomalyRaw.DatabaseID = &value
}
- anomaly.Severity = api.AnomalySeverityFromType(anomaly.Type)
+ anomalyRaw.Severity = api.AnomalySeverityFromType(anomalyRaw.Type)
- return &anomaly, err
+ return &anomalyRaw, err
}
// archiveAnomaly archives an anomaly by ID.