-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolarMetrics.h
208 lines (185 loc) · 5.32 KB
/
SolarMetrics.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#ifndef SOLAR_IFTTT_PROMETHEUS_H
#define SOLAR_IFTTT_PROMETHEUS_H
#include "automation/sensor/Sensor.h"
#include <Poco/URI.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/RegularExpression.h>
#include <Poco/NumberParser.h>
#include <Poco/Exception.h>
#include <Poco/String.h>
#include <string>
#include <map>
#include <unordered_map>
#include <iostream>
#include <functional>
using namespace Poco;
using namespace std;
namespace Prometheus
{
const RegularExpression METRIC_RE("^(\\w+)([{](.*?),?[}])?\\s+(.*)", 0, true);
const RegularExpression METRIC_ATTRIBS_RE("^\\s*(\\w+)\\s*=\\s*\"((?:[^\"\\\\]|\\\\.)*)\"\\s*,?\\s*", 0, true);
struct Metric
{
string name;
float value;
map<string, string> attributes;
};
class MetricVector : public vector<Metric>
{
public:
float avg()
{
float avg = 0;
for (auto const &metric : *this) {
if ( isnan(metric.value) ) {
return metric.value;
}
avg += metric.value;
}
avg = avg / size();
return avg;
}
float total()
{
float total = 0;
for (auto const &metric : *this) {
if ( isnan(metric.value) ) {
return metric.value;
}
total += metric.value;
}
return total;
}
friend std::ostream &operator<<(std::ostream &os, const MetricVector &metrics)
{
for (auto metric : metrics)
{
os << "\t"
<< "{";
for (auto attr : metric.attributes)
{
os << attr.first << ": " << attr.second << ", ";
}
os << "} " << metric.value << endl;
}
return os;
}
};
class MetricMap : public unordered_map<string, MetricVector>
{
friend std::ostream &operator<<(std::ostream &os, const MetricMap &mm)
{
for (auto metricMapEntry : mm)
{
string metricName = metricMapEntry.first;
os << metricName << ": " << endl;
MetricVector metrics = metricMapEntry.second;
os << metrics;
}
return os;
}
};
class DataSource
{
public:
Poco::URI url;
Prometheus::MetricMap metrics;
Poco::Net::HTTPClientSession session;
string path;
function<bool(const Metric &)> metricFilter;
DataSource(const Poco::URI &url, function<bool(const Metric &)> metricFilter) : url(url),
metricFilter(metricFilter),
session(url.getHost(), url.getPort()),
path(url.getPathAndQuery())
{
if (path.empty())
path = "/";
}
bool parseMetrics(istream &inputStream, MetricMap &metricsMap, function<bool(const Metric &)> &metricFilter)
{
for (string line; getline(inputStream, line);)
{
if (Prometheus::METRIC_RE.match(line))
{
vector<string> captures;
Prometheus::METRIC_RE.split(line, captures);
string strKey(captures[1]);
string strAttributes(captures[3]);
string strVal(captures[4]);
//cout << ">>>" << strKey << "{" << strAttributes << "} " << strVal << endl;
double value = 0;
if (!NumberParser::tryParseFloat(strVal, value) )
{
if ( Poco::toLower(strVal) == "nan" ) {
value = NAN;
} else {
std::cerr << "Failed parsing Prometheus guage '" << strKey << "': " << strVal << endl;
return false;
}
}
RegularExpression::MatchVec matches;
Prometheus::Metric metric;
metric.name = strKey;
metric.value = value;
while (Prometheus::METRIC_ATTRIBS_RE.split(strAttributes, captures))
{
strAttributes = strAttributes.substr(captures[0].length());
metric.attributes[captures[1]] = captures[2];
}
if (metricFilter(metric))
{
metricsMap[strKey].push_back(metric);
}
}
else if (!line.empty() && line[0] != '#')
{
std::cerr << "Failed parsing Prometheus record: " << line << endl;
return false;
}
}
return true;
}
virtual bool loadMetrics()
{
metrics.clear();
try
{
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
session.sendRequest(request);
Poco::Net::HTTPResponse response;
std::istream &rs = session.receiveResponse(response);
if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
{
if (!parseMetrics(rs, metrics, metricFilter))
{
cerr << "FAILED parsing metrics." << endl;
automation::sleep(5000);
return false;
}
}
else
{
cerr << "FAILED retrieving metrics. URL: " << url.toString() << ", reason: " << Poco::Net::HTTPResponse::getReasonForStatus(response.getStatus()) << endl;
automation::sleep(5000);
return false;
}
}
catch (Poco::Exception &ex)
{
cerr << "FAILED loading prometheus metrics." << endl;
cerr << ex.displayText() << endl;
}
return true;
}
friend std::ostream &operator<<(std::ostream &os, const DataSource &ds)
{
os << "DataSource{ URL:" << ds.url.toString() << endl;
os << ds.metrics;
os << "}";
return os;
}
};
}; // namespace Prometheus
#endif //SOLAR_IFTTT_PROMETHEUS_H