-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* optimized ses and added des * added coefficient of variation * fix bug identified by @vlvkobal: use all available points when resampling is required and the timeframe is not enough for a single point
- Loading branch information
Showing
15 changed files
with
428 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# double exponential smoothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#include "des.h" | ||
|
||
|
||
// ---------------------------------------------------------------------------- | ||
// single exponential smoothing | ||
|
||
struct grouping_des { | ||
calculated_number alpha; | ||
calculated_number alpha_other; | ||
calculated_number beta; | ||
calculated_number beta_other; | ||
|
||
calculated_number level; | ||
calculated_number trend; | ||
|
||
size_t count; | ||
}; | ||
|
||
#define MAX_WINDOW_SIZE 10 | ||
|
||
static inline void set_alpha(RRDR *r, struct grouping_des *g) { | ||
// https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average | ||
// A commonly used value for alpha is 2 / (N + 1) | ||
calculated_number window = (r->group > MAX_WINDOW_SIZE) ? MAX_WINDOW_SIZE : r->group; | ||
|
||
g->alpha = 2.0 / ((calculated_number)window + 1.0); | ||
g->alpha_other = 1.0 - g->alpha; | ||
|
||
//info("alpha for chart '%s' is " CALCULATED_NUMBER_FORMAT, r->st->name, g->alpha); | ||
} | ||
|
||
static inline void set_beta(RRDR *r, struct grouping_des *g) { | ||
// https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average | ||
// A commonly used value for alpha is 2 / (N + 1) | ||
calculated_number window = (r->group > MAX_WINDOW_SIZE) ? MAX_WINDOW_SIZE : r->group; | ||
|
||
g->beta = 2.0 / ((calculated_number)window + 1.0); | ||
g->beta_other = 1.0 - g->beta; | ||
|
||
//info("beta for chart '%s' is " CALCULATED_NUMBER_FORMAT, r->st->name, g->beta); | ||
} | ||
|
||
void *grouping_init_des(RRDR *r) { | ||
struct grouping_des *g = (struct grouping_des *)malloc(sizeof(struct grouping_des)); | ||
set_alpha(r, g); | ||
set_beta(r, g); | ||
g->level = 0.0; | ||
g->trend = 0.0; | ||
g->count = 0; | ||
return g; | ||
} | ||
|
||
// resets when switches dimensions | ||
// so, clear everything to restart | ||
void grouping_reset_des(RRDR *r) { | ||
struct grouping_des *g = (struct grouping_des *)r->grouping_data; | ||
g->level = 0.0; | ||
g->trend = 0.0; | ||
g->count = 0; | ||
|
||
// fprintf(stderr, "\nDES: "); | ||
|
||
} | ||
|
||
void grouping_free_des(RRDR *r) { | ||
freez(r->grouping_data); | ||
r->grouping_data = NULL; | ||
} | ||
|
||
void grouping_add_des(RRDR *r, calculated_number value) { | ||
struct grouping_des *g = (struct grouping_des *)r->grouping_data; | ||
|
||
if(isnormal(value)) { | ||
if(likely(g->count > 0)) { | ||
// we have at least a number so far | ||
|
||
if(unlikely(g->count == 1)) { | ||
// the second value we got | ||
g->trend = value - g->trend; | ||
g->level = value; | ||
} | ||
|
||
// for the values, except the first | ||
calculated_number last_level = g->level; | ||
g->level = (g->alpha * value) + (g->alpha_other * (g->level + g->trend)); | ||
g->trend = (g->beta * (g->level - last_level)) + (g->beta_other * g->trend); | ||
} | ||
else { | ||
// the first value we got | ||
g->level = g->trend = value; | ||
} | ||
|
||
g->count++; | ||
} | ||
|
||
//fprintf(stderr, "value: " CALCULATED_NUMBER_FORMAT ", level: " CALCULATED_NUMBER_FORMAT ", trend: " CALCULATED_NUMBER_FORMAT "\n", value, g->level, g->trend); | ||
} | ||
|
||
calculated_number grouping_flush_des(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr) { | ||
struct grouping_des *g = (struct grouping_des *)r->grouping_data; | ||
|
||
if(unlikely(!g->count || !isnormal(g->level))) { | ||
*rrdr_value_options_ptr |= RRDR_VALUE_EMPTY; | ||
return 0.0; | ||
} | ||
|
||
//fprintf(stderr, " RESULT for %zu values = " CALCULATED_NUMBER_FORMAT " \n", g->count, g->level); | ||
|
||
return g->level; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#ifndef NETDATA_API_QUERIES_DES_H | ||
#define NETDATA_API_QUERIES_DES_H | ||
|
||
#include "../query.h" | ||
#include "../rrdr.h" | ||
|
||
extern void *grouping_init_des(RRDR *r); | ||
extern void grouping_reset_des(RRDR *r); | ||
extern void grouping_free_des(RRDR *r); | ||
extern void grouping_add_des(RRDR *r, calculated_number value); | ||
extern calculated_number grouping_flush_des(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr); | ||
|
||
#endif //NETDATA_API_QUERIES_DES_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,45 @@ | ||
# single exponential smoothing | ||
# 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 based on the number of values processed, using the formula: | ||
|
||
``` | ||
alpha = 2 / (number_of_values + 1) | ||
``` | ||
|
||
## 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&dimensions=success&group=min&after=-60&label=min) | ||
- ![](https://registry.my-netdata.io/api/v1/badge.svg?chart=web_log_nginx.response_statuses&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&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&dimensions=success&group=max&after=-60&label=max) | ||
|
||
## References | ||
|
||
- [https://en.wikipedia.org/wiki/Moving_average#exponential-moving-average](https://en.wikipedia.org/wiki/Moving_average#exponential-moving-average) | ||
- [https://en.wikipedia.org/wiki/Exponential_smoothing](https://en.wikipedia.org/wiki/Exponential_smoothing). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.