summaryrefslogtreecommitdiff
path: root/datastore-leveldb/src/web.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'datastore-leveldb/src/web.cpp')
-rw-r--r--datastore-leveldb/src/web.cpp44
1 files changed, 26 insertions, 18 deletions
diff --git a/datastore-leveldb/src/web.cpp b/datastore-leveldb/src/web.cpp
index efff6b9..125a4ad 100644
--- a/datastore-leveldb/src/web.cpp
+++ b/datastore-leveldb/src/web.cpp
@@ -1,17 +1,14 @@
#include "web.h"
#include "db.h"
#include <algorithm>
-#include <vector>
+#include <chrono>
#include <leveldb/comparator.h>
-#include <iomanip>
static const leveldb::Comparator *cmp = leveldb::BytewiseComparator();
-static inline std::string make_key(uint64_t timestamp) {
- std::stringstream key;
- key << "ts-";
- key << std::setfill('0') << std::setw(20) << timestamp;
- return key.str();
+static inline uint64_t now() {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) {
@@ -21,25 +18,36 @@ static inline bool parse_key(leveldb::Slice &&key, uint64_t *value) {
return true;
}
-
const boost::regex web_handle_api_value_R(
- "/api/value/([a-zA-Z0-9\\.]+)/([0-9]+)");
+ "/api/value/([a-zA-Z0-9\\.]+)");
void web_handle_api_value(const boost::cmatch &match, struct mg_connection *conn) {
std::string sensor(match[1].str());
- uint64_t timestamp = std::stoul(match[2].str());
+ uint64_t timestamp = now();
char buf[1024];
- int count = mg_read(conn, buf, 1024);
- std::string value(buf, count);
+ int bytesRead = mg_read(conn, buf, 1024);
+ std::string value(buf, bytesRead);
- leveldb::DB *db = getDB(sensor);
- if (db == nullptr) {
- std::cout << "failed to get db for " << sensor << std::endl;
+ if (db_insert(sensor, timestamp, value)) {
+ mg_printf(conn, "HTTP/1.1 200 OK Value received\r\n\r\n");
+ } else {
mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n");
- return;
}
+}
- db->Put(leveldb::WriteOptions(), make_key(timestamp), value);
- mg_printf(conn, "HTTP/1.1 200 Value received\r\n\r\n");
+const boost::regex web_handle_api_value_timestamp_R(
+ "/api/value/([a-zA-Z0-9\\.]+)/([0-9]+)");
+void web_handle_api_value_timestamp(const boost::cmatch &match, struct mg_connection *conn) {
+ std::string sensor(match[1].str());
+ uint64_t timestamp = std::stoul(match[2].str());
+ char buf[1024];
+ int bytesRead = mg_read(conn, buf, 1024);
+ std::string value(buf, bytesRead);
+
+ if (db_insert(sensor, timestamp, value)) {
+ mg_printf(conn, "HTTP/1.1 200 OK Value received\r\n\r\n");
+ } else {
+ mg_printf(conn, "HTTP/1.1 500 Internal Error\r\n\r\n");
+ }
}
static inline void print_json_tuple(struct mg_connection *conn,