Skip to content

Commit ce45107

Browse files
committed
Insert and bulk insert actions on timeseries
1 parent c47d0c9 commit ce45107

11 files changed

Lines changed: 394 additions & 140 deletions

File tree

code/src/sixsq/nuvla/db/binding.clj

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,31 +105,6 @@
105105
then a 400 (bad-request) response must be returned. Other appropriate
106106
error codes can also be thrown.")
107107

108-
(add-metric
109-
[this collection-id data options]
110-
"This function adds the given metric to the database. The metric
111-
must not already exist in the database.
112-
113-
On success, the function must return a 201 ring response with the
114-
relative URL of the new metric as the Location.
115-
116-
On failure, the function must throw an ex-info containing the error
117-
ring response. The error must be 409 (conflict) if the metric
118-
exists already. Other appropriate error codes can also be thrown.")
119-
120-
(bulk-insert-metrics
121-
[this collection-id data options]
122-
"This function insert the given metrics in the database where the
123-
collection-id corresponds to the name of a metrics Collection.
124-
125-
On success, the function must return the summary map of what was done
126-
on the db.
127-
128-
On failure, the function must throw an ex-info containing the error
129-
ring response. If the resource-id does not correspond to a Collection,
130-
then a 400 (bad-request) response must be returned. Other appropriate
131-
error codes can also be thrown.")
132-
133108
(bulk-delete
134109
[this collection-id options]
135110
"This function removes the given resources in the database where the
@@ -157,15 +132,39 @@
157132
error codes can also be thrown.")
158133

159134
(create-timeseries
160-
[this timeseries-id options]
161-
"This function creates the given timeseries in the database.")
135+
[this index options]
136+
"This function creates a timeseries with the given index name in the database.")
162137

163138
(retrieve-timeseries
164-
[this timeseries-id]
139+
[this index]
165140
"This function retrieves the identified timeseries from the database.
166141
167142
On success, this returns the clojure map representation of the
168143
timeseries. The response must not be embedded in a ring response.
169144
170145
On failure, this function must throw an ex-info containing the error
171-
ring response. If the resource doesn't exist, use a 404 status."))
146+
ring response. If the resource doesn't exist, use a 404 status.")
147+
148+
(add-timeseries-datapoint
149+
[this index data options]
150+
"This function adds the given timeseries datapoint to the database.
151+
The datapoint with the given timestamp and dimensions must not already exist in the database.
152+
153+
On success, the function must return a 201 ring response with the
154+
relative URL of the new metric as the Location.
155+
156+
On failure, the function must throw an ex-info containing the error
157+
ring response. The error must be 409 (conflict) if the metric
158+
exists already. Other appropriate error codes can also be thrown.")
159+
160+
(bulk-insert-timeseries-datapoints
161+
[this index data options]
162+
"This function insert the given timeseries datapoints in the database.
163+
164+
On success, the function must return the summary map of what was done
165+
on the db.
166+
167+
On failure, the function must throw an ex-info containing the error
168+
ring response. If the resource-id does not correspond to a Collection,
169+
then a 400 (bad-request) response must be returned. Other appropriate
170+
error codes can also be thrown."))

code/src/sixsq/nuvla/db/es/binding.clj

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns sixsq.nuvla.db.es.binding
22
"Binding protocol implemented for an Elasticsearch database that makes use
33
of the Elasticsearch REST API."
4-
(:require [clojure.tools.logging :as log]
4+
(:require [clojure.data.json :as json]
5+
[clojure.tools.logging :as log]
56
[qbits.spandex :as spandex]
67
[sixsq.nuvla.auth.utils.acl :as acl-utils]
78
[sixsq.nuvla.db.binding :refer [Binding]]
@@ -234,13 +235,12 @@
234235
msg (str "unexpected exception querying: " (or error e))]
235236
(throw (r/ex-response msg 500))))))
236237

237-
(defn add-metric-data
238-
[client collection-id data {:keys [refresh]
239-
:or {refresh true}
240-
:as _options}]
238+
(defn add-timeseries-datapoint
239+
[client index data {:keys [refresh]
240+
:or {refresh true}
241+
:as _options}]
241242
(try
242-
(let [index (escu/collection-id->index collection-id)
243-
updated-data (-> data
243+
(let [updated-data (-> data
244244
(dissoc :timestamp)
245245
(assoc "@timestamp" (:timestamp data)))
246246
response (spandex/request client {:url [index :_doc]
@@ -251,20 +251,19 @@
251251
(if success?
252252
{:status 201
253253
:body {:status 201
254-
:message (str collection-id " metric added")}}
255-
(r/response-conflict collection-id)))
254+
:message (str index " metric added")}}
255+
(r/response-conflict index)))
256256
(catch Exception e
257257
(let [{:keys [status body] :as _response} (ex-data e)
258258
error (:error body)]
259259
(if (= 409 status)
260-
(r/response-conflict collection-id)
260+
(r/response-conflict index)
261261
(r/response-error (str "unexpected exception: " (or error e))))))))
262262

263-
(defn bulk-insert-metrics
264-
[client collection-id data _options]
263+
(defn bulk-insert-timeseries-datapoints
264+
[client index data _options]
265265
(try
266-
(let [index (escu/collection-id->index collection-id)
267-
data-transform (fn [{:keys [timestamp] :as doc}]
266+
(let [data-transform (fn [{:keys [timestamp] :as doc}]
268267
(-> doc
269268
(dissoc :timestamp)
270269
(assoc "@timestamp" timestamp)))
@@ -517,12 +516,6 @@
517516
(query-native [_ collection-id query]
518517
(query-data-native client collection-id query))
519518

520-
(add-metric [_ collection-id data options]
521-
(add-metric-data client collection-id data options))
522-
523-
(bulk-insert-metrics [_ collection-id data options]
524-
(bulk-insert-metrics client collection-id data options))
525-
526519
(bulk-delete [_ collection-id options]
527520
(bulk-delete-data client collection-id options))
528521

@@ -535,6 +528,13 @@
535528
(retrieve-timeseries [_ timeseries-id]
536529
(retrieve-timeseries-impl client timeseries-id))
537530

531+
(add-timeseries-datapoint [_ index data options]
532+
(add-timeseries-datapoint client index data options))
533+
534+
(bulk-insert-timeseries-datapoints [_ index data options]
535+
(bulk-insert-timeseries-datapoints client index data options))
536+
537+
538538
Closeable
539539
(close [_]
540540
(when sniffer

code/src/sixsq/nuvla/db/impl.clj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@
4545
(defn query-native [collection-id query]
4646
(p/query-native *impl* collection-id query))
4747

48-
(defn add-metric [collection-id data & [options]]
49-
(p/add-metric *impl* collection-id data options))
50-
51-
(defn bulk-insert-metrics [collection-id data & [options]]
52-
(p/bulk-insert-metrics *impl* collection-id data options))
53-
5448
(defn bulk-delete [collection-id & [options]]
5549
(p/bulk-delete *impl* collection-id options))
5650

5751
(defn bulk-edit [collection-id & [options]]
5852
(p/bulk-edit *impl* collection-id options))
5953

60-
(defn create-timeseries [timeseries-id & [options]]
61-
(p/create-timeseries *impl* timeseries-id options))
54+
(defn create-timeseries [index & [options]]
55+
(p/create-timeseries *impl* index options))
6256

6357
(defn retrieve-timeseries [timeseries-id]
6458
(p/retrieve-timeseries *impl* timeseries-id))
6559

60+
(defn add-timeseries-datapoint [index data & [options]]
61+
(p/add-timeseries-datapoint *impl* index data options))
62+
63+
(defn bulk-insert-timeseries-datapoints [index data & [options]]
64+
(p/bulk-insert-timeseries-datapoints *impl* index data options))
65+
6666
(defn close []
6767
(when-let [^Closeable impl *impl*]
6868
(try

code/src/sixsq/nuvla/server/resources/common/std_crud.clj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[clojure.walk :as w]
99
[sixsq.nuvla.auth.acl-resource :as a]
1010
[sixsq.nuvla.auth.utils :as auth]
11+
[sixsq.nuvla.db.es.common.utils :as escu]
1112
[sixsq.nuvla.db.impl :as db]
1213
[sixsq.nuvla.server.middleware.cimi-params.impl :as impl]
1314
[sixsq.nuvla.server.resources.common.crud :as crud]
@@ -213,23 +214,24 @@
213214
(str "_" resource-name))]
214215
(create-bulk-job action-name resource-name authn-info acl body))))
215216

216-
(defn add-metric-fn
217+
(defn add-timeseries-datapoint-fn
217218
[resource-name collection-acl _resource-uri & {:keys [validate-fn options]}]
218219
(validate-collection-acl collection-acl)
219220
(fn [{:keys [body] :as request}]
220221
(a/throw-cannot-add collection-acl request)
221222
(validate-fn body)
222-
(db/add-metric resource-name body options)))
223+
(db/add-timeseries-datapoint (escu/collection-id->index resource-name)
224+
body options)))
223225

224-
(defn bulk-insert-metrics-fn
225-
[resource-name collection-acl _collection-uri]
226+
(defn bulk-insert-timeseries-datapoints-fn
227+
[index collection-acl _collection-uri]
226228
(validate-collection-acl collection-acl)
227229
(fn [{:keys [body] :as request}]
228230
(throw-bulk-header-missing request)
229231
(a/throw-cannot-add collection-acl request)
230232
(a/throw-cannot-bulk-action collection-acl request)
231233
(let [options (select-keys request [:nuvla/authn :body])
232-
response (db/bulk-insert-metrics resource-name body options)]
234+
response (db/bulk-insert-timeseries-datapoints index body options)]
233235
(r/json-response response))))
234236

235237
(defn generic-bulk-operation-fn

code/src/sixsq/nuvla/server/resources/spec/timeseries.cljc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
(s/def ::field-type
1616
(assoc (st/spec field-types)
17+
:json-schema/type "string"
1718
:json-schema/description "Timeseries field name"))
1819

1920
(s/def ::dimension
@@ -31,13 +32,21 @@
3132

3233
(s/def ::metric-type
3334
(assoc (st/spec metric-types)
35+
:json-schema/type "string"
3436
:json-schema/description "Timeseries metric type"))
3537

38+
(s/def ::optional
39+
(-> (st/spec boolean?)
40+
(assoc :name "optional"
41+
:json-schema/type "boolean"
42+
:json-schema/description "optional value ? (default false)")))
43+
3644
(s/def ::metric
3745
(assoc (st/spec (su/only-keys
3846
:req-un [::field-name
3947
::field-type
40-
::metric-type]))
48+
::metric-type]
49+
:opt-un [::optional]))
4150
:json-schema/type "map"
4251
:json-schema/description "Timeseries metric"))
4352

0 commit comments

Comments
 (0)