2024-08-27 07:15:17 +00:00
package plug
import (
"context"
2024-11-06 12:39:30 +00:00
"slices"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
2024-08-27 07:56:52 +00:00
log "github.com/sirupsen/logrus"
2024-08-27 07:49:20 +00:00
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
2024-08-27 07:15:17 +00:00
"gorm.io/gorm"
)
var BatchSize = 50
2024-08-27 08:04:55 +00:00
var BasicLoggingFields log . Fields
2024-08-27 07:15:17 +00:00
2024-08-27 10:25:00 +00:00
type BatchSummery struct {
AddedPosts int64
AddedFavorites int64
}
2024-11-01 20:06:03 +00:00
func BatchPostProcessingWithSummery ( ctx context . Context , userSource models . UserSource , posts [ ] models . Post ) ( [ ] models . Post , [ ] models . UserFavorite , error ) {
2024-08-27 07:49:20 +00:00
ctx , span := tracer . Start ( ctx , "BatchPostProcessing" )
defer span . End ( )
2024-08-27 08:04:55 +00:00
span . SetAttributes (
attribute . String ( "user_source_id" , string ( userSource . ID ) ) ,
attribute . String ( "user_source_user_id" , string ( userSource . UserID ) ) ,
attribute . String ( "user_source_source_id" , string ( userSource . SourceID ) ) ,
)
BasicLoggingFields = log . Fields {
"user_source_id" : userSource . ID ,
"user_source_user_id" : userSource . UserID ,
"user_source_source_id" : userSource . SourceID ,
}
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . Info ( "Starting BatchPostProcessing" )
2024-08-27 07:15:17 +00:00
db , err := database . GetGorm ( ctx )
if err != nil {
2024-08-27 07:49:20 +00:00
span . RecordError ( err )
span . SetStatus ( codes . Error , err . Error ( ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithError ( err ) . WithFields ( BasicLoggingFields ) . Error ( "Failed to get Gorm DB" )
2024-11-01 20:10:13 +00:00
return nil , nil , err
2024-08-27 07:15:17 +00:00
}
postIDs := make ( [ ] string , 0 , len ( posts ) )
for _ , post := range posts {
postIDs = append ( postIDs , post . References [ 0 ] . SourcePostID )
}
2024-08-27 07:49:20 +00:00
span . AddEvent ( "Collected post IDs" , trace . WithAttributes ( attribute . Int ( "post_count" , len ( postIDs ) ) ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . WithField ( "post_count" , len ( postIDs ) ) . Info ( "Collected post IDs" )
2024-08-27 07:15:17 +00:00
2024-11-01 20:06:03 +00:00
existingPostsReferences , err := getAnthrovePostReferences ( ctx , db , userSource . SourceID , postIDs )
2024-08-27 07:15:17 +00:00
if err != nil {
2024-08-27 07:49:20 +00:00
span . RecordError ( err )
span . SetStatus ( codes . Error , err . Error ( ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithError ( err ) . WithFields ( BasicLoggingFields ) . Error ( "Failed to fetch existing posts" )
2024-11-01 20:10:13 +00:00
return nil , nil , err
2024-08-27 07:15:17 +00:00
}
2024-11-01 20:06:03 +00:00
span . AddEvent ( "Fetched existing posts" , trace . WithAttributes ( attribute . Int ( "existing_post_count" , len ( existingPostsReferences ) ) ) )
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . WithField ( "existing_post_count" , len ( existingPostsReferences ) ) . Info ( "Fetched existing posts" )
2024-08-27 07:15:17 +00:00
var existingPostIDs [ ] models . PostID
2024-11-01 20:06:03 +00:00
for _ , post := range existingPostsReferences {
2024-08-27 07:15:17 +00:00
existingPostIDs = append ( existingPostIDs , models . PostID ( post . PostID ) )
}
var existingFavPostIDs [ ] models . PostID
2024-08-27 07:49:20 +00:00
existingFavPostIDs , err = getAlreadyFavoritesPostIDs ( ctx , db , existingPostIDs , userSource . ID )
2024-08-27 07:15:17 +00:00
if err != nil {
2024-08-27 07:49:20 +00:00
span . RecordError ( err )
span . SetStatus ( codes . Error , err . Error ( ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithError ( err ) . WithFields ( BasicLoggingFields ) . Error ( "Failed to fetch existing favorite posts" )
2024-11-01 20:10:13 +00:00
return nil , nil , err
2024-08-27 07:15:17 +00:00
}
2024-08-27 07:49:20 +00:00
span . AddEvent ( "Fetched existing favorite posts" , trace . WithAttributes ( attribute . Int ( "existing_fav_post_count" , len ( existingFavPostIDs ) ) ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . WithField ( "existing_fav_post_count" , len ( existingFavPostIDs ) ) . Info ( "Fetched existing favorite posts" )
2024-08-27 07:15:17 +00:00
2024-11-01 20:06:03 +00:00
anthroveFaves := make ( [ ] models . UserFavorite , 0 , len ( existingPostsReferences ) )
newPosts := make ( [ ] models . Post , 0 , len ( existingPostsReferences ) )
2024-11-01 21:15:43 +00:00
for _ , post := range posts {
2024-11-01 20:06:03 +00:00
if ! slices . ContainsFunc ( existingPostsReferences , func ( reference models . PostReference ) bool {
2024-08-27 07:15:17 +00:00
found := reference . SourcePostID == post . References [ 0 ] . SourcePostID
if found {
if ! slices . Contains ( existingFavPostIDs , models . PostID ( reference . PostID ) ) {
anthroveFaves = append ( anthroveFaves , models . UserFavorite {
UserID : userSource . UserID ,
PostID : models . PostID ( reference . PostID ) ,
UserSourceID : userSource . ID ,
} )
}
}
return found
} ) {
anthroveFaves = append ( anthroveFaves , models . UserFavorite {
UserID : userSource . UserID ,
PostID : post . ID ,
UserSourceID : userSource . ID ,
2024-10-26 21:13:24 +00:00
UserSource : models . UserSource { } ,
2024-08-27 07:15:17 +00:00
} )
newPosts = append ( newPosts , post )
}
}
2024-08-27 07:49:20 +00:00
span . AddEvent ( "Processed posts for favorites and new posts" , trace . WithAttributes ( attribute . Int ( "new_post_count" , len ( newPosts ) ) , attribute . Int ( "new_fav_count" , len ( anthroveFaves ) ) ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . Info ( "Processed posts for favorites and new posts" )
2024-08-27 07:15:17 +00:00
2024-11-01 20:06:03 +00:00
return newPosts , anthroveFaves , nil
2024-08-27 07:15:17 +00:00
}
2024-11-01 20:06:03 +00:00
func getAnthrovePostReferences ( ctx context . Context , gorm * gorm . DB , id models . SourceID , postIDs [ ] string ) ( [ ] models . PostReference , error ) {
ctx , span := tracer . Start ( ctx , "getAnthrovePostReferences" )
2024-08-27 07:49:20 +00:00
defer span . End ( )
2024-08-27 07:15:17 +00:00
2024-11-01 20:06:03 +00:00
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . Info ( "Starting getAnthrovePostReferences" )
2024-08-27 07:56:52 +00:00
2024-08-27 07:49:20 +00:00
var existingPosts [ ] models . PostReference
2024-08-27 07:15:17 +00:00
err := gorm . WithContext ( ctx ) . Model ( models . PostReference { } ) . Find ( & existingPosts , "source_id = ? AND source_post_id IN ?" , id , postIDs ) . Error
if err != nil {
2024-08-27 07:49:20 +00:00
span . RecordError ( err )
span . SetStatus ( codes . Error , err . Error ( ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithError ( err ) . WithFields ( BasicLoggingFields ) . Error ( "Failed to fetch Anthrove posts" )
2024-08-27 07:15:17 +00:00
return existingPosts , err
}
2024-08-27 07:49:20 +00:00
span . AddEvent ( "Fetched Anthrove posts" , trace . WithAttributes ( attribute . Int ( "post_count" , len ( existingPosts ) ) ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . WithField ( "post_count" , len ( existingPosts ) ) . Info ( "Fetched Anthrove posts" )
2024-08-27 07:15:17 +00:00
return existingPosts , nil
}
func getAlreadyFavoritesPostIDs ( ctx context . Context , gorm * gorm . DB , existingPostIDs [ ] models . PostID , userSourceID models . UserSourceID ) ( [ ] models . PostID , error ) {
2024-08-27 07:49:20 +00:00
ctx , span := tracer . Start ( ctx , "getAlreadyFavoritesPostIDs" )
defer span . End ( )
2024-08-27 07:15:17 +00:00
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . Info ( "Starting getAlreadyFavoritesPostIDs" )
2024-08-27 07:56:52 +00:00
2024-08-27 07:49:20 +00:00
var existingFavPostIDS [ ] models . PostID
2024-08-27 07:15:17 +00:00
err := gorm . WithContext ( ctx ) . Model ( & models . UserFavorite { } ) . Select ( "post_id" ) . Find ( & existingFavPostIDS , "user_source_id = ? AND post_id IN ?" , userSourceID , existingPostIDs ) . Error
if err != nil {
2024-08-27 07:49:20 +00:00
span . RecordError ( err )
span . SetStatus ( codes . Error , err . Error ( ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithError ( err ) . WithFields ( BasicLoggingFields ) . Error ( "Failed to fetch already favorite post IDs" )
2024-08-27 07:15:17 +00:00
return existingFavPostIDS , err
}
2024-08-27 07:49:20 +00:00
span . AddEvent ( "Fetched already favorite post IDs" , trace . WithAttributes ( attribute . Int ( "fav_post_count" , len ( existingFavPostIDS ) ) ) )
2024-08-27 08:04:55 +00:00
log . WithContext ( ctx ) . WithFields ( BasicLoggingFields ) . WithField ( "fav_post_count" , len ( existingFavPostIDS ) ) . Info ( "Fetched already favorite post IDs" )
2024-08-27 07:15:17 +00:00
return existingFavPostIDS , nil
}