15 #include <fmt/format.h>
20 auto it = find(alerts.begin(), alerts.end(), alert);
21 if (it != alerts.end()) {
23 *it = std::move(alert);
26 alerts.emplace_back(std::move(alert));
31 auto it = std::remove(alerts.begin(), alerts.end(), alert);
32 if (it != alerts.end()) {
33 alerts.erase(it, alerts.end());
39 Alert MakeAlert(std::string_view category, std::string key, std::string description) {
40 return Alert{{std::string(category), std::move(key)},
41 std::move(description),
42 Alert::TimePoint::clock::now()};
46 return Alert{std::move(
id), std::move(description), Alert::TimePoint::clock::now()};
50 return AlertId{std::string(category), std::move(key)};
54 return lhs.key == rhs.key && lhs.category == rhs.category;
67 return lhs.id == rhs.id;
83 auto t = std::chrono::system_clock::to_time_t(alert.
timestamp);
84 os << std::put_time(std::gmtime(&t),
"%FT%TZ") <<
" (" << alert.
id
89 std::ostream&
operator<<(std::ostream& os, std::vector<Alert>
const& alerts) {
91 for (
auto const& alert : alerts) {
92 os <<
"[" << count <<
"]: " << alert <<
"\n";
101 , file_id(std::move(file_id))
105 , timestamp(timestamp) {
110 , file_id(std::move(file_id))
114 , timestamp(TimePoint::clock::now()) {
118 return id == rhs.id && file_id == rhs.file_id && state == rhs.state &&
error == rhs.error &&
119 alerts == rhs.alerts && result == rhs.result;
123 return !(*
this == rhs);
127 os <<
"Status(id='" << s.
id <<
"', file_id='" << s.
file_id <<
"', state=" << s.
state
128 <<
", error=" << (s.
error ?
"true" :
"false") <<
", result='" << s.
result <<
"')";
139 : m_status(
nullptr) {
140 std::swap(m_status, other.m_status);
148 std::swap(m_status, other.m_status);
161 m_status->EnableSignals();
162 m_status->SignalChanges(
true);
171 : m_status(std::move(
id), std::move(file_id)) {
175 if (m_status == status) {
178 if (m_status.
id != status.
id) {
179 throw std::invalid_argument(fmt::format(
180 "Precondition not met (equality of DAQ IDs): {} != {}", m_status.
id, status.
id));
188 return m_status == rhs.m_status;
192 return !(*
this == rhs);
196 return m_status == rhs;
200 return !(*
this == rhs);
216 return m_status.
state;
220 return m_status.
error;
224 if (s == m_status.state) {
225 if (!
error ||
error.value() == m_status.error) {
230 if (
error.has_value()) {
231 m_status.error = *
error;
237 if (
error == m_status.error) {
240 m_status.error =
error;
245 m_status.
result = std::move(result);
261 m_status.timestamp = Status::TimePoint::clock::now();
263 if (force || m_deferred_signals == 0) {
270 ObservableStatus::operator
Status()
const {
279 assert(m_deferred_signals > 0);
280 --m_deferred_signals;
283 ++m_deferred_signals;
287 os <<
"ObservableStatus(id='" << s.
GetId() <<
"', file_id='" << s.
GetFileId()
288 <<
"', state=" << s.
GetState() <<
", error=" << (s.
GetError() ?
"true" :
"false") <<
")";
Defer signal changes until later time.
void Reset() noexcept
If object is valid.
DeferSignal & operator=(DeferSignal &&) noexcept
Stores data acquisition status and allows subscription to status changes.
void SignalChanges(bool forced=false) noexcept
Signal changes to observers.
State GetState() const noexcept
void SetError(bool error) noexcept
Set error flag for data acquisition.
ObservableStatus & operator=(ObservableStatus &&)=default
Status const & GetStatus() const noexcept
Connect observer that is invoked when state is modified.
void ClearAlert(AlertId const &alert)
Clear alert.
bool operator==(ObservableStatus const &rhs) const noexcept
void SetState(State s, std::optional< bool > error=std::nullopt) noexcept
Set state of data acquisition.
void DisableSignals() noexcept
void SetResult(std::string result)
Set resulting data product path.
void EnableSignals() noexcept
std::string const & GetFileId() const noexcept
bool GetError() const noexcept
bool operator!=(ObservableStatus const &rhs) const noexcept
void SetAlert(Alert alert)
Set alert.
std::vector< Alert > const & GetAlerts() const noexcept
ObservableStatus(std::string id, std::string file_id) noexcept
Construct a new object.
std::string const & GetId() const noexcept
daqif::DaqStatus & operator<<(daqif::DaqStatus &status, daq::Status const &rhs)
Convert daq::Status -> daqif::DaqStatus by populating from rhs.
AlertId MakeAlertId(std::string_view category, std::string key)
std::string key
Unique key for each alert.
bool operator==(DaqContext const &lhs, DaqContext const &rhs) noexcept
bool operator!=(AlertId const &lhs, AlertId const &rhs) noexcept
std::string category
Standardized category.
void SetAlert(std::vector< Alert > &alerts, Alert alert)
Set alert.
bool ClearAlert(std::vector< Alert > &alerts, AlertId const &alert)
Clear alert.
State
Observable states of the data acquisition process.
@ NotStarted
Initial state of data acquisition.
Alert MakeAlert(std::string_view category, std::string key, std::string description)
Construct alert.
Describes an active Data Acquisition alert.
Uniquely identfies an alert.
Contains declaration for Status and ObservableStatus.
Non observable status object that keeps stores status of data acquisition.
bool operator!=(Status const &rhs) const noexcept
bool operator==(Status const &rhs) const noexcept
std::chrono::time_point< std::chrono::steady_clock > TimePoint
std::string result
Path to resulting data product.
std::vector< Alert > alerts
Active alerts.