Repository navigation
Expand file tree
/
Copy pathHelpers.cpp
More file actions
322 lines (272 loc) · 8.08 KB
/
Copy pathHelpers.cpp
File metadata and controls
322 lines (272 loc) · 8.08 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "Helpers.h"
#include <cppconn/prepared_statement.h>
std::vector<Entry> getLatestDirtyData(std::unique_ptr<sql::Connection>& dirtyCon, uint64_t timestampLowerLimit, uint64_t timestampUpperLimit)
{
std::vector<Entry> newData;
std::string query =
"SELECT "
"`TIMESTAMP`, `VALUE`, `HISTORY_ID`, `STATUS` "
"FROM "
"HISTORYNUMERICTRENDRECORD "
"WHERE "
"`TIMESTAMP` >= (?) AND `TIMESTAMP` < (?) ";
//Create prepared statement object
std::unique_ptr<sql::PreparedStatement> prep_stmt(dirtyCon->prepareStatement(query));
//Bind parameters
prep_stmt->setUInt64(1, timestampLowerLimit);
prep_stmt->setUInt64(2, timestampUpperLimit);
//Perform query
std::unique_ptr<sql::ResultSet> res(prep_stmt->executeQuery());
//Fill vector with data
while(res->next())
{
Entry d;
d.timestamp = res->getUInt64("TIMESTAMP");
d.value = res->getDouble("VALUE");
d.sensorName = res->getString("HISTORY_ID");
d.status = res->getInt("STATUS");
newData.push_back(d);
}
return newData;
}
int insertCleanData(std::unique_ptr<sql::Connection>& cleanCon, std::vector<Entry> data, std::map<std::string, int> sensors)
{
//If data is empty, we return immediately.
//Else the empty insert will give an error
if(data.empty())
{
return 0;
}
//Start of query
std::string query =
"INSERT INTO `HISTORYNUMERICTRENDRECORD` "
"(`TIMESTAMP`, `VALUE`, `HISTORY_ID`, `STATUS`) "
"VALUES ";
//Append all the values in data vector to the same query
//This is done to save time
for(auto c : data)
{
//Translate sensorName to sensorId
auto sensorSearch = sensors.find(c.sensorName);
int sensorId;
if(sensorSearch != sensors.end())
{
sensorId = sensorSearch->second;
}
else
{
continue;
}
query += "(";
query += std::to_string(millis_to_seconds(c.timestamp)) + ", ";
query += doubleToScientific(c.value, 5) + ", ";
query += std::to_string(sensorId) + ", ";
query += std::to_string(c.status) + "), ";
}
//Erase the last redundant comma and space
query.erase(query.length()-2);
//Create statement
std::unique_ptr<sql::Statement> stmt(cleanCon->createStatement());
//Perform query
stmt->execute(query);
//Returns number of rows inserted
return data.size();
}
int insertCleanDataInFile(std::vector<Entry> data, std::string filename, std::map<std::string, int> sensors)
{
//Open file in append mode
std::ofstream file(filename, std::fstream::app);
for(auto& c : data)
{
//Find sensor id from sensorname
int sensorId = sensors.at(c.sensorName);
//Insert to file
file << sensorId << ",";
file << millis_to_seconds(c.timestamp) << ",";
file << doubleToScientific(c.value, 5) << ",";
file << c.status << "\n";
}
file.close();
return data.size();
}
std::map<std::string, int> getSensorMap(std::unique_ptr<sql::Connection>& cleanCon)
{
std::string query =
"SELECT "
"`ID`, `NAME` "
"FROM "
"`HISTORY_TYPE_MAP`";
//Create stmt and perform query
std::unique_ptr<sql::Statement> stmt(cleanCon->createStatement());
std::unique_ptr<sql::ResultSet> res(stmt->executeQuery(query));
//Fill a std::map with all sensors
std::map<std::string, int> sensors;
while(res->next())
{
int id = res->getInt("ID");
std::string name = res->getString("NAME");
sensors.insert(std::pair<std::string, int>(name, id));
}
return sensors;
}
uint64_t getInsertUpperLimit()
{
//Get time
auto t = std::chrono::system_clock::now();
//Convert to millis and uint32
uint64_t limit = std::chrono::duration_cast<std::chrono::seconds>(t.time_since_epoch()).count();
//Substract 24 hours
limit -= 24*60*60;
return seconds_to_millis(limit);
}
uint64_t getInsertLowerLimit(std::unique_ptr<sql::Connection>& cleanCon)
{
//Get last entry of cleandb in millis
uint64_t lastEntry = getLastEntryTimestamp(cleanCon);
//Add one second to it, so that we wont try and fetch an already existing row
return lastEntry + 1000;
}
uint64_t getLastEntryTimestamp(std::unique_ptr<sql::Connection>& cleanCon)
{
//Get max timestamp from clean db, if empty return 0
std::string query =
"SELECT "
"MAX(max_ts) AS LastEntry "
"FROM "
"(SELECT "
"HISTORY_ID, MAX(TIMESTAMP) AS max_ts "
"FROM "
"HISTORYNUMERICTRENDRECORD "
"GROUP BY "
"HISTORY_ID) AS max_res";
//Create stmt and perform query
std::unique_ptr<sql::Statement> stmt(cleanCon->createStatement());
std::unique_ptr<sql::ResultSet> res(stmt->executeQuery(query));
uint64_t lastEntry = 0;
while(res->next())
{
lastEntry = res->getUInt64("LastEntry");
}
//CleanDB holds seconds, we want millis
return seconds_to_millis(lastEntry);
}
void printTimeFromMillis(uint64_t epochMillis, TeeStream& log)
{
time_t t = epochMillis/1000;
log << "\t" << epochMillis << "\n\t" << std::put_time(std::localtime(&t), "%Y/%m/%d %T") << "\n";
}
uint64_t seconds_to_millis(uint64_t i)
{
return i * 1000;
}
uint64_t millis_to_seconds(uint64_t i)
{
return (i / 1000);
}
std::string doubleToScientific(double d, int precision)
{
std::ostringstream s;
s << std::scientific;
s << std::setprecision(precision);
s << d;
return s.str();
}
std::string getDirtyConnectionString()
{
return "tcp://" + sourceDbHostNameV6 + ":" + sourceDbPort;
}
std::string getCleanConnectionString()
{
return "tcp://" + destDbHostNameV6 + ":" + destDbPort;
}
int getSensorCountFromDb(std::unique_ptr<sql::Connection>& con)
{
std::string query =
"SELECT "
"COUNT(*) AS CNT "
"FROM "
"`HISTORY_TYPE_MAP`";
//Create stmt and perform query
std::unique_ptr<sql::Statement> stmt(con->createStatement());
std::unique_ptr<sql::ResultSet> res(stmt->executeQuery(query));
res->next();
return res->getInt("CNT");
}
int fillSensorTable(std::unique_ptr<sql::Connection>& dirtyCon, std::unique_ptr<sql::Connection>& cleanCon)
{
//Get count of sensors from dirtyDb
int dirtySensorCount = getSensorCountFromDb(dirtyCon);
//Get count of sensors from cleanDb
int cleanSensorCount = getSensorCountFromDb(cleanCon);
//If sensor counts are equal, there is no need to update
if(dirtySensorCount == cleanSensorCount)
{
return 0;
}
//Extract all sensors from dirty db
std::string query =
"SELECT "
"`ID`, `ID_`, `VALUEFACETS` "
"FROM "
"`HISTORY_TYPE_MAP`";
std::unique_ptr<sql::Statement> stmt(dirtyCon->createStatement());
std::unique_ptr<sql::ResultSet> res(stmt->executeQuery(query));
std::vector<SensorEntry> entries;
while(res->next())
{
SensorEntry s;
s.id = res ->getInt("ID");
s.name = res->getString("ID_");
s.valuefacet = res->getString("VALUEFACETS");
entries.push_back(s);
}
//Sort entries based on ID
std::sort(entries.begin(), entries.end(), sensorEntrySort);
//Insert sensor entries
//Unique constraint in database will handle duplicates.
//Will not throw an error
//Using multiple prepared statements and not multiple insert because of difficulty with escape characters in valuefacets.
for(auto& s : entries)
{
query =
"INSERT IGNORE INTO "
"`HISTORY_TYPE_MAP`"
"(`ID`, `NAME`, `VALUEFACETS`) "
"VALUES "
"(?, ?, ?) ";
std::unique_ptr<sql::PreparedStatement> prep_stmt(cleanCon->prepareStatement(query));
prep_stmt->setInt(1, s.id);
prep_stmt->setString(2, s.name);
prep_stmt->setString(3, s.valuefacet);
prep_stmt->execute();
}
return entries.size();
}
//Has to take epoch in seconds!
void printTimeFromSeconds(uint64_t epoch, TeeStream& log)
{
time_t t = epoch;
log << "\t" << epoch << "\n\t" << std::put_time(std::localtime(&t), "%Y/%m/%d %T") << "\n";
}
bool sensorEntrySort(const SensorEntry& a, const SensorEntry& b)
{
return a.id < b.id;
}
std::string getLogPath()
{
//Get path of executable
char result[ PATH_MAX ];
ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
std::string s( result, (count > 0) ? count : 0 );
//Find position of last /
size_t pos = s .find_last_of('/', s.size());
//Erase name of executable and last slash, so we are left with only the path to the current dir
s.erase(pos, s.size() - pos);
return s;
}
void logCurrentTime(TeeStream& log)
{
auto start = std::chrono::system_clock::now();
std::time_t start_time = std::chrono::system_clock::to_time_t(start);
log << "Time: " << std::ctime(&start_time) << "\n" << std::flush;
}