Skip to content

Commit f5a406f

Browse files
committed
added aggregation support (server v0.5)
1 parent a7be1ff commit f5a406f

2 files changed

Lines changed: 74 additions & 1 deletion

File tree

keywords.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ inc KEYWORD2
2020
dec KEYWORD2
2121
put_min KEYWORD2
2222
put_max KEYWORD2
23-
datetime KEYWORD2
23+
datetime KEYWORD2
24+
max KEYWORD2
25+
min KEYWORD2
26+
avg KEYWORD2
27+
count KEYWORD2

src/CloudStorage.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class BaseCloudStorage {
128128
);
129129
}
130130

131+
/* Atomic Operations */
132+
131133
//Increment
132134
cloud_storage_utils::ResultWrapper<int> inc(String key, int value = 1) {
133135
return atomic<int>(key, "inc", value);
@@ -154,6 +156,29 @@ class BaseCloudStorage {
154156
cloud_storage_utils::ResultWrapper<String> datetime(String key) {
155157
return atomic<String>(key, "date", "");
156158
}
159+
160+
/* Aggregate Operations */
161+
//Get the minimum item in array
162+
template <class Ty>
163+
cloud_storage_utils::ResultWrapper<Ty> min(String collectionKey) {
164+
return aggregation<Ty>(collectionKey, "min");
165+
}
166+
167+
//Get the maximum item in array
168+
template <class Ty>
169+
cloud_storage_utils::ResultWrapper<Ty> max(String collectionKey) {
170+
return aggregation<Ty>(collectionKey, "max");
171+
}
172+
173+
//Get the average of array
174+
cloud_storage_utils::ResultWrapper<double> avg(String collectionKey) {
175+
return aggregation<double>(collectionKey, "average");
176+
}
177+
178+
//Get the size of array
179+
cloud_storage_utils::ResultWrapper<int> count(String collectionKey) {
180+
return aggregation<int>(collectionKey, "count");
181+
}
157182

158183
private:
159184
String _username, _password;
@@ -284,6 +309,50 @@ class BaseCloudStorage {
284309
getValueByKey<Ty>(root["result"], key)
285310
);
286311
}
312+
313+
// Utility method for constructing generic *Aggregate* request json string
314+
String buildAggregationRequestJson(String collectionKey, String action) {
315+
// Compose request json object
316+
StaticJsonBuffer<200> jsonBuffer;
317+
JsonObject& root = jsonBuffer.createObject();
318+
root["username"] = _username;
319+
root["password"] = _password;
320+
root["collection_key"] = collectionKey;
321+
root["action"] = action;
322+
323+
// Return string form of the object
324+
String jsonString;
325+
root.printTo(jsonString);
326+
return jsonString;
327+
}
328+
329+
330+
//Generic Aggregation Request
331+
template <class Ty>
332+
cloud_storage_utils::ResultWrapper<Ty> aggregation(String key, String action) {
333+
// Build request json
334+
String jsonString = buildAggregationRequestJson(key, action);
335+
336+
// Construct http request
337+
RequestType request(
338+
_baseServerUrl + "/data/collection/aggregate",
339+
http::Method::GET,
340+
jsonString
341+
);
342+
request.addHeader("Content-Type", "application/json; charset=utf-8");
343+
344+
//Execute request
345+
http::Response response = request.execute();
346+
if(response.statusCode != 200) return false;
347+
348+
// Parse response body and extract the wanted value
349+
StaticJsonBuffer<300> jsonBuffer;
350+
JsonObject& root = jsonBuffer.parseObject(response.body);
351+
return cloud_storage_utils::ResultWrapper<Ty>(
352+
!root["error"],
353+
root["result"]
354+
);
355+
}
287356
};
288357

289358
typedef BaseCloudStorage<http::GenericEspRequest> CloudStorage;

0 commit comments

Comments
 (0)