Skip to content

Commit

Permalink
Implement plotting of non-time based data (#23)
Browse files Browse the repository at this point in the history
* Implement plots

* src/ServiceDiscovery/Services: remove mentions of Postgres in array encoding/decoding functions

Rename
encodePostgresArray -> encodeArray
decodePostgresArray -> decodeArray

Although the current encoding is that used by postgresql, middleman can
talk to different database engines and recode the array if needed.

A proper approach here would be to convert the array to a JSON array ---
should be implemented in the future

---------

Co-authored-by: Evgenii Zhemchugov <[email protected]>
  • Loading branch information
jini-zh and Evgenii Zhemchugov authored Aug 5, 2024
1 parent ed00064 commit d609975
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
90 changes: 81 additions & 9 deletions src/ServiceDiscovery/Services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ bool Services::GetROOTplot(const std::string& plot_name, int& version, std::stri

}

bool Services::GetPlot(){
// placeholder for evgenii
return true;
}

bool Services::SQLQuery(const std::string& database, const std::string& query, std::vector<std::string>* responses, const unsigned int timeout){

if(responses) responses->clear();
Expand Down Expand Up @@ -444,10 +439,87 @@ bool Services::SendTemporaryROOTplot(const std::string& plot_name, const std::st

}

bool Services::SendPlot(){

// placeholder for evgenii

static std::vector<float> decodeArray(const std::string& string) {
if (string.length() < 2) return std::vector<float>(); // "{}"
size_t length = 1;
for (size_t i = 0; i < string.size(); ++i) if (string[i] == ',') ++length;

std::vector<float> result(length);

std::stringstream ss;
ss << string;
size_t i = 0;
char c;
ss >> c; // '{'
while (ss && c != '}' && i < length) ss >> result[i++] >> c;
return result;
};

static std::string encodeArray(const std::vector<float>& array) {
std::stringstream ss;
ss << "'{";
bool first = true;
for (float x : array) {
if (!first) ss << ',';
first = false;
ss << x;
};
ss << "}'";
return ss.str();
}

bool Services::GetPlot(const std::string& name, Plot& plot, unsigned timeout) {
std::string cmd = "{\"name\":\"" + name + "\"}";
std::string err = "";
std::string response;
if (!m_backend_client.SendCommand("R_PLOT", cmd, &response, &timeout, &err)) {
std::cerr << "GetPlot error: " << err << std::endl;
return false;
};

plot.name = name;

Store data;
data.JsonParser(response);

#define get(slot, var) if (!data.Get(slot, var)) return false;
std::string array;
get("x", array);
plot.x = decodeArray(array);
get("y", array);
plot.y = decodeArray(array);
get("title", plot.title);
get("xlabel", plot.xlabel);
get("ylabel", plot.ylabel);
get("info", plot.info);
#undef get

return true;
}

bool Services::SendPlot(Plot& plot, unsigned timeout) {
Store data;
data.Set("name", plot.name);
data.Set("x", encodeArray(plot.x));
data.Set("y", encodeArray(plot.y));
data.Set("title", plot.title);
data.Set("xlabel", plot.xlabel);
data.Set("ylabel", plot.ylabel);

std::string string;
plot.info >> string;
data.Set("info", string);

data >> string;
std::string err;
if (!m_backend_client.SendCommand(
"W_PLOT", string, static_cast<std::string*>(nullptr), &timeout, &err
))
{
std::cerr << "SendPlot error: " << err << std::endl;
return false;
};

return true;
}

Expand Down
14 changes: 12 additions & 2 deletions src/ServiceDiscovery/Services.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
#include <ServicesBackend.h>

namespace ToolFramework {

struct Plot {
std::string name;
std::string title;
std::string xlabel;
std::string ylabel;
std::vector<float> x;
std::vector<float> y;
Store info;
};

class Services{

Expand All @@ -41,8 +51,8 @@ namespace ToolFramework {
bool SendTemporaryROOTplot(const std::string& plot_name, const std::string& draw_options, const std::string& json_data, int* version=nullptr, const unsigned int timestamp=0);
bool SendPersistentROOTplot(const std::string& plot_name, const std::string& draw_options, const std::string& json_data, int* version=nullptr, const unsigned int timestamp=0, const unsigned int timeout=300);
bool GetROOTplot(const std::string& plot_name, int& version, std::string& draw_option, std::string& json_data, std::string* timestamp=nullptr, const unsigned int timeout=300);
bool SendPlot();
bool GetPlot();
bool SendPlot(Plot& plot, unsigned timeout=300);
bool GetPlot(const std::string& name, Plot& plot, unsigned timeout=300);

SlowControlCollection* GetSlowControlCollection();
SlowControlElement* GetSlowControlVariable(std::string key);
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceDiscovery/ServicesBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bool ServicesBackend::Initialise(std::string configfile){
// configuration options can be parsed via a Store class
if(configfile!="") m_variables.Initialise(configfile);

Initialise(m_variables);
return Initialise(m_variables);

}

Expand Down

0 comments on commit d609975

Please sign in to comment.