modified InfluxDB class for inlfuxDB V2

This commit is contained in:
Ryan Tang 2024-03-01 16:58:27 -05:00
parent 661aa5f928
commit aa361938a4
8 changed files with 432 additions and 180 deletions

View File

@ -12,7 +12,7 @@ CAENLIBS = -lCAENDigitizer
ROOTLIBS = `root-config --cflags --glibs`
OBJS = ClassDigitizer.o MultiBuilder.o
OBJS = ClassDigitizer.o MultiBuilder.o ClassInfluxDB.o
ALL = test test_indep EventBuilder EventBuilderNoTrace DataReader DumpFSU2ROOT SettingsExplorer
@ -29,9 +29,12 @@ MultiBuilder.o : ../MultiBuilder.cpp ../MultiBuilder.h ../Hit.h
ClassDigitizer.o : ../ClassDigitizer.cpp ../ClassDigitizer.h ../RegisterAddress.h ../macro.h ../ClassData.h
$(CC) $(COPTS) -c ../ClassDigitizer.cpp
test : test.cpp ../ClassDigitizer.o ../MultiBuilder.o
ClassInfluxDB.o : ../ClassInfluxDB.cpp ../ClassInfluxDB.h
$(CC) $(COPTS) -c ../ClassInfluxDB.cpp -lcurl
test : test.cpp ../ClassDigitizer.o ../MultiBuilder.o ../ClassInfluxDB.o
@echo "--------- making test"
$(CC) $(COPTS) -o test test.cpp ../ClassDigitizer.o ../MultiBuilder.o $(CAENLIBS) $(ROOTLIBS)
$(CC) $(COPTS) -o test test.cpp ../ClassDigitizer.o ../MultiBuilder.o ../ClassInfluxDB.o $(CAENLIBS) $(ROOTLIBS) -lcurl
test_indep : test_indep.cpp ../RegisterAddress.h ../macro.h
@echo "--------- making test_indep"

View File

@ -2,6 +2,7 @@
#include "../ClassData.h"
#include "../ClassDigitizer.h"
#include "../MultiBuilder.h"
#include "../ClassInfluxDB.h"
#include <TROOT.h>
#include <TSystem.h>
@ -15,6 +16,7 @@
#include <sys/time.h> /** struct timeval, select() */
#include <termios.h> /** tcgetattr(), tcsetattr() */
#include <vector>
#include <regex>
static struct termios g_old_kbd_mode;
@ -24,11 +26,138 @@ static void raw(void);
int keyboardhit();
int getch(void);
// #include <curl/curl.h>
size_t WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp){
// printf(" InfluxDB::%s \n", __func__);
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
//^======================================
int main(int argc, char* argv[]){
Digitizer * digi = new Digitizer(0, 26006, false, true);
digi->Reset();
InfluxDB * influx = new InfluxDB();
influx->SetURL("https://fsunuc.physics.fsu.edu/influx/");
//influx->SetURL("http://128.186.111.5:8086/");
influx->SetToken("wS-Oy17bU99qH0cTPJ-Q5tbiOWfaKyoASUx7WwmdM7KG8EJ1BpRowYkqpnPw8oeatnDaZfZtwIFT0kv_aIOAxQ==");
influx->TestingConnection();
influx->CheckDatabases();
influx->PrintDataBaseList();
// printf("=-------------------------\n");
// influx->TestingConnection(true);
printf("%s \n", influx->Query("testing", "show measurements").c_str());
// printf("%s \n", influx->Query("testing", "SELECT * from haha ORDER by time DESC LIMIT 5").c_str());
delete influx;
// CURL *curl = curl_easy_init();
// CURLcode res;
// struct curl_slist * headers = nullptr;
// headers = curl_slist_append(headers, "Authorization: Token wS-Oy17bU99qH0cTPJ-Q5tbiOWfaKyoASUx7WwmdM7KG8EJ1BpRowYkqpnPw8oeatnDaZfZtwIFT0kv_aIOAxQ==");
// // // headers = curl_slist_append(headers, "Content-Type: text/plain; charset=utf-8");
// headers = curl_slist_append(headers, "Accept: application/csv");
// printf("%s\n",headers->data);
// printf("%s\n", headers->next->data);
// curl_slist_free_all(headers);
// // printf("%p \n",headers);
// headers = curl_slist_append(headers, "Accept: application/csv");
// printf("%s\n",headers->data);
// curl_easy_setopt(curl, CURLOPT_POST, 1);
// curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// std::string databaseIP = "https://fsunuc.physics.fsu.edu/influx/";
// std::string databaseIP = "http://128.186.111.5:8086/";
//*===================== Check version
// curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "ping").c_str());
// curl_easy_setopt(curl, CURLOPT_HEADER, 1);
//*===================== Query data
//=============== query databases
// curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query").c_str());
// std::string postFields="q=Show databases";
//=============== query measurement
// curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query?db=testing").c_str());
// std::string postFields="q=SELECT * FROM \"haha\"";
//=============== write measurement
// curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "write?db=testing").c_str());
// std::string postFields = "haha,BD=1 state=2.345";
// postFields += "\n";
// postFields += "haha,BD=2 state=9.876";
// postFields += "\n";
// curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(postFields.length()));
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
// std::string readBuffer;
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// // //curl_easy_setopt(curl, CURLOPT_URL, "https://fsunuc.physics.fsu.edu/influx/api/v2/write?org=FSUFoxLab&bucket=testing");
// res = curl_easy_perform(curl);
// long respondCode;
// curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode);
// printf("respond code : %ld \n", respondCode);
// if( res == CURLE_OK ) {
// printf("================respond \n%s\n", readBuffer.c_str());
// }else{
// printf("=========== curl_easy_perform fail.\n");
// }
// curl_slist_free_all(headers);
// curl_easy_cleanup(curl);
// std::regex pattern(R"(X-Influxdb-Version: (.*))");
// std::smatch match;
// if (regex_search(readBuffer, match, pattern)) {
// // Extract and print the version
// std::string version = match[1];
// unsigned short vno = -1;
// size_t dotPosition = version.find('.');
// if( dotPosition != std::string::npos){
// vno = atoi(version.substr(dotPosition-1, 1).c_str());
// }
// printf("%s | %d\n", version.c_str(), vno);
// }
//============================================= end of influxDB example
// Digitizer * digi = new Digitizer(0, 26006, false, true);
// digi->Reset();
//digi->ProgramBoard_PHA();
@ -94,8 +223,8 @@ int main(int argc, char* argv[]){
builder->PrintAllEvent(); // TODO
*/
digi->CloseDigitizer();
delete digi;
// digi->CloseDigitizer();
// delete digi;
return 0;
}

270
ClassInfluxDB.cpp Normal file
View File

@ -0,0 +1,270 @@
#include "ClassInfluxDB.h"
#include <regex>
InfluxDB::InfluxDB(){
curl = curl_easy_init();
databaseIP = "";
respondCode = 0;
dataPoints = "";
headers = nullptr;
influxVersionStr = "";
influxVersion = -1;
token = "";
connectionOK = false;
}
InfluxDB::InfluxDB(std::string url, bool verbose){
curl = curl_easy_init();
if( verbose) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
SetURL(url);
respondCode = 0;
dataPoints = "";
headers = nullptr;
influxVersionStr = "";
influxVersion = -1;
token = "";
connectionOK = false;
}
InfluxDB::~InfluxDB(){
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
void InfluxDB::SetURL(std::string url){
// check the last char of url is "/"
if( url.back() != '/') {
this->databaseIP = url + "/";
}else{
this->databaseIP = url;
}
headers = curl_slist_append(headers, "Accept: application/csv");
}
void InfluxDB::SetToken(std::string token){
this->token = token;
headers = curl_slist_append(headers, ("Authorization: Token " + token).c_str());
}
bool InfluxDB::TestingConnection(bool debug){
CheckInfluxVersion();
if( respond != CURLE_OK ) return false;
connectionOK = true;
return true;
}
std::string InfluxDB::CheckInfluxVersion(bool debug){
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "ping").c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
std::string respondStr;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respondStr);
Execute();
if( respond != CURLE_OK) return "CURL Error.";
if( debug) printf("%s\n", respondStr.c_str());
//Find Version from readBuffer
std::regex pattern(R"(X-Influxdb-Version: (.*))");
std::smatch match;
if (regex_search(respondStr, match, pattern)) {
influxVersionStr = match[1];
size_t dotPosition = influxVersionStr.find('.');
if( dotPosition != std::string::npos){
influxVersion = atoi(influxVersionStr.substr(dotPosition-1, 1).c_str());
}
}
printf("Influx Version : %s | %u\n", influxVersionStr.c_str(), influxVersion);
return respondStr;
}
std::string InfluxDB::CheckDatabases(){
if( ! connectionOK ) return "no connection. try TestConnection() again.";
if( influxVersion == 2 && token.empty() ) return "token no provided, abort.";
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query").c_str());
std::string postFields="q=Show databases";
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(postFields.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
std::string respondStr;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respondStr);
Execute();
// printf("|%s|\n", respondStr.c_str());
if( respond != CURLE_OK) return "CURL Error.";
databaseList.clear();
// Split the input string into lines
std::istringstream iss(respondStr);
std::vector<std::string> lines;
std::string line;
while (std::getline(iss, line)) {
lines.push_back(line);
}
// Extract the third column from each line and store it in a vector
std::vector<std::string> thirdColumn;
for (const auto& l : lines) {
std::istringstream lineIss(l);
std::string token;
for (int i = 0; std::getline(lineIss, token, ','); ++i) {
if (i == 2) { // Third column
databaseList.push_back(token);
break;
}
}
}
// {//============ when output is JSON
// size_t pos = readBuffer.find("values");
// if( pos > 0 ){
// std::string kaka = readBuffer.substr(pos+8);
// pos = kaka.find("}");
// kaka = kaka.substr(0, pos);
// int len = kaka.length();
// bool startFlag = false;
// std::string lala;
// char yaya = '"';
// for( int i = 0; i < len; i++){
// if( startFlag == false && kaka[i] == yaya ) {
// startFlag = true;
// lala = "";
// continue;
// }
// if( startFlag && kaka[i] == yaya ){
// startFlag = false;
// databaseList.push_back(lala);
// continue;
// }
// if( startFlag ) lala += kaka[i];
// }
// }
// }
return respondStr;
}
void InfluxDB::PrintDataBaseList(){
for( size_t i = 0; i < databaseList.size(); i++){
printf("%2ld| %s\n", i, databaseList[i].c_str());
}
}
std::string InfluxDB::Query(std::string databaseName, std::string influxQL_query){
if( ! connectionOK ) return "no connection. try TestConnection() again.";
if( influxVersion == 2 && token.empty() ) return "token no provided, abort.";
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query?db=" + databaseName).c_str());
std::string postFields = "q=" + influxQL_query;
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(postFields.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
std::string respondStr;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respondStr);
Execute();
//printf("|%s|\n", readBuffer.c_str());
return respondStr;
}
void InfluxDB::CreateDatabase(std::string databaseName){
if( ! connectionOK ) return ;
if( influxVersion == 2 && token.empty() ) return;
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query").c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
std::string postFields = "q=CREATE DATABASE " + databaseName;
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(postFields.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
Execute();
}
void InfluxDB::AddDataPoint(std::string fullString){
// printf(" InfluxDB::%s |%s| \n", __func__, fullString.c_str());
dataPoints += fullString + "\n";
}
void InfluxDB::ClearDataPointsBuffer(){
// printf(" InfluxDB::%s \n", __func__);
dataPoints = "";
}
void InfluxDB::PrintDataPoints(){
// printf(" InfluxDB::%s \n", __func__);
printf("%s\n", dataPoints.c_str());
}
void InfluxDB::WriteData(std::string databaseName){
if( ! connectionOK ) return ;
if( influxVersion == 2 && token.empty() ) return;
// printf(" InfluxDB::%s \n", __func__);
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_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(dataPoints.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataPoints.c_str());
Execute();
}
void InfluxDB::Execute(){
// printf(" InfluxDB::%s \n", __func__);
try{
respond = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode);
//printf("==== respond %d (OK = %d)\n", respond, CURLE_OK);
if( respond != CURLE_OK ) printf("############# InfluxDB::Execute fail | %ld\n", respondCode);
} 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){
// printf(" InfluxDB::%s \n", __func__);
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

View File

@ -10,17 +10,23 @@
class InfluxDB{
private:
bool isURLValid;
CURL * curl;
CURLcode respond;
long respondCode;
std::string databaseIP;
std::string dataPoints;
std::string token;
struct curl_slist * headers;
std::vector<std::string> databaseList;
unsigned short influxVersion;
std::string influxVersionStr;
bool connectionOK;
static size_t WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp);
void Execute();
@ -28,14 +34,21 @@ class InfluxDB{
public:
InfluxDB(std::string url, bool verbose = false);
InfluxDB();
~InfluxDB();
void SetURL(std::string url);
bool TestingConnection();
bool IsURLValid() const {return isURLValid;}
void SetToken(std::string token);
bool TestingConnection(bool debug = false);
bool IsConnectionOK() const {return connectionOK;}
/// Query
unsigned short GetVersionNo() const {return influxVersion;}
std::string GetVersionString() const {return influxVersionStr;}
/// Query, query will be in CSV format
std::string CheckInfluxVersion(bool debug = false);
std::string CheckDatabases(); /// this save the list of database into databaseList
void PrintDataBaseList();
std::string Query(std::string databaseName, std::string query);
/// the CheckDatabases() function must be called before

View File

@ -17,7 +17,7 @@
#include "Scope.h"
#include "DigiSettingsPanel.h"
#include "SingleSpectra.h"
#include "influxdb.h"
#include "ClassInfluxDB.h"
#include "analyzers/Analyser.h"
//^#===================================================== MainWindow

View File

@ -33,7 +33,7 @@ HEADERS += ClassData.h \
FSUDAQ.h \
macro.h \
RegisterAddress.h \
influxdb.h\
ClassInfluxDB.h\
Scope.h \
SingleSpectra.h \
Hit.h \
@ -49,7 +49,7 @@ SOURCES += ClassDigitizer.cpp \
DigiSettingsPanel.cpp \
FSUDAQ.cpp \
main.cpp \
influxdb.cpp\
ClassInfluxDB.cpp\
Scope.cpp \
SingleSpectra.cpp \
MultiBuilder.cpp \

View File

@ -16,7 +16,7 @@
#include "CustomThreads.h"
#include "CustomWidgets.h"
#include "MultiBuilder.h"
#include "influxdb.h"
#include "ClassInfluxDB.h"
/**************************************

View File

@ -1,163 +0,0 @@
#include "influxdb.h"
InfluxDB::InfluxDB(std::string url, bool verbose){
curl = curl_easy_init();
if( verbose) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
SetURL(url);
respondCode = 0;
dataPoints = "";
}
InfluxDB::~InfluxDB(){
curl_easy_cleanup(curl);
}
void InfluxDB::SetURL(std::string url){
// check the last char of url is "/"
if( url.back() != '/') {
this->databaseIP = url + "/";
}else{
this->databaseIP = url;
}
}
bool InfluxDB::TestingConnection(){
CheckDatabases();
if( respond != CURLE_OK ) return false;
return true;
}
std::string InfluxDB::CheckDatabases(){
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query").c_str());
std::string postFields="q=Show databases";
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(postFields.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
Execute();
//printf("|%s|\n", readBuffer.c_str());
if( respond != CURLE_OK) return "";
databaseList.clear();
size_t pos = readBuffer.find("values");
if( pos > 0 ){
std::string kaka = readBuffer.substr(pos+8);
pos = kaka.find("}");
kaka = kaka.substr(0, pos);
int len = kaka.length();
bool startFlag = false;
std::string lala;
char yaya = '"';
for( int i = 0; i < len; i++){
if( startFlag == false && kaka[i] == yaya ) {
startFlag = true;
lala = "";
continue;
}
if( startFlag && kaka[i] == yaya ){
startFlag = false;
databaseList.push_back(lala);
continue;
}
if( startFlag ) lala += kaka[i];
}
}
return readBuffer;
}
std::string InfluxDB::Query(std::string databaseName, std::string query){
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query?db=" + databaseName).c_str());
std::string postFields = "q=" + query;
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(postFields.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack);
std::string readBuffer;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
Execute();
//printf("|%s|\n", readBuffer.c_str());
return readBuffer;
}
void InfluxDB::CreateDatabase(std::string databaseName){
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "query").c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
std::string postFields = "q=CREATE DATABASE " + databaseName;
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(postFields.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
Execute();
}
void InfluxDB::AddDataPoint(std::string fullString){
// printf(" InfluxDB::%s |%s| \n", __func__, fullString.c_str());
dataPoints += fullString + "\n";
}
void InfluxDB::ClearDataPointsBuffer(){
// printf(" InfluxDB::%s \n", __func__);
dataPoints = "";
}
void InfluxDB::PrintDataPoints(){
// printf(" InfluxDB::%s \n", __func__);
printf("%s\n", dataPoints.c_str());
}
void InfluxDB::WriteData(std::string databaseName){
// printf(" InfluxDB::%s \n", __func__);
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<long>(dataPoints.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataPoints.c_str());
Execute();
}
void InfluxDB::Execute(){
// printf(" InfluxDB::%s \n", __func__);
try{
respond = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode);
//printf("==== respond %d (OK = %d)\n", respond, CURLE_OK);
if( respond != CURLE_OK) printf("############# InfluxDB::Execute fail | %ld\n", respondCode);
} 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){
// printf(" InfluxDB::%s \n", __func__);
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}