ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
status.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <daq/status.hpp>
3 #include <ostream>
4 
5 #include <fmt/format.h>
6 
7 namespace daq {
8 
9 void SetAlert(std::vector<Alert>& alerts, Alert alert) {
10  auto it = find(alerts.begin(), alerts.end(), alert);
11  if (it != alerts.end()) {
12  // Replace
13  *it = std::move(alert);
14  } else {
15  // Or add
16  alerts.emplace_back(std::move(alert));
17  }
18 }
19 
20 bool ClearAlert(std::vector<Alert>& alerts, AlertId const& alert) {
21  auto it = std::remove(alerts.begin(), alerts.end(), alert);
22  if (it != alerts.end()) {
23  alerts.erase(it, alerts.end());
24  return true;
25  }
26  return false;
27 }
28 
29 Alert MakeAlert(std::string_view category, std::string key, std::string description) {
30  return Alert{{std::string(category), std::move(key)},
31  std::move(description),
32  Alert::TimePoint::clock::now()};
33 }
34 
35 Alert MakeAlert(AlertId id, std::string description) {
36  return Alert{std::move(id), std::move(description), Alert::TimePoint::clock::now()};
37 }
38 
39 AlertId MakeAlertId(std::string_view category, std::string key) {
40  return AlertId{std::string(category), std::move(key)};
41 }
42 
43 bool operator==(AlertId const& lhs, AlertId const& rhs) noexcept {
44  return lhs.key == rhs.key && lhs.category == rhs.category;
45 }
46 
47 bool operator!=(AlertId const& lhs, AlertId const& rhs) noexcept {
48  return !(lhs == rhs);
49 }
50 
51 bool operator==(Alert const& lhs, Alert const& rhs) noexcept {
52  return lhs.id == rhs.id;
53 }
54 
55 bool operator==(Alert const& lhs, AlertId const& rhs) noexcept {
56  return lhs.id == rhs;
57 }
58 
59 bool operator==(AlertId const& lhs, Alert const& rhs) noexcept {
60  return lhs == rhs.id;
61 }
62 
63 bool operator!=(Alert const& lhs, Alert const& rhs) noexcept {
64  return !(lhs == rhs);
65 }
66 
68  std::string id, std::string file_id, State state, bool error, TimePoint timestamp) noexcept
69  : id(std::move(id))
70  , file_id(std::move(file_id))
71  , state(state)
72  , error(error)
73  , alerts()
74  , timestamp(timestamp) {
75 }
76 
77 Status::Status(std::string id, std::string file_id) noexcept
78  : id(std::move(id))
79  , file_id(std::move(file_id))
80  , state(State::NotStarted)
81  , error(false)
82  , alerts()
83  , timestamp(TimePoint::clock::now()) {
84 }
85 
86 bool Status::operator==(Status const& rhs) const noexcept {
87  return id == rhs.id && file_id == rhs.file_id && state == rhs.state && error == rhs.error &&
88  alerts == rhs.alerts && result == rhs.result;
89 }
90 
91 bool Status::operator!=(Status const& rhs) const noexcept {
92  return !(*this == rhs);
93 }
94 
95 std::ostream& operator<<(std::ostream& os, Status const& s) {
96  os << "Status(id='" << s.id << "', file_id='" << s.file_id << "', state=" << s.state
97  << ", error=" << (s.error ? "true" : "false") << ", result='" << s.result << "')";
98  return os;
99 }
100 
102  if (m_status) {
103  m_status->DisableSignals();
104  }
105 }
106 
108  : m_status(nullptr) {
109  std::swap(m_status, other.m_status);
110 }
111 
114  // Reset this before stealing resources from other
115  Reset();
116 
117  std::swap(m_status, other.m_status);
118  return *this;
119 }
120 
122  Reset();
123 }
124 
126  if (!m_status) {
127  return;
128  }
129  // Signal
130  m_status->EnableSignals();
131  m_status->SignalChanges(true);
132 
133  m_status = nullptr;
134 }
135 
136 ObservableStatus::ObservableStatus(Status status) : m_status(std::move(status)) {
137 }
138 
139 ObservableStatus::ObservableStatus(std::string id, std::string file_id) noexcept
140  : m_status(std::move(id), std::move(file_id)) {
141 }
142 
144  if (m_status == status) {
145  return *this;
146  }
147  if (m_status.id != status.id) {
148  throw std::invalid_argument(fmt::format(
149  "Precondition not met (equality of DAQ IDs): {} != {}", m_status.id, status.id));
150  }
151  m_status = status;
152  SignalChanges();
153  return *this;
154 }
155 
156 bool ObservableStatus::operator==(ObservableStatus const& rhs) const noexcept {
157  return m_status == rhs.m_status;
158 }
159 
160 bool ObservableStatus::operator!=(ObservableStatus const& rhs) const noexcept {
161  return !(*this == rhs);
162 }
163 
164 bool ObservableStatus::operator==(Status const& rhs) const noexcept {
165  return m_status == rhs;
166 }
167 
168 bool ObservableStatus::operator!=(Status const& rhs) const noexcept {
169  return !(*this == rhs);
170 }
171 
172 std::string const& ObservableStatus::GetId() const noexcept {
173  return m_status.id;
174 }
175 
176 std::vector<Alert> const& ObservableStatus::GetAlerts() const noexcept {
177  return m_status.alerts;
178 }
179 
180 std::string const& ObservableStatus::GetFileId() const noexcept {
181  return m_status.file_id;
182 }
183 
185  return m_status.state;
186 }
187 
188 bool ObservableStatus::GetError() const noexcept {
189  return m_status.error;
190 }
191 
192 void ObservableStatus::SetState(State s, std::optional<bool> error) noexcept {
193  if (s == m_status.state) {
194  if (!error || error.value() == m_status.error) {
195  return;
196  }
197  }
198  m_status.state = s;
199  if (error.has_value()) {
200  m_status.error = *error;
201  }
202  SignalChanges();
203 }
204 
205 void ObservableStatus::SetError(bool error) noexcept {
206  if (error == m_status.error) {
207  return;
208  }
209  m_status.error = error;
210  SignalChanges();
211 }
212 
213 void ObservableStatus::SetResult(std::string result) {
214  m_status.result = std::move(result);
215  SignalChanges();
216 }
217 
219  ::daq::SetAlert(m_status.alerts, std::move(alert));
220  SignalChanges();
221 }
222 
224  if (::daq::ClearAlert(m_status.alerts, alert)) {
225  SignalChanges();
226  }
227 }
228 
229 void ObservableStatus::SignalChanges(bool force) noexcept {
230  m_status.timestamp = Status::TimePoint::clock::now();
231  try {
232  if (force || m_deferred_signals == 0) {
233  m_signal(*this);
234  }
235  } catch (...) {
236  }
237 }
238 
239 ObservableStatus::operator Status() const {
240  return m_status;
241 }
242 
243 Status const& ObservableStatus::GetStatus() const noexcept {
244  return m_status;
245 }
246 
248  assert(m_deferred_signals > 0);
249  --m_deferred_signals;
250 }
252  ++m_deferred_signals;
253 }
254 
255 std::ostream& operator<<(std::ostream& os, ObservableStatus const& s) {
256  os << "ObservableStatus(id='" << s.GetId() << "', file_id='" << s.GetFileId()
257  << "', state=" << s.GetState() << ", error=" << (s.GetError() ? "true" : "false") << ")";
258  return os;
259 }
260 
261 } // namespace daq
daq::ObservableStatus::EnableSignals
void EnableSignals() noexcept
Definition: status.cpp:247
daq::Status::Status
Status()=default
daq::State
State
Observable states of the data acquisition process.
Definition: state.hpp:39
daq::ObservableStatus::DeferSignal::DeferSignal
DeferSignal()=default
daq::ObservableStatus::DeferSignal::~DeferSignal
~DeferSignal() noexcept
Definition: status.cpp:121
daq::ObservableStatus::SetAlert
void SetAlert(Alert alert)
Set alert.
Definition: status.cpp:218
daq::Status::error
bool error
Definition: status.hpp:139
daq::Status::operator==
bool operator==(Status const &rhs) const noexcept
Definition: status.cpp:86
daq::MakeAlertId
AlertId MakeAlertId(std::string_view category, std::string key)
Definition: status.cpp:39
daq::ObservableStatus::DisableSignals
void DisableSignals() noexcept
Definition: status.cpp:251
daq::Status::alerts
std::vector< Alert > alerts
Active alerts.
Definition: status.hpp:143
daq::ObservableStatus::DeferSignal::Reset
void Reset() noexcept
If object is valid.
Definition: status.cpp:125
daq::ObservableStatus::operator=
ObservableStatus & operator=(ObservableStatus &&)=default
daq::ObservableStatus::DeferSignal::operator=
DeferSignal & operator=(DeferSignal &&) noexcept
Definition: status.cpp:113
daq::SetAlert
void SetAlert(std::vector< Alert > &alerts, Alert alert)
Set alert.
Definition: status.cpp:9
daq::ObservableStatus::SetResult
void SetResult(std::string result)
Set resulting data product path.
Definition: status.cpp:213
daq::ObservableStatus::GetState
State GetState() const noexcept
Definition: status.cpp:184
daq::ObservableStatus::DeferSignal
Defer signal changes until later time.
Definition: status.hpp:173
daq::ObservableStatus::SignalChanges
void SignalChanges(bool forced=false) noexcept
Signal changes to observers.
Definition: status.cpp:229
daq::ObservableStatus::GetError
bool GetError() const noexcept
Definition: status.cpp:188
daq::ObservableStatus::GetFileId
std::string const & GetFileId() const noexcept
Definition: status.cpp:180
daq
Definition: asyncProcess.cpp:15
daq::ObservableStatus::operator!=
bool operator!=(ObservableStatus const &rhs) const noexcept
Definition: status.cpp:160
daq::Status::TimePoint
std::chrono::time_point< std::chrono::steady_clock > TimePoint
Definition: status.hpp:121
daq::ObservableStatus
Stores data acquisition status and allows subscription to status changes.
Definition: status.hpp:161
daq::ObservableStatus::SetError
void SetError(bool error) noexcept
Set error flag for data acquisition.
Definition: status.cpp:205
daq::ObservableStatus::GetId
std::string const & GetId() const noexcept
Definition: status.cpp:172
daq::Alert
Describes an active Data Acquisition alert.
Definition: status.hpp:71
daq::ObservableStatus::operator==
bool operator==(ObservableStatus const &rhs) const noexcept
Definition: status.cpp:156
daq::Status::result
std::string result
Path to resulting data product.
Definition: status.hpp:148
daq::Status::operator!=
bool operator!=(Status const &rhs) const noexcept
Definition: status.cpp:91
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::ObservableStatus::ClearAlert
void ClearAlert(AlertId const &alert)
Clear alert.
Definition: status.cpp:223
daq::ClearAlert
bool ClearAlert(std::vector< Alert > &alerts, AlertId const &alert)
Clear alert.
Definition: status.cpp:20
status.hpp
Contains declaration for Status and ObservableStatus.
daq::MakeAlert
Alert MakeAlert(std::string_view category, std::string key, std::string description)
Construct alert.
Definition: status.cpp:29
daq::ObservableStatus::ObservableStatus
ObservableStatus(std::string id, std::string file_id) noexcept
Construct a new object.
Definition: status.cpp:139
daq::Status::id
std::string id
Definition: status.hpp:136
daq::ObservableStatus::GetStatus
Status const & GetStatus() const noexcept
Connect observer that is invoked when state is modified.
Definition: status.cpp:243
daq::ObservableStatus::GetAlerts
std::vector< Alert > const & GetAlerts() const noexcept
Definition: status.cpp:176
daq::AlertId
Uniquely identfies an alert.
Definition: status.hpp:52
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::operator!=
bool operator!=(AlertId const &lhs, AlertId const &rhs) noexcept
Definition: status.cpp:47
daq::ObservableStatus::SetState
void SetState(State s, std::optional< bool > error=std::nullopt) noexcept
Set state of data acquisition.
Definition: status.cpp:192
daq::operator==
bool operator==(DaqContext const &lhs, DaqContext const &rhs) noexcept
Definition: daqContext.cpp:12
error
Definition: main.cpp:23
daq::Status::file_id
std::string file_id
Definition: status.hpp:137