Move utils#Name() to types#Name()

This commit is contained in:
Eric Lippmann 2024-05-21 11:49:34 +02:00
parent b2a43fd47d
commit 2f3bf491d7
7 changed files with 29 additions and 25 deletions

View file

@ -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.

View file

@ -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))
}
}

View file

@ -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)),

View file

@ -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))
})

View file

@ -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 {

13
pkg/types/utils.go Normal file
View file

@ -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:]
}

View file

@ -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: