From dc4cf3be4238411119cf18de02c205489aa72517 Mon Sep 17 00:00:00 2001 From: David Janowski Date: Mon, 22 May 2023 22:10:05 +0200 Subject: [PATCH] added most types of relationships (HAS_SOURCE, HAS_TAG, IS_FAVORITE) --- main.go | 90 ++++++++++++++++++++++++++++++++++++++-- neo4jAPI/relationship.go | 63 ++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 neo4jAPI/relationship.go diff --git a/main.go b/main.go index c238e6d..f2d1c63 100644 --- a/main.go +++ b/main.go @@ -47,20 +47,38 @@ func main() { } start := time.Now() - uploadTags(favorites, ctx, driver) + //uploadTags(favorites, ctx, driver) elapsed := time.Since(start) log.Printf("upload of Tags took %d", elapsed) start = time.Now() - uploadPosts(favorites, ctx, driver) + //uploadPosts(favorites, ctx, driver) elapsed = time.Since(start) log.Printf("upload of Posts took %d", elapsed) start = time.Now() - uploadSources(favorites, ctx, driver) + //uploadSources(favorites, ctx, driver) elapsed = time.Since(start) log.Printf("upload of Sources took %d", elapsed) + start = time.Now() + // err = makePostToTagRelationship(favorites, ctx, driver) + elapsed = time.Since(start) + log.Printf("upload of Sources took %v", elapsed) + + start = time.Now() + // makeUserToPostRelationship(favorites, user, ctx, driver) + elapsed = time.Since(start) + log.Printf("upload of Sources took %v", elapsed) + + start = time.Now() + err = makePostToSourceRelationship(favorites, ctx, driver) + if err != nil { + log.Fatal(err) + } + elapsed = time.Since(start) + log.Printf("upload of Sources took %v", elapsed) + } func uploadPosts(posts []models.Post, ctx context.Context, driver neo4j.DriverWithContext) error { for _, fav := range posts { @@ -158,3 +176,69 @@ func uploadSources(posts []models.Post, ctx context.Context, driver neo4j.Driver } return nil } + +func makePostToTagRelationship(posts []models.Post, ctx context.Context, driver neo4j.DriverWithContext) error { + + for _, fav := range posts { + id := fav.ID + for _, generalTags := range fav.Tags.General { + log.Printf("PostID: %d has General Tag: %s", id, generalTags) + err := neo4jAPI.PostToTagRelationship(ctx, driver, id, generalTags) + if err != nil { + return err + } + } + for _, characterTags := range fav.Tags.Character { + log.Printf("PostID: %d has Character Tag: %s", id, characterTags) + err := neo4jAPI.PostToTagRelationship(ctx, driver, id, characterTags) + if err != nil { + return err + } + } + for _, copyrightTags := range fav.Tags.Copyright { + log.Printf("PostID: %d has Copyright Tag: %s", id, copyrightTags) + err := neo4jAPI.PostToTagRelationship(ctx, driver, id, copyrightTags) + if err != nil { + return err + } + } + for _, artistTags := range fav.Tags.Artist { + log.Printf("PostID: %d has Artist Tag: %s", id, artistTags) + err := neo4jAPI.PostToTagRelationship(ctx, driver, id, artistTags) + if err != nil { + return err + } + } + } + return nil +} + +func makeUserToPostRelationship(posts []models.Post, user models.E621User, ctx context.Context, driver neo4j.DriverWithContext) error { + + for _, post := range posts { + log.Printf("UserID: %d has PostID: %d in his favorites", user.ID, post.ID) + err := neo4jAPI.UserToPostRelationship(ctx, driver, post.ID, user.ID) + if err != nil { + return err + } + } + + return nil +} + +func makePostToSourceRelationship(posts []models.Post, ctx context.Context, driver neo4j.DriverWithContext) error { + + for _, post := range posts { + postID := post.ID + for _, source := range post.Sources { + log.Printf("PostID: %d has Source URL: %s", postID, source) + err := neo4jAPI.PostToSourceRelationship(ctx, driver, postID, source) + if err != nil { + return err + } + } + + } + + return nil +} diff --git a/neo4jAPI/relationship.go b/neo4jAPI/relationship.go new file mode 100644 index 0000000..75d5efc --- /dev/null +++ b/neo4jAPI/relationship.go @@ -0,0 +1,63 @@ +package neo4jAPI + +import ( + "context" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" +) + +func PostToTagRelationship(ctx context.Context, driver neo4j.DriverWithContext, e621PostID int64, e621Tag string) error { + query := ` + MATCH (p:e621Post {e621PostID: $e621PostID}) + MATCH (t:e621Tag {e621Tag: $e621Tag}) + MERGE (p)-[:HAS_TAG]->(t) + ` + params := map[string]interface{}{ + "e621PostID": e621PostID, + "e621Tag": e621Tag, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + return nil +} + +func PostToSourceRelationship(ctx context.Context, driver neo4j.DriverWithContext, e621PostID int64, sourceURL string) error { + query := ` + MATCH (p:e621Post {e621PostID: $e621PostID}) + MATCH (s:Source {URL: $sourceURL}) + MERGE (p)-[:HAS_SOURCE]->(s) + ` + params := map[string]interface{}{ + "e621PostID": e621PostID, + "sourceURL": sourceURL, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + return nil +} + +func UserToPostRelationship(ctx context.Context, driver neo4j.DriverWithContext, e621PostID int64, e621UserID int64) error { + query := ` + MATCH (p:e621Post {e621PostID: $e621PostID}) + MATCH (u:e621User {e621ID: $e621ID}) + MERGE (u)-[:IS_FAVORITE]->(p) + ` + params := map[string]interface{}{ + "e621PostID": e621PostID, + "e621ID": e621UserID, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return err + } + + return nil +}