Skip to content

Commit 23a888b

Browse files
committed
store sequence start/end/abort and timer in influx
1 parent e11f04d commit 23a888b

File tree

8 files changed

+93
-37
lines changed

8 files changed

+93
-37
lines changed

CMakePresets.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"NO_CANLIB": "ON",
1818
"NO_INFLUX": "ON",
1919
"CMAKE_BUILD_TYPE": "Debug"
20+
},
21+
"environment": {
22+
"ECUI_CONFIG_PATH": "${sourceDir}/sample_config"
2023
}
2124
},
2225
{

include/SequenceManager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class SequenceManager : public Singleton<SequenceManager>
3838
bool GetAutoAbort();
3939
void SetAutoAbort(bool active);
4040

41-
void StartSequence(nlohmann::json jsonSeq, nlohmann::json jsonAbortSeq, std::string comments);
41+
void StartSequence(nlohmann::json jsonSeq, nlohmann::json jsonAbortSeq, std::string comments, std::string sequenceName);
4242
void AbortSequence(std::string abortMsg="abort");
4343

4444
void WritePostSeqComment(std::string msg);
@@ -108,6 +108,7 @@ class SequenceManager : public Singleton<SequenceManager>
108108
nlohmann::json jsonSequence = nlohmann::json::object();
109109
nlohmann::json jsonAbortSequence = nlohmann::json::object();
110110

111+
std::string sequenceName;
111112
std::string comments;
112113
std::string currentDirPath;
113114
std::string logFileName;
@@ -129,5 +130,7 @@ class SequenceManager : public Singleton<SequenceManager>
129130
EventManager *eventManager = nullptr;
130131
FileSystemAbstraction *fileSystem = nullptr;
131132

133+
InfluxDbLogger logger;
134+
132135
std::thread sequenceThread;
133136
};

include/logging/InfluxDbLogger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class InfluxDbLogger
1414
void Init(std::string db_hostname, unsigned db_port, std::string db_name, std::string measurement, timestamp_precision_t precision, std::size_t buffer_size);
1515

1616
void log(std::string source, std::string msg, std::size_t timestamp, Severity severity);
17+
18+
void log(const std::string &key, const std::string &msg, std::size_t timestamp) const;
19+
1720
void log(std::string key, std::size_t value, std::size_t timestamp);
1821
void log(std::string key, double value, std::size_t timestamp);
1922
void log(std::string key, bool value, std::size_t timestamp);

src/LLController.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ void LLController::OnECUISocketRecv(nlohmann::json msg)
122122
//send(sock, strmsg.c_str(), strmsg.size(), 0);
123123
nlohmann::json seq = msg["content"][0];
124124
nlohmann::json abortSeq = msg["content"][1];
125-
seqManager->StartSequence(msg["content"][0], msg["content"][1], msg["content"][2]);
125+
std::string seqName = (msg["content"].size() > 3) ? msg["content"][3].get<std::string>() : "Unknown Sequence";
126+
seqManager->StartSequence(msg["content"][0], msg["content"][1], msg["content"][2], seqName);
126127
}
127128
else if (type.compare("send-postseq-comment") == 0)
128129
{

src/SequenceManager.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ void SequenceManager::Init(Config &config)
8989

9090
isInitialized = true;
9191
fileSystem = FileSystemAbstraction::Instance();
92+
93+
#ifndef NO_INFLUX
94+
logger.Init(config["/INFLUXDB/database_ip"],
95+
config["/INFLUXDB/database_port"],
96+
config["/INFLUXDB/database_name"],
97+
config["/INFLUXDB/sequences_measurement"], MICROSECONDS,
98+
config["/INFLUXDB/buffer_size"]);
99+
#endif
92100
}
93101

94102
bool SequenceManager::GetAutoAbort()
@@ -110,6 +118,9 @@ void SequenceManager::AbortSequence(std::string abortMsg)
110118
if(sequenceRunning)
111119
{
112120
EcuiSocket::SendJson("abort", abortMsg);
121+
#ifndef NO_INFLUX
122+
logger.log("sequence_abort", this->sequenceName + ": " + abortMsg, utils::getCurrentTimestamp());
123+
#endif
113124
Debug::info("Aborting... " + abortMsg);
114125

115126
sequenceToStop = true;
@@ -230,12 +241,13 @@ bool SequenceManager::LoadSequence(nlohmann::json jsonSeq)
230241
return true;
231242
}
232243

233-
void SequenceManager::StartSequence(nlohmann::json jsonSeq, nlohmann::json jsonAbortSeq, std::string comments)
244+
void SequenceManager::StartSequence(nlohmann::json jsonSeq, nlohmann::json jsonAbortSeq, std::string comments, std::string sequenceName)
234245
{
235246
if (sequenceThread.joinable())
236247
sequenceThread.join();
237248
if (!sequenceRunning && !isAbortRunning)
238249
{
250+
this->sequenceName = sequenceName;
239251
jsonSequence = jsonSeq;
240252
jsonAbortSequence = jsonAbortSeq;
241253
SequenceManager::comments = comments;
@@ -395,6 +407,10 @@ void SequenceManager::sequenceLoop(int64_t interval_us)
395407

396408
bool firstIteration = true;
397409

410+
#ifndef NO_INFLUX
411+
logger.log("sequence_start", this->sequenceName, utils::getCurrentTimestamp());
412+
#endif
413+
398414
while(!sequenceToStop)
399415
{
400416
if (!firstIteration) {
@@ -403,7 +419,6 @@ void SequenceManager::sequenceLoop(int64_t interval_us)
403419
}
404420
firstIteration = false;
405421

406-
407422
int64_t sequenceTime_us = sequenceLoopTimer.getTimeElapsed_us() + startTime_us;
408423

409424
if(sequenceTime_us > endTime_us)
@@ -422,6 +437,9 @@ void SequenceManager::sequenceLoop(int64_t interval_us)
422437
if(sequenceTime_us >= nextTimerSync_us)
423438
{
424439
EcuiSocket::SendJson("timer-sync", ((sequenceTime_us/1000.0) / 1000.0));
440+
#ifndef NO_INFLUX
441+
logger.log("sequence_time", sequenceTime_us / 1000000.0, utils::getCurrentTimestamp());
442+
#endif
425443
nextTimerSync_us += timerSyncInterval;
426444
}
427445

@@ -530,6 +548,11 @@ void SequenceManager::sequenceLoop(int64_t interval_us)
530548

531549
Debug::info("Sequence ended");
532550

551+
#ifndef NO_INFLUX
552+
logger.log("sequence_end", this->sequenceName, utils::getCurrentTimestamp());
553+
logger.flush();
554+
#endif
555+
533556
Debug::flush();
534557

535558
sequenceRunning = false;

src/logging/InfluxDbLogger.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ void InfluxDbLogger::log(std::string source, std::string msg, std::size_t timest
3030
dbWriter->endDataPoint(timestamp);
3131
}
3232

33+
void InfluxDbLogger::log(const std::string &key, const std::string &msg, const std::size_t timestamp) const {
34+
dbWriter->startDataPoint();
35+
dbWriter->addTag("key", key);
36+
dbWriter->tagsDone();
37+
dbWriter->addField("msg", msg);
38+
dbWriter->endDataPoint(timestamp);
39+
}
40+
3341
void InfluxDbLogger::log(std::string key, std::size_t value, std::size_t timestamp){
3442
dbWriter->startDataPoint();
3543
dbWriter->addTag("key", key);

src/logging/influxDb.c

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,66 @@ void set_credentials(influxDbContext *cntxt, const char *username, const char *p
4242
safe_str_cpy(cntxt->password, password, SETTINGS_LENGTH);
4343
}
4444

45-
int initDbContext(influxDbContext *cntxt, const char *hostname, const char *port, const char *database) {
46-
struct addrinfo hints, *ai = NULL;
47-
48-
cntxt->sock_fd = -1;
49-
memset(cntxt->user, 0, SETTINGS_LENGTH);
50-
memset(cntxt->password, 0, SETTINGS_LENGTH);
51-
memset(cntxt->ts_precision, 0, SETTINGS_LENGTH);
45+
int createSocket(influxDbContext *cntxt)
46+
{
47+
if (cntxt->sock_fd >= 0) {
48+
// close the existing socket if it is open
49+
close(cntxt->sock_fd);
50+
cntxt->sock_fd = -1;
51+
}
5252

53-
safe_str_cpy(cntxt->hostname, hostname, SETTINGS_LENGTH);
54-
safe_str_cpy(cntxt->port, port, SETTINGS_LENGTH);
55-
safe_str_cpy(cntxt->db_name, database, SETTINGS_LENGTH);
53+
struct addrinfo hints, *ai = NULL;
5654

5755
memset(&hints, 0, sizeof(struct addrinfo));
5856
hints.ai_family = AF_INET;
5957
hints.ai_socktype = SOCK_STREAM;
6058

61-
do {
62-
if(getaddrinfo(hostname, port, &hints, &ai) != 0) {
63-
break;
64-
}
65-
66-
cntxt->sock_fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
59+
if (getaddrinfo(cntxt->hostname, cntxt->port, &hints, &ai) != 0)
60+
{
61+
return -1;
62+
}
6763

68-
if(cntxt->sock_fd < 0) {
69-
break;
70-
}
64+
cntxt->sock_fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
65+
if (cntxt->sock_fd < 0)
66+
{
67+
freeaddrinfo(ai);
68+
return -1;
69+
}
7170

72-
if(connect(cntxt->sock_fd, ai->ai_addr, ai->ai_addrlen) < 0) {
73-
(void) close(cntxt->sock_fd);
74-
cntxt->sock_fd = -1;
75-
}
71+
if (connect(cntxt->sock_fd, ai->ai_addr, ai->ai_addrlen) < 0)
72+
{
73+
close(cntxt->sock_fd);
74+
cntxt->sock_fd = -1;
75+
freeaddrinfo(ai);
76+
return -1;
7677
}
77-
while (0);
7878

7979
freeaddrinfo(ai);
80-
8180
return cntxt->sock_fd;
8281
}
8382

83+
int initDbContext(influxDbContext *cntxt, const char *hostname, const char *port, const char *database) {
84+
85+
cntxt->sock_fd = -1;
86+
memset(cntxt->user, 0, SETTINGS_LENGTH);
87+
memset(cntxt->password, 0, SETTINGS_LENGTH);
88+
memset(cntxt->ts_precision, 0, SETTINGS_LENGTH);
89+
90+
safe_str_cpy(cntxt->hostname, hostname, SETTINGS_LENGTH);
91+
safe_str_cpy(cntxt->port, port, SETTINGS_LENGTH);
92+
safe_str_cpy(cntxt->db_name, database, SETTINGS_LENGTH);
93+
94+
return createSocket(cntxt);
95+
}
96+
8497
int deInitDbContext(influxDbContext *cntxt) {
8598
return close(cntxt->sock_fd);
8699
}
87100

88101

89-
int sendData(influxDbContext *cntxt, char *data, size_t length) {
102+
int sendData(influxDbContext *cntxt, char *data, size_t length)
103+
{
104+
createSocket(cntxt);
90105
char http_header[2048];
91106
size_t ret;
92107
size_t header_length = 0, sent = 0;

tests/SequenceManagerTest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ TEST_F(SequenceManagerTest, StartIsExecutedOnlyOnce) {
118118

119119
nlohmann::json sequence = StartIsExecutedOnlyOnce_json;
120120

121-
sequenceManager->StartSequence(sequence,nlohmann::json(),"");
121+
sequenceManager->StartSequence(sequence,nlohmann::json(),"", "");
122122

123123
}
124124

@@ -149,15 +149,15 @@ TEST_F(SequenceManagerTest, LinearInterpolationIsCorrect) {
149149
}));
150150

151151
nlohmann::json sequence = LinearInterpolationTest1_json;
152-
sequenceManager->StartSequence(sequence, nlohmann::json(), "");
152+
sequenceManager->StartSequence(sequence, nlohmann::json(), "", "");
153153

154154
// Wait for sequence to finish
155155
while (sequenceManager->IsSequenceRunning()) {}
156156

157157
// We should have approximatly 100 calls.
158158
EXPECT_NEAR(observed.size(), expected_calls,1);
159159
// Check linearity: value should be start_value + (end_value - start_value) * (t/duration)
160-
for (size_t i = 0; i < observed.size(); ++i) {
160+
for (int i = 0; i < observed.size(); ++i) {
161161
double t = observed[i].first;
162162
double value = observed[i].second;
163163
double expected = start_value + (end_value - start_value) * (t / duration);
@@ -180,7 +180,7 @@ TEST_F(SequenceManagerTest, AbortSequenceSetsValueAndStopsQuickly) {
180180
nlohmann::json sequence = StartIsExecutedOnlyOnce_json;
181181
nlohmann::json abort_sequence = SimpleAbortScenario_json;
182182

183-
sequenceManager->StartSequence(sequence, abort_sequence, "");
183+
sequenceManager->StartSequence(sequence, abort_sequence, "", "");
184184

185185
// Wait 0.5s, then abort
186186
std::this_thread::sleep_for(std::chrono::milliseconds(500));
@@ -213,7 +213,7 @@ TEST_F(SequenceManagerTest, TimeSwappedSequenceSendsValuesInCorrectOrder) {
213213
EXPECT_CALL(*event_manager_mock, ExecuteCommand("valve_1", ElementsAre(100), false)).InSequence(seq);
214214

215215
nlohmann::json sequence = TimeStampsSwapped_json;
216-
sequenceManager->StartSequence(sequence, nlohmann::json(), "");
216+
sequenceManager->StartSequence(sequence, nlohmann::json(), "", "");
217217

218218
}
219219

@@ -228,7 +228,7 @@ TEST_F(SequenceManagerTest, UTF8CharacterIn_json) {
228228
EXPECT_CALL(*event_manager_mock, ExecuteCommand("valve_öä😀", ElementsAre(0), false)).InSequence(seq);
229229

230230
nlohmann::json sequence = UTF8CharacterIn_json;
231-
sequenceManager->StartSequence(sequence, nlohmann::json(), "");
231+
sequenceManager->StartSequence(sequence, nlohmann::json(), "", "");
232232

233233
}
234234

@@ -242,6 +242,6 @@ TEST_F(SequenceManagerTest, SingleValueIsExecuted) {
242242

243243
nlohmann::json sequence = SingleValueIsExecuted_json;
244244

245-
sequenceManager->StartSequence(sequence,nlohmann::json(),"");
245+
sequenceManager->StartSequence(sequence,nlohmann::json(),"", "");
246246

247247
}

0 commit comments

Comments
 (0)