summaryrefslogtreecommitdiffstats
path: root/web/api/queries/ses
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--web/api/queries/ses/Makefile.am8
-rw-r--r--web/api/queries/ses/README.md61
-rw-r--r--web/api/queries/ses/ses.c92
-rw-r--r--web/api/queries/ses/ses.h17
4 files changed, 178 insertions, 0 deletions
diff --git a/web/api/queries/ses/Makefile.am b/web/api/queries/ses/Makefile.am
new file mode 100644
index 0000000..161784b
--- /dev/null
+++ b/web/api/queries/ses/Makefile.am
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+AUTOMAKE_OPTIONS = subdir-objects
+MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
+
+dist_noinst_DATA = \
+ README.md \
+ $(NULL)
diff --git a/web/api/queries/ses/README.md b/web/api/queries/ses/README.md
new file mode 100644
index 0000000..c279701
--- /dev/null
+++ b/web/api/queries/ses/README.md
@@ -0,0 +1,61 @@
+<!--
+title: "Single (or Simple) Exponential Smoothing (`ses`)"
+custom_edit_url: https://github.com/netdata/netdata/edit/master/web/api/queries/ses/README.md
+-->
+
+# Single (or Simple) Exponential Smoothing (`ses`)
+
+> This query is also available as `ema` and `ewma`.
+
+An exponential moving average (`ema`), also known as an exponentially weighted moving average (`ewma`)
+is a first-order infinite impulse response filter that applies weighting factors which decrease
+exponentially. The weighting for each older datum decreases exponentially, never reaching zero.
+
+In simple terms, this is like an average value, but more recent values are given more weight.
+
+Netdata automatically adjusts the weight (`alpha`) based on the number of values processed,
+using the formula:
+
+```
+window = max(number of values, 15)
+alpha = 2 / (window + 1)
+```
+
+You can change the fixed value `15` by setting in `netdata.conf`:
+
+```
+[web]
+ ses max window = 15
+```
+
+## how to use
+
+Use it in alarms like this:
+
+```
+ alarm: my_alarm
+ on: my_chart
+lookup: ses -1m unaligned of my_dimension
+ warn: $this > 1000
+```
+
+`ses` does not change the units. For example, if the chart units is `requests/sec`, the exponential
+moving average will be again expressed in the same units.
+
+It can also be used in APIs and badges as `&group=ses` in the URL.
+
+## Examples
+
+Examining last 1 minute `successful` web server responses:
+
+- ![](https://registry.my-netdata.io/api/v1/badge.svg?chart=web_log_nginx.response_statuses&options=unaligned&dimensions=success&group=min&after=-60&label=min)
+- ![](https://registry.my-netdata.io/api/v1/badge.svg?chart=web_log_nginx.response_statuses&options=unaligned&dimensions=success&group=average&after=-60&label=average&value_color=yellow)
+- ![](https://registry.my-netdata.io/api/v1/badge.svg?chart=web_log_nginx.response_statuses&options=unaligned&dimensions=success&group=ses&after=-60&label=single+exponential+smoothing&value_color=orange)
+- ![](https://registry.my-netdata.io/api/v1/badge.svg?chart=web_log_nginx.response_statuses&options=unaligned&dimensions=success&group=max&after=-60&label=max)
+
+## References
+
+- <https://en.wikipedia.org/wiki/Moving_average#exponential-moving-average>
+- <https://en.wikipedia.org/wiki/Exponential_smoothing>.
+
+[![analytics](https://www.google-analytics.com/collect?v=1&aip=1&t=pageview&_s=1&ds=github&dr=https%3A%2F%2Fgithub.com%2Fnetdata%2Fnetdata&dl=https%3A%2F%2Fmy-netdata.io%2Fgithub%2Fweb%2Fapi%2Fqueries%2Fses%2FREADME&_u=MAC~&cid=5792dfd7-8dc4-476b-af31-da2fdb9f93d2&tid=UA-64295674-3)](<>)
diff --git a/web/api/queries/ses/ses.c b/web/api/queries/ses/ses.c
new file mode 100644
index 0000000..772505f
--- /dev/null
+++ b/web/api/queries/ses/ses.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "ses.h"
+
+
+// ----------------------------------------------------------------------------
+// single exponential smoothing
+
+struct grouping_ses {
+ calculated_number alpha;
+ calculated_number alpha_other;
+ calculated_number level;
+ size_t count;
+};
+
+static size_t max_window_size = 15;
+
+void grouping_init_ses(void) {
+ long long ret = config_get_number(CONFIG_SECTION_WEB, "ses max window", (long long)max_window_size);
+ if(ret <= 1) {
+ config_set_number(CONFIG_SECTION_WEB, "ses max window", (long long)max_window_size);
+ }
+ else {
+ max_window_size = (size_t) ret;
+ }
+}
+
+static inline calculated_number window(RRDR *r, struct grouping_ses *g) {
+ (void)g;
+
+ calculated_number points;
+ if(r->group == 1) {
+ // provide a running DES
+ points = r->internal.points_wanted;
+ }
+ else {
+ // provide a SES with flush points
+ points = r->group;
+ }
+
+ return (points > max_window_size) ? max_window_size : points;
+}
+
+static inline void set_alpha(RRDR *r, struct grouping_ses *g) {
+ // https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
+ // A commonly used value for alpha is 2 / (N + 1)
+ g->alpha = 2.0 / (window(r, g) + 1.0);
+ g->alpha_other = 1.0 - g->alpha;
+}
+
+void *grouping_create_ses(RRDR *r) {
+ struct grouping_ses *g = (struct grouping_ses *)callocz(1, sizeof(struct grouping_ses));
+ set_alpha(r, g);
+ g->level = 0.0;
+ return g;
+}
+
+// resets when switches dimensions
+// so, clear everything to restart
+void grouping_reset_ses(RRDR *r) {
+ struct grouping_ses *g = (struct grouping_ses *)r->internal.grouping_data;
+ g->level = 0.0;
+ g->count = 0;
+}
+
+void grouping_free_ses(RRDR *r) {
+ freez(r->internal.grouping_data);
+ r->internal.grouping_data = NULL;
+}
+
+void grouping_add_ses(RRDR *r, calculated_number value) {
+ struct grouping_ses *g = (struct grouping_ses *)r->internal.grouping_data;
+
+ if(calculated_number_isnumber(value)) {
+ if(unlikely(!g->count))
+ g->level = value;
+
+ g->level = g->alpha * value + g->alpha_other * g->level;
+ g->count++;
+ }
+}
+
+calculated_number grouping_flush_ses(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr) {
+ struct grouping_ses *g = (struct grouping_ses *)r->internal.grouping_data;
+
+ if(unlikely(!g->count || !calculated_number_isnumber(g->level))) {
+ *rrdr_value_options_ptr |= RRDR_VALUE_EMPTY;
+ return 0.0;
+ }
+
+ return g->level;
+}
diff --git a/web/api/queries/ses/ses.h b/web/api/queries/ses/ses.h
new file mode 100644
index 0000000..603fdb5
--- /dev/null
+++ b/web/api/queries/ses/ses.h
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#ifndef NETDATA_API_QUERIES_SES_H
+#define NETDATA_API_QUERIES_SES_H
+
+#include "../query.h"
+#include "../rrdr.h"
+
+extern void grouping_init_ses(void);
+
+extern void *grouping_create_ses(RRDR *r);
+extern void grouping_reset_ses(RRDR *r);
+extern void grouping_free_ses(RRDR *r);
+extern void grouping_add_ses(RRDR *r, calculated_number value);
+extern calculated_number grouping_flush_ses(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr);
+
+#endif //NETDATA_API_QUERIES_SES_H