diff --git a/src/frontend/query.c b/src/frontend/query.c
index 4ae4f64a5e0a1373f5ff6ddfb416c277aa546f36..af0aa5424eb7eee46af1fb5561dcb27170275f09 100644 (file)
--- a/src/frontend/query.c
+++ b/src/frontend/query.c
typedef struct {
char *type;
char *id;
+ sdb_time_t last_update;
} metric_store_t;
static int
metric_fetcher_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
{
metric_store_t *st = SDB_OBJ_WRAPPER(user_data)->data;
+ sdb_time_t last_update = 0;
+ size_t idx = 0, i;
- if ((! metric->store.type) || (! metric->store.id))
- return 0;
-
- st->type = strdup(metric->store.type);
- st->id = strdup(metric->store.id);
- if ((! st->type) || (! st->id))
+ if (! metric->stores_num)
return -1;
+
+ /* Find the most up to date data store.
+ * TODO: Consider merging multiple results? */
+ for (i = 0; i < metric->stores_num; ++i) {
+ if (metric->stores[i].last_update > last_update) {
+ last_update = metric->stores[i].last_update;
+ idx = i;
+ }
+ }
+ st->type = strdup(metric->stores[idx].type);
+ st->id = strdup(metric->stores[idx].id);
+ st->last_update = metric->stores[idx].last_update;
return 0;
} /* metric_fetcher_metric */
f = sdb_store_json_formatter(buf, type, flags);
sdb_strbuf_memcpy(buf, &res_type, sizeof(res_type));
- status = sdb_plugin_query(ast, &sdb_store_json_writer, SDB_OBJ(f), errbuf);
+ status = sdb_plugin_query(ast, &sdb_store_json_writer, SDB_OBJ(f),
+ &(sdb_query_opts_t){ true }, errbuf);
if (status < 0)
sdb_strbuf_clear(buf);
sdb_store_json_finish(f);
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->store_last_update;
status = sdb_plugin_store_metric(st->hostname, st->name,
&metric_store, st->last_update);
break;
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 };
@@ -239,7 +251,8 @@ exec_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *buf, sdb_strbuf_t *errbu
opts.end = ts->end;
status = sdb_plugin_query(SDB_AST_NODE(&fetch),
- &metric_fetcher, SDB_OBJ(&obj), errbuf);
+ &metric_fetcher, SDB_OBJ(&obj),
+ &(sdb_query_opts_t){ true }, errbuf);
if ((status < 0) || (! st.type) || (! st.id)) {
sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series '%s/%s' "
"- no data-store configured for the stored metric",
@@ -248,7 +261,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);
@@ -256,11 +275,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)
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;
}
ast = sdb_ast_store_create(SDB_HOST, /* host */ NULL,
/* parent */ 0, NULL, sstrdup(host.name), host.last_update,
- /* metric store */ NULL, NULL, SDB_DATA_NULL);
+ /* metric store */ NULL, NULL, 0, SDB_DATA_NULL);
}
break;
}
ast = sdb_ast_store_create(SDB_SERVICE, sstrdup(svc.hostname),
/* parent */ 0, NULL, sstrdup(svc.name), svc.last_update,
- /* metric store */ NULL, NULL, SDB_DATA_NULL);
+ /* metric store */ NULL, NULL, 0, SDB_DATA_NULL);
}
break;
ast = sdb_ast_store_create(SDB_METRIC, sstrdup(metric.hostname),
/* parent */ 0, NULL, sstrdup(metric.name), metric.last_update,
sstrdup(metric.store_type), sstrdup(metric.store_id),
- SDB_DATA_NULL);
+ metric.store_last_update, SDB_DATA_NULL);
}
break;
}
}
ast = sdb_ast_store_create(SDB_ATTRIBUTE, sstrdup(hostname),
parent_type, sstrdup(parent), sstrdup(attr.key),
- attr.last_update, /* metric store */ NULL, NULL,
+ attr.last_update, /* metric store */ NULL, NULL, 0,
attr.value);
}