Code

parser: Let the TIMESERIES command accept optional data-source names.
[sysdb.git] / src / core / timeseries.c
1 /*
2  * SysDB - src/core/timeseries.c
3  * Copyright (C) 2014-2016 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/timeseries.h"
34 #include "utils/strings.h"
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
40 /*
41  * public API
42  */
44 sdb_timeseries_info_t *
45 sdb_timeseries_info_create(size_t data_names_len, const char * const *data_names)
46 {
47         sdb_timeseries_info_t *ts_info;
49         ts_info = calloc(1, sizeof(*ts_info));
50         if (! ts_info)
51                 return NULL;
53         if (stringv_copy(&ts_info->data_names, &ts_info->data_names_len,
54                                 data_names, data_names_len)) {
55                 sdb_timeseries_info_destroy(ts_info);
56                 return NULL;
57         }
58         return ts_info;
59 } /* sdb_timeseries_info_create */
61 void
62 sdb_timeseries_info_destroy(sdb_timeseries_info_t *ts_info)
63 {
64         if (! ts_info)
65                 return;
67         stringv_free(&ts_info->data_names, &ts_info->data_names_len);
68         free(ts_info);
69 } /* sdb_timeseries_info_destroy */
71 sdb_timeseries_t *
72 sdb_timeseries_create(size_t data_names_len, const char * const *data_names,
73                 size_t data_len)
74 {
75         sdb_timeseries_t *ts;
76         size_t i;
78         ts = calloc(1, sizeof(*ts));
79         if (! ts)
80                 return NULL;
82         if (stringv_copy(&ts->data_names, &ts->data_names_len,
83                                 data_names, data_names_len)) {
84                 sdb_timeseries_destroy(ts);
85                 return NULL;
86         }
88         ts->data = calloc(data_names_len, sizeof(*ts->data));
89         if (! ts->data) {
90                 sdb_timeseries_destroy(ts);
91                 return NULL;
92         }
93         for (i = 0; i < data_names_len; ++i) {
94                 ts->data[i] = calloc(data_len, sizeof(**ts->data));
95                 if (! ts->data[i]) {
96                         sdb_timeseries_destroy(ts);
97                         return NULL;
98                 }
99         }
100         ts->data_len = data_len;
101         return ts;
102 } /* sdb_timeseries_create */
104 void
105 sdb_timeseries_destroy(sdb_timeseries_t *ts)
107         size_t i;
109         if (! ts)
110                 return;
112         if (ts->data) {
113                 for (i = 0; i < ts->data_names_len; ++i) {
114                         if (ts->data[i])
115                                 free(ts->data[i]);
116                         ts->data[i] = NULL;
117                 }
118                 free(ts->data);
119         }
120         ts->data = NULL;
121         ts->data_len = 0;
123         stringv_free(&ts->data_names, &ts->data_names_len);
124         free(ts);
125 } /* sdb_timeseries_destroy */
127 int
128 sdb_timeseries_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
130         char start_str[64];
131         char end_str[64];
133         size_t i;
135         if ((! ts) || (! buf))
136                 return -1;
138         /* TODO: make time format configurable */
139         if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
140                 snprintf(start_str, sizeof(start_str), "<error>");
141         start_str[sizeof(start_str) - 1] = '\0';
142         if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
143                 snprintf(end_str, sizeof(end_str), "<error>");
144         end_str[sizeof(end_str) - 1] = '\0';
146         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
147                         start_str, end_str);
149         for (i = 0; i < ts->data_names_len; ++i) {
150                 size_t j;
151                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
153                 for (j = 0; j < ts->data_len; ++j) {
154                         char time_str[64];
156                         if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
157                                 snprintf(time_str, sizeof(time_str), "<error>");
158                         time_str[sizeof(time_str) - 1] = '\0';
160                         /* Some GNU libc versions may print '-nan' which we dont' want */
161                         if (isnan(ts->data[i][j].value))
162                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
163                                                 "\"value\": \"nan\"}", time_str);
164                         else
165                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
166                                                 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
168                         if (j < ts->data_len - 1)
169                                 sdb_strbuf_append(buf, ",");
170                 }
172                 if (i < ts->data_names_len - 1)
173                         sdb_strbuf_append(buf, "],");
174                 else
175                         sdb_strbuf_append(buf, "]");
176         }
177         sdb_strbuf_append(buf, "}}");
178         return 0;
179 } /* sdb_timeseries_tojson */
181 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */