Code

Move the timeseries serialize to the timeseries module.
[sysdb.git] / src / core / timeseries.c
1 /*
2  * SysDB - src/core/timeseries.c
3  * Copyright (C) 2014 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"
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
39 /*
40  * public API
41  */
43 sdb_timeseries_t *
44 sdb_timeseries_create(size_t data_names_len, const char * const *data_names,
45                 size_t data_len)
46 {
47         sdb_timeseries_t *ts;
48         size_t i;
50         ts = calloc(1, sizeof(*ts));
51         if (! ts)
52                 return NULL;
54         ts->data = calloc(data_names_len, sizeof(*ts->data));
55         if (! ts->data) {
56                 sdb_timeseries_destroy(ts);
57                 return NULL;
58         }
59         ts->data_names_len = data_names_len;
60         for (i = 0; i < data_names_len; ++i) {
61                 ts->data[i] = calloc(data_len, sizeof(**ts->data));
62                 if (! ts->data[i]) {
63                         sdb_timeseries_destroy(ts);
64                         return NULL;
65                 }
66         }
67         ts->data_len = data_len;
69         ts->data_names = calloc(data_names_len, sizeof(*ts->data_names));
70         if (! ts->data_names) {
71                 sdb_timeseries_destroy(ts);
72                 return NULL;
73         }
74         for (i = 0; i < data_names_len; ++i) {
75                 ts->data_names[i] = strdup(data_names[i]);
76                 if (! ts->data_names[i]) {
77                         sdb_timeseries_destroy(ts);
78                         return NULL;
79                 }
80         }
81         return ts;
82 } /* sdb_timeseries_create */
84 void
85 sdb_timeseries_destroy(sdb_timeseries_t *ts)
86 {
87         size_t i;
89         if (! ts)
90                 return;
92         if (ts->data) {
93                 for (i = 0; i < ts->data_names_len; ++i) {
94                         if (ts->data[i])
95                                 free(ts->data[i]);
96                         ts->data[i] = NULL;
97                 }
98                 free(ts->data);
99         }
100         ts->data = NULL;
101         ts->data_len = 0;
103         if (ts->data_names) {
104                 for (i = 0; i < ts->data_names_len; ++i) {
105                         if (ts->data_names[i])
106                                 free(ts->data_names[i]);
107                         ts->data_names[i] = NULL;
108                 }
109                 free(ts->data_names);
110         }
111         ts->data_names = NULL;
112         ts->data_names_len = 0;
113         free(ts);
114 } /* sdb_timeseries_destroy */
116 int
117 sdb_timeseries_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
119         char start_str[64];
120         char end_str[64];
122         size_t i;
124         if ((! ts) || (! buf))
125                 return -1;
127         /* TODO: make time format configurable */
128         if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
129                 snprintf(start_str, sizeof(start_str), "<error>");
130         start_str[sizeof(start_str) - 1] = '\0';
131         if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
132                 snprintf(end_str, sizeof(end_str), "<error>");
133         end_str[sizeof(end_str) - 1] = '\0';
135         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
136                         start_str, end_str);
138         for (i = 0; i < ts->data_names_len; ++i) {
139                 size_t j;
140                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
142                 for (j = 0; j < ts->data_len; ++j) {
143                         char time_str[64];
145                         if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
146                                 snprintf(time_str, sizeof(time_str), "<error>");
147                         time_str[sizeof(time_str) - 1] = '\0';
149                         /* Some GNU libc versions may print '-nan' which we dont' want */
150                         if (isnan(ts->data[i][j].value))
151                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
152                                                 "\"value\": \"nan\"}", time_str);
153                         else
154                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
155                                                 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
157                         if (j < ts->data_len - 1)
158                                 sdb_strbuf_append(buf, ",");
159                 }
161                 if (i < ts->data_names_len - 1)
162                         sdb_strbuf_append(buf, "],");
163                 else
164                         sdb_strbuf_append(buf, "]");
165         }
166         sdb_strbuf_append(buf, "}}");
167         return 0;
168 } /* sdb_timeseries_tojson */
169 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */