-
Notifications
You must be signed in to change notification settings - Fork 1
/
influxdb.cpp
132 lines (113 loc) · 2.99 KB
/
influxdb.cpp
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
/*
* FogLAMP InfluxDB north plugin.
*
* Copyright (c) 2020 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Mark Riddoch
*/
#include <influxdb.h>
#include <unistd.h>
#include <logger.h>
#include <chrono>
using namespace std;
using namespace influxdb;
InfluxDBPlugin::InfluxDBPlugin() : m_connected(false)
{
}
InfluxDBPlugin::~InfluxDBPlugin()
{
}
/**
* Send the readings to the InfluxDB channel
*
* @param readings The Readings to send
* @return The number of readings sent
*/
uint32_t
InfluxDBPlugin::send(const vector<Reading *> readings)
{
ostringstream payload;
int sent = 0;
try
{
if (!m_connected)
{
try {
if ((m_influxdb = influxdb::InfluxDBFactory::Get(getURL())) == NULL)
{
Logger::getLogger()->fatal("Unable to connect to influxDB server");
return 0;
}
m_connected = true;
} catch (const runtime_error& re) {
Logger::getLogger()->fatal("Failed to connect to influxdb: %s %s", getURL().c_str(), re.what());
return 0;
} catch (exception &e) {
Logger::getLogger()->fatal("Failed to connect to influxdb: %s", e.what());
return 0;
}
Logger::getLogger()->info("Connected to %s", getURL().c_str());
m_influxdb->batchOf(100);
}
for (auto it = readings.cbegin(); it != readings.cend(); ++it)
{
string assetName = (*it)->getAssetName();
auto point = Point(assetName);
struct timeval tv;
(*it)->getUserTimestamp(&tv);
chrono::time_point<chrono::system_clock> timestamp;
timestamp = chrono::system_clock::time_point{chrono::seconds{tv.tv_sec} + chrono::milliseconds{tv.tv_usec / 1000}};
point.setTimestamp(timestamp);
vector<Datapoint *> datapoints = (*it)->getReadingData();
for (auto dit = datapoints.cbegin(); dit != datapoints.cend();
++dit)
{
string name = (*dit)->getName();
if ((*dit)->getData().getType() == DatapointValue::dataTagType::T_INTEGER)
{
// sending data as integers generates some errors
//point.addField(name.c_str(), (int)((*dit)->getData().toInt()));
point.addField(name.c_str(), (double)((*dit)->getData().toInt()));
}
else if ((*dit)->getData().getType() == DatapointValue::dataTagType::T_FLOAT)
{
point.addField(name.c_str(), (*dit)->getData().toDouble());
}
else
{
point.addField(name.c_str(), (*dit)->getData().toString().c_str());
}
}
m_influxdb->write(forward<Point&&>(point));
sent++;
}
m_influxdb->flushBuffer();
}
catch (const std::exception &e)
{
// the write operation raises an error only at the end of the block defined with batchOf
sent = 0;
Logger::getLogger()->error("Error while sending data %s" , e.what());
}
return sent;
}
string InfluxDBPlugin::getURL()
{
string rval = "http://";
if (m_username.compare(""))
{
rval += m_username;
rval += ":";
rval += m_password;
rval += "@";
}
rval += m_host;
rval += ":";
rval += m_port;
rval += "/?db=";
rval += m_db;
Logger::getLogger()->info("db is %s, URL %s", m_db.c_str(), rval.c_str());
return rval;
}