added most types of relationships (HAS_SOURCE, HAS_TAG, IS_FAVORITE)

This commit is contained in:
David Janowski 2023-05-22 22:10:05 +02:00
parent 11dac75882
commit dc4cf3be42
2 changed files with 150 additions and 3 deletions

90
main.go
View File

@ -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
}

63
neo4jAPI/relationship.go Normal file
View File

@ -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
}