-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler_update_graph.go
187 lines (161 loc) · 5.75 KB
/
handler_update_graph.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
package handlers
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/clems4ever/go-graphkb/internal/client"
"github.com/clems4ever/go-graphkb/internal/knowledge"
"github.com/clems4ever/go-graphkb/internal/metrics"
"github.com/clems4ever/go-graphkb/internal/sources"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/semaphore"
)
func handleUpdate(registry sources.Registry, fn func(source string, body io.Reader) error, sem *semaphore.Weighted, operationDescriptor string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ok, source, err := IsTokenValid(registry, r)
if err != nil {
ReplyWithInternalError(w, err)
return
}
promLabels := prometheus.Labels{
"source": source,
"operation": operationDescriptor,
}
if !ok {
metrics.GraphUpdateRequestsUnauthorizedCounter.
With(promLabels).
Inc()
ReplyWithUnauthorized(w)
return
}
{
ok = sem.TryAcquire(1)
if !ok {
metrics.GraphUpdateRequestsRateLimitedCounter.
With(promLabels).
Inc()
ReplyWithTooManyRequests(w)
return
}
defer sem.Release(1)
metrics.GraphUpdateRequestsReceivedCounter.
With(promLabels).
Inc()
if err = fn(source, r.Body); err != nil {
metrics.GraphUpdateRequestsFailedCounter.
With(promLabels).
Inc()
ReplyWithInternalError(w, err)
return
}
metrics.GraphUpdateRequestsSucceededCounter.
With(promLabels).
Inc()
metrics.LastSuccessfulDatasourceUpdateTimestampGauge.
With(prometheus.Labels{"source": source}).
Set(float64(time.Now().Unix()))
}
_, err = fmt.Fprint(w, "Graph update has been processed")
if err != nil {
ReplyWithInternalError(w, err)
return
}
}
}
// PutSchema upsert an asset into the graph of the data source
func PutSchema(registry sources.Registry, graphUpdater *knowledge.GraphUpdater, sem *semaphore.Weighted) http.HandlerFunc {
return handleUpdate(registry, func(source string, body io.Reader) error {
requestBody := client.PutGraphSchemaRequestBody{}
if err := json.NewDecoder(body).Decode(&requestBody); err != nil {
return err
}
// TODO(c.michaud): verify compatibility of the schema with graph updates
err := graphUpdater.UpdateSchema(source, requestBody.Schema)
if err != nil {
return fmt.Errorf("Unable to update the schema: %v", err)
}
labels := prometheus.Labels{"source": source}
metrics.GraphUpdateSchemaUpdatedCounter.
With(labels).
Inc()
return nil
}, sem, "update_schema")
}
// PutAssets upsert several assets into the graph of the data source
func PutAssets(registry sources.Registry, graphUpdater *knowledge.GraphUpdater, sem *semaphore.Weighted) http.HandlerFunc {
return handleUpdate(registry, func(source string, body io.Reader) error {
requestBody := client.PutGraphAssetRequestBody{}
if err := json.NewDecoder(body).Decode(&requestBody); err != nil {
return err
}
// TODO(c.michaud): verify compatibility of the schema with graph updates
err := graphUpdater.InsertAssets(source, requestBody.Assets)
if err != nil {
return fmt.Errorf("Unable to insert assets: %v", err)
}
labels := prometheus.Labels{"source": source}
metrics.GraphUpdateAssetsInsertedCounter.
With(labels).
Add(float64(len(requestBody.Assets)))
return nil
}, sem, "insert_assets")
}
// PutRelations upsert multiple relations into the graph of the data source
func PutRelations(registry sources.Registry, graphUpdater *knowledge.GraphUpdater, sem *semaphore.Weighted) http.HandlerFunc {
return handleUpdate(registry, func(source string, body io.Reader) error {
requestBody := client.PutGraphRelationRequestBody{}
if err := json.NewDecoder(body).Decode(&requestBody); err != nil {
return err
}
// TODO(c.michaud): verify compatibility of the schema with graph updates
err := graphUpdater.InsertRelations(source, requestBody.Relations)
if err != nil {
return fmt.Errorf("Unable to insert relation: %v", err)
}
labels := prometheus.Labels{"source": source}
metrics.GraphUpdateRelationsInsertedCounter.
With(labels).
Add(float64(len(requestBody.Relations)))
return nil
}, sem, "insert_relations")
}
// DeleteAssets delete multiple assets from the graph of the data source
func DeleteAssets(registry sources.Registry, graphUpdater *knowledge.GraphUpdater, sem *semaphore.Weighted) http.HandlerFunc {
return handleUpdate(registry, func(source string, body io.Reader) error {
requestBody := client.DeleteGraphAssetRequestBody{}
if err := json.NewDecoder(body).Decode(&requestBody); err != nil {
return err
}
// TODO(c.michaud): verify compatibility of the schema with graph updates
err := graphUpdater.RemoveAssets(source, requestBody.Assets)
if err != nil {
return fmt.Errorf("Unable to remove assets: %v", err)
}
labels := prometheus.Labels{"source": source}
metrics.GraphUpdateAssetsDeletedCounter.
With(labels).
Add(float64(len(requestBody.Assets)))
return nil
}, sem, "delete_assets")
}
// DeleteRelations remove multiple relations from the graph of the data source
func DeleteRelations(registry sources.Registry, graphUpdater *knowledge.GraphUpdater, sem *semaphore.Weighted) http.HandlerFunc {
return handleUpdate(registry, func(source string, body io.Reader) error {
requestBody := client.DeleteGraphRelationRequestBody{}
if err := json.NewDecoder(body).Decode(&requestBody); err != nil {
return err
}
// TODO(c.michaud): verify compatibility of the schema with graph updates
err := graphUpdater.RemoveRelations(source, requestBody.Relations)
if err != nil {
return fmt.Errorf("Unable to remove relation: %v", err)
}
labels := prometheus.Labels{"source": source}
metrics.GraphUpdateRelationsDeletedCounter.
With(labels).
Add(float64(len(requestBody.Relations)))
return nil
}, sem, "delete_relations")
}