From baeea2369e2feabf67121ffc771d7c29b5ae43f9 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sat, 16 Aug 2014 21:38:21 +0200 Subject: [PATCH] frontend: Let CONNECTION_QUERY support 'TIMESERIES' queries. --- src/frontend/query.c | 35 +++++++++++++++++++++++++++++++ src/include/frontend/connection.h | 15 +++++++++++++ t/integration/simple_query.sh | 3 +++ 3 files changed, 53 insertions(+) diff --git a/src/frontend/query.c b/src/frontend/query.c index a30bee3..e0b7181 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -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); @@ -310,5 +314,36 @@ sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m, 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; + + 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; + } + + 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_OK, + (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 : */ diff --git a/src/include/frontend/connection.h b/src/include/frontend/connection.h index 16ad39a..e963c94 100644 --- a/src/include/frontend/connection.h +++ b/src/include/frontend/connection.h @@ -225,6 +225,21 @@ int sdb_fe_exec_lookup(sdb_conn_t *conn, sdb_store_matcher_t *m, sdb_store_matcher_t *filter); +/* + * sdb_fe_exec_timeseries: + * Execute the 'TIMESERIES' command. Send the time-series for the specified + * host's metric, serialized as JSON, to the client. See + * sdb_store_fetch_timeseries for details. + * + * Returns: + * - 0 on success + * - a negative value else + */ +int +sdb_fe_exec_timeseries(sdb_conn_t *conn, + const char *hostname, const char *metric, + sdb_timeseries_opts_t *opts); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/t/integration/simple_query.sh b/t/integration/simple_query.sh index 4398b2b..a2df1b9 100755 --- a/t/integration/simple_query.sh +++ b/t/integration/simple_query.sh @@ -161,5 +161,8 @@ output="$( run_sysdb -H "$SOCKET_FILE" \ -c "LOOKUP hosts MATCHING attribute.invalid = 'none'" )" echo $output | grep -E '^\[\]$' +run_sysdb -H "$SOCKET_FILE" \ + -c "TIMESERIES 'invalid.host'.'invalid-metric'" && exit 1 + stop_sysdbd -- 2.30.2