1 #ifndef PUBSEQ_GATEWAY_ALERTS__HPP
2 #define PUBSEQ_GATEWAY_ALERTS__HPP
3 
4 /*  $Id: alerts.hpp 629837 2021-04-22 12:47:49Z ivanov $
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *               National Center for Biotechnology Information
9  *
10  *  This software/database is a "United States Government Work" under the
11  *  terms of the United States Copyright Act.  It was written as part of
12  *  the author's official duties as a United States Government employee and
13  *  thus cannot be copyrighted.  This software/database is freely available
14  *  to the public for use. The National Library of Medicine and the U.S.
15  *  Government have not placed any restriction on its use or reproduction.
16  *
17  *  Although all reasonable efforts have been taken to ensure the accuracy
18  *  and reliability of the software and data, the NLM and the U.S.
19  *  Government do not and cannot warrant the performance or results that
20  *  may be obtained by using this software or data. The NLM and the U.S.
21  *  Government disclaim all warranties, express or implied, including
22  *  warranties of performance, merchantability or fitness for any particular
23  *  purpose.
24  *
25  *  Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors:  Sergey Satskiy
30  *
31  * File Description:
32  *   PSG server alerts
33  *
34  */
35 
36 
37 #include <map>
38 #include <chrono>
39 #include <mutex>
40 using namespace std;
41 
42 #include <connect/services/json_over_uttp.hpp>
43 USING_NCBI_SCOPE;
44 
45 
46 enum EPSGS_AlertType {
47     ePSGS_Unknown = -1,
48     ePSGS_ConfigAuthDecrypt = 0,
49     ePSGS_ConfigHttpWorkers = 1,
50     ePSGS_ConfigListenerBacklog = 2,
51     ePSGS_ConfigMaxConnections = 3,
52     ePSGS_ConfigTimeout = 4,
53     ePSGS_ConfigRetries = 5,
54     ePSGS_ConfigExcludeCacheSize = 6,
55     ePSGS_ConfigExcludeCachePurgeSize = 7,
56     ePSGS_ConfigExcludeCacheInactivity = 8,
57     ePSGS_ConfigStatScaleType = 9,
58     ePSGS_ConfigStatMinMaxVal = 10,
59     ePSGS_ConfigStatNBins = 11,
60     ePSGS_OpenCassandra = 12,
61     ePSGS_NoValidCassandraMapping = 13,         // PSG has no valid cassandra mapping at hand
62     ePSGS_InvalidCassandraMapping = 14,         // PSG has detected an invalid mapping in cassandra
63     ePSGS_NewCassandraMappingAccepted = 15,     // PSG accepted an updated mapping from cassandra
64     ePSGS_NewCassandraSatNamesMapping = 16,     // PSG has detected new sat names mapping in cassandra
65                                                 // however it can be accepted only
66                                                 // after restart
67     ePSGS_OpenCache = 17,                       // PSG cannot create or open the LMDB cache
68     ePSGS_NoCassandraPublicCommentsMapping = 18 // PSG has no public comments mapping
69 };
70 
71 enum EPSGS_AlertAckResult {
72     ePSGS_AlertNotFound = 0,
73     ePSGS_AlertAlreadyAcknowledged = 1,
74     ePSGS_AlertAcknowledged = 2
75 };
76 
77 
78 struct SPSGAlertAttributes
79 {
80     chrono::system_clock::time_point    m_LastDetectedTimestamp;
81     chrono::system_clock::time_point    m_AcknowledgedTimestamp;
82     bool                                m_On;
83     size_t                              m_Count;            // total, i.e. since startup
84     size_t                              m_CountSinceAck;    // since acknowledge
85     string                              m_User;
86     string                              m_Message;
87 
SPSGAlertAttributesSPSGAlertAttributes88     SPSGAlertAttributes() :
89         m_LastDetectedTimestamp(chrono::system_clock::now()),
90         m_AcknowledgedTimestamp(),
91         m_On(true), m_Count(1), m_CountSinceAck(1)
92     {}
93 
94     CJsonNode Serialize(void) const;
95 };
96 
97 
98 
99 
100 class CPSGAlerts
101 {
102     public:
103         void Register(EPSGS_AlertType alert_type, const string &  message);
104         EPSGS_AlertAckResult Acknowledge(const string &  alert_id,
105                                          const string &  user);
106         EPSGS_AlertAckResult Acknowledge(EPSGS_AlertType alert_type,
107                                          const string &  user);
108         CJsonNode Serialize(void) const;
109         CJsonNode SerializeActive(void) const;
110 
111     private:
112         mutable mutex                               m_Lock;
113         map<EPSGS_AlertType, SPSGAlertAttributes>   m_Alerts;
114 
115         EPSGS_AlertType x_IdToType(const string &  alert_id) const;
116         string x_TypeToId(EPSGS_AlertType  type) const;
117 };
118 
119 
120 #endif /* PUBSEQ_GATEWAY_ALERTS__HPP */
121 
122