2024-02-16 14:54:36 +00:00
|
|
|
package internal
|
2024-02-16 15:16:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-02-16 20:51:09 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
2024-02-16 15:16:57 +00:00
|
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-02-16 22:20:05 +00:00
|
|
|
func CreateTagNodeWitRelation(ctx context.Context, driver neo4j.DriverWithContext, anthrovePostID models.AnthrovePostID, anthroveTag *models.AnthroveTag) error {
|
2024-02-16 15:16:57 +00:00
|
|
|
query := `
|
|
|
|
MATCH (anthrovePost:AnthrovePost {post_id: $anthrove_post_id})
|
|
|
|
MERGE (tagNode:Tag {name: $tag_name, type: $tag_type})
|
|
|
|
MERGE (anthrovePost)-[:HAS]->(tagNode)
|
|
|
|
`
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"tag_name": anthroveTag.Name,
|
|
|
|
"tag_type": anthroveTag.Type,
|
|
|
|
"anthrove_post_id": anthrovePostID,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_post_id": anthrovePostID,
|
|
|
|
"tag_name": anthroveTag.Name,
|
|
|
|
"tag_type": anthroveTag.Type,
|
|
|
|
}).Trace("graph: created tag node")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-15 13:31:17 +00:00
|
|
|
|
|
|
|
func GetTags(ctx context.Context, driver neo4j.DriverWithContext) ([]models.TagsWithFrequency, error) {
|
|
|
|
var userTags []models.TagsWithFrequency
|
|
|
|
|
|
|
|
query := `
|
|
|
|
MATCH (:AnthrovePost)-[:HAS]->(t:Tag)
|
|
|
|
RETURN t as tag, COUNT(t) AS frequency
|
|
|
|
ORDER BY frequency DESC
|
|
|
|
`
|
|
|
|
params := map[string]any{}
|
|
|
|
|
|
|
|
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Records) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range result.Records {
|
|
|
|
record := result.Records[i]
|
|
|
|
|
|
|
|
tag, _, err := neo4j.GetRecordValue[neo4j.Node](record, "tag")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
frequency, _, err := neo4j.GetRecordValue[int64](record, "frequency")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userTags = append(userTags, models.TagsWithFrequency{
|
|
|
|
Frequency: frequency,
|
|
|
|
Tags: models.AnthroveTag{
|
|
|
|
Name: tag.Props["name"].(string),
|
|
|
|
Type: tag.Props["type"].(string),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"tag_amount": len(userTags),
|
|
|
|
}).Trace("graph: created tag node")
|
|
|
|
|
|
|
|
return userTags, nil
|
|
|
|
}
|