Code

integration: Split off mock_timeseries from mock_plugin.
[sysdb.git] / t / integration / mock_timeseries.c
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 sdb_timeseries_t *
49 mock_fetch_ts(const char *id, sdb_timeseries_opts_t *opts,
50                 sdb_object_t *user_data)
51 {
52         sdb_timeseries_t *ts;
53         const char *names[] = { "nameA", "nameB" };
54         size_t i, j;
56         if (*id != '/') {
57                 sdb_log(SDB_LOG_ERR, "mock::timeseries: Invalid time-series %s", id);
58                 exit(1);
59         }
61         if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) {
62                 sdb_log(SDB_LOG_ERR, "mock::timeseries: Invalid user data %p "
63                                 "passed to collect", SDB_OBJ_WRAPPER(user_data)->data);
64                 exit(1);
65         }
67         ts = sdb_timeseries_create(SDB_STATIC_ARRAY_LEN(names), names, 10);
68         if (! ts)
69                 return NULL;
71         ts->start = opts->start;
72         ts->end = opts->end;
74         for (i = 0; i < 10; ++i) {
75                 for (j = 0; j < SDB_STATIC_ARRAY_LEN(names); ++j) {
76                         ts->data[j][i].timestamp = ts->start
77                                 + i * (ts->end - ts->start) / 10;
78                         ts->data[j][i].value = (double)(i + j);
79                 }
80         }
81         return ts;
82 } /* mock_fetch_ts */
84 int
85 sdb_module_init(sdb_plugin_info_t *info)
86 {
87         sdb_object_t *user_data;
89         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "a mock timeseries fetcher");
90         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
91                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
92         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
93         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
94         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
96         user_data = sdb_object_create_wrapper("mock_data", MAGIC_DATA, NULL);
97         if (! user_data) {
98                 sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to allocate user data");
99                 exit(1);
100         }
101         sdb_plugin_register_ts_fetcher("mock", mock_fetch_ts, user_data);
102         sdb_object_deref(user_data);
103         return 0;
104 } /* sdb_module_init */
106 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */