diff --git a/pkg/common/sync_subject.go b/pkg/common/sync_subject.go index 56b7ebab..3a56306c 100644 --- a/pkg/common/sync_subject.go +++ b/pkg/common/sync_subject.go @@ -4,7 +4,7 @@ import ( "github.com/icinga/icingadb/pkg/contracts" "github.com/icinga/icingadb/pkg/database" v1 "github.com/icinga/icingadb/pkg/icingadb/v1" - "github.com/icinga/icingadb/pkg/utils" + "github.com/icinga/icingadb/pkg/types" ) // SyncSubject defines information about entities to be synchronized. @@ -63,7 +63,7 @@ func (s SyncSubject) FactoryForDelta() database.EntityFactoryFunc { // Name returns the declared name of the entity. func (s SyncSubject) Name() string { - return utils.Name(s.entity) + return types.Name(s.entity) } // WithChecksum returns whether entities from the factory implement contracts.Checksumer. diff --git a/pkg/database/utils.go b/pkg/database/utils.go index 04380b9c..a4c62bed 100644 --- a/pkg/database/utils.go +++ b/pkg/database/utils.go @@ -2,7 +2,7 @@ package database import ( "github.com/icinga/icingadb/pkg/strcase" - "github.com/icinga/icingadb/pkg/utils" + "github.com/icinga/icingadb/pkg/types" "github.com/pkg/errors" ) @@ -16,6 +16,6 @@ func TableName(t interface{}) string { if tn, ok := t.(TableNamer); ok { return tn.TableName() } else { - return strcase.Snake(utils.Name(t)) + return strcase.Snake(types.Name(t)) } } diff --git a/pkg/icingadb/delta.go b/pkg/icingadb/delta.go index c8ef0537..340eaecb 100644 --- a/pkg/icingadb/delta.go +++ b/pkg/icingadb/delta.go @@ -7,7 +7,7 @@ import ( "github.com/icinga/icingadb/pkg/contracts" "github.com/icinga/icingadb/pkg/database" "github.com/icinga/icingadb/pkg/logging" - "github.com/icinga/icingadb/pkg/utils" + "github.com/icinga/icingadb/pkg/types" "go.uber.org/zap" "time" ) @@ -104,8 +104,8 @@ func (delta *Delta) run(ctx context.Context, actualCh, desiredCh <-chan database delta.Update = update delta.Delete = actual - delta.logger.Debugw(fmt.Sprintf("Finished %s delta", utils.Name(delta.Subject.Entity())), - zap.String("subject", utils.Name(delta.Subject.Entity())), + delta.logger.Debugw(fmt.Sprintf("Finished %s delta", types.Name(delta.Subject.Entity())), + zap.String("subject", types.Name(delta.Subject.Entity())), zap.Duration("time_total", time.Since(start)), zap.Duration("time_actual", endActual.Sub(start)), zap.Duration("time_desired", endDesired.Sub(start)), diff --git a/pkg/icingadb/sync.go b/pkg/icingadb/sync.go index b9f2848a..c1b6cf0c 100644 --- a/pkg/icingadb/sync.go +++ b/pkg/icingadb/sync.go @@ -11,7 +11,7 @@ import ( "github.com/icinga/icingadb/pkg/icingaredis/telemetry" "github.com/icinga/icingadb/pkg/logging" "github.com/icinga/icingadb/pkg/strcase" - "github.com/icinga/icingadb/pkg/utils" + "github.com/icinga/icingadb/pkg/types" "github.com/pkg/errors" "go.uber.org/zap" "golang.org/x/sync/errgroup" @@ -38,7 +38,7 @@ func NewSync(db *DB, redis *icingaredis.Client, logger *logging.Logger) *Sync { // SyncAfterDump waits for a config dump to finish (using the dump parameter) and then starts a sync for the given // sync subject using the Sync function. func (s Sync) SyncAfterDump(ctx context.Context, subject *common.SyncSubject, dump *DumpSignals) error { - typeName := utils.Name(subject.Entity()) + typeName := types.Name(subject.Entity()) key := "icinga:" + strcase.Delimited(typeName, ':') startTime := time.Now() @@ -109,12 +109,12 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error { // Create if len(delta.Create) > 0 { - s.logger.Infof("Inserting %d items of type %s", len(delta.Create), strcase.Delimited(utils.Name(delta.Subject.Entity()), ' ')) + s.logger.Infof("Inserting %d items of type %s", len(delta.Create), strcase.Delimited(types.Name(delta.Subject.Entity()), ' ')) var entities <-chan database.Entity if delta.Subject.WithChecksum() { pairs, errs := s.redis.HMYield( ctx, - fmt.Sprintf("icinga:%s", strcase.Delimited(utils.Name(delta.Subject.Entity()), ':')), + fmt.Sprintf("icinga:%s", strcase.Delimited(types.Name(delta.Subject.Entity()), ':')), delta.Create.Keys()...) // Let errors from Redis cancel our group. com.ErrgroupReceive(g, errs) @@ -136,10 +136,10 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error { // Update if len(delta.Update) > 0 { - s.logger.Infof("Updating %d items of type %s", len(delta.Update), strcase.Delimited(utils.Name(delta.Subject.Entity()), ' ')) + s.logger.Infof("Updating %d items of type %s", len(delta.Update), strcase.Delimited(types.Name(delta.Subject.Entity()), ' ')) pairs, errs := s.redis.HMYield( ctx, - fmt.Sprintf("icinga:%s", strcase.Delimited(utils.Name(delta.Subject.Entity()), ':')), + fmt.Sprintf("icinga:%s", strcase.Delimited(types.Name(delta.Subject.Entity()), ':')), delta.Update.Keys()...) // Let errors from Redis cancel our group. com.ErrgroupReceive(g, errs) @@ -160,7 +160,7 @@ func (s Sync) ApplyDelta(ctx context.Context, delta *Delta) error { // Delete if len(delta.Delete) > 0 { - s.logger.Infof("Deleting %d items of type %s", len(delta.Delete), strcase.Delimited(utils.Name(delta.Subject.Entity()), ' ')) + s.logger.Infof("Deleting %d items of type %s", len(delta.Delete), strcase.Delimited(types.Name(delta.Subject.Entity()), ' ')) g.Go(func() error { return s.db.Delete(ctx, delta.Subject.Entity(), delta.Delete.IDs(), OnSuccessIncrement[any](stat)) }) diff --git a/pkg/icingaredis/client.go b/pkg/icingaredis/client.go index 72bb302b..c31352e4 100644 --- a/pkg/icingaredis/client.go +++ b/pkg/icingaredis/client.go @@ -9,6 +9,7 @@ import ( "github.com/icinga/icingadb/pkg/logging" "github.com/icinga/icingadb/pkg/periodic" "github.com/icinga/icingadb/pkg/strcase" + "github.com/icinga/icingadb/pkg/types" "github.com/icinga/icingadb/pkg/utils" "github.com/pkg/errors" "github.com/redis/go-redis/v9" @@ -212,7 +213,7 @@ func (c *Client) XReadUntilResult(ctx context.Context, a *redis.XReadArgs) ([]re // YieldAll yields all entities from Redis that belong to the specified SyncSubject. func (c Client) YieldAll(ctx context.Context, subject *common.SyncSubject) (<-chan database.Entity, <-chan error) { - key := strcase.Delimited(utils.Name(subject.Entity()), ':') + key := strcase.Delimited(types.Name(subject.Entity()), ':') if subject.WithChecksum() { key = "icinga:checksum:" + key } else { diff --git a/pkg/types/utils.go b/pkg/types/utils.go new file mode 100644 index 00000000..806c3cc1 --- /dev/null +++ b/pkg/types/utils.go @@ -0,0 +1,13 @@ +package types + +import ( + "fmt" + "strings" +) + +// Name returns the declared name of type t. +func Name(t any) string { + s := strings.TrimLeft(fmt.Sprintf("%T", t), "*") + + return s[strings.LastIndex(s, ".")+1:] +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index b35f1040..d8d2f9c0 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -24,16 +24,6 @@ func FromUnixMilli(ms int64) time.Time { return time.Unix(int64(sec), int64(dec*(1e9))) } -// Name returns the declared name of type t. -// Name is used in combination with Key -// to automatically guess an entity's -// database table and Redis key. -func Name(t interface{}) string { - s := strings.TrimLeft(fmt.Sprintf("%T", t), "*") - - return s[strings.LastIndex(s, ".")+1:] -} - // Timed calls the given callback with the time that has elapsed since the start. // // Timed should be installed by defer: