Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (87 loc) · 3.5 KB
/
Copy pathmain.cpp
File metadata and controls
104 lines (87 loc) · 3.5 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
#include "../lib/Helpers.h"
int main()
{
std::cout << getLogPath() << "\n";
std::ofstream logFile(getLogPath() + "/status.log", std::fstream::app);
Tee tee(std::cout, logFile);
TeeStream log(tee);
log << "NEW LOG INSERT!\n" << std::flush;
logCurrentTime(log);
//Set the max time interval for each select statement
const int minuteFetchIncrement = 60;
try
{
sql::Driver* driver = get_driver_instance();
std::unique_ptr<sql::Connection> dirtyCon(driver->connect(getDirtyConnectionString(), sourceDbUsername, sourceDbPassword));
std::unique_ptr<sql::Connection> cleanCon(driver->connect(getCleanConnectionString(), destDbUsername, destDbPassword));
dirtyCon->setSchema("vestsiden");
cleanCon->setSchema("vestsiden");
//Earliest sensor reading in dirty db
const uint64_t totalInsertLowerLimit = getInsertLowerLimit(cleanCon);
/* const uint64_t totalInsertLowerLimit = 1605657541000;//getInsertLowerLimit(cleanCon); */
if(totalInsertLowerLimit == 0)
{
log << "Lower timestamp from clean db is equal to 0. Use bulk insert instead\n";
return 0;
}
log << "Got lower limit for file:\n";
printTimeFromMillis(totalInsertLowerLimit, log);
//Ten minutes ago at runtime
const uint64_t totalInsertUpperLimit = getInsertUpperLimit();
log << "Got upper limit for file:\n";
printTimeFromMillis(totalInsertUpperLimit, log);
//Dont want to select too many rows at the time, so we need fill the file incrementally
uint64_t currentLower = totalInsertLowerLimit;
//Convert the max time interval to millis
const uint64_t incrementValue = minuteFetchIncrement * 60 * 1000;
uint64_t currentUpper = currentLower + incrementValue;
if(currentUpper > totalInsertUpperLimit)
{
currentUpper = totalInsertUpperLimit;
}
//Get sensors for translating sensorname to ID
const auto sensors = getSensorMap(cleanCon);
int totalLinesWritten = 0;
log << "Starting fetch and insert.." << std::flush;
//Loop until the file has been filled with all rows from dirty db
while(currentLower < totalInsertUpperLimit)
{
auto t1 = std::chrono::high_resolution_clock::now();
log << "Fetching data..\n\tLower bound: " << currentLower << "\n\tUpper bound: " << currentUpper << "\n";
auto data = getLatestDirtyData(dirtyCon, currentLower, currentUpper);
//Insert clean data into database.
log << "Inserting data..\n";
int linesWritten = insertCleanData(cleanCon, data, sensors);
totalLinesWritten += linesWritten;
log << "Lines written: " << linesWritten << "\n";
//Increment timestamps for select
currentLower = currentUpper;
currentUpper += incrementValue;
//We dont want to read more than the total limit
if(currentUpper > totalInsertUpperLimit)
{
currentUpper = totalInsertUpperLimit;
}
//Print time loop took
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>( t2 - t1 ).count();
log << "Iteration took: " << duration << "seconds\n";
log << "Total lines written: " << totalLinesWritten << "\n" << std::flush;
}
log << "Done, cleaning up..\n";
}
catch(sql::SQLException& e)
{
log << "# ERR: SQLException in " << __FILE__;
log << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl;
log << "# ERR: " << e.what();
log << " (MySQL error code: " << e.getErrorCode();
log << ", SQLState: " << e.getSQLState() << " )" << std::endl;
logCurrentTime(log);
log << "Exit on error\n\n\n";
return -1;
}
logCurrentTime(log);
log << "Clean exit\n\n\n";
return 0;
}