ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
dpSpec.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_dpm_libmerge
4  * @copyright ESO - European Southern Observatory
5  */
6 #ifndef DAQ_DPM_DP_SPEC_HPP
7 #define DAQ_DPM_DP_SPEC_HPP
8 #include <filesystem>
9 #include <optional>
10 #include <string>
11 #include <string_view>
12 #include <variant>
13 #include <vector>
14 
15 #include <nlohmann/json.hpp>
16 
17 #include <daq/fits/keyword.hpp>
18 
19 namespace daq {
20 
21 struct DpSpecError : std::invalid_argument {
22  using std::invalid_argument::invalid_argument;
23 };
24 
25 /**
26  * Close representation of the JSON structure but with stronger types.
27  */
28 struct DpSpec {
29  struct Filter {
30  std::vector<std::string> selection_patterns;
31  };
32  struct Transform {
33  std::vector<std::string> selection_patterns;
34  std::string regex;
35  std::string format;
36  };
37  using KeywordRule = std::variant<Filter, Transform>;
38  using KeywordRules = std::vector<KeywordRule>;
39 
41  std::string source_name;
44  };
45  struct SourceFitsFile {
46  std::string source_name;
47  std::string origin;
49  };
50  struct Target {
51  std::string file_id;
52  /**
53  * Optioal user chosen file prefix to make it easier to identify the produced file.
54  */
55  std::string file_prefix;
56  std::optional<SourceFitsFile> source;
57  };
58 
59  using SourceTypes = std::variant<SourceFitsKeywords, SourceFitsFile>;
60 
61  std::string id;
63  std::vector<SourceTypes> sources;
64 };
65 
66 /**
67  * Parse JSON to construct the DpSpec structure
68  *
69  * @param spec Data Product Specification to parse.
70  * @throw DpSpecError if JSON structure is invalid. Exception may be nested.
71  */
72 DpSpec ParseDpSpec(nlohmann::json const& spec);
73 
74 /**
75  * Describes parsed origin string into its components "host" and "path".
76  */
77 struct Origin {
78  /**
79  * @returns `host:path` if host is not empty, otherwise just `path`
80  */
81  std::string RsyncPath() const;
82  std::string host;
83  std::filesystem::path path;
84 };
85 
86 /**
87  * Parse origin string from DpSpec into component parts.
88  *
89  * Origin string use the format `host:path`, e.g. `10.0.0.1:/path/to/file` with host being optional.
90  */
91 Origin ParseSourceOrigin(std::string const& origin);
92 
93 } // namespace daq::dpm
94 
95 #endif // #ifndef DAQ_DPM_DP_SPEC_HPP
daq::DpSpec::SourceFitsFile
Definition: dpSpec.hpp:45
daq::DpSpec::Transform
Definition: dpSpec.hpp:32
daq::DpSpec::Transform::format
std::string format
Definition: dpSpec.hpp:35
daq::DpSpec
Close representation of the JSON structure but with stronger types.
Definition: dpSpec.hpp:28
daq::DpSpecError
Definition: dpSpec.hpp:21
daq::DpSpec::Target::file_id
std::string file_id
Definition: dpSpec.hpp:51
daq::Origin::RsyncPath
std::string RsyncPath() const
Definition: dpSpec.cpp:282
daq::Origin::path
std::filesystem::path path
Definition: dpSpec.hpp:83
keyword.hpp
Contains data structure for FITS keywords.
daq::DpSpec::sources
std::vector< SourceTypes > sources
Definition: dpSpec.hpp:63
daq::ParseDpSpec
DpSpec ParseDpSpec(Json const &json)
Parse JSON to construct the DpSpec structure.
Definition: dpSpec.cpp:211
daq::DpSpec::SourceFitsKeywords::source_name
std::string source_name
Definition: dpSpec.hpp:41
daq
Definition: asyncProcess.cpp:15
daq::DpSpec::Target::source
std::optional< SourceFitsFile > source
Definition: dpSpec.hpp:56
daq::DpSpec::SourceFitsKeywords
Definition: dpSpec.hpp:40
daq::DpSpec::KeywordRule
std::variant< Filter, Transform > KeywordRule
Definition: dpSpec.hpp:37
daq::DpSpec::id
std::string id
Definition: dpSpec.hpp:61
daq::DpSpec::SourceFitsKeywords::keywords
fits::KeywordVector keywords
Definition: dpSpec.hpp:42
daq::DpSpec::KeywordRules
std::vector< KeywordRule > KeywordRules
Definition: dpSpec.hpp:38
daq::DpSpec::Filter
Definition: dpSpec.hpp:29
daq::DpSpec::SourceFitsFile::keyword_rules
KeywordRules keyword_rules
Definition: dpSpec.hpp:48
daq::DpSpec::SourceTypes
std::variant< SourceFitsKeywords, SourceFitsFile > SourceTypes
Definition: dpSpec.hpp:59
daq::Origin::host
std::string host
Definition: dpSpec.hpp:82
daq::DpSpec::Transform::selection_patterns
std::vector< std::string > selection_patterns
Definition: dpSpec.hpp:33
daq::DpSpec::SourceFitsKeywords::keyword_rules
KeywordRules keyword_rules
Definition: dpSpec.hpp:43
daq::Origin
Describes parsed origin string into its components "host" and "path".
Definition: dpSpec.hpp:77
daq::DpSpec::target
Target target
Definition: dpSpec.hpp:62
daq::fits::KeywordVector
std::vector< KeywordVariant > KeywordVector
Vector of keywords.
Definition: keyword.hpp:414
daq::DpSpec::Transform::regex
std::string regex
Definition: dpSpec.hpp:34
daq::DpSpec::Target
Definition: dpSpec.hpp:50
daq::DpSpec::SourceFitsFile::source_name
std::string source_name
Definition: dpSpec.hpp:46
daq::DpSpec::Target::file_prefix
std::string file_prefix
Optioal user chosen file prefix to make it easier to identify the produced file.
Definition: dpSpec.hpp:55
daq::DpSpec::Filter::selection_patterns
std::vector< std::string > selection_patterns
Definition: dpSpec.hpp:30
daq::DpSpec::SourceFitsFile::origin
std::string origin
Definition: dpSpec.hpp:47
daq::ParseSourceOrigin
Origin ParseSourceOrigin(std::string const &origin_str)
Parse origin string from DpSpec into component parts.
Definition: dpSpec.cpp:251
daq::json
nlohmann::json json
Definition: json.cpp:20