This repository was archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage.cpp
More file actions
144 lines (134 loc) · 3.4 KB
/
average.cpp
File metadata and controls
144 lines (134 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* FogLAMP Average notification rule plugin
*
* Copyright (c) 2019 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Mark Riddoch
*/
#include <plugin_api.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <logger.h>
#include <plugin_exception.h>
#include <iostream>
#include <config_category.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <builtin_rule.h>
#include "version.h"
#include "average.h"
using namespace std;
/**
* Average rule constructor
*
* Call parent class BuiltinRule constructor
*/
AverageRule::AverageRule() : BuiltinRule()
{
}
/**
* Average destructor
*/
AverageRule::~AverageRule()
{
}
/**
* Configure the rule plugin
*
* @param config The configuration object to process
*/
void AverageRule::configure(const ConfigCategory& config)
{
// Remove current triggers
// Configuration change is protected by a lock
lockConfig();
if (hasTriggers())
{
removeTriggers();
}
// Release lock
unlockConfig();
string assetName = config.getValue("asset");
addTrigger(assetName, NULL);
m_deviation = strtol(config.getValue("deviation").c_str(), NULL, 10);
m_direction = config.getValue("direction");
string aveType = config.getValue("averageType");
if (aveType.compare("Simple Moving Average") == 0)
{
m_aveType = SMA;
}
else
{
m_aveType = EMA;
}
m_factor = strtol(config.getValue("factor").c_str(), NULL, 10);
for (auto it = m_averages.begin(); it != m_averages.end(); it++)
{
it->second->setAverageType(m_aveType, m_factor);
}
}
/**
* Evaluate a long value to see if the alert should trigger.
* This will also build the value of the average reading using this
* value.
*
* @param asset The asset name we are processing
* @param datapoint The data point we are processing
* @param value The value to consider
* @return true if the value exceeds the defined percentage deviation
*/
bool AverageRule::evaluate(const string& asset, const string& datapoint, long value)
{
return evaluate(asset, datapoint, (double)value);
}
/**
* Evaluate a double value to see if the alert should trigger.
* This will also build the value of the average reading using this
* value.
*
* @param asset The asset name we are processing
* @param datapoint The data point we are processing
* @param value The value to consider
* @return true if the value exceeds the defined percentage deviation
*/
bool AverageRule::evaluate(const string& asset, const string& datapoint, double value)
{
map<string, Averages *>::iterator it;
if ((it = m_averages.find(datapoint)) == m_averages.end())
{
Averages *ave = new Averages();
ave->addValue(value);
ave->setAverageType(m_aveType, m_factor);
m_averages.insert(pair<string, Averages *>(datapoint, ave));
return false;
}
double average = it->second->average();
it->second->addValue(value);
double deviation = ((value - average) * 100) /average;
bool rval = false;
if (m_direction.compare("Both") == 0)
{
rval = fabs(deviation) > m_deviation;
}
else if (m_direction.compare("Above Average") == 0)
{
rval = deviation > m_deviation;
}
else if (m_direction.compare("Below Average") == 0)
{
rval = -deviation > m_deviation;
}
if (rval)
{
Logger::getLogger()->warn("Deviation of %.1f%% in %s.%s triggered alert, value is %.2f, average is %.2f",
deviation,
asset.c_str(),
datapoint.c_str(),
value,
average);
}
return rval;
}