added InfluxDB Class for pushing data to influxDB 1.8

This commit is contained in:
carina@hades 2022-11-03 13:12:45 -04:00
parent 2483abef70
commit 27c8b502d3
4 changed files with 168 additions and 4 deletions

View File

@ -11,7 +11,7 @@ CAENLIBS = -lCAENDigitizer
ROOTLIBS = `root-config --cflags --glibs`
OBJS = startStopDialog.o programSetting.o triggerSummary.o registerSetting.o channelSettingPSD.o channelSettingPHA.o boardSetting.o ClassDigitizer.o FSUDAQ.o
OBJS = influxdb.o startStopDialog.o programSetting.o triggerSummary.o registerSetting.o channelSettingPSD.o channelSettingPHA.o boardSetting.o ClassDigitizer.o FSUDAQ.o
#########################################################################
@ -23,9 +23,9 @@ clean :
ClassDigitizer.o : ClassDigitizer.cpp ClassDigitizer.h RegisterAddress.h macro.h ClassData.h
$(CC) $(COPTS) -c ClassDigitizer.cpp
../test : test.cpp ClassDigitizer.o
../test : test.cpp ClassDigitizer.o influxdb.o
@echo "--------- making test"
$(CC) $(COPTS) -o ../test test.cpp ClassDigitizer.o $(CAENLIBS) $(ROOTLIBS)
$(CC) $(COPTS) -o ../test test.cpp ClassDigitizer.o influxdb.o $(CAENLIBS) $(ROOTLIBS) -lcurl
../Analysis/EventBuilder : EventBuilder.cpp ClassData.h
@echo "--------- making EventBuilder"
@ -37,7 +37,7 @@ ClassDigitizer.o : ClassDigitizer.cpp ClassDigitizer.h RegisterAddress.h macro.h
../FSUDAQ : FSUDAQDict.cxx $(OBJS) ClassData.h
@echo "----------- creating FSUDAQ"
$(CC) $(COPTS) FSUDAQDict.cxx $(OBJS) -o ../FSUDAQ $(CAENLIBS) $(ROOTLIBS)
$(CC) $(COPTS) FSUDAQDict.cxx $(OBJS) -o ../FSUDAQ $(CAENLIBS) $(ROOTLIBS) -lcurl
@ln -s -f DAQ/FSUDAQDict_rdict.pcm ../.
FSUDAQDict.cxx : FSUDAQ.h FSUDAQLinkDef.h
@ -64,6 +64,10 @@ registerSetting.o : registerSetting.h registerSetting.cpp
@echo "----------- creating registerSetting.o"
$(CC) $(COPTS) -c registerSetting.cpp $(ROOTLIBS)
influxdb.o : influxdb.h influxdb.cpp
@echo "----------- creating influxdb.o"
$(CC) $(COPTS) -c influxdb.cpp -lcurl
triggerSummary.o : triggerSummary.h triggerSummary.cpp
@echo "----------- creating triggerSummary.o"
$(CC) $(COPTS) -c triggerSummary.cpp $(ROOTLIBS)

102
DAQ/influxdb.cpp Normal file
View File

@ -0,0 +1,102 @@
#include "influxdb.h"
InfluxDB::InfluxDB(std::string url, bool verbose){
curl = curl_easy_init();
if( verbose) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
this->databaseIP = url;
respondCode = 0;
dataPoints = "";
}
InfluxDB::~InfluxDB(){
curl_easy_cleanup(curl);
}
std::string InfluxDB::ShowDatabases(){
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());
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){
dataPoints += fullString + "\n";
}
void InfluxDB::ClearDataPoints(){
dataPoints = "";
}
void InfluxDB::PrintDataPoints(){
printf("%s\n", dataPoints.c_str());
}
void InfluxDB::WriteData(std::string databaseName){
curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "write?db=" + databaseName).c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(dataPoints.length()));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataPoints.c_str());
Execute();
}
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("############# fail\n");
}
size_t InfluxDB::WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp){
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

44
DAQ/influxdb.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef INFLUXDB_H
#define INFLUXDB_H
#include <stdio.h>
#include <iostream>
#include <string>
#include <curl/curl.h>
class InfluxDB{
private:
CURL * curl;
CURLcode respond;
long respondCode;
std::string databaseIP;
std::string dataPoints;
static size_t WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp);
void Execute();
public:
/// url = https://fsunuc.physics.fsu.edu/InfluxDB/
InfluxDB(std::string url, bool verbose = false);
~InfluxDB();
/// Query
std::string ShowDatabases();
std::string Query(std::string databaseName, std::string query);
void CreateDatabase(std::string databaseName);
/// for single or batch write,
/// 1, addDataPoint first, you can add as many as you like
/// 2, writeData.
void AddDataPoint(std::string fullString);
void ClearDataPoints();
void PrintDataPoints();
void WriteData(std::string databaseName);
};
#endif

View File

@ -2,6 +2,8 @@
#include "ClassData.h"
#include "ClassDigitizer.h"
#include "influxdb.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TApplication.h"
@ -72,7 +74,19 @@ int getch(void){
int main(int argc, char* argv[]){
InfluxDB influx("https://fsunuc.physics.fsu.edu/influx/", false);
//influx.ShowDatabases();
//influx.Query("testing", "Show measurements");
influx.AddDataPoint("Rate,Bd=0,Ch=0 value=10");
influx.AddDataPoint("Rate,Bd=0,Ch=7 value=20");
influx.AddDataPoint("Rate,Bd=0,Ch=15 value=30");
influx.WriteData("testing");
/**////##################### Demo for loading and change setting without open a digitizer
/**
Digitizer * dig = new Digitizer();
dig->OpenDigitizer(0, 1, false, true);
dig->LoadSettingBinaryToMemory("expDir/settings/setting_323.bin");