ifw-daq  3.0.1
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/json.hpp>
13 #include <daq/state.hpp>
14 #include <daq/status.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};
77  return {daqif::StateAcquiring, daqif::Aborting};
78 
80  return {daqif::StateMerging, daqif::NotScheduled};
81  case State::Scheduled:
82  return {daqif::StateMerging, daqif::Scheduled};
83  case State::Collecting:
84  return {daqif::StateMerging, daqif::Collecting};
85  case State::Merging:
86  return {daqif::StateMerging, daqif::Merging};
87  case State::Releasing:
88  return {daqif::StateMerging, daqif::Releasing};
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::Collecting:
122  return State::Collecting;
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
134  case daqif::Aborted:
135  return State::Aborted;
136 
137  // GCOVR_EXCL_START
138  default:
139  fmt::print(
140  stderr, "Invalid daqif::DaqSubState value {:d} (undefined behaviour)", state.substate);
141  std::abort();
142  // GCOVR_EXCL_STOP
143  };
144 }
145 
146 std::string_view ToString(daqif::DaqState state) noexcept {
147  using namespace std::string_view_literals;
148  switch (state) {
149  case daqif::StateAcquiring:
150  return "StateAcquiring";
151  case daqif::StateMerging:
152  return "StateMerging";
153  case daqif::StateCompleted:
154  return "StateCompleted";
155  case daqif::DaqState::StateUndefined:
156  [[fallthrough]];
157  default:
158  return "Undefined";
159  };
160 }
161 
162 std::string_view ToString(daqif::DaqSubState state) noexcept {
163  using namespace std::string_view_literals;
164  switch (state) {
165  case daqif::DaqSubState::NotStarted:
166  return "NotStarted";
167  case daqif::DaqSubState::Starting:
168  return "Starting";
169  case daqif::DaqSubState::Acquiring:
170  return "Acquiring";
171  case daqif::DaqSubState::Stopping:
172  return "Stopping";
173  case daqif::DaqSubState::Stopped:
174  return "Stopped";
175  case daqif::DaqSubState::Aborting:
176  return "Aborting";
177  case daqif::DaqSubState::Aborted:
178  return "Aborted";
179  case daqif::DaqSubState::NotScheduled:
180  return "NotScheduled";
181  case daqif::DaqSubState::Scheduled:
182  return "Scheduled";
183  case daqif::DaqSubState::Collecting:
184  return "Collecting";
185  case daqif::DaqSubState::Merging:
186  return "Merging";
187  case daqif::DaqSubState::Releasing:
188  return "Releasing";
189  case daqif::DaqSubState::Completed:
190  return "Completed";
191  case daqif::DaqSubState::Undefined:
192  [[fallthrough]];
193  default:
194  return "Undefined";
195  };
196 }
197 
198 } // 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:63
std::string_view ToString(daqif::DaqState state) noexcept
Definition: conversion.cpp:146
std::ostream & operator<<(std::ostream &os, AsyncProcessIf const &proc)
Formats proc representation in the form [<pid>] <args>
State
Observable states of the data acquisition process.
Definition: state.hpp:39
@ 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:153
State state
Definition: status.hpp:176
std::string id
Definition: status.hpp:174
std::string result
Path to resulting data product.
Definition: status.hpp:193
std::string file_id
Definition: status.hpp:175
bool error
Definition: status.hpp:177
std::vector< Alert > alerts
Active alerts.
Definition: status.hpp:181
std::chrono::time_point< std::chrono::system_clock > TimePoint
Definition: status.hpp:154
TimePoint timestamp
Timestamp of last update.
Definition: status.hpp:198