ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
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/json.hpp>
13#include <daq/state.hpp>
14#include <daq/status.hpp>
15
16#include <daqif/malTypes.hpp>
17
18namespace daq {
19
20daqif::DaqStatus& operator<<(daqif::DaqStatus& status, daq::Status const& rhs) {
21 using Seconds = std::chrono::duration<double, std::ratio<1>>;
22 status.setId(rhs.id);
23 status.setFileId(rhs.file_id);
24 auto full_state = MakeState(rhs.state);
25 status.setState(full_state.state);
26 status.setSubState(full_state.substate);
27 status.setError(HasError(rhs));
28 status.setResult(rhs.result);
29 status.setTimestamp(
30 std::chrono::time_point_cast<Seconds>(rhs.timestamp).time_since_epoch().count());
31
32 nlohmann::json j = rhs.alerts;
33 status.setMessage(j.dump(2));
34
35 return status;
36}
37
38Status& operator<<(Status& status, daqif::DaqStatus const& rhs) {
39 using Seconds = std::chrono::duration<double, std::ratio<1>>;
40 status.id = rhs.getId();
41 status.file_id = rhs.getFileId();
42 status.state = MakeState({rhs.getState(), rhs.getSubState()});
43 status.result = rhs.getResult();
45 std::chrono::duration_cast<Status::TimePoint::duration>(Seconds(rhs.getTimestamp())));
46 return status;
47}
48
49daqif::InternalDaqStatus& operator<<(daqif::InternalDaqStatus& status, Status const& rhs) {
50 nlohmann::json j = rhs;
51 status.setPayload(std::make_shared<daqif::Blob>(j.dump(2)));
52 return status;
53}
54
55Status& operator<<(Status& status, daqif::InternalDaqStatus const& rhs) {
56 std::string_view sv = AsStringView(*rhs.getPayload());
57 auto json = nlohmann::json::parse(std::begin(sv), std::end(sv));
58 status = json.get<Status>();
59 return status;
60}
61
62std::filesystem::space_info&
63operator<<(std::filesystem::space_info& space, daqif::StorageStatus const& rhs) {
64 space.available = rhs.getAvailable();
65 space.capacity = rhs.getCapacity();
66 space.free = rhs.getFree();
67 return space;
68}
69
70daqif::StorageStatus& operator<<(daqif::StorageStatus& storage, std::filesystem::space_info& rhs) {
71 storage.setAvailable(rhs.available);
72 storage.setCapacity(rhs.capacity);
73 storage.setFree(rhs.free);
74 return storage;
75}
76
78 using namespace daq;
79 switch (state) {
81 return {daqif::StateAcquiring, daqif::NotStarted};
82 case State::Starting:
83 return {daqif::StateAcquiring, daqif::Starting};
85 return {daqif::StateAcquiring, daqif::Acquiring};
86 case State::Stopping:
87 return {daqif::StateAcquiring, daqif::Stopping};
88 case State::Stopped:
89 return {daqif::StateAcquiring, daqif::Stopped};
91 return {daqif::StateAcquiring, daqif::Aborting};
92
94 return {daqif::StateMerging, daqif::NotScheduled};
96 return {daqif::StateMerging, daqif::Scheduled};
98 return {daqif::StateMerging, daqif::Collecting};
99 case State::Merging:
100 return {daqif::StateMerging, daqif::Merging};
101 case State::Releasing:
102 return {daqif::StateMerging, daqif::Releasing};
104 return {daqif::StateMerging, daqif::Aborting};
105
106 case State::Completed:
107 return {daqif::StateCompleted, daqif::Completed};
108 case State::Aborted:
109 return {daqif::StateCompleted, daqif::Aborted};
110 // GCOVR_EXCL_START
111 default:
112 return {daqif::StateUndefined, daqif::Undefined};
113 // GCOVR_EXCL_STOP
114 };
115}
116
118 using namespace daq;
119 switch (state.substate) {
120 case daqif::NotStarted:
121 return State::NotStarted;
122 case daqif::Starting:
123 return State::Starting;
124 case daqif::Acquiring:
125 return State::Acquiring;
126 case daqif::Stopping:
127 return State::Stopping;
128 case daqif::Stopped:
129 return State::Stopped;
130
131 case daqif::NotScheduled:
132 return State::NotScheduled;
133 case daqif::Scheduled:
134 return State::Scheduled;
135 case daqif::Collecting:
136 return State::Collecting;
137 case daqif::Merging:
138 return State::Merging;
139 case daqif::Releasing:
140 return State::Releasing;
141
142 case daqif::Completed:
143 return State::Completed;
144
145 case daqif::Aborting:
146 return (state.state == daqif::StateAcquiring) ? State::AbortingAcquiring
148 case daqif::Aborted:
149 return State::Aborted;
150
151 // GCOVR_EXCL_START
152 default:
153 fmt::print(
154 stderr, "Invalid daqif::DaqSubState value {:d} (undefined behaviour)", state.substate);
155 std::abort();
156 // GCOVR_EXCL_STOP
157 };
158}
159
160std::string_view ToString(daqif::DaqState state) noexcept {
161 using namespace std::string_view_literals;
162 switch (state) {
163 case daqif::StateAcquiring:
164 return "StateAcquiring";
165 case daqif::StateMerging:
166 return "StateMerging";
167 case daqif::StateCompleted:
168 return "StateCompleted";
169 case daqif::DaqState::StateUndefined:
170 [[fallthrough]];
171 default:
172 return "Undefined";
173 };
174}
175
176std::string_view ToString(daqif::DaqSubState state) noexcept {
177 using namespace std::string_view_literals;
178 switch (state) {
179 case daqif::DaqSubState::NotStarted:
180 return "NotStarted";
181 case daqif::DaqSubState::Starting:
182 return "Starting";
183 case daqif::DaqSubState::Acquiring:
184 return "Acquiring";
185 case daqif::DaqSubState::Stopping:
186 return "Stopping";
187 case daqif::DaqSubState::Stopped:
188 return "Stopped";
189 case daqif::DaqSubState::Aborting:
190 return "Aborting";
191 case daqif::DaqSubState::Aborted:
192 return "Aborted";
193 case daqif::DaqSubState::NotScheduled:
194 return "NotScheduled";
195 case daqif::DaqSubState::Scheduled:
196 return "Scheduled";
197 case daqif::DaqSubState::Collecting:
198 return "Collecting";
199 case daqif::DaqSubState::Merging:
200 return "Merging";
201 case daqif::DaqSubState::Releasing:
202 return "Releasing";
203 case daqif::DaqSubState::Completed:
204 return "Completed";
205 case daqif::DaqSubState::Undefined:
206 [[fallthrough]];
207 default:
208 return "Undefined";
209 };
210}
211
212} // namespace daq
Declares JSON support for serialization.
Contains support functions for daqif.
Declares daq::State and related functions.
daqif::FullState MakeState(State state) noexcept
Converts daq::State to DaqSubstate.
Definition: conversion.cpp:77
std::string_view ToString(daqif::DaqState state) noexcept
Definition: conversion.cpp:160
std::ostream & operator<<(std::ostream &os, AsyncProcessIf const &proc)
Formats proc representation in the form [<pid>] <args>
bool HasError(Status const &status) noexcept
Definition: status.cpp:179
State
Observable states of the data acquisition process.
Definition: state.hpp:41
@ Completed
Completed DAQ.
@ NotScheduled
Before daq is acknowledged by dpm it remains in NotScheduled.
@ Scheduled
daq is acknowledged by dpm and is scheduled for merging (i.e.
@ Releasing
Releasing Data Product to receivers.
@ Collecting
Input files are being collected.
@ Aborted
Data acquisition has been aborted by user.
@ Merging
DAQ is being merged.
@ Stopping
Transitional state between Acquiring and Stopped.
@ AbortingMerging
Transitional state for aborting during merging.
@ Acquiring
All data sources have reported data acquisition is in progress.
@ Stopped
All data sources have reported they have stopped acquiring data.
@ Starting
Transitional state between NotStarted and Acquiring when sources have not begun acquiring data yet.
@ AbortingAcquiring
Transitional state for aborting during acquisition.
@ NotStarted
Initial state of data acquisition.
Describes the full state and substate.
Definition: state.hpp:17
Contains declaration for Status and ObservableStatus.
Non observable status object that keeps stores status of data acquisition.
Definition: status.hpp:164
State state
Definition: status.hpp:186
std::string id
Definition: status.hpp:184
std::string result
Path to resulting data product.
Definition: status.hpp:202
std::string file_id
Definition: status.hpp:185
std::vector< Alert > alerts
Active alerts.
Definition: status.hpp:190
std::chrono::time_point< std::chrono::system_clock > TimePoint
Definition: status.hpp:165
TimePoint timestamp
Timestamp of last update.
Definition: status.hpp:207