Skip to content

Commit

Permalink
Fix bug in timestamp and add last successful timestamp as upload
Browse files Browse the repository at this point in the history
  • Loading branch information
UsualSpec committed Jul 12, 2024
1 parent c26b9dc commit dcb3b38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 5 additions & 4 deletions client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ void AutopowerClient::putResponseToServer(uint32_t statuscode, std::string messa

std::string AutopowerClient::getReadableTimestamp(std::time_t rtme) {
struct tm *msmtStartTime;
time(&rtme);
msmtStartTime = gmtime(&rtme);
char tbuffer[1000];
strftime(tbuffer, sizeof tbuffer, "%FT%T", msmtStartTime);
char tbuffer[std::size("yyyy-mm-ddThh:mm:ssZ")];
strftime(tbuffer, sizeof tbuffer, "%FT%TZ", msmtStartTime);
return std::string(tbuffer);
}
std::string AutopowerClient::getCurrentReadableTimestamp() {
// Get UTC timestamp. Unfortunately std::chrono is more complicated. Hence
// use time_t.
std::time_t rtme;
std::time_t rtme = std::time(nullptr);
return getReadableTimestamp(rtme);
}

Expand Down Expand Up @@ -303,6 +302,7 @@ void AutopowerClient::getAndSavePpData() {
txn.exec_prepared("addMsmtPoint", getInternalMsmtId(), msmtPoint.measurement, msmtPoint.rawTimestamp);
txn.commit();
setHasWrittenOnce(true); // specify success of writing at least once to DB
setLastSampleTimestamp(msmtPoint.timestamp);
} catch (std::exception &e) {
std::string exprContent = e.what();
std::string errorMsg = "Error while writing measurement to database: " + exprContent;
Expand Down Expand Up @@ -733,6 +733,7 @@ void AutopowerClient::manageMsmt() {
statusObject["measurementSettings"]["uploadInterval"] = periodicUploadMinutes;
statusObject["measurementSettings"]["sharedMsmtId"] = getSharedMsmtId();
statusObject["ppIsRunning"] = false;
statusObject["lastSampleTimestamp"] = getReadableTimestamp(getLastSampleTimestamp());
if (measuring() && getHasWrittenOnce() && !getHasExited()) {
// we assume that pinpoint should now be running. Check with kill() via getPpIsCurrentlyRunning()
statusObject["ppIsRunning"] = getPpIsCurrentlyRunning();
Expand Down
14 changes: 14 additions & 0 deletions client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ class AutopowerClient {
bool thisMeasurementHasWrittenOnce = false;
uint32_t periodicUploadMinutes = 5; // time to periodically upload the data in minutes. If this is 0 we never upload the content and rely on manually uploading the files


std::time_t lastSampleTimestamp; // the last timestamp in raw string form written to the DB
std::shared_mutex lastSampleMtx; // sample mutex

void setLastSampleTimestamp(std::time_t lastSampleTimestamp) {
std::unique_lock<std::shared_mutex>wl(lastSampleMtx);
this->lastSampleTimestamp = lastSampleTimestamp;
}

std::time_t getLastSampleTimestamp() {
std::shared_lock<std::shared_mutex>rl(lastSampleMtx);
return this->lastSampleTimestamp;
}

std::unique_ptr<autopapi::CMeasurementApi::Stub> stub; // stub for connecting to GRPC server
bool measuring() {
// read measuring
Expand Down

0 comments on commit dcb3b38

Please sign in to comment.