2024-08-10 22:13:46 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-08-12 07:58:14 +00:00
|
|
|
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
|
2024-08-10 22:13:46 +00:00
|
|
|
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"
|
2024-08-10 22:13:46 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-08-10 22:24:02 +00:00
|
|
|
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")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
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})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 22:24:02 +00:00
|
|
|
tagAlias := models.TagAlias{
|
2024-08-10 22:13:46 +00:00
|
|
|
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")
|
|
|
|
|
2024-08-10 22:24:02 +00:00
|
|
|
result := client.WithContext(ctx).Create(&tagAlias)
|
2024-08-10 22:13:46 +00:00
|
|
|
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-10 22:13:46 +00:00
|
|
|
}
|
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-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2024-08-10 22:24:02 +00:00
|
|
|
return tagAlias, nil
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10 22:13:46 +00:00
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
span.SetAttributes(
|
|
|
|
attribute.Int64("batch_size", int64(batchSize)),
|
2024-08-12 08:07:29 +00:00
|
|
|
attribute.Int64("tag_aliases_count", int64(len(tagsAliases))),
|
2024-08-12 07:58:14 +00:00
|
|
|
)
|
|
|
|
|
2024-08-12 08:07:29 +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-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
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})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
logger.WithContext(ctx).WithFields(log.Fields{
|
2024-08-12 08:07:29 +00:00
|
|
|
"tag_aliases_count": len(tagsAliases),
|
2024-08-12 07:58:14 +00:00
|
|
|
}).Debug("attempting to create tags aliases")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
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{
|
2024-08-12 08:07:29 +00:00
|
|
|
"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{
|
2024-08-12 08:07:29 +00:00
|
|
|
"tag_aliases_count": len(tagsAliases),
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 07:58:14 +00:00
|
|
|
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
logger.WithContext(ctx).WithFields(log.Fields{
|
2024-08-12 08:07:29 +00:00
|
|
|
"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")
|
2024-08-10 22:13:46 +00:00
|
|
|
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")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
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})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
logger.WithContext(ctx).WithFields(log.Fields{
|
|
|
|
"tag_alias_name": tagAliasName,
|
|
|
|
}).Debug("attempting to delete tag alias")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
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-10 22:13:46 +00:00
|
|
|
}
|
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-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2024-08-10 22:13:46 +00:00
|
|
|
return nil
|
|
|
|
}
|