aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuca Deri <deri@ntop.org>2021-02-05 10:59:09 +0100
committerLuca Deri <deri@ntop.org>2021-02-05 10:59:09 +0100
commit60b58dbd67b47b0145c36f3747f83f857905bfbf (patch)
tree0454d63a758c66bea26d8d56dd616dd67a2bcace /src
parent1eedf734be7b2cfe2ac5e0d497c19c77b877ae2f (diff)
RSI enhancements
Diffstat (limited to 'src')
-rw-r--r--src/include/ndpi_api.h.in2
-rw-r--r--src/include/ndpi_typedefs.h2
-rw-r--r--src/lib/ndpi_analyze.c26
3 files changed, 17 insertions, 13 deletions
diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in
index 0d68237a6..d90bcde19 100644
--- a/src/include/ndpi_api.h.in
+++ b/src/include/ndpi_api.h.in
@@ -1370,7 +1370,7 @@ extern "C" {
/* Data analysis */
struct ndpi_analyze_struct* ndpi_alloc_data_analysis(u_int16_t _max_series_len);
void ndpi_init_data_analysis(struct ndpi_analyze_struct *s, u_int16_t _max_series_len);
- void ndpi_free_data_analysis(struct ndpi_analyze_struct *d);
+ void ndpi_free_data_analysis(struct ndpi_analyze_struct *d, u_int8_t free_pointer);
void ndpi_reset_data_analysis(struct ndpi_analyze_struct *d);
void ndpi_data_add_value(struct ndpi_analyze_struct *s, const u_int32_t value);
diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h
index a3910a7df..859a7f0e4 100644
--- a/src/include/ndpi_typedefs.h
+++ b/src/include/ndpi_typedefs.h
@@ -1545,7 +1545,7 @@ struct ndpi_analyze_struct {
/* **************************************** */
struct ndpi_rsi_struct {
- struct ndpi_analyze_struct *gains, *losses;
+ struct ndpi_analyze_struct gains, losses;
u_int32_t last_value;
};
diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c
index 3b1b2557f..32b1a4920 100644
--- a/src/lib/ndpi_analyze.c
+++ b/src/lib/ndpi_analyze.c
@@ -68,9 +68,9 @@ struct ndpi_analyze_struct* ndpi_alloc_data_analysis(u_int16_t _max_series_len)
/* ********************************************************************************* */
-void ndpi_free_data_analysis(struct ndpi_analyze_struct *d) {
+void ndpi_free_data_analysis(struct ndpi_analyze_struct *d, u_int8_t free_pointer) {
if(d->values) ndpi_free(d->values);
- ndpi_free(d);
+ if(free_pointer) ndpi_free(d);
}
/* ********************************************************************************* */
@@ -827,29 +827,33 @@ int ndpi_cluster_bins(struct ndpi_bin *bins, u_int16_t num_bins,
*/
void ndpi_init_rsi(struct ndpi_rsi_struct *s, u_int16_t num_learning_values) {
- s->gains = ndpi_alloc_data_analysis(num_learning_values);
- s->losses = ndpi_alloc_data_analysis(num_learning_values);
+ ndpi_init_data_analysis(&s->gains, num_learning_values);
+ ndpi_init_data_analysis(&s->losses, num_learning_values);
s->last_value = 0;
}
void ndpi_free_rsi(struct ndpi_rsi_struct *s) {
- ndpi_free_data_analysis(s->gains), ndpi_free_data_analysis(s->losses);
+ ndpi_free_data_analysis(&s->gains, 0), ndpi_free_data_analysis(&s->losses, 0);
}
+/*
+ This function adds a new value and returns the computed RSI, or -1
+ if there are too many points (< num_learning_values)
+*/
float ndpi_rsi_add_value(struct ndpi_rsi_struct *s, const u_int32_t value) {
- if(s->gains->num_data_entries == 0)
- ndpi_data_add_value(s->gains, 0), ndpi_data_add_value(s->losses, 0);
+ if(s->gains.num_data_entries == 0)
+ ndpi_data_add_value(&s->gains, 0), ndpi_data_add_value(&s->losses, 0);
else {
if(value > s->last_value)
- ndpi_data_add_value(s->gains, value - s->last_value), ndpi_data_add_value(s->losses, 0);
+ ndpi_data_add_value(&s->gains, value - s->last_value), ndpi_data_add_value(&s->losses, 0);
else
- ndpi_data_add_value(s->losses, s->last_value - value), ndpi_data_add_value(s->gains, 0);
+ ndpi_data_add_value(&s->losses, s->last_value - value), ndpi_data_add_value(&s->gains, 0);
}
s->last_value = value;
- if(s->gains->num_data_entries >= s->gains->num_values_array_len) {
- float relative_strength = ndpi_data_average(s->gains) / ndpi_data_average(s->losses);
+ if(s->gains.num_data_entries >= s->gains.num_values_array_len) {
+ float relative_strength = ndpi_data_window_average(&s->gains) / ndpi_data_window_average(&s->losses);
/* printf("RSI: %f\n", relative_strength); */
return(100. - (100. / (1. + relative_strength)));