183 lines
6.5 KiB
Go
183 lines
6.5 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/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})
|
|
}
|
|
|
|
if len(scrapeHistory.ScrapeTaskID) == 0 {
|
|
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
|
|
}
|
|
|
|
if len(scrapeHistory.ScrapeTaskID) != 25 {
|
|
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
|
|
}
|
|
|
|
if len(scrapeHistory.UserSourceID) == 0 {
|
|
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty})
|
|
}
|
|
|
|
if len(scrapeHistory.UserSourceID) != 25 {
|
|
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIsWrongLength})
|
|
}
|
|
|
|
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 len(id) == 0 {
|
|
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
|
|
}
|
|
|
|
if len(id) != 25 {
|
|
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
|
|
}
|
|
|
|
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 len(scrapeHistory.ScrapeTaskID) == 0 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
|
|
}
|
|
|
|
if len(scrapeHistory.ScrapeTaskID) != 25 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
|
|
}
|
|
|
|
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 len(id) == 0 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
|
|
}
|
|
|
|
if len(id) != 25 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
|
|
}
|
|
|
|
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
|
|
}
|