From 5d72b4069b19c95929c104d29b413cc56f4a4bde Mon Sep 17 00:00:00 2001 From: "Ryan@WorkStation" Date: Tue, 21 Feb 2023 15:53:12 -0500 Subject: [PATCH] fixed influx error when value is nan --- influxdb.cpp | 15 +++++++++++---- influxdb.h | 2 ++ mainwindow.cpp | 16 +++++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/influxdb.cpp b/influxdb.cpp index 08b484e..d6ebc62 100644 --- a/influxdb.cpp +++ b/influxdb.cpp @@ -129,6 +129,8 @@ void InfluxDB::PrintDataPoints(){ } void InfluxDB::WriteData(std::string databaseName){ + if( dataPoints.length() == 0 ) return; + //printf("|%s|\n", (databaseIP + "write?db=" + databaseName).c_str()); curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "write?db=" + databaseName).c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast(dataPoints.length())); @@ -138,10 +140,15 @@ void InfluxDB::WriteData(std::string databaseName){ void InfluxDB::Execute(){ - respond = curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode); - //printf("==== respond code %ld \n", respondCode); - if( respond != CURLE_OK) printf("############# InfluxDB::Execute fail\n"); + try{ + respond = curl_easy_perform(curl); + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode); + //printf("==== respond code %ld \n", respondCode); + if( respond != CURLE_OK) printf("############# InfluxDB::Execute fail\n"); + } catch (std::exception& e){ // in case of unexpected error + printf("%s\n", e.what()); + respond = CURLE_SEND_ERROR; + } } size_t InfluxDB::WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp){ diff --git a/influxdb.h b/influxdb.h index ad98a65..e573178 100644 --- a/influxdb.h +++ b/influxdb.h @@ -47,9 +47,11 @@ class InfluxDB{ /// 1, addDataPoint first, you can add as many as you like /// 2, writeData. void AddDataPoint(std::string fullString); + unsigned int GetDataLength() const {return dataPoints.length();} void ClearDataPointsBuffer(); void PrintDataPoints(); void WriteData(std::string databaseName); + bool IsWriteOK() const {return (respondCode == CURLE_OK) ? true: false;} }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 9e18651..3ba3d01 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -528,7 +528,7 @@ void MainWindow::OpenScope(){ if( !scope ){ scope = new Scope(digi, nDigi, readDataThread); connect(scope, &Scope::CloseWindow, this, [=](){ bnStartACQ->setEnabled(true); }); - //connect(scope, &Scope::UpdateScalar, this, &MainWindow::UpdateScalar); + connect(scope, &Scope::UpdateScalar, this, &MainWindow::UpdateScalar); connect(scope, &Scope::SendLogMsg, this, &MainWindow::LogMsg); }else{ scope->show(); @@ -676,12 +676,12 @@ void MainWindow::UpdateScalar(){ if( influx ){ for( int ch = 0; ch < digi[iDigi]->GetNChannels(); ch++ ){ influx->AddDataPoint("Rate,Bd=" + std::to_string(digi[iDigi]->GetSerialNumber()) + ",Ch=" + std::to_string(ch) + " value=" + haha[ch]); - influx->AddDataPoint("AccpRate,Bd=" + std::to_string(digi[iDigi]->GetSerialNumber()) + ",Ch=" + std::to_string(ch) + " value=" + std::to_string(acceptRate[ch])); + if( !std::isnan(acceptRate[ch]) ) influx->AddDataPoint("AccpRate,Bd=" + std::to_string(digi[iDigi]->GetSerialNumber()) + ",Ch=" + std::to_string(ch) + " value=" + std::to_string(acceptRate[ch])); } } } - if( influx ){ + if( influx && influx->GetDataLength() > 0 ){ //influx->PrintDataPoints(); influx->WriteData(DatabaseName.toStdString()); influx->ClearDataPointsBuffer(); @@ -926,6 +926,16 @@ void MainWindow::SetupInflux(){ if( foundDatabase ){ LogMsg(" Database " + DatabaseName + " found."); + + influx->AddDataPoint("Rate,Bd=0,Ch=0 value=1"); + influx->WriteData(DatabaseName.toStdString()); + influx->ClearDataPointsBuffer(); + if( influx->IsWriteOK() ){ + LogMsg("test write OK."); + }else{ + LogMsg("test write FAIL."); + } + }else{ LogMsg(" Database " + DatabaseName + " NOT found."); delete influx;