-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodeprovider.go
1122 lines (927 loc) · 32.4 KB
/
nodeprovider.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Copyright 2022 Anders Håål
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/golang/gddo/httputil/header"
"github.com/gomodule/redigo/redis"
"github.com/gorilla/mux"
"github.com/mitchellh/mapstructure"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
rg "github.com/redislabs/redisgraph-go"
"github.com/segmentio/ksuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
length int
}
func (lrw *loggingResponseWriter) WriteHeader(code int) {
lrw.statusCode = code
lrw.ResponseWriter.WriteHeader(code)
}
func (lrw *loggingResponseWriter) Write(b []byte) (int, error) {
if lrw.statusCode == 0 {
lrw.statusCode = http.StatusOK
}
n, err := lrw.ResponseWriter.Write(b)
lrw.length += n
return n, err
}
var version = "undefined"
func main() {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", ExporterName)
fmt.Printf("Version %s\n", version)
flag.PrintDefaults()
}
SetDefaultValues()
flag.Int("p", viper.GetInt("port"), "The port to start on")
logFile := flag.String("logfile", viper.GetString("logfile"), "Set log file, default stdout")
logFormat := flag.String("logformat", viper.GetString("logformat"), "Set log format to text or json, default json")
config := flag.String("config", viper.GetString("config"), "Set configuration file, default config.yaml")
usage := flag.Bool("u", false, "Show usage")
writeConfig := flag.Bool("default", false, "Write default config")
flag.Parse()
log.SetFormatter(&log.JSONFormatter{})
if *logFormat == "text" {
log.SetFormatter(&log.TextFormatter{})
}
viper.SetConfigName(*config) // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.nodegraph-provider")
viper.AddConfigPath("/usr/local/etc/nodegraph-provider")
viper.AddConfigPath("/etc/nodegraph-provider")
if *usage {
flag.Usage()
os.Exit(0)
}
if *logFile != "" {
f, err := os.OpenFile(*logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
log.SetOutput(f)
}
if *writeConfig {
err := viper.WriteConfigAs("./nodeprovider_default_config.yaml")
if err != nil {
log.Error("Can not write default config file - ", err)
}
os.Exit(0)
}
// Find and read the config file
err := viper.ReadInConfig()
if err != nil {
log.Error("Configuration file not valid - ", err)
os.Exit(1)
}
// Read the graph schema
var graphs = map[string]map[string][]interface{}{}
err = viper.UnmarshalKey("graph_schemas", &graphs)
if err != nil {
log.Error("Unable to decode node and edge fields into struct - ", err)
os.Exit(1)
}
// Get all fields as map used for validation
var nodeFields = make(map[string]map[string]string)
var edgeFields = make(map[string]map[string]string)
for graphName, graph := range graphs {
nodes := make(map[string]string)
for _, fieldInterface := range graph["node_fields"] {
field := Field{}
err = mapstructure.Decode(fieldInterface, &field)
if err != nil {
log.Error("Unable parse node fields into struct - ", err)
os.Exit(1)
}
nodes[field.FieldName] = field.Type
}
nodeFields[graphName] = nodes
edges := make(map[string]string)
for _, fieldInterface := range graph["edge_fields"] {
field := Field{}
err = mapstructure.Decode(fieldInterface, &field)
if err != nil {
log.Error("Unable parse node fields into struct - ", err)
os.Exit(1)
}
edges[field.FieldName] = field.Type
}
edgeFields[graphName] = edges
}
// Read the redis connection configuration
var redisConnection = RedisConnection{}
redisConnection.Host = viper.GetString("redis.host")
redisConnection.Port = viper.GetString("redis.port")
redisConnection.DB = viper.GetString("redis.db")
redisConnection.MaxActive = viper.GetInt("redis.max_active")
redisConnection.MaxIdle = viper.GetInt("redis.max_idle")
var pool *redis.Pool
// Redis connection pool
pool = &redis.Pool{
MaxIdle: redisConnection.MaxIdle,
MaxActive: redisConnection.MaxActive,
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", redisConnection.Host, redisConnection.Port))
if err != nil {
log.Printf("ERROR: fail init redis pool: %s", err.Error())
os.Exit(1)
}
return conn, err
},
}
allConfig := AllConfig{
AllGraphs: graphs,
NodeFields: nodeFields,
EdgeFields: edgeFields,
RedisConnection: redisConnection,
RedisPool: pool,
}
handler := &HandlerInit{allConfig}
// Create a Prometheus histogram for response time of the exporter
responseTime := promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: MetricsPrefix + "request_duration_seconds",
Help: "Histogram of the time (in seconds) each request took to complete.",
Buckets: []float64{0.050, 0.100, 0.200, 0.500, 0.800, 1.00, 2.000, 3.000},
},
[]string{"url", "method", "status"},
)
promHandler := &PrometheusInit{responseTime}
// Setup handler for routes
setupRoutes(handler, promHandler)
log.Info(fmt.Sprintf("%s starting on port %d", ExporterName, viper.GetInt("port")))
log.Info(fmt.Sprintf("connecting to redis at %s:%s on db %s using pool max active %v and max idle %v", redisConnection.Host, redisConnection.Port, redisConnection.DB, redisConnection.MaxActive, redisConnection.MaxIdle))
s := &http.Server{
ReadTimeout: viper.GetDuration("httpserver.read_timeout") * time.Second,
WriteTimeout: viper.GetDuration("httpserver.write_timeout") * time.Second,
Addr: ":" + strconv.Itoa(viper.GetInt("port")),
}
log.Fatal(s.ListenAndServe())
}
func setupRoutes(handler *HandlerInit, promHandler *PrometheusInit) {
rtr := mux.NewRouter()
// Route handlers for Node Graph API Datasource Plugin
// the graph path must be part of the data source url like http://localhost:9393/micro
// where micro is the redis key to the graph model
rtr.HandleFunc("/{graph:.+}/api/graph/data", handler.getData).Methods("GET")
rtr.HandleFunc("/{graph:.+}/api/graph/fields", handler.getFields).Methods("GET")
rtr.HandleFunc("/{graph:.+}/api/health", handler.getHealth).Methods("GET")
// Routes to the create and update of nodes and edges
// Node
rtr.HandleFunc("/api/nodes/{graph:.+}", handler.nodes).Methods("POST")
rtr.HandleFunc("/api/nodes/{graph:.+}/{id:.+}", handler.nodes).Methods("PUT")
rtr.HandleFunc("/api/nodes/{graph:.+}/{id:.+}", handler.nodes).Methods("DELETE")
rtr.HandleFunc("/api/nodes/{graph:.+}/{id:.+}", handler.nodes).Methods("GET")
// Edge
rtr.HandleFunc("/api/edges/{graph:.+}", handler.edges).Methods("POST")
rtr.HandleFunc("/api/edges/{graph:.+}/{source_id:.+}/{target_id:.+}", handler.edges).Methods("PUT")
rtr.HandleFunc("/api/edges/{graph:.+}/{source_id:.+}/{target_id:.+}", handler.edges).Methods("DELETE")
rtr.HandleFunc("/api/edges/{graph:.+}/{source_id:.+}/{target_id:.+}", handler.edges).Methods("GET")
// Graph
rtr.HandleFunc("/api/graphs/{graph:.+}", handler.createGraph).Methods("POST")
rtr.HandleFunc("/api/graphs/{graph:.+}", handler.deleteGraph).Methods("DELETE")
rtr.Use(logcall)
rtr.Use(promHandler.promMonitor)
http.Handle("/", rtr)
// Setup handler for exporter metrics
http.Handle("/metrics", promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
// Opt into OpenMetrics to support exemplars.
EnableOpenMetrics: true,
},
))
}
type HandlerInit struct {
AllConfig AllConfig
}
func (h HandlerInit) deleteGraph(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
// Get the name of the graph model
name := params["graph"]
conn := h.AllConfig.RedisPool.Get()
defer conn.Close()
// Check if the graph key exists, if not return 404
exists, _ := conn.Do("DEL", name)
if exists == int64(0) {
sendStatus(w, fmt.Sprintf("No data exists for graph %s", name), http.StatusNotFound)
return
}
}
func (h HandlerInit) createGraph(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
// Get the graphName of the graph model
graphName := params["graph"]
decoder := json.NewDecoder(r.Body)
bodyJsonMap := make(map[string][]interface{})
err := decoder.Decode(&bodyJsonMap)
if err != nil {
log.WithFields(log.Fields{
"object": "graph",
"graph": graphName,
"requestid": r.Context().Value("requestid"),
"error": "Not a valid json",
}).Warn("Create failed")
sendStatus(w, fmt.Sprintf("Not a valid json: %v", err), http.StatusBadRequest)
return
}
// Check if nodes and edges is in the body
_, ok := bodyJsonMap["nodes"]
if !ok {
sendStatus(w, fmt.Sprintf("No nodes in the json"), http.StatusBadRequest)
return
}
_, ok = bodyJsonMap["edges"]
if !ok {
sendStatus(w, fmt.Sprintf("No edges in the json"), http.StatusBadRequest)
return
}
var nodes []*rg.Node
// Map the name of the node to the Node
var nodesMap = make(map[interface{}]*rg.Node)
// Validate fields
for _, nodeData := range bodyJsonMap["nodes"] {
switch value := nodeData.(type) {
case map[string]interface{}:
node := rg.Node{Label: "Node", Properties: value}
nodes = append(nodes, &node)
nodesMap[value["id"]] = &node
default:
sendStatus(w, fmt.Sprintf("Nodes are not correct format"), http.StatusBadRequest)
return
}
}
var edges []*rg.Edge
for _, edgeData := range bodyJsonMap["edges"] {
switch value := edgeData.(type) {
case map[string]interface{}:
_, ok := value["source"]
if !ok {
sendStatus(w, fmt.Sprintf("Create edge failed, missing source"), http.StatusBadRequest)
return
}
_, ok = value["target"]
if !ok {
sendStatus(w, fmt.Sprintf("Create edge failed, missing target"), http.StatusBadRequest)
return
}
properties := make(map[string]interface{})
for k, v := range value {
if k != "target" && k != "source" {
properties[k] = v
}
}
source := nodesMap[value["source"]]
target := nodesMap[value["target"]]
edge := rg.Edge{Source: source, Destination: target, Relation: "Edge", Properties: properties}
edges = append(edges, &edge)
default:
sendStatus(w, fmt.Sprintf("Edge are not correct format"), http.StatusBadRequest)
return
}
}
conn := h.AllConfig.RedisPool.Get()
defer conn.Close()
graph := rg.GraphNew(graphName, conn)
err = graph.Delete()
if err != nil {
log.WithFields(log.Fields{
"object": "graph",
"graph": graphName,
"requestid": r.Context().Value("requestid"),
"error": err,
}).Info("Delete graph - not exists")
}
for _, node := range nodes {
graph.AddNode(node)
}
for _, edge := range edges {
err := graph.AddEdge(edge)
if err != nil {
log.WithFields(log.Fields{
"object": "graph",
"graph": graphName,
"requestid": r.Context().Value("requestid"),
"error": err,
}).Error("Add edge failed")
sendStatus(w, fmt.Sprintf("Add edge failed %s - %v", graphName, err), http.StatusServiceUnavailable)
return
}
}
graph.Commit()
/*
According to the Commit method error should be returned but not
if err != nil {
log.WithFields(log.Fields{
"object": "graph",
"graph": graphName,
"requestid": r.Context().Value("requestid"),
"error": err,
}).Error("Commit nodes and edges failed")
sendStatus(w, fmt.Sprintf("Commit nodes and edges failed %s - %v", graphName, err), http.StatusServiceUnavailable)
return
}
*/
log.WithFields(log.Fields{
"object": "graph",
"graph": graphName,
"requestid": r.Context().Value("requestid"),
}).Info("Created graph")
sendStatus(w, fmt.Sprintf("Create graph %s", graphName), http.StatusCreated)
return
}
// getFields returns the fields name and data type for nodes and edges.
// The returned data is in the format according to the nodegraph-api datasource
// https://github.com/hoptical/nodegraph-api-plugin#fetch-graph-fields
func (h HandlerInit) getFields(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
params := mux.Vars(r)
name := params["graph"]
var nodeFields []interface{}
for _, fields := range h.AllConfig.AllGraphs[name]["node_fields"] {
values := fields.(map[interface{}]interface{})
nodeField := map[string]interface{}{}
for k, v := range values {
nodeField[k.(string)] = v
}
nodeFields = append(nodeFields, nodeField)
}
var edgeFields []interface{}
for _, fields := range h.AllConfig.AllGraphs[name]["edge_fields"] {
values := fields.(map[interface{}]interface{})
edgeField := map[string]interface{}{}
for k, v := range values {
edgeField[k.(string)] = v
}
edgeFields = append(edgeFields, edgeField)
}
response := make(map[string]interface{})
response["edges_fields"] = edgeFields
response["nodes_fields"] = nodeFields
bodyText, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(bodyText)
return
}
// getData return the nodes and edges based on the WHERE clause in the query parameter.
// The returned data is in the format according to the nodegraph-api datasource
// https://github.com/hoptical/nodegraph-api-plugin#fetch-graph-data
// The query parameter must be a valid chyper WHERE clause expression, e.g.
// query=source.title+%3D+%27bookinfo%2Fproductpage%27+and+rel.mainStat+%3E+1
// The query expression should not include WHERE and must be url encoded.
// If the query parameter is not set all nodes and edges in the graph is returned.
// Source nodes must be named source, target nodes must be named target and edges must
// be named edge.
func (h HandlerInit) getData(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
params := mux.Vars(r)
// Get the name of the graph model
name := params["graph"]
whereClause := ""
whereQuery, ok := r.URL.Query()["query"]
if ok && len(whereQuery) == 1 && len(whereQuery[0]) > 0 {
whereClause = fmt.Sprintf("WHERE %s", whereQuery[0])
}
conn := h.AllConfig.RedisPool.Get()
defer conn.Close()
// Check if the graph key exists, if not return 404
exists, _ := conn.Do("EXISTS", name)
if exists == int64(0) {
sendStatus(w, fmt.Sprintf("No data exists for graph %s", name), http.StatusNotFound)
return
}
graph := rg.GraphNew(name, conn)
// Get edges
query := fmt.Sprintf("Match (source:Node)-[edge:Edge]->(target:Node) %s Return source,edge,target", whereClause)
result, err := graph.Query(query)
//result.PrettyPrint()
if err != nil {
log.WithFields(log.Fields{
"object": "edges",
"requestid": r.Context().Value("requestid"),
"error": err,
}).Error("Get edges")
sendStatus(w, fmt.Sprintf("Get edge data failed %v", err), http.StatusInternalServerError)
return
}
//result.PrettyPrint()
var edges []interface{}
nodesMap := make(map[interface{}]interface{})
for result.Next() { // Next returns true until the iterator is depleted.
edge := make(map[string]interface{})
res := result.Record()
// Get source node
source := res.GetByIndex(0).(*rg.Node)
if _, ok := nodesMap[source.GetProperty("id")]; !ok {
node := make(map[string]interface{})
for key, value := range source.Properties {
// Add check that correct to field
node[key] = value
}
nodesMap[source.GetProperty("id")] = node
}
// Get target
target := res.GetByIndex(2).(*rg.Node)
if _, ok := nodesMap[target.GetProperty("id")]; !ok {
node := make(map[string]interface{})
for key, value := range target.Properties {
// Add check that correct to field
node[key] = value
}
nodesMap[target.GetProperty("id")] = node
}
// Get edge
edgeData := res.GetByIndex(1).(*rg.Edge)
for key, value := range edgeData.Properties {
edge[key] = value
}
edge["source"] = source.GetProperty("id")
edge["target"] = target.GetProperty("id")
edge["id"] = fmt.Sprintf("%s:%s", source.GetProperty("id"), target.GetProperty("id"))
edges = append(edges, edge)
}
// Process the find nodes
var nodes []interface{}
for _, value := range nodesMap {
nodes = append(nodes, value)
}
response := make(map[string]interface{})
response["edges"] = edges
response["nodes"] = nodes
bodyText, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(bodyText)
return
}
// getHealth is used by the data source to verify connection
func (h HandlerInit) getHealth(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
params := mux.Vars(r)
name := params["graph"]
bodyText := fmt.Sprintf("API is working well! Using graph %s\n", name)
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte(bodyText))
return
}
// nodes manage POST, PUT, DELETE and GET for node objects
func (h HandlerInit) nodes(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
// Get the name of the graph model
name := params["graph"]
if h.validateHeader(w, r) {
return
}
conn := h.AllConfig.RedisPool.Get()
defer conn.Close()
graph := rg.GraphNew(name, conn)
// POST node
if r.Method == http.MethodPost {
properties, done := h.validateJsonBody(w, r, h.AllConfig.NodeFields, name)
if done {
return
}
query := fmt.Sprintf("MATCH (n:Node {id: '%s'}) RETURN n", properties["id"])
result, err := graph.Query(query)
//result.PrettyPrint()
if err != nil {
log.WithFields(log.Fields{
"object": "node",
"requestid": r.Context().Value("requestid"),
"nodeid": properties["id"],
"error": err,
}).Error("Failed to check if node exist")
sendStatus(w, fmt.Sprintf("Failed to create node %s", properties["id"]), http.StatusInternalServerError)
return
}
if result.Empty() {
node := rg.Node{Label: "Node", Properties: properties}
graph.AddNode(&node)
_, err := graph.Commit()
if err != nil {
log.WithFields(log.Fields{
"object": "node",
"requestid": r.Context().Value("requestid"),
"nodeid": properties["id"],
"error": err,
}).Error("Failed to create node")
sendStatus(w, fmt.Sprintf("Failed to create node %s", properties["id"]), http.StatusInternalServerError)
return
}
log.WithFields(log.Fields{
"object": "node",
"requestid": r.Context().Value("requestid"),
"nodeid": properties["id"],
}).Info("Create")
sendStatus(w, fmt.Sprintf("Create node id %s", properties["id"]), http.StatusCreated)
return
} else {
log.WithFields(log.Fields{
"object": "node",
"requestid": r.Context().Value("requestid"),
"nodeid": properties["id"],
}).Info("Create - already exists")
sendStatus(w, fmt.Sprintf("Node with id %s already exists", properties["id"]), http.StatusConflict)
return
}
}
// Check if the node exists
if r.Method == http.MethodPut || r.Method == http.MethodDelete || r.Method == http.MethodGet {
id := params["id"]
query := fmt.Sprintf("MATCH (n:Node {id: '%s'}) RETURN n", id)
result, err := graph.Query(query)
if err != nil {
log.WithFields(log.Fields{
"object": "node",
"requestid": r.Context().Value("requestid"),
"id": id,
"error": err,
}).Error("Check if node exists")
sendStatus(w, fmt.Sprintf("Check if node exist failed for %s", id), http.StatusInternalServerError)
return
}
if result.Empty() {
sendStatus(w, fmt.Sprintf("Node id %s does not exists", id), http.StatusNotFound)
return
}
}
// Read the path parameters
id := params["id"]
// GET node
if r.Method == http.MethodGet {
query := fmt.Sprintf("MATCH (n:Node {id: '%s'}) RETURN n", id)
result, err := graph.Query(query)
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"id": id,
"error": err,
}).Error("Delete")
sendStatus(w, fmt.Sprintf("Get failed for %s", id), http.StatusInternalServerError)
return
}
var resp = rg.Node{}
for result.Next() {
record := result.Record()
resp = *(record.Values()[0]).(*rg.Node)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(resp.Properties)
return
}
// PUT node
if r.Method == http.MethodPut {
values := r.URL.Query()
for k := range values {
if _, ok := h.AllConfig.NodeFields[name][k]; !ok {
sendStatus(w, fmt.Sprintf("Create node failed, %s is no a valid property", k), http.StatusBadRequest)
return
}
}
nodeProperties := make([]string, 0, len(values))
for k, v := range values {
nodeProperties = append(nodeProperties, fmt.Sprintf("n.%s = %v", k, v[0]))
}
properties := fmt.Sprintf("%s", strings.Join(nodeProperties, ","))
query := fmt.Sprintf("MATCH (n:Node {id: '%s'}) SET %s RETURN n", id, properties)
_, err := graph.Query(query)
//result.PrettyPrint()
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"id": id,
"error": err,
}).Error("Update")
sendStatus(w, fmt.Sprintf("Update failed for %s", id), http.StatusInternalServerError)
return
}
log.WithFields(log.Fields{
"object": "node",
"requestid": r.Context().Value("requestid"),
"nodeid": id,
"properties": properties,
}).Info("Update")
sendStatus(w, fmt.Sprintf("Update node id %s", id), http.StatusOK)
return
}
// DELETE node
if r.Method == http.MethodDelete {
query := fmt.Sprintf("MATCH (n:Node {id: '%s'}) DELETE n", id)
_, err := graph.Query(query)
//result.PrettyPrint()
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"id": id,
"error": err,
}).Error("Delete")
sendStatus(w, fmt.Sprintf("Delete failed for %s", id), http.StatusInternalServerError)
return
}
log.WithFields(log.Fields{
"object": "node",
"requestid": r.Context().Value("requestid"),
"nodeid": id,
}).Info("Delete")
sendStatus(w, fmt.Sprintf("Delete node id %s", id), http.StatusOK)
return
}
}
// edges manage POST, PUT, DELETE and GET for node objects
func (h HandlerInit) edges(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
// Get the name of the graph model
name := params["graph"]
if h.validateHeader(w, r) {
return
}
conn := h.AllConfig.RedisPool.Get()
defer conn.Close()
graph := rg.GraphNew(name, conn)
// POST edge
if r.Method == http.MethodPost {
bodyJsonMap, done := h.validateJsonBody(w, r, h.AllConfig.EdgeFields, name)
if done {
return
}
query := fmt.Sprintf("MATCH (n:Node)-[r:Edge]->(m:Node) WHERE n.id = '%s' and m.id = '%s' RETURN n,r,m",
bodyJsonMap["source"], bodyJsonMap["target"])
result, err := graph.Query(query)
//result.PrettyPrint()
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"sourceid": bodyJsonMap["source"],
"targetid": bodyJsonMap["target"],
"error": err,
}).Error("Failed to check is edge exist")
sendStatus(w, fmt.Sprintf("Create failed between source id %s and target id %s", bodyJsonMap["source"], bodyJsonMap["target"]), http.StatusInternalServerError)
return
}
if result.Empty() {
edgeProperties := make([]string, 0, len(bodyJsonMap)-2)
for k, v := range bodyJsonMap {
if k != "source" && k != "target" {
// Exclude source_id and target_id
edgeProperties = append(edgeProperties, fmt.Sprintf("%s:%v", k, v))
}
}
properties := fmt.Sprintf("{%s}", strings.Join(edgeProperties, ","))
query := fmt.Sprintf("MATCH (a:Node),(b:Node) WHERE a.id = '%s' AND b.id = '%s' CREATE (a)-[r:Edge %s]->(b) RETURN r",
bodyJsonMap["source"], bodyJsonMap["target"], properties)
result, err := graph.Query(query)
//result.PrettyPrint()
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"sourceid": bodyJsonMap["source"],
"targetid": bodyJsonMap["target"],
"error": err,
}).Error("Failed to create edge")
sendStatus(w, fmt.Sprintf("Create edge failed between source id %s and target id %s", bodyJsonMap["source"], bodyJsonMap["target"]), http.StatusInternalServerError)
return
}
if result.Empty() {
sendStatus(w, fmt.Sprintf("Create edge failed between source id %s and target id %s since some node(s) do not exists", bodyJsonMap["source"], bodyJsonMap["target"]), http.StatusBadRequest)
return
}
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"sourceid": bodyJsonMap["source"],
"targetid": bodyJsonMap["target"],
}).Info("Create")
sendStatus(w, fmt.Sprintf("Create edge sourceid %s and target %s", bodyJsonMap["source"], bodyJsonMap["target"]), http.StatusCreated)
return
} else {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"sourceid": bodyJsonMap["source_id"],
"targetid": bodyJsonMap["target_id"],
}).Info("Create - already exists")
sendStatus(w, fmt.Sprintf("Edge between source id %s and target id %s already exists", bodyJsonMap["source"], bodyJsonMap["target"]), http.StatusConflict)
return
}
}
// Check if the edge exists
if r.Method == http.MethodPut || r.Method == http.MethodDelete || r.Method == http.MethodGet {
sourceId := params["source_id"]
targetId := params["target_id"]
query := fmt.Sprintf("MATCH (n:Node)-[r:Edge]->(m:Node) WHERE n.id = '%s' and m.id = '%s' RETURN r",
sourceId, targetId)
result, err := graph.Query(query)
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"error": err,
}).Error("Check if edge exists")
sendStatus(w, fmt.Sprintf("Check if edge exist failed"), http.StatusInternalServerError)
return
}
//result.PrettyPrint()
if result.Empty() {
sendStatus(w, fmt.Sprintf("Edge between source id %s and target id %s does not exists", sourceId, targetId), http.StatusNotFound)
return
}
}
// Read the path parameters
sourceId := params["source_id"]
targetId := params["target_id"]
// GET edge
if r.Method == http.MethodGet {
query := fmt.Sprintf("MATCH (n:Node)-[r:Edge]->(m:Node) WHERE n.id = '%s' and m.id = '%s' RETURN r", sourceId, targetId)
result, err := graph.Query(query)
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"sourceid": sourceId,
"targetid": targetId,
"error": err,
}).Error("Update")
sendStatus(w, fmt.Sprintf("Update failed between source id %s and target id %s", sourceId, targetId), http.StatusInternalServerError)
return
}
var resp = rg.Edge{}
for result.Next() {
record := result.Record()
resp = *(record.Values()[0]).(*rg.Edge)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(resp.Properties)
return
}
// PUT edge
if r.Method == http.MethodPut {
values := r.URL.Query()
for k := range values {
if _, ok := h.AllConfig.EdgeFields[name][k]; !ok {
sendStatus(w, fmt.Sprintf("Create edge failed, %s is no a valid property", k), http.StatusBadRequest)
return
}
}
edgeProperties := make([]string, 0, len(values))
for k, v := range values {
edgeProperties = append(edgeProperties, fmt.Sprintf("r.%s = %v", k, v[0]))
}
properties := fmt.Sprintf("%s", strings.Join(edgeProperties, ","))
query := fmt.Sprintf("MATCH (n:Node)-[r:Edge]->(m:Node) WHERE n.id = '%s' and m.id = '%s' SET %s RETURN r",
sourceId, targetId, properties)
_, err := graph.Query(query)
//result.PrettyPrint()
if err != nil {
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"sourceid": sourceId,
"targetid": targetId,
"error": err,
}).Error("Update")
sendStatus(w, fmt.Sprintf("Update failed between source id %s and target id %s", sourceId, targetId), http.StatusInternalServerError)
return
}
log.WithFields(log.Fields{
"object": "edge",
"requestid": r.Context().Value("requestid"),
"sourceid": sourceId,
"targetid": targetId,
"properties": properties,
}).Info("Update")
sendStatus(w, fmt.Sprintf("Update edge between source id %s and target id %s", sourceId, targetId), http.StatusOK)
return
}
// DELETE edge
if r.Method == http.MethodDelete {
query := fmt.Sprintf("MATCH (n:Node)-[r:Edge]->(m:Node) WHERE n.id = '%s' and m.id = '%s' DELETE r",
sourceId, targetId)
_, err := graph.Query(query)