ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
conversion.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_libdaq
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Contains definitions for daq/conversion.hpp
7  */
8 #include <daq/conversion.hpp>
9 #include <fmt/format.h>
10 #include <string_view>
11 
12 #include <daq/state.hpp>
13 #include <daq/status.hpp>
14 #include <daq/json.hpp>
15 
16 namespace daq {
17 
18 daqif::DaqStatus& operator<<(daqif::DaqStatus& status, daq::Status const& rhs) {
19  using Seconds = std::chrono::duration<double, std::ratio<1>>;
20  status.setId(rhs.id);
21  status.setFileId(rhs.file_id);
22  auto full_state = MakeState(rhs.state);
23  status.setState(full_state.state);
24  status.setSubState(full_state.substate);
25  status.setError(rhs.error);
26  status.setResult(rhs.result);
27  status.setTimestamp(
28  std::chrono::time_point_cast<Seconds>(rhs.timestamp).time_since_epoch().count());
29 
30  nlohmann::json j = rhs.alerts;
31  status.setMessage(j.dump(2));
32 
33  return status;
34 }
35 
36 Status& operator<<(Status& status, daqif::DaqStatus const& rhs) {
37  using Seconds = std::chrono::duration<double, std::ratio<1>>;
38  status.id = rhs.getId();
39  status.file_id = rhs.getFileId();
40  status.state = MakeState({rhs.getState(), rhs.getSubState()});
41  status.error = rhs.getError();
42  status.result = rhs.getResult();
44  std::chrono::duration_cast<Status::TimePoint::duration>(Seconds(rhs.getTimestamp())));
45  return status;
46 }
47 
48 std::filesystem::space_info&
49 operator<<(std::filesystem::space_info& space, daqif::StorageStatus const& rhs) {
50  space.available = rhs.getAvailable();
51  space.capacity = rhs.getCapacity();
52  space.free = rhs.getFree();
53  return space;
54 }
55 
56 daqif::StorageStatus& operator<<(daqif::StorageStatus& storage, std::filesystem::space_info& rhs) {
57  storage.setAvailable(rhs.available);
58  storage.setCapacity(rhs.capacity);
59  storage.setFree(rhs.free);
60  return storage;
61 }
62 
63 daqif::FullState MakeState(State state) noexcept {
64  using namespace daq;
65  switch (state) {
66  case State::NotStarted:
67  return {daqif::StateAcquiring, daqif::NotStarted};
68  case State::Starting:
69  return {daqif::StateAcquiring, daqif::Starting};
70  case State::Acquiring:
71  return {daqif::StateAcquiring, daqif::Acquiring};
72  case State::Stopping:
73  return {daqif::StateAcquiring, daqif::Stopping};
74  case State::Stopped:
75  return {daqif::StateAcquiring, daqif::Stopped};
76  case State::AbortingAcquiring:
77  return {daqif::StateAcquiring, daqif::Aborting};
78 
79  case State::NotScheduled:
80  return {daqif::StateMerging, daqif::NotScheduled};
81  case State::Scheduled:
82  return {daqif::StateMerging, daqif::Scheduled};
84  return {daqif::StateMerging, daqif::Transferring};
85  case State::Merging:
86  return {daqif::StateMerging, daqif::Merging};
87  case State::Releasing:
88  return {daqif::StateMerging, daqif::Releasing};
89  case State::AbortingMerging:
90  return {daqif::StateMerging, daqif::Aborting};
91 
92  case State::Completed:
93  return {daqif::StateCompleted, daqif::Completed};
94  case State::Aborted:
95  return {daqif::StateCompleted, daqif::Aborted};
96  // GCOVR_EXCL_START
97  default:
98  return {daqif::StateUndefined, daqif::Undefined};
99  // GCOVR_EXCL_STOP
100  };
101 }
102 
104  using namespace daq;
105  switch (state.substate) {
106  case daqif::NotStarted:
107  return State::NotStarted;
108  case daqif::Starting:
109  return State::Starting;
110  case daqif::Acquiring:
111  return State::Acquiring;
112  case daqif::Stopping:
113  return State::Stopping;
114  case daqif::Stopped:
115  return State::Stopped;
116 
117  case daqif::NotScheduled:
118  return State::NotScheduled;
119  case daqif::Scheduled:
120  return State::Scheduled;
121  case daqif::Transferring:
122  return State::Transferring;
123  case daqif::Merging:
124  return State::Merging;
125  case daqif::Releasing:
126  return State::Releasing;
127 
128  case daqif::Completed:
129  return State::Completed;
130 
131  case daqif::Aborting:
132  return (state.state == daqif::StateAcquiring) ? State::AbortingAcquiring
133  : State::AbortingMerging;
134  case daqif::Aborted:
135  return State::Aborted;
136  default:
137  throw std::invalid_argument(
138  fmt::format("Invalid daqif::DaqSubState value {:d}", state.substate));
139  };
140 }
141 
142 std::string_view ToString(daqif::DaqState state) noexcept {
143  using namespace std::string_view_literals;
144  switch (state) {
145  case daqif::StateAcquiring:
146  return "StateAcquiring";
147  case daqif::StateMerging:
148  return "StateMerging";
149  case daqif::StateCompleted:
150  return "StateCompleted";
151  case daqif::DaqState::StateUndefined:
152  [[fallthrough]];
153  default:
154  return "Undefined";
155  };
156 }
157 
158 std::string_view ToString(daqif::DaqSubState state) noexcept {
159  using namespace std::string_view_literals;
160  switch (state) {
161  case daqif::DaqSubState::NotStarted:
162  return "NotStarted";
163  case daqif::DaqSubState::Starting:
164  return "Starting";
165  case daqif::DaqSubState::Acquiring:
166  return "Acquiring";
167  case daqif::DaqSubState::Stopping:
168  return "Stopping";
169  case daqif::DaqSubState::Stopped:
170  return "Stopped";
171  case daqif::DaqSubState::Aborting:
172  return "Aborting";
173  case daqif::DaqSubState::Aborted:
174  return "Aborted";
175  case daqif::DaqSubState::NotScheduled:
176  return "NotScheduled";
177  case daqif::DaqSubState::Scheduled:
178  return "Scheduled";
179  case daqif::DaqSubState::Transferring:
180  return "Transferring";
181  case daqif::DaqSubState::Merging:
182  return "Merging";
183  case daqif::DaqSubState::Releasing:
184  return "Releasing";
185  case daqif::DaqSubState::Completed:
186  return "Completed";
187  case daqif::DaqSubState::Undefined:
188  [[fallthrough]];
189  default:
190  return "Undefined";
191  };
192 }
193 
194 } // namespace daq
daq::State
State
Observable states of the data acquisition process.
Definition: state.hpp:39
daq::Status::error
bool error
Definition: status.hpp:139
daqif::FullState
Describes the full state and substate.
Definition: state.hpp:17
daq::Status::alerts
std::vector< Alert > alerts
Active alerts.
Definition: status.hpp:143
daq::State::Transferring
@ Transferring
Input files are being transferred.
daq::Status::timestamp
TimePoint timestamp
Definition: status.hpp:149
daqif::FullState::state
DaqState state
Definition: state.hpp:18
conversion.hpp
Contains support functions for daqif.
daq
Definition: asyncProcess.cpp:15
daq::Status::TimePoint
std::chrono::time_point< std::chrono::steady_clock > TimePoint
Definition: status.hpp:121
daq::State::Merging
@ Merging
DAQ is being merged.
daq::Status::result
std::string result
Path to resulting data product.
Definition: status.hpp:148
json.hpp
Declares JSON support for serialization.
daq::operator<<
daqif::DaqStatus & operator<<(daqif::DaqStatus &status, daq::Status const &rhs)
Convert daq::Status -> daqif::DaqStatus by populating from rhs.
Definition: conversion.cpp:18
daq::ToString
std::string_view ToString(daqif::DaqState state) noexcept
Definition: conversion.cpp:142
status.hpp
Contains declaration for Status and ObservableStatus.
daqif::FullState::substate
DaqSubState substate
Definition: state.hpp:19
daq::Status::id
std::string id
Definition: status.hpp:136
state.hpp
Declares daq::State and related functions.
daq::Status
Non observable status object that keeps stores status of data acquisition.
Definition: status.hpp:120
daq::Status::state
State state
Definition: status.hpp:138
daq::MakeState
daqif::FullState MakeState(State state) noexcept
Converts daq::State to DaqSubstate.
Definition: conversion.cpp:63
daq::Status::file_id
std::string file_id
Definition: status.hpp:137
daq::State::DAQ states handled by OCM.
DAQ states handled by OCM.
Initial state of data acquisition.
daq::json
nlohmann::json json
Definition: json.cpp:20