diff --git a/pkg/database/scrape_history.go b/pkg/database/scrape_history.go new file mode 100644 index 0000000..8cf424f --- /dev/null +++ b/pkg/database/scrape_history.go @@ -0,0 +1,166 @@ +package database + +import ( + "context" + "errors" + + "git.anthrove.art/Anthrove/otter-space-sdk/v4/internal/utils" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models" + log "github.com/sirupsen/logrus" + "go.opentelemetry.io/otel/attribute" + "gorm.io/gorm" +) + +func CreateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory) (models.ScrapeHistory, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateScrapeHistory") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "scrape_task_id": scrapeHistory.ScrapeTaskID, + "user_source_id": scrapeHistory.UserSourceID, + }) + + span.SetAttributes( + attribute.String("scrape_task_id", string(scrapeHistory.ScrapeTaskID)), + attribute.String("user_source_id", string(scrapeHistory.UserSourceID)), + ) + + utils.HandleEvent(span, localLogger, "Starting scrapeHistory creation") + + if client == nil { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } + + result := client.WithContext(ctx).Create(&scrapeHistory) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) + } + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, result.Error) + } + + utils.HandleEvent(span, localLogger, "scrapeHistory created successfully") + return scrapeHistory, nil +} + +func GetScrapeHistoryByID(ctx context.Context, id models.ScrapeTaskID) (models.ScrapeHistory, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetScrapeHistoryByID") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "scrape_task_id": id, + }) + + span.SetAttributes( + attribute.String("scrape_task_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting get scrapeHistory by ID") + + var post models.ScrapeHistory + + if client == nil { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } + + if len(id) == 0 { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty}) + } + + result := client.WithContext(ctx).First(&post, "scrape_task_id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) + } + return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, result.Error) + } + + utils.HandleEvent(span, localLogger, "scrapeHistory retrieved successfully") + return post, nil +} + +// UpdateScrapeHistory updates the scrape history information in the database. +// Only a few parameter can be updated: +// - FinishedAt +// - Error +// - AddedPosts +// - DeletedPosts +func UpdateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateScrapeHistory") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "scrape_task_id": scrapeHistory.ScrapeTaskID, + }) + + span.SetAttributes( + attribute.String("scrape_task_id", string(scrapeHistory.ScrapeTaskID)), + ) + + utils.HandleEvent(span, localLogger, "Starting scrapeHistory update") + + if client == nil { + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } + + if len(scrapeHistory.ScrapeTaskID) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty}) + } + + updateScrapeHistory := models.ScrapeHistory{ + ScrapeTaskID: scrapeHistory.ScrapeTaskID, + UserSourceID: scrapeHistory.UserSourceID, + FinishedAt: scrapeHistory.FinishedAt, + Error: scrapeHistory.Error, + AddedPosts: scrapeHistory.AddedPosts, + DeletedPosts: scrapeHistory.DeletedPosts, + } + + result := client.WithContext(ctx).Model(&updateScrapeHistory).Updates(updateScrapeHistory) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) + } + return utils.HandleError(ctx, span, localLogger, result.Error) + } + + utils.HandleEvent(span, localLogger, "scrapeHistory updated successfully") + return nil +} + +func DeleteScrapeHistory(ctx context.Context, id models.ScrapeTaskID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteScrapeHistory") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "scrape_task_id": id, + }) + + span.SetAttributes( + attribute.String("scrape_task_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting delete scrapeHistory") + + var scrapeHistory models.ScrapeHistory + + if client == nil { + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } + + if len(id) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty}) + } + + result := client.WithContext(ctx).Delete(&scrapeHistory, "scrape_task_id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) + } + return utils.HandleError(ctx, span, localLogger, result.Error) + } + + utils.HandleEvent(span, localLogger, "scrapeHistory deleted successfully") + return nil +} diff --git a/pkg/error/validation.go b/pkg/error/validation.go index f91a447..2fe7289 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -35,6 +35,9 @@ const ( PoolIDIsWrongLength = "PoolID has the wrong length" PoolURLIsEmpty = "PoolURL cannot be empty" + + ScrapeTaskIDIsEmpty = "ScrapeTaskID cannot be empty" + ScrapeTaskIDIsWrongLength = "ScrapeTaskID has the wrong length" ) type EntityValidationFailed struct { diff --git a/pkg/models/const.go b/pkg/models/const.go index 9755bfd..fb7a04f 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -23,6 +23,8 @@ type ( UserSourceID string UserFavoriteID string + + ScrapeTaskID string ) const ( diff --git a/pkg/models/scrape_history.go b/pkg/models/scrape_history.go index e06f6c5..f5c6ccd 100644 --- a/pkg/models/scrape_history.go +++ b/pkg/models/scrape_history.go @@ -5,13 +5,13 @@ import ( ) type ScrapeHistory struct { - ScrapeTaskID string `json:"scrape_task_id" gorm:"primaryKey"` - UserSourceID string `json:"user_source_id" gorm:""` - CreatedAt time.Time `json:"created_at" gorm:""` - FinishedAt time.Time `json:"finished_at" gorm:"null"` - Error string `json:"error" gorm:"null"` - AddedPosts int `json:"added_posts" gorm:"not null"` - DeletedPosts int `json:"deleted_posts" gorm:"not null"` + ScrapeTaskID ScrapeTaskID `json:"scrape_task_id" gorm:"primaryKey"` + UserSourceID UserSourceID `json:"user_source_id" gorm:""` + CreatedAt time.Time `json:"created_at" gorm:""` + FinishedAt time.Time `json:"finished_at" gorm:""` + Error string `json:"error" gorm:"null"` + AddedPosts int `json:"added_posts" gorm:"not null"` + DeletedPosts int `json:"deleted_posts" gorm:"not null"` } func (ScrapeHistory) TableName() string {