Code

Store a separate last_update timestamp for metric stores.
[sysdb.git] / src / frontend / query.c
index 9a157fb892d10888e35a74c84d77d5a18e635599..5d00636dd99e77219303c645bdf019f8ac59bfeb 100644 (file)
 /*
  * metric fetcher:
  * Implements the callbacks necessary to read a metric object.
- * TODO: FETCH should allow to ignore child elements (attributes); then, we'd
- * only need store_host/store_metric.
  */
 
 typedef struct {
        char *type;
        char *id;
+       sdb_time_t last_update;
 } metric_store_t;
 
 static int
@@ -72,20 +71,14 @@ metric_fetcher_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
 
        st->type = strdup(metric->store.type);
        st->id = strdup(metric->store.id);
+       st->last_update = metric->store.last_update;
        if ((! st->type) || (! st->id))
                return -1;
        return 0;
 } /* metric_fetcher_metric */
 
-static int
-metric_fetcher_attr(sdb_store_attribute_t __attribute__((unused)) *attr,
-               sdb_object_t __attribute__((unused)) *user_data)
-{
-       return 0;
-} /* metric_fetcher_attr */
-
 static sdb_store_writer_t metric_fetcher = {
-       metric_fetcher_host, NULL, metric_fetcher_metric, metric_fetcher_attr,
+       metric_fetcher_host, NULL, metric_fetcher_metric, NULL,
 };
 
 /*
@@ -165,6 +158,7 @@ exec_store(sdb_ast_store_t *st, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
                snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
                metric_store.type = st->store_type;
                metric_store.id = st->store_id;
+               metric_store.last_update = st->last_update;
                status = sdb_plugin_store_metric(st->hostname, st->name,
                                &metric_store, st->last_update);
                break;
@@ -231,7 +225,7 @@ exec_store(sdb_ast_store_t *st, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
 static int
 exec_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
 {
-       metric_store_t st = { NULL, NULL };
+       metric_store_t st = { NULL, NULL, 0 };
        sdb_object_wrapper_t obj = SDB_OBJECT_WRAPPER_STATIC(&st);
        sdb_ast_fetch_t fetch = SDB_AST_FETCH_INIT;
        sdb_timeseries_opts_t opts = { 0, 0 };
@@ -257,7 +251,13 @@ exec_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *buf, sdb_strbuf_t *errbu
        }
        if (status >= 0) {
                series = sdb_plugin_fetch_timeseries(st.type, st.id, &opts);
-               if (! series) {
+               if (series) {
+                       uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES);
+                       sdb_strbuf_memcpy(buf, &res_type, sizeof(res_type));
+                       sdb_timeseries_tojson(series, buf);
+                       sdb_timeseries_destroy(series);
+               }
+               else {
                        sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series '%s/%s' "
                                        "- %s fetcher callback returned no data for '%s'",
                                        ts->hostname, ts->metric, st.type, st.id);
@@ -265,11 +265,6 @@ exec_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *buf, sdb_strbuf_t *errbu
                }
        }
 
-       if (status >= 0) {
-               sdb_timeseries_tojson(series, buf);
-               sdb_timeseries_destroy(series);
-       }
-
        free(fetch.hostname);
        free(fetch.name);
        if (st.type)
@@ -403,8 +398,9 @@ sdb_conn_fetch(sdb_conn_t *conn)
 
        ast = sdb_ast_fetch_create((int)type,
                        hostname[0] ? strdup(hostname) : NULL,
+                       -1, NULL,
                        name[0] ? strdup(name) : NULL,
-                       /* filter = */ NULL);
+                       /* full */ 1, /* filter = */ NULL);
        status = exec_cmd(conn, ast);
        sdb_object_deref(SDB_OBJ(ast));
        return status;
@@ -485,7 +481,7 @@ sdb_conn_lookup(sdb_conn_t *conn)
 int
 sdb_conn_store(sdb_conn_t *conn)
 {
-       sdb_ast_node_t *ast;
+       sdb_ast_node_t *ast = NULL;
        const char *buf = sdb_strbuf_string(conn->buf);
        size_t len = conn->cmd_len;
        uint32_t type;