Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jsonable class and related method #14

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ find_package(genconfig REQUIRED)
find_package(ers REQUIRED)
find_package(logging REQUIRED)
find_package(oksdbinterfaces REQUIRED)

find_package(nlohmann_json REQUIRED)

#find_package(Boost COMPONENTS unit_test_framework REQUIRED)

Expand All @@ -22,7 +22,7 @@ daq_oks_codegen(dunedaq.schema.xml)
daq_add_library(dalMethods.cpp
test_circular_dependency.cpp disabled-components.cpp
LINK_LIBRARIES oksdbinterfaces::oksdbinterfaces okssystem::okssystem
logging::logging)
logging::logging nlohmann_json::nlohmann_json)


daq_add_application(listApps list_apps.cxx
Expand Down
1 change: 1 addition & 0 deletions cmake/coredalConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ find_dependency(genconfig)
find_dependency(ers)
find_dependency(logging)
find_dependency(oksdbinterfaces)
find_package(nlohmann_json)

# Figure out whether or not this dependency is an installed package or
# in repo form
Expand Down
17 changes: 16 additions & 1 deletion include/coredal/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "oksdbinterfaces/Configuration.hpp"
#include "oksdbinterfaces/DalObject.hpp"
#include "nlohmann/json.hpp"


namespace dunedaq {
Expand Down Expand Up @@ -40,7 +41,21 @@ namespace coredal {

const dunedaq::coredal::Session * get_session(dunedaq::oksdbinterfaces::Configuration& conf, const std::string& name, unsigned long rlevel = 10, const std::vector<std::string> * rclasses = nullptr);


template <typename T> void add_json_value(oksdbinterfaces::ConfigObject& obj,
std::string& name,
bool multi_value,
nlohmann::json& attributes) {
if (!multi_value) {
T value;
obj.get(name, value);
attributes[name] = value;
}
else {
std::vector<T> value_vector;
obj.get(name, value_vector);
attributes[name] = nlohmann::json(value_vector);
}
}
} // namespace coredal


Expand Down
8 changes: 7 additions & 1 deletion schema/coredal/dunedaq.schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@

<oks-schema>

<info name="" type="" num-of-items="42" oks-format="schema" oks-version="862f2957270" created-by="jcfree" created-on="mu2edaq13.fnal.gov" creation-time="20230123T223700" last-modified-by="eflumerf" last-modified-on="ironvirt9.mshome.net" last-modification-time="20240207T101454"/>
<info name="" type="" num-of-items="43" oks-format="schema" oks-version="cf3a4f0 built &quot;Aug 18 2023&quot;" created-by="jcfree" created-on="mu2edaq13.fnal.gov" creation-time="20230123T223700" last-modified-by="gjc" last-modified-on="21d162e26c87" last-modification-time="20240212T103039"/>

<class name="Application" description="A software executable" is-abstract="yes">
<attribute name="application_name" description="Name of the application binary to run." type="string" is-not-null="yes"/>
Expand Down Expand Up @@ -185,6 +185,12 @@
<attribute name="numa_id" type="u8" init-value="0" is-not-null="yes"/>
</class>

<class name="Jsonable">
<method name="to_json" description="">
<method-implementation language="c++" prototype="nlohmann::json to_json(bool diect=false) const" body="BEGIN_HEADER_PROLOGUE&#xA;#include &quot;nlohmann/json.hpp&quot;&#xA;END_HEADER_PROLOGUE"/>
</method>
</class>

<class name="NetworkConnection">
<superclass name="Connection"/>
<attribute name="connection_type" description="Type of the network connection " type="enum" range="kSendRecv,kPubSub" init-value="kSendRecv" is-not-null="yes"/>
Expand Down
101 changes: 101 additions & 0 deletions src/dalMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "coredal/Component.hpp"
#include "coredal/DaqApplication.hpp"
#include "coredal/DaqModule.hpp"
#include "coredal/Jsonable.hpp"
#include "coredal/Resource.hpp"
#include "coredal/ResourceSetAND.hpp"
#include "coredal/ResourceSetOR.hpp"
Expand All @@ -20,7 +21,10 @@

#include "test_circular_dependency.hpp"

#include "nlohmann/json.hpp"
#include "oksdbinterfaces/ConfigObject.hpp"
#include "oksdbinterfaces/Configuration.hpp"
#include "oksdbinterfaces/Schema.hpp"

#include <list>
#include <set>
Expand Down Expand Up @@ -175,4 +179,101 @@ DaqApplication::get_used_hostresources() const {
return res;
}

nlohmann::json get_json_config(oksdbinterfaces::Configuration& confdb,
const std::string& class_name,
const std::string& uid,
bool direct_only) {
using nlohmann::json;
using namespace oksdbinterfaces;
TLOG_DBG(9) << "Getting attributes for " << uid << " of class " << class_name;
json attributes;
auto class_info = confdb.get_class_info(class_name);
ConfigObject obj;
confdb.get(class_name, uid, obj);
for (auto attr : class_info.p_attributes) {
if (attr.p_type == type_t::u8_type) {
add_json_value<uint8_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::u16_type) {
add_json_value<uint16_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::u32_type) {
add_json_value<uint32_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::u64_type) {
add_json_value<uint64_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::s8_type) {
add_json_value<int8_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::s16_type) {
add_json_value<int16_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::s32_type ||
attr.p_type == type_t::s16_type) {
add_json_value<int32_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::s64_type) {
add_json_value<int64_t>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::float_type) {
add_json_value<float>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::double_type) {
add_json_value<double>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if (attr.p_type == type_t::bool_type) {
add_json_value<bool>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
else if ((attr.p_type == type_t::string_type) ||
(attr.p_type == type_t::enum_type) ||
(attr.p_type == type_t::date_type) ||
(attr.p_type == type_t::time_type)) {
add_json_value<std::string>(obj, attr.p_name, attr.p_is_multi_value, attributes);
}
}
if (!direct_only) {
TLOG_DBG(9) << "Processing relationships";
for (auto iter: class_info.p_relationships) {
std::string rel_name = iter.p_name;
if (iter.p_cardinality == cardinality_t::zero_or_one ||
iter.p_cardinality == cardinality_t::only_one) {
ConfigObject rel_obj;
obj.get(rel_name, rel_obj);
if (!rel_obj.is_null()) {
TLOG_DBG(9) << "Getting attibute of relationship " << rel_name;
attributes[rel_name] = get_json_config(confdb, rel_obj.class_name(),
rel_obj.UID(),
direct_only);
}
else {
TLOG_DBG(9) << "Relationship " << rel_name << " not set";
}
}
else {
TLOG_DBG(9) << "Relationship " << rel_name << " is multi value. "
<< "Getting attibutes for relationship.";
std::vector<ConfigObject> rel_vec;
obj.get(rel_name, rel_vec);
std::vector<json> configs;
for (auto rel_obj : rel_vec) {
TLOG_DBG(9) << "Getting attibute of relationship " << rel_obj.UID();
auto rel_conf = get_json_config(confdb, rel_obj.class_name(), rel_obj.UID(),
direct_only);
configs.push_back(rel_conf);
}
attributes[rel_name] = configs;
}
}
}
json json_config;
json_config[uid] = attributes;
return json_config;
}

nlohmann::json Jsonable::to_json(bool direct_only) const {

return get_json_config(p_db, class_name(), UID(), direct_only);
}

}
Loading