6#include "CrestApi/CrestCondException.h"
11using json = nlohmann::json;
29 auto a = std::stol(as);
30 auto b = std::stol(bs);
43 if(t.second.compare(
type)==0){
48 std::string errStr=
"The type [" +
type +
"] of parameter [" + name +
"] is not defined.";
49 throw CommonCrestException(errStr.c_str());
55 [&name](
const auto &column)
56 { return column.first == name; });
60 m_row[name] = nlohmann::json::value_t::null;
71 if (column.first != name)
76 switch (column.second)
79 m_row[name] = va_arg(ap,
int)==1;
82 m_row[name] =
static_cast<unsigned char>(va_arg(ap,
int));
86 m_row[name] = va_arg(ap,
int);
90 m_row[name] = va_arg(ap,
unsigned int);
93 m_row[name] = va_arg(ap, uint64_t);
96 m_row[name] = va_arg(ap, int64_t);
100 m_row[name] = va_arg(ap,
double);
110 m_row[name] = std::string(va_arg(ap,
const char *));
113 throw CommonCrestException(
"Unsupported column type.");
126 nlohmann::json arr_data =
m_isVectorPayload ? nlohmann::json::array() : nlohmann::json();
128 for (
const auto &data_row : data)
132 m_payload[channel_id] = std::move(arr_data);
143 nlohmann::json row_arr_data = nlohmann::json::array();
146 if (data_row.find(column.first) == data_row.end())
148 std::string
msg =
"The data does not contain the column: " + column.first;
149 throw CommonCrestException(
msg.c_str());
151 row_arr_data.push_back(data_row[column.first]);
179 throw CommonCrestException(
"No IOV in CrestContainer");
185 std::vector<std::string> chs;
187 chs.push_back(
x.key());
206 std::string
msg =
"Channel id " + std::string(channel_id) +
" is not found.";
207 throw CommonCrestException(
msg.c_str());
215 [&name](
const auto &column)
216 { return column.first == name; });
224 throw CommonCrestException(
"The column name is not found.");
237 std::string
msg =
"The payload is not a vector.";
238 throw CommonCrestException(
msg.c_str());
253 std::string
msg =
"The payload is empty.";
254 throw CommonCrestException(
msg.c_str());
267 std::string
msg =
"The iov data is empty.";
268 throw CommonCrestException(
msg.c_str());
295 json pspec_data=json::array();
301 throw CommonCrestException(
"Type do not exist in the map.");
303 j[column.first] = pos->second;
305 pspec_data.push_back(j);
314 for (
const auto &column : j)
316 for (
const auto &[name,
type] : column.items())
324 for (
const auto &[name,
type] : j.items())
350 std::ofstream
file(filename);
353 file << std::setprecision(6) << j.dump(4);
358 std::cerr <<
"CondContainer::dumpJsonToFile: Error opening file: " << filename << std::endl;
359 throw std::runtime_error(
"CondContainer::dumpJsonToFile: Error opening file.");
367 std::ifstream specfile(spec_filename);
368 nlohmann::json jspec;
369 if (specfile.is_open())
376 std::cerr <<
"CondContainer::readJsonFromFile: Error opening file: " << spec_filename << std::endl;
377 throw std::runtime_error(
"CondContainer::readJsonFromFile: Error opening file. ");
382 std::ifstream
file(filename);
391 std::cerr <<
"CondContainer::readJsonFromFile: Error opening file: " << filename << std::endl;
392 throw std::runtime_error(
"CondContainer::readJsonFromFile: Error opening file. ");
399 if (thisVal.is_null()){
400 m_row[colName] =
"NULL";
404 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
405 strVal=strVal.substr(1,strVal.size()-2);
406 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
411 m_row[colName] =
"NULL";
417 const bool newVal=(strVal ==
"true");
418 m_row[colName] = newVal;
423 m_row[colName]=std::stoul(strVal);
428 m_row[colName]=std::stol(strVal);
433 m_row[colName]=std::stoul(strVal);
438 m_row[colName]=std::stoi(strVal);
443 m_row[colName]=std::stoull(strVal);
448 m_row[colName]=std::stoull(strVal);
453 m_row[colName]=std::stoll(strVal);
458 m_row[colName]=std::stof(strVal);
463 m_row[colName]=std::stod(strVal);
472 m_row[colName]=thisVal.get<std::string>();
479 m_row[colName]=thisVal.get<std::string>();
484 throw std::runtime_error(
"UNTREATED TYPE!");
489 catch(json::exception& e){
490 std::cerr << e.what() << std::endl;
491 throw std::runtime_error(e.what());
498 for (
size_t i = 0; i < values.size(); ++i)
509 if(values[i].is_null()){
510 m_row[colName] =
nullptr;
516 m_row[colName] = values[i].get<
bool>();
519 m_row[colName] = values[i].get<
unsigned char>();
523 m_row[colName] = values[i].get<
int>();
527 m_row[colName] = values[i].get<
unsigned int>();
530 m_row[colName] = values[i].get<uint64_t>();
533 m_row[colName] = values[i].get<int64_t>();
537 m_row[colName] = values[i].get<
double>();
544 m_row[colName] = values[i].get<std::string>();
549 m_row[colName] = values[i].get<std::string>();
552 throw CommonCrestException(
"CrestContainer::parseData: Unsupported column type.");
558 if(!values.is_array())
559 std::cerr <<
"CrestContainer::parseData values is not array"<<std::endl;
561 std::cerr <<
"CrestContainer::parseData values number="<<values.size() <<
" m_payload_spec.size()="<<
m_payload_spec.size()<<std::endl;
562 throw CommonCrestException(
"CrestContainer::parseData: Mismatch in number of values.");
569 nlohmann::json j = j_in;
573 ss >> std::quoted(st);
577 if (j.contains(
"data") && j[
"data"].is_object())
582 else if(j.is_object()){
587 std::cerr <<
"CrestContainer::fromJson json:"<<j<<std::endl;
588 throw CommonCrestException(
"CrestContainer::fromJson: JSON is not a JSON object.");
590 std::vector<uint64_t> ret;
591 ret.push_back(since);
600 throw CommonCrestException(
"CrestContainer::fromJson: Unsupported type of payload.");
605 nlohmann::json j = j_in;
609 ss >> std::quoted(st);
612 nlohmann::json dcs_data;
613 if (j.contains(
"data") && j[
"data"].is_object())
615 dcs_data = j[
"data"];
619 std::vector<uint64_t> ret;
622 for (
const auto& [key, value] : dcs_data.items()){
623 uint64_t since=std::stoull(key);
625 ret.push_back(since);
637 for (
const auto &channel : j_in.items())
639 std::string channelKey = channel.key();
640 const auto &data_ch = channel.value();
641 nlohmann::json vecJson(json::value_t::array);
645 vecJson.push_back(data_ch);
647 for (
const auto &values : vecJson)
661 nlohmann::json resources = nlohmann::json::array();
663 nlohmann::json storeDto;
667 storeDto[
"since"] = key;
668 storeDto[
"data"] = value.dump();
669 storeDto[
"streamerInfo"] =
"";
670 resources.push_back(storeDto);
675 nlohmann::json storeDto;
676 nlohmann::json dcs_payload;
680 curPayload.update(value);
681 dcs_payload[std::to_string(key)]=curPayload;
682 if(period>0 && (key-since)>period){
683 storeDto[
"since"]=since;
684 storeDto[
"data"]=dcs_payload.dump();
685 storeDto[
"streamerInfo"] =
"";
686 resources.push_back(storeDto);
691 if(dcs_payload.size()>0){
692 storeDto[
"since"]=since;
693 storeDto[
"data"]=dcs_payload.dump();
694 storeDto[
"streamerInfo"] =
"";
695 resources.push_back(storeDto);
698 j[
"resources"] = resources;
699 j[
"format"] =
"StoreSetDto";
700 j[
"datatype"] =
"data";
701 j[
"size"] = resources.size();
bool compareStrTimestamp(std::string &as, std::string &bs)
Header file for CrestContainer class.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
std::string getJsonPayload()
It returns the json representation of the container.
std::vector< uint64_t > fromJson(uint64_t since, const nlohmann::json &j)
It reads a json object to fill the container.
bool isVectorPayload()
It sets the Vector mode of the container.
int getColumnIndex(const std::string &name)
It returns the index of the column with the given name.
nlohmann::json getStoreSetDto(uint64_t period=-1)
It return StoreSetDto in json format.
void dumpJsonToFile(const nlohmann::json &j, const std::string &filename)
It creates a file with the json representation of the container.
void parseOldFormat(const std::string &colName, const TypeId &typespec, const nlohmann::json &j)
void addIov(const uint64_t since)
It adds an IOV to the json object m_iov_data.
std::string getJsonIovData()
It returns the json representation of the container.
std::vector< std::string > channelIds()
Return list of channel id in string format.
void addRecord(const std::string &name, int number,...)
It adds a record to the payload.
void readCommonType(uint64_t since, const nlohmann::json &j_in)
nlohmann::json getPayloadSpec()
Return payload specification in json format.
void selectIov(const uint64_t since)
It select timestamp as active.
CrestContainer(ModeId mode=ModeId::Standard)
Constructor of CrestContainer.
uint64_t getFirstTime()
It return minimal timestamp in concainer.
void addColumn(const std::string &name, TypeId type)
It adds a column to the payload specification.
const nlohmann::json & getIovData()
Return selected IOV in json format.
void addExternalData(const std::string &channel_id, const nlohmann::json &data)
It associate the payload row to a channel_id.
nlohmann::json m_vector_data
nlohmann::json m_iov_data
const nlohmann::json getPayloadChannel(const std::string &channel_id)
Return payload in json format for selected channel id.
std::vector< std::pair< std::string, TypeId > > m_payload_spec
void parseData(const nlohmann::json &values)
It reads a json object, parse it and fill the container.
void addNullRecord(const std::string &name)
It adds a null value to the payload.
~CrestContainer()
Destructor of CrestContainer.
const std::vector< std::pair< std::string, TypeId > > & getMPayloadSpec()
Return tag specification in vector format.
std::map< uint64_t, nlohmann::json > m_full_data
void flush()
It reinitializes the containers.
nlohmann::json createRowArray(const nlohmann::json &data_row) const
const nlohmann::json & getPayload()
Return current payload in json format.
void setPayloadSpec(const nlohmann::json &j)
std::vector< uint64_t > readDcsFullType(const nlohmann::json &j_in)
const nlohmann::json & getRow()
void setIovData(const nlohmann::json &j)
void clear()
It clear data the container.
void addData(const std::string &channel_id)
It associate the payload row to a channel_id.
void putRow2Vector()
It adds row data to vector.
nlohmann::json readJsonFromFile(const std::string &filename, const std::string &spec_filename)
It reads a json file and returns the json object.
void setPayload(const nlohmann::json &j)
void setVectorPayload(bool isVectorPayload)
Set the Vectore mode of the container.
static const std::map< TypeId, std::string > s_typeToString
std::string number(const double &d, const std::string &s)