X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Ffrontend%2Fquery.c;h=52d27ec3c47ba7e7dc940ba552e76b43cf0b38a2;hp=a30bee3e4b8f274730ff650d2ca84cc168fee58d;hb=ee8df3b190c2eb49e460dcf03f81288a5d825c39;hpb=b4d485cde96751e1ec832d0e75a3e6081006a1a4 diff --git a/src/frontend/query.c b/src/frontend/query.c index a30bee3..52d27ec 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -102,7 +102,7 @@ sdb_fe_query(sdb_conn_t *conn) char query[conn->cmd_len + 1]; strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); query[sizeof(query) - 1] = '\0'; - sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %d command%s " + sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s " "in multi-statement query '%s'", sdb_llist_len(parsetree) - 1, sdb_llist_len(parsetree) == 2 ? "" : "s", @@ -187,6 +187,10 @@ sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node) if (CONN_LOOKUP(node)->filter) filter = CONN_LOOKUP(node)->filter->matcher; return sdb_fe_exec_lookup(conn, m, filter); + case CONNECTION_TIMESERIES: + return sdb_fe_exec_timeseries(conn, + CONN_TS(node)->hostname, CONN_TS(node)->metric, + &CONN_TS(node)->opts); default: sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd); @@ -201,6 +205,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name, { sdb_strbuf_t *buf; sdb_store_obj_t *host; + uint32_t type = htonl(CONNECTION_FETCH); host = sdb_store_get_host(name); if (! host) { @@ -224,6 +229,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name, return -1; } + sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t)); if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " "host '%s' to JSON", name); @@ -233,7 +239,7 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, const char *name, return -1; } - sdb_connection_send(conn, CONNECTION_OK, + sdb_connection_send(conn, CONNECTION_DATA, (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); sdb_strbuf_destroy(buf); sdb_object_deref(SDB_OBJ(host)); @@ -244,6 +250,7 @@ int sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter) { sdb_strbuf_t *buf; + uint32_t type = htonl(CONNECTION_LIST); buf = sdb_strbuf_create(1024); if (! buf) { @@ -257,6 +264,7 @@ sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter) return -1; } + sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t)); if (sdb_store_tojson(buf, filter, /* flags = */ SDB_SKIP_ALL)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " "store to JSON"); @@ -265,7 +273,7 @@ sdb_fe_exec_list(sdb_conn_t *conn, sdb_store_matcher_t *filter) return -1; } - sdb_connection_send(conn, CONNECTION_OK, + sdb_connection_send(conn, CONNECTION_DATA, (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); sdb_strbuf_destroy(buf); return 0; @@ -276,6 +284,7 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m, sdb_store_matcher_t *filter) { tojson_data_t data = { NULL, filter, 0 }; + uint32_t type = htonl(CONNECTION_LOOKUP); data.buf = sdb_strbuf_create(1024); if (! data.buf) { @@ -289,6 +298,7 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m, return -1; } + sdb_strbuf_memcpy(data.buf, &type, sizeof(uint32_t)); sdb_strbuf_append(data.buf, "["); /* Let the JSON serializer handle the filter instead of the scanner. Else, @@ -304,11 +314,44 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m, sdb_strbuf_append(data.buf, "]"); - sdb_connection_send(conn, CONNECTION_OK, + sdb_connection_send(conn, CONNECTION_DATA, (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf)); sdb_strbuf_destroy(data.buf); return 0; } /* sdb_fe_exec_lookup */ +int +sdb_fe_exec_timeseries(sdb_conn_t *conn, + const char *hostname, const char *metric, + sdb_timeseries_opts_t *opts) +{ + sdb_strbuf_t *buf; + uint32_t type = htonl(CONNECTION_TIMESERIES); + + buf = sdb_strbuf_create(1024); + if (! buf) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create " + "buffer to handle TIMESERIES command: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + return -1; + } + + sdb_strbuf_memcpy(buf, &type, sizeof(uint32_t)); + if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series"); + sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series"); + sdb_strbuf_destroy(buf); + return -1; + } + + sdb_connection_send(conn, CONNECTION_DATA, + (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); + sdb_strbuf_destroy(buf); + return 0; +} /* sdb_fe_exec_timeseries */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */