ifw-daq  3.0.0-pre2
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};
77  return {daqif::StateAcquiring, daqif::Aborting};
78 
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};
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
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
Declares JSON support for serialization.
Contains support functions for daqif.
Declares daq::State and related functions.
daqif::DaqStatus & operator<<(daqif::DaqStatus &status, daq::Status const &rhs)
Convert daq::Status -> daqif::DaqStatus by populating from rhs.
Definition: conversion.cpp:18
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:142
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.
@ 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.
@ Transferring
Input files are being transferred.
@ 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.
DaqState state
Definition: state.hpp:18
DaqSubState substate
Definition: state.hpp:19
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:124
std::chrono::time_point< std::chrono::steady_clock > TimePoint
Definition: status.hpp:125
State state
Definition: status.hpp:142
std::string id
Definition: status.hpp:140
std::string result
Path to resulting data product.
Definition: status.hpp:152
std::string file_id
Definition: status.hpp:141
bool error
Definition: status.hpp:143
std::vector< Alert > alerts
Active alerts.
Definition: status.hpp:147
TimePoint timestamp
Definition: status.hpp:153