-
Notifications
You must be signed in to change notification settings - Fork 12
/
log_entry_read_repository.go
61 lines (52 loc) · 2.01 KB
/
log_entry_read_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package repository
import (
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"golang.org/x/net/context"
)
type LogEntryReadRepository interface {
GetById(ctx context.Context, tenant, id string) (*dbtype.Node, error)
}
type logEntryReadRepository struct {
driver *neo4j.DriverWithContext
database string
}
func NewLogEntryReadRepository(driver *neo4j.DriverWithContext, database string) LogEntryReadRepository {
return &logEntryReadRepository{
driver: driver,
database: database,
}
}
func (r *logEntryReadRepository) prepareReadSession(ctx context.Context) neo4j.SessionWithContext {
return utils.NewNeo4jReadSession(ctx, *r.driver, utils.WithDatabaseName(r.database))
}
func (r *logEntryReadRepository) GetById(ctx context.Context, tenant, id string) (*dbtype.Node, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "LogEntryReadRepository.GetById")
defer span.Finish()
tracing.SetNeo4jRepositorySpanTags(span, tenant)
span.SetTag(tracing.SpanTagEntityId, id)
cypher := fmt.Sprintf(`MATCH (l:LogEntry {id:$id}) WHERE l:LogEntry_%s return l`, tenant)
params := map[string]any{
"id": id,
}
span.LogFields(log.String("cypher", cypher))
tracing.LogObjectAsJson(span, "params", params)
session := r.prepareReadSession(ctx)
defer session.Close(ctx)
result, err := session.ExecuteRead(ctx, func(tx neo4j.ManagedTransaction) (any, error) {
queryResult, err := tx.Run(ctx, cypher, params)
return utils.ExtractSingleRecordFirstValueAsNode(ctx, queryResult, err)
})
if err != nil {
span.LogFields(log.Bool("result.found", false))
tracing.TraceErr(span, err)
return nil, err
}
span.LogFields(log.Bool("result.found", result != nil))
return result.(*dbtype.Node), nil
}