otter-space-sdk/pkg/database/tagAlias.go

163 lines
4.9 KiB
Go
Raw Normal View History

package database
import (
"context"
"errors"
2024-08-12 07:58:14 +00:00
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
2024-08-12 07:58:14 +00:00
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) {
2024-08-12 07:58:14 +00:00
ctx, span := tracer.Start(ctx, "CreateTagAlias")
defer span.End()
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
attribute.String("tag_name", string(tagName)),
)
span.AddEvent("Starting tag alias creation")
if client == nil {
2024-08-12 07:58:14 +00:00
return models.TagAlias{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
tagAlias := models.TagAlias{
Name: tagAliasName,
TagID: tagName,
}
2024-08-12 07:58:14 +00:00
logger.WithContext(ctx).WithFields(log.Fields{
"tag_alias_name": tagAliasName,
"tag_name": tagName,
}).Debug("attempting to create tag alias")
result := client.WithContext(ctx).Create(&tagAlias)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
2024-08-12 07:58:14 +00:00
loggerFields := log.Fields{
"tag_alias_name": tagAliasName,
"tag_name": tagName,
}
return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
2024-08-12 07:58:14 +00:00
loggerFields := log.Fields{
"tag_alias_name": tagAliasName,
"tag_name": tagName,
}
return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
2024-08-12 07:58:14 +00:00
logger.WithContext(ctx).WithFields(log.Fields{
"tag_alias_name": tagAliasName,
"tag_name": tagName,
}).Debug("tag alias created")
span.AddEvent("Tag alias created successfully")
return tagAlias, nil
}
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
2024-08-12 07:58:14 +00:00
ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch")
defer span.End()
2024-08-12 07:58:14 +00:00
span.SetAttributes(
attribute.Int64("batch_size", int64(batchSize)),
attribute.Int64("tag_aliases_count", int64(len(tagsAliases))),
2024-08-12 07:58:14 +00:00
)
span.AddEvent("Starting batch tag alias creation")
2024-08-12 07:58:14 +00:00
if client == nil {
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
2024-08-12 07:58:14 +00:00
if tagsAliases == nil || len(tagsAliases) == 0 {
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty})
}
if batchSize == 0 {
2024-08-12 07:58:14 +00:00
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
2024-08-12 07:58:14 +00:00
logger.WithContext(ctx).WithFields(log.Fields{
"tag_aliases_count": len(tagsAliases),
2024-08-12 07:58:14 +00:00
}).Debug("attempting to create tags aliases")
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
2024-08-12 07:58:14 +00:00
loggerFields := log.Fields{
"tag_aliases_count": len(tagsAliases),
2024-08-12 07:58:14 +00:00
}
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
loggerFields := log.Fields{
"tag_aliases_count": len(tagsAliases),
}
2024-08-12 07:58:14 +00:00
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
2024-08-12 07:58:14 +00:00
logger.WithContext(ctx).WithFields(log.Fields{
"tag_aliases_count": len(tagsAliases),
2024-08-12 07:58:14 +00:00
}).Debug("batch tags aliases created")
span.AddEvent("Batch tags aliases created successfully")
return nil
}
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
2024-08-12 07:58:14 +00:00
ctx, span := tracer.Start(ctx, "DeleteTagAlias")
defer span.End()
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
)
span.AddEvent("Starting tag alias deletion")
var tagAlias models.TagAlias
if client == nil {
2024-08-12 07:58:14 +00:00
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagAliasName) == 0 {
2024-08-12 07:58:14 +00:00
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
}
2024-08-12 07:58:14 +00:00
logger.WithContext(ctx).WithFields(log.Fields{
"tag_alias_name": tagAliasName,
}).Debug("attempting to delete tag alias")
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
2024-08-12 07:58:14 +00:00
loggerFields := log.Fields{
"tag_alias_name": tagAliasName,
}
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
}
2024-08-12 07:58:14 +00:00
loggerFields := log.Fields{
"tag_alias_name": tagAliasName,
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
2024-08-12 07:58:14 +00:00
logger.WithContext(ctx).WithFields(log.Fields{
"tag_alias_name": tagAliasName,
}).Debug("tag alias deleted")
span.AddEvent("Tag alias deleted successfully")
return nil
}