2024-08-27 05:50:36 +00:00
|
|
|
package plug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-27 06:26:39 +00:00
|
|
|
|
2024-08-29 13:26:58 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
|
2024-08-27 05:50:36 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
)
|
|
|
|
|
|
|
|
func upsertSource(ctx context.Context, source models.Source) (models.Source, error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "upsertSource")
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2024-08-27 07:35:25 +00:00
|
|
|
localSource, err := database.GetSourceByDomain(ctx, source.Domain)
|
2024-08-27 05:50:36 +00:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "Database error: NoDataFound" {
|
|
|
|
span.AddEvent("No Source found, initializing source")
|
|
|
|
|
|
|
|
log.WithContext(ctx).WithField("source_domain", source.Domain).Info("No Source found, initializing source!")
|
|
|
|
|
|
|
|
source, err = database.CreateSource(ctx, source)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithError(err).WithField("source_domain", source.Domain).Error("Failed to create source")
|
|
|
|
return models.Source{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
span.AddEvent("Source created")
|
|
|
|
log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).WithField("source_id", source.ID).Info("Source created!")
|
|
|
|
|
2024-08-27 07:35:25 +00:00
|
|
|
return source, nil
|
|
|
|
|
2024-08-27 05:50:36 +00:00
|
|
|
} else {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).Error("Failed to get source by domain")
|
|
|
|
return models.Source{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-27 07:35:25 +00:00
|
|
|
return localSource, nil
|
2024-08-27 05:50:36 +00:00
|
|
|
}
|