Welcome to mirror list, hosted at ThFree Co, Russian Federation.

evaluate_percentage_deviations_in_remainder.c « zbxserver « libs « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c962bc19647f88e80867ea5351364e2ced7e505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "zbxmocktest.h"
#include "zbxmockdata.h"
#include "zbxmockassert.h"
#include "zbxmockutil.h"

#include "valuecache.h"
#include "zbxserver.h"
#include "zbxtrends.h"
#include "zbxparam.h"

#include "mocks/valuecache/valuecache_mock.h"

#include "../../../src/libs/zbxserver/anomalystl.h"
#include "../../../src/libs/zbxserver/evalfunc_common.h"

int	__wrap_substitute_simple_macros(zbx_uint64_t *actionid, const ZBX_DB_EVENT *event, const ZBX_DB_EVENT *r_event,
		zbx_uint64_t *userid, const zbx_uint64_t *hostid, const DC_HOST *dc_host, const DC_ITEM *dc_item,
		DB_ALERT *alert, const DB_ACKNOWLEDGE *ack, const zbx_service_alarm_t *service_alarm,
		const ZBX_DB_SERVICE *service, const char *tz, char **data, int macro_type, char *error,
		int maxerrlen);

int	__wrap_DCget_data_expected_from(zbx_uint64_t itemid, int *seconds);

int	__wrap_substitute_simple_macros(zbx_uint64_t *actionid, const ZBX_DB_EVENT *event, const ZBX_DB_EVENT *r_event,
		zbx_uint64_t *userid, const zbx_uint64_t *hostid, const DC_HOST *dc_host, const DC_ITEM *dc_item,
		DB_ALERT *alert, const DB_ACKNOWLEDGE *ack, const zbx_service_alarm_t *service_alarm,
		const ZBX_DB_SERVICE *service, const char *tz, char **data, int macro_type, char *error,
		int maxerrlen)
{
	ZBX_UNUSED(actionid);
	ZBX_UNUSED(event);
	ZBX_UNUSED(r_event);
	ZBX_UNUSED(userid);
	ZBX_UNUSED(hostid);
	ZBX_UNUSED(dc_host);
	ZBX_UNUSED(dc_item);
	ZBX_UNUSED(alert);
	ZBX_UNUSED(ack);
	ZBX_UNUSED(tz);
	ZBX_UNUSED(data);
	ZBX_UNUSED(macro_type);
	ZBX_UNUSED(error);
	ZBX_UNUSED(maxerrlen);
	ZBX_UNUSED(service_alarm);
	ZBX_UNUSED(service);

	return SUCCEED;
}

int	__wrap_DCget_data_expected_from(zbx_uint64_t itemid, int *seconds)
{
	ZBX_UNUSED(itemid);
	*seconds = zbx_vcmock_get_ts().sec - 600;

	return SUCCEED;
}

void	zbx_mock_test_entry(void **state)
{
	int				start_detect_period, end_detect_period, detect_period_season_shift, err,
					detect_period, start_evaluate_period, end_evaluate_period, evaluate_seconds = 0,
					evaluate_nvalues = 0;
	double				deviations_count, result;
	char				*error = NULL, *evaluate_period = NULL;
	const char			*params, *dev_alg = NULL;
	DC_ITEM				item;
	zbx_vcmock_ds_item_t		*ds_item;
	zbx_timespec_t			ts, ts_evaluate_end;
	zbx_mock_handle_t		handle;
	zbx_vector_history_record_t	values_in;
	zbx_value_type_t		detect_period_season_type;

	/* ZBX_DOUBLE_EPSILON = 0.000001; results into output that is different from python test case output */
	zbx_update_epsilon_to_python_compatible_precision();

	zbx_history_record_vector_create(&values_in);

	err = zbx_vc_init(&error);
	zbx_mock_assert_result_eq("Value cache initialization failed", SUCCEED, err);
	zbx_vc_enable();
	zbx_vcmock_ds_init();
	memset(&item, 0, sizeof(DC_ITEM));
	ds_item = zbx_vcmock_ds_first_item();
	item.itemid = ds_item->itemid;
	item.value_type = ds_item->value_type;

	deviations_count = zbx_mock_get_parameter_float("in.deviations_count");
	dev_alg = zbx_mock_get_parameter_string("in.dev_alg");
	handle = zbx_mock_get_parameter_handle("in");
	zbx_vcmock_set_time(handle, "time");
	ts = zbx_vcmock_get_ts();
	ts_evaluate_end = ts;

	params = zbx_mock_get_parameter_string("in.params");

	if (2 != zbx_num_param(params))
	{
		fail_msg("invalid number of parameters");
		goto out;
	}

	if (SUCCEED != get_function_parameter_str(params, 1, &evaluate_period))
	{
		fail_msg("invalid second parameter");
		goto out;
	}

	if (SUCCEED != zbx_trends_parse_range(ts.sec, evaluate_period, &start_evaluate_period, &end_evaluate_period,
			&error))
	{
		fail_msg("failed to parse seconds parameter: %s", error);
		goto out;
	}

	ts_evaluate_end.sec = end_evaluate_period;
	evaluate_seconds = end_evaluate_period - start_evaluate_period;

	if (SUCCEED != get_function_parameter_hist_range(ts.sec, params, 2, &detect_period, &detect_period_season_type,
			&detect_period_season_shift))
	{
		fail_msg("invalid third parameter");
		goto out;
	}

	start_detect_period = ts_evaluate_end.sec - detect_period;
	end_detect_period = ts_evaluate_end.sec;

	if (FAIL == zbx_vc_get_values(item.itemid, item.value_type, &values_in, evaluate_seconds, evaluate_nvalues,
			&ts_evaluate_end))
	{
		fail_msg("cannot get values from value cache");
		goto out;
	}

	zbx_vc_flush_stats();

	if (0 >= values_in.values_num)
	{
		fail_msg("not enough data");
		goto out;
	}

	if (SUCCEED != zbx_get_percentage_of_deviations_in_stl_remainder(&values_in, deviations_count, dev_alg,
			start_detect_period, end_detect_period, &result, &error))
	{
		fail_msg("zbx_get_percentage_of_deviations_in_stl_remainder returned error: %s\n", error);
		zbx_free(error);
	}
	else
	{
		const char	*expected_value;

		handle = zbx_mock_get_parameter_handle("out.value");

		if (ZBX_MOCK_SUCCESS != (err = zbx_mock_string_ex(handle, &expected_value)))
			fail_msg("Cannot read output value: %s", zbx_mock_error_string(err));

		zbx_mock_assert_double_eq("function result", atof(expected_value), result);
	}
out:
	zbx_history_record_vector_destroy(&values_in, item.value_type);

	zbx_vcmock_ds_destroy();
	zbx_free(evaluate_period);
	ZBX_UNUSED(state);
}