SDK v3 #8

Merged
SoXX merged 77 commits from dev/issue-5 into main 2024-08-14 13:27:35 +00:00
Showing only changes of commit ca1e1a1da5 - Show all commits

View File

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