|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 EPAM Systems, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <core/common/tools/logger.hpp> |
| 8 | + |
| 9 | +#include "alerts.hpp" |
| 10 | + |
| 11 | +namespace aos::cm::alerts { |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +/*********************************************************************************************************************** |
| 16 | + * Static |
| 17 | + **********************************************************************************************************************/ |
| 18 | + |
| 19 | +class GetTimestamp : public StaticVisitor<Time> { |
| 20 | +public: |
| 21 | + Res Visit(const cloudprotocol::AlertItem& alert) const { return alert.mTimestamp; } |
| 22 | +}; |
| 23 | + |
| 24 | +class SetTimestamp : public StaticVisitor<void> { |
| 25 | +public: |
| 26 | + explicit SetTimestamp(const Time& time) |
| 27 | + : mTime(time) |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + template <typename T> |
| 32 | + Res Visit(T& val) const |
| 33 | + { |
| 34 | + val.mTimestamp = mTime; |
| 35 | + } |
| 36 | + |
| 37 | +private: |
| 38 | + Time mTime; |
| 39 | +}; |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +/*********************************************************************************************************************** |
| 44 | + * Public |
| 45 | + **********************************************************************************************************************/ |
| 46 | + |
| 47 | +Error Alerts::Init(const Config& config, communication::CommunicationItf& communication) |
| 48 | +{ |
| 49 | + LOG_DBG() << "Initialize alerts"; |
| 50 | + |
| 51 | + mConfig = config; |
| 52 | + mCommunication = &communication; |
| 53 | + |
| 54 | + return ErrorEnum::eNone; |
| 55 | +} |
| 56 | + |
| 57 | +Error Alerts::Start() |
| 58 | +{ |
| 59 | + LockGuard lock {mMutex}; |
| 60 | + |
| 61 | + LOG_DBG() << "Start alerts module"; |
| 62 | + |
| 63 | + if (mIsRunning) { |
| 64 | + return ErrorEnum::eWrongState; |
| 65 | + } |
| 66 | + |
| 67 | + mIsRunning = true; |
| 68 | + |
| 69 | + return mSendTimer.Start(mConfig.mSendPeriod, [this](void*) { |
| 70 | + if (auto err = SendAlerts(); !err.IsNone()) { |
| 71 | + LOG_ERR() << "Failed to send alerts" << Log::Field(err); |
| 72 | + } |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +Error Alerts::Stop() |
| 77 | +{ |
| 78 | + { |
| 79 | + LockGuard lock {mMutex}; |
| 80 | + |
| 81 | + LOG_DBG() << "Stop alerts module"; |
| 82 | + |
| 83 | + if (!mIsRunning) { |
| 84 | + return ErrorEnum::eWrongState; |
| 85 | + } |
| 86 | + |
| 87 | + mIsRunning = false; |
| 88 | + } |
| 89 | + |
| 90 | + return mSendTimer.Stop(); |
| 91 | +} |
| 92 | + |
| 93 | +Error Alerts::SendAlert(const cloudprotocol::AlertVariant& alert) |
| 94 | +{ |
| 95 | + LockGuard lock {mMutex}; |
| 96 | + |
| 97 | + LOG_DBG() << "Send alert" << Log::Field("alert", alert); |
| 98 | + |
| 99 | + if (IsDuplicated(alert)) { |
| 100 | + ++mDuplicatedAlerts; |
| 101 | + |
| 102 | + return ErrorEnum::eNone; |
| 103 | + } |
| 104 | + |
| 105 | + if (auto err = CacheAlert(alert); !err.IsNone()) { |
| 106 | + ++mSkippedAlerts; |
| 107 | + |
| 108 | + return err; |
| 109 | + } |
| 110 | + |
| 111 | + return ErrorEnum::eNone; |
| 112 | +} |
| 113 | + |
| 114 | +void Alerts::OnConnect() |
| 115 | +{ |
| 116 | + LockGuard lock {mMutex}; |
| 117 | + |
| 118 | + LOG_DBG() << "Publisher connected"; |
| 119 | + |
| 120 | + mIsConnected = true; |
| 121 | +} |
| 122 | + |
| 123 | +void Alerts::OnDisconnect() |
| 124 | +{ |
| 125 | + LockGuard lock {mMutex}; |
| 126 | + |
| 127 | + LOG_DBG() << "Publisher disconnected"; |
| 128 | + |
| 129 | + mIsConnected = false; |
| 130 | +} |
| 131 | + |
| 132 | +/*********************************************************************************************************************** |
| 133 | + * Private |
| 134 | + **********************************************************************************************************************/ |
| 135 | + |
| 136 | +Error Alerts::SendAlerts() |
| 137 | +{ |
| 138 | + LockGuard lock {mMutex}; |
| 139 | + |
| 140 | + LOG_DBG() << "Send alerts"; |
| 141 | + |
| 142 | + if (!mIsRunning || !mIsConnected || mAlerts.IsEmpty()) { |
| 143 | + return ErrorEnum::eNone; |
| 144 | + } |
| 145 | + |
| 146 | + if (mSkippedAlerts > 0) { |
| 147 | + LOG_WRN() << "Alerts skipped due to channel is full" << Log::Field("count", mSkippedAlerts); |
| 148 | + } |
| 149 | + |
| 150 | + if (mDuplicatedAlerts > 0) { |
| 151 | + LOG_WRN() << "Alerts skipped due to duplication" << Log::Field("count", mDuplicatedAlerts); |
| 152 | + } |
| 153 | + |
| 154 | + auto cloudMessage = MakeUnique<cloudprotocol::MessageVariant>(&mAllocator); |
| 155 | + |
| 156 | + while (!mAlerts.IsEmpty()) { |
| 157 | + cloudMessage->SetValue(mAlerts.Front()); |
| 158 | + |
| 159 | + if (auto err = mCommunication->SendMessage(*cloudMessage); !err.IsNone()) { |
| 160 | + return AOS_ERROR_WRAP(err); |
| 161 | + } |
| 162 | + |
| 163 | + mAlerts.Erase(mAlerts.begin()); |
| 164 | + } |
| 165 | + |
| 166 | + return ErrorEnum::eNone; |
| 167 | +} |
| 168 | + |
| 169 | +bool Alerts::IsDuplicated(const cloudprotocol::AlertVariant& alert) |
| 170 | +{ |
| 171 | + auto alertCopy = MakeUnique<cloudprotocol::AlertVariant>(&mAllocator, alert); |
| 172 | + |
| 173 | + return mAlerts.FindIf([&alertCopy](const cloudprotocol::Alerts& alerts) { |
| 174 | + return alerts.mItems.FindIf([&alertCopy](const cloudprotocol::AlertVariant& item) { |
| 175 | + alertCopy->ApplyVisitor(SetTimestamp(alertCopy->ApplyVisitor(GetTimestamp()))); |
| 176 | + |
| 177 | + return *alertCopy == item; |
| 178 | + }) != alerts.mItems.end(); |
| 179 | + }) != mAlerts.end(); |
| 180 | +} |
| 181 | + |
| 182 | +Error Alerts::CacheAlert(const cloudprotocol::AlertVariant& alert) |
| 183 | +{ |
| 184 | + if (mAlerts.IsFull() && mAlerts.Back().mItems.IsFull()) { |
| 185 | + return AOS_ERROR_WRAP(ErrorEnum::eNoMemory); |
| 186 | + } |
| 187 | + |
| 188 | + if (mAlerts.IsEmpty()) { |
| 189 | + if (auto err = mAlerts.EmplaceBack(); !err.IsNone()) { |
| 190 | + return AOS_ERROR_WRAP(err); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + if (mAlerts.Back().mItems.IsFull()) { |
| 195 | + if (auto err = mAlerts.EmplaceBack(); !err.IsNone()) { |
| 196 | + return AOS_ERROR_WRAP(err); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + if (auto err = mAlerts.Back().mItems.PushBack(alert); !err.IsNone()) { |
| 201 | + return AOS_ERROR_WRAP(err); |
| 202 | + } |
| 203 | + |
| 204 | + return ErrorEnum::eNone; |
| 205 | +} |
| 206 | + |
| 207 | +} // namespace aos::cm::alerts |
0 commit comments