1 /*
2 * SysDB - t/integration/mock_timeseries.c
3 * Copyright (C) 2015 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/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
37 #include <stdlib.h>
38 #include <string.h>
40 #define MAGIC_DATA (void *)0x4711
42 SDB_PLUGIN_MAGIC;
44 /*
45 * plugin API
46 */
48 static const char *names[] = { "nameA", "nameB" };
50 static sdb_timeseries_info_t *
51 mock_describe_ts(const char *id, sdb_object_t *user_data)
52 {
53 if (*id != '/') {
54 sdb_log(SDB_LOG_ERR, "mock::timeseries: Invalid time-series %s", id);
55 exit(1);
56 }
58 if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
59 sdb_log(SDB_LOG_ERR, "mock::timeseries: Invalid user data %p "
60 "passed to collect", SDB_OBJ_WRAPPER(user_data)->data);
61 exit(1);
62 }
64 return sdb_timeseries_info_create(SDB_STATIC_ARRAY_LEN(names), names);
65 } /* mock_describe_ts */
67 static sdb_timeseries_t *
68 mock_fetch_ts(const char *id, sdb_timeseries_opts_t *opts,
69 sdb_object_t *user_data)
70 {
71 sdb_timeseries_t *ts;
72 size_t i, j;
74 if (*id != '/') {
75 sdb_log(SDB_LOG_ERR, "mock::timeseries: Invalid time-series %s", id);
76 exit(1);
77 }
79 if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
80 sdb_log(SDB_LOG_ERR, "mock::timeseries: Invalid user data %p "
81 "passed to collect", SDB_OBJ_WRAPPER(user_data)->data);
82 exit(1);
83 }
85 ts = sdb_timeseries_create(SDB_STATIC_ARRAY_LEN(names), names, 10);
86 if (! ts)
87 return NULL;
89 ts->start = opts->start;
90 ts->end = opts->end;
92 for (i = 0; i < 10; ++i) {
93 for (j = 0; j < SDB_STATIC_ARRAY_LEN(names); ++j) {
94 ts->data[j][i].timestamp = ts->start
95 + i * (ts->end - ts->start) / 10;
96 ts->data[j][i].value = (double)(i + j);
97 }
98 }
99 return ts;
100 } /* mock_fetch_ts */
102 static sdb_timeseries_fetcher_t mock_fetcher = {
103 mock_describe_ts, mock_fetch_ts,
104 };
106 int
107 sdb_module_init(sdb_plugin_info_t *info)
108 {
109 sdb_object_t *user_data;
111 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "a mock timeseries fetcher");
112 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
113 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
114 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
115 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
116 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
118 user_data = sdb_object_create_wrapper("mock_data", MAGIC_DATA, NULL);
119 if (! user_data) {
120 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to allocate user data");
121 exit(1);
122 }
123 sdb_plugin_register_timeseries_fetcher("mock", &mock_fetcher, user_data);
124 sdb_object_deref(user_data);
125 return 0;
126 } /* sdb_module_init */
128 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */