fixed influx error when value is nan

This commit is contained in:
Ryan Tang 2023-02-21 15:53:12 -05:00
parent 36b871802d
commit 5d72b4069b
3 changed files with 26 additions and 7 deletions

View File

@ -129,6 +129,8 @@ void InfluxDB::PrintDataPoints(){
} }
void InfluxDB::WriteData(std::string databaseName){ 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_URL, (databaseIP + "write?db=" + databaseName).c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(dataPoints.length())); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(dataPoints.length()));
@ -138,10 +140,15 @@ void InfluxDB::WriteData(std::string databaseName){
void InfluxDB::Execute(){ void InfluxDB::Execute(){
respond = curl_easy_perform(curl); try{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode); respond = curl_easy_perform(curl);
//printf("==== respond code %ld \n", respondCode); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode);
if( respond != CURLE_OK) printf("############# InfluxDB::Execute fail\n"); //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){ size_t InfluxDB::WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp){

View File

@ -47,9 +47,11 @@ class InfluxDB{
/// 1, addDataPoint first, you can add as many as you like /// 1, addDataPoint first, you can add as many as you like
/// 2, writeData. /// 2, writeData.
void AddDataPoint(std::string fullString); void AddDataPoint(std::string fullString);
unsigned int GetDataLength() const {return dataPoints.length();}
void ClearDataPointsBuffer(); void ClearDataPointsBuffer();
void PrintDataPoints(); void PrintDataPoints();
void WriteData(std::string databaseName); void WriteData(std::string databaseName);
bool IsWriteOK() const {return (respondCode == CURLE_OK) ? true: false;}
}; };

View File

@ -528,7 +528,7 @@ void MainWindow::OpenScope(){
if( !scope ){ if( !scope ){
scope = new Scope(digi, nDigi, readDataThread); scope = new Scope(digi, nDigi, readDataThread);
connect(scope, &Scope::CloseWindow, this, [=](){ bnStartACQ->setEnabled(true); }); 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); connect(scope, &Scope::SendLogMsg, this, &MainWindow::LogMsg);
}else{ }else{
scope->show(); scope->show();
@ -676,12 +676,12 @@ void MainWindow::UpdateScalar(){
if( influx ){ if( influx ){
for( int ch = 0; ch < digi[iDigi]->GetNChannels(); ch++ ){ 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("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->PrintDataPoints();
influx->WriteData(DatabaseName.toStdString()); influx->WriteData(DatabaseName.toStdString());
influx->ClearDataPointsBuffer(); influx->ClearDataPointsBuffer();
@ -926,6 +926,16 @@ void MainWindow::SetupInflux(){
if( foundDatabase ){ if( foundDatabase ){
LogMsg("<font style=\"color : green;\"> Database <b>" + DatabaseName + "</b> found."); LogMsg("<font style=\"color : green;\"> Database <b>" + DatabaseName + "</b> 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{ }else{
LogMsg("<font style=\"color : red;\"> Database <b>" + DatabaseName + "</b> NOT found."); LogMsg("<font style=\"color : red;\"> Database <b>" + DatabaseName + "</b> NOT found.");
delete influx; delete influx;