Coverage Report

Created: 2024-02-25 06:37

/src/ntopng/include/DESCounter.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * (C) 2013-24 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _DES_COUNTER_H_
23
#define _DES_COUNTER_H_
24
25
#include "ntop_includes.h"
26
27
/* ************************ */
28
29
/* Counter based on Double Exponential smoothing algorithm */
30
class DESCounter : public BehaviouralCounter {
31
 private:
32
  struct ndpi_des_struct des;
33
34
 public:
35
  DESCounter(double alpha = 0.9, double beta = 0.035, float significance = 0.05)
36
99.4k
      : BehaviouralCounter() {
37
99.4k
    if (ndpi_des_init(&des, alpha, beta, significance) != 0)
38
0
      throw "Error while creating DES";
39
99.4k
  }
40
41
0
  DESCounter(struct ndpi_des_struct *_des) {
42
0
    memcpy(&des, _des, sizeof(struct ndpi_des_struct));
43
0
  }
44
45
67.1k
  bool addObservation(u_int64_t value) {
46
67.1k
    double forecast, confidence_band;
47
67.1k
    bool rc =
48
67.1k
        (ndpi_des_add_value(&des, value, &forecast, &confidence_band) == 1)
49
67.1k
            ? true
50
67.1k
            : false;
51
67.1k
    double l_forecast = forecast - confidence_band;
52
67.1k
    double h_forecast = forecast + confidence_band;
53
54
67.1k
    last_value = value;
55
67.1k
    last_lower = (u_int64_t)floor(((l_forecast < 0) ? 0 : l_forecast));
56
67.1k
    last_upper = (u_int64_t)round(h_forecast + 0.5);
57
58
67.1k
    if (rc) {
59
0
      is_anomaly =
60
0
          ((value < last_lower) || (value > last_upper)) ? true : false;
61
62
0
      if (is_anomaly) tot_num_anomalies++;
63
64
0
      return (is_anomaly);
65
0
    }
66
67
67.1k
    return (rc);
68
67.1k
  }
69
70
0
  inline void resetStats() { ndpi_des_reset(&des); }
71
0
  inline DESCounter *clone() { return (new (std::nothrow) DESCounter(&des)); }
72
0
  inline void set(DESCounter *c) {
73
0
    memcpy(&des, &c->des, sizeof(struct ndpi_des_struct));
74
0
  }
75
};
76
77
#endif /* _DES_COUNTER_H_ */