Skip to content

Commit cf644d6

Browse files
committed
Add 'observed' relation between source and assets.
1 parent 5c1220e commit cf644d6

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

internal/knowledge/graph_updater.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,66 @@ func NewGraphUpdater(graphDB GraphDB, schemaPersistor schema.Persistor) *GraphUp
2525
return &GraphUpdater{graphDB, schemaPersistor}
2626
}
2727

28-
func (sl *GraphUpdater) doUpdate(updates SourceSubGraphUpdates) error {
29-
previousSchema, err := sl.schemaPersistor.LoadSchema(context.Background(), updates.Source)
28+
// Augment the graph of the user with "observed" relation from the source to the each asset
29+
func (sl *GraphUpdater) appendObservedRelations(source string, updates *GraphUpdatesBulk) {
30+
assetsToAdd := []Asset{Asset{Type: "source", Key: source}}
31+
observedRelationsToRemove := []Relation{}
32+
observedRelationsToAdd := []Relation{}
33+
34+
for _, a := range updates.AssetUpserts {
35+
observedRelationsToAdd = append(observedRelationsToAdd, Relation{
36+
Type: "observed",
37+
From: AssetKey(assetsToAdd[0]),
38+
To: AssetKey(a),
39+
})
40+
}
41+
42+
for _, a := range updates.AssetRemovals {
43+
observedRelationsToRemove = append(observedRelationsToRemove, Relation{
44+
Type: "observed",
45+
From: AssetKey(assetsToAdd[0]),
46+
To: AssetKey(a),
47+
})
48+
}
49+
50+
updates.AssetUpserts = append(updates.AssetUpserts, assetsToAdd...)
51+
updates.RelationUpserts = append(updates.RelationUpserts, observedRelationsToAdd...)
52+
updates.RelationRemovals = append(updates.RelationRemovals, observedRelationsToRemove...)
53+
}
54+
55+
func (sl *GraphUpdater) updateSchema(source string, sg *schema.SchemaGraph) error {
56+
for _, a := range sg.Assets() {
57+
sg.AddRelation(schema.AssetType("source"), "observed", a)
58+
}
59+
sg.AddAsset("source")
60+
61+
previousSchema, err := sl.schemaPersistor.LoadSchema(context.Background(), source)
3062
if err != nil {
3163
fmt.Printf("[ERROR] Unable to read schema from DB: %v.\n", err)
3264
fmt.Println("[WARNING] The graph has not been updated.")
3365
return err
3466
}
3567

36-
schemaEqual := previousSchema.Equal(updates.Schema)
68+
schemaEqual := previousSchema.Equal(*sg)
3769

3870
if !schemaEqual {
3971
fmt.Println("The schema needs an update")
40-
if err := sl.schemaPersistor.SaveSchema(context.Background(), updates.Source, updates.Schema); err != nil {
72+
if err := sl.schemaPersistor.SaveSchema(context.Background(), source, *sg); err != nil {
4173
fmt.Printf("[ERROR] Unable to write schema in DB: %v.\n", err)
4274
fmt.Println("[WARNING] The graph has not been updated.")
4375
return err
4476
}
4577
}
78+
return nil
79+
}
80+
81+
func (sl *GraphUpdater) doUpdate(updates SourceSubGraphUpdates) error {
82+
if err := sl.updateSchema(updates.Source, &updates.Schema); err != nil {
83+
return err
84+
}
85+
86+
sl.appendObservedRelations(updates.Source, &updates.Updates)
87+
4688
if err := sl.graphDB.UpdateGraph(updates.Source, &updates.Updates); err != nil {
4789
fmt.Printf("[ERROR] Unable to write schema in graph DB: %v\n", err)
4890
return err

internal/server/server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,20 @@ func postGraphUpdates(graphUpdatesC chan knowledge.SourceSubGraphUpdates) http.H
311311
}
312312
}
313313

314+
func flushDatabase(graphDB knowledge.GraphDB) http.HandlerFunc {
315+
return func(w http.ResponseWriter, r *http.Request) {
316+
if err := graphDB.FlushAll(); err != nil {
317+
replyWithInternalError(w, err)
318+
return
319+
}
320+
321+
if err := graphDB.InitializeSchema(); err != nil {
322+
replyWithInternalError(w, err)
323+
return
324+
}
325+
}
326+
}
327+
314328
// Secret is the secret provider function for basic auth
315329
func Secret(user, realm string) string {
316330
if user == "admin" {
@@ -335,6 +349,7 @@ func StartServer(database knowledge.GraphDB, schemaPersistor schema.Persistor,
335349
getSourceGraphHandler := getSourceGraph(schemaPersistor)
336350
getDatabaseDetailsHandler := getDatabaseDetails(database)
337351
postQueryHandler := postQuery(database)
352+
flushDatabaseHandler := flushDatabase(database)
338353

339354
if viper.GetString("password") != "" {
340355
authenticator := auth.NewBasicAuthenticator("example.com", Secret)
@@ -349,12 +364,15 @@ func StartServer(database knowledge.GraphDB, schemaPersistor schema.Persistor,
349364
getSourceGraphHandler = AuthMiddleware(getSourceGraphHandler)
350365
getDatabaseDetailsHandler = AuthMiddleware(getDatabaseDetailsHandler)
351366
postQueryHandler = AuthMiddleware(postQueryHandler)
367+
flushDatabaseHandler = AuthMiddleware(flushDatabaseHandler)
352368
}
353369

354370
r.HandleFunc("/api/sources", listSourcesHandler).Methods("GET")
355371
r.HandleFunc("/api/schema", getSourceGraphHandler).Methods("GET")
356372
r.HandleFunc("/api/database", getDatabaseDetailsHandler).Methods("GET")
357373

374+
r.HandleFunc("/api/admin/flush", flushDatabaseHandler).Methods("POST")
375+
358376
r.HandleFunc("/api/graph/read", getGraphRead(database)).Methods("GET")
359377
r.HandleFunc("/api/graph/update", postGraphUpdates(graphUpdatesC)).Methods("POST")
360378

0 commit comments

Comments
 (0)