Coverage Report

Created: 2025-12-31 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntopng/include/DESCounter.h
Line
Count
Source
1
/*
2
 *
3
 * (C) 2013-25 - 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
131k
      : BehaviouralCounter() {
37
131k
    if (ndpi_des_init(&des, alpha, beta, significance) != 0)
38
0
      throw "Error while creating DES";
39
131k
  }
40
41
0
  DESCounter(struct ndpi_des_struct *_des) {
42
0
    memcpy(&des, _des, sizeof(struct ndpi_des_struct));
43
0
  }
44
45
67.4k
  bool addObservation(u_int64_t value) {
46
67.4k
    double forecast, confidence_band;
47
67.4k
    bool rc = (ndpi_des_add_value(&des, value, &forecast, &confidence_band) == 1) ? true : false;
48
67.4k
    double l_forecast = forecast - confidence_band;
49
67.4k
    double h_forecast = forecast + confidence_band;
50
51
67.4k
    last_value = value;
52
67.4k
    last_lower = (u_int64_t)floor(((l_forecast < 0) ? 0 : l_forecast));
53
67.4k
    last_upper = (u_int64_t)round(h_forecast + 0.5);
54
55
67.4k
    if (rc) {
56
0
      is_anomaly =
57
0
          ((value < last_lower) || (value > last_upper)) ? true : false;
58
59
0
      if (is_anomaly) tot_num_anomalies++;
60
61
0
      return (is_anomaly);
62
0
    }
63
64
67.4k
    return (rc);
65
67.4k
  }
66
67
0
  inline void resetStats() { ndpi_des_reset(&des); }
68
0
  inline DESCounter *clone() { return (new (std::nothrow) DESCounter(&des)); }
69
0
  inline void set(DESCounter *c) {
70
0
    memcpy(&des, &c->des, sizeof(struct ndpi_des_struct));
71
0
  }
72
};
73
74
#endif /* _DES_COUNTER_H_ */