NCBI C++ ToolKit
alerts.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: alerts.cpp 103078 2024-09-05 12:15:34Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Sergey Satskiy
27  *
28  * File Description: PSG server alerts
29  *
30  */
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbistd.hpp>
33 
34 #include "alerts.hpp"
36 
37 
38 struct SAlertToId
39 {
41  string id;
42 };
43 
44 
46  { ePSGS_ConfigAuthDecrypt, "ConfigAuthDecrypt" },
47  { ePSGS_ConfigHttpWorkers, "ConfigHttpWorkers" },
48  { ePSGS_ConfigListenerBacklog, "ConfigListenerBacklog" },
49  { ePSGS_ConfigMaxConnections, "ConfigMaxConnections" },
50  { ePSGS_ConfigTimeout, "ConfigTimeout" },
51  { ePSGS_ConfigRetries, "ConfigRetries" },
52  { ePSGS_ConfigExcludeCacheSize, "ConfigExcludeCacheSize" },
53  { ePSGS_ConfigExcludeCachePurgeSize, "ConfigExcludeCachePurgeSize" },
54  { ePSGS_ConfigExcludeCacheInactivity, "ConfigExcludeCacheInactivity" },
55  { ePSGS_ConfigStatScaleType, "ConfigStatScaleType" },
56  { ePSGS_ConfigStatMinMaxVal, "ConfigStatMinMaxVal" },
57  { ePSGS_ConfigStatNBins, "ConfigStatNBins" },
58  { ePSGS_OpenCassandra, "OpenCassandra" },
59  { ePSGS_NoValidCassandraMapping, "NoValidCassandraMapping" },
60  { ePSGS_InvalidCassandraMapping, "InvalidCassandraMapping" },
61  { ePSGS_NewCassandraMappingAccepted, "NewCassandraMappingAccepted"},
62  { ePSGS_NewCassandraSatNamesMapping, "NewCassandraSatNamesMapping" },
63  { ePSGS_NewCassandraPublicCommentMapping, "NewCassandraPublicCommentMapping" },
64  { ePSGS_OpenCache, "OpenCache" },
65  { ePSGS_NoCassandraPublicCommentsMapping, "NoCassandraPublicCommentsMapping" },
66  { ePSGS_NoIPGKeyspace, "NoIPGKeyspace" },
67  { ePSGS_ConfigHealthTimeout, "ConfigHealthTimeout" },
68  { ePSGS_ConfigCassandraHealthTimeout, "ConfigCassandraHealthTimeout" },
69  { ePSGS_ConfigLMDBHealthTimeout, "ConfigLMDBHealthTimeout" },
70  { ePSGS_ConfigCDDHealthTimeout, "ConfigWGSHealthTimeout" },
71  { ePSGS_ConfigWGSHealthTimeout, "ConfigWGSHealthTimeout" },
72  { ePSGS_ConfigSNPHealthTimeout, "ConfigSNPHealthTimeout" },
73  { ePSGS_ConfigTcpMaxConnSoftLimit, "ConfigTcpMaxConnSoftLimit" },
74  { ePSGS_ConfigTcpMaxConn, "ConfigTcpMaxConn" }
75 };
76 const size_t kAlertToIdMapSize = sizeof(kAlertToIdMap) / sizeof(SAlertToId);
77 
78 
79 
81 {
83 
84  if (m_AcknowledgedTimestamp.time_since_epoch().count() == 0)
85  result.SetString("AcknowledgedTime", "n/a");
86  else
87  result.SetString("AcknowledgedTime",
89 
90  result.SetString("LastDetectedTime",
92  result.SetBoolean("On", m_On);
93  result.SetInteger("Count", m_Count);
94  result.SetInteger("CountSinceAcknowledge", m_CountSinceAck);
95  result.SetString("User", m_User);
96  result.SetString("Message", m_Message);
97  return result;
98 }
99 
100 
102  const string & message)
103 {
104  map<EPSGS_AlertType,
105  SPSGAlertAttributes>::iterator found;
106  lock_guard<mutex> guard(m_Lock);
107 
108  found = m_Alerts.find(alert_type);
109  if (found != m_Alerts.end()) {
110  // Alert has already been there
111  found->second.m_LastDetectedTimestamp = chrono::system_clock::now();
112  found->second.m_On = true;
113  ++found->second.m_Count;
114  ++found->second.m_CountSinceAck;
115  found->second.m_Message = message;
116  return;
117  }
118 
119  // Brand new alert
120  SPSGAlertAttributes attrs;
121  attrs.m_Message = message;
122  m_Alerts[alert_type] = attrs;
123 }
124 
125 
127  const string & user)
128 {
129  EPSGS_AlertType type = x_IdToType(alert_id);
131  return ePSGS_AlertNotFound;
132 
133  return Acknowledge(type, user);
134 }
135 
136 
138  const string & user)
139 {
140  map<EPSGS_AlertType,
141  SPSGAlertAttributes>::iterator found;
142  lock_guard<mutex> guard(m_Lock);
143 
144  found = m_Alerts.find(alert_type);
145  if (found == m_Alerts.end())
146  return ePSGS_AlertNotFound;
147 
148  if (!found->second.m_On)
150 
151  found->second.m_AcknowledgedTimestamp = chrono::system_clock::now();
152  found->second.m_On = false;
153  found->second.m_User = user;
154  found->second.m_CountSinceAck = 0;
156 }
157 
158 
159 EPSGS_AlertType CPSGAlerts::x_IdToType(const string & alert_id) const
160 {
161  for (size_t k = 0; k < kAlertToIdMapSize; ++k) {
162  if (NStr::CompareNocase(alert_id, kAlertToIdMap[k].id) == 0)
163  return kAlertToIdMap[k].type;
164  }
166 }
167 
168 
170 {
171  for (size_t k = 0; k < kAlertToIdMapSize; ++k) {
172  if (kAlertToIdMap[k].type == type)
173  return kAlertToIdMap[k].id;
174  }
175  return "unknown";
176 }
177 
178 
179 // Provides the alerts
181 {
183  map<EPSGS_AlertType,
184  SPSGAlertAttributes>::const_iterator k;
185  lock_guard<mutex> guard(m_Lock);
186 
187  for (k = m_Alerts.begin(); k != m_Alerts.end(); ++k) {
188  result.SetByKey(x_TypeToId(k->first), k->second.Serialize());
189  }
190  return result;
191 }
192 
193 
194 // Provides only the active alerts
196 {
198  map<EPSGS_AlertType,
199  SPSGAlertAttributes>::const_iterator k;
200  lock_guard<mutex> guard(m_Lock);
201 
202  for (k = m_Alerts.begin(); k != m_Alerts.end(); ++k) {
203  if (k->second.m_On) {
204  result.SetByKey(x_TypeToId(k->first), k->second.Serialize());
205  }
206  }
207  return result;
208 }
209 
const size_t kAlertToIdMapSize
Definition: alerts.cpp:76
const SAlertToId kAlertToIdMap[]
Definition: alerts.cpp:45
EPSGS_AlertAckResult
Definition: alerts.hpp:79
@ ePSGS_AlertAcknowledged
Definition: alerts.hpp:82
@ ePSGS_AlertNotFound
Definition: alerts.hpp:80
@ ePSGS_AlertAlreadyAcknowledged
Definition: alerts.hpp:81
EPSGS_AlertType
Definition: alerts.hpp:46
@ ePSGS_ConfigListenerBacklog
Definition: alerts.hpp:50
@ ePSGS_ConfigRetries
Definition: alerts.hpp:53
@ ePSGS_ConfigAuthDecrypt
Definition: alerts.hpp:48
@ ePSGS_ConfigExcludeCachePurgeSize
Definition: alerts.hpp:55
@ ePSGS_NoValidCassandraMapping
Definition: alerts.hpp:61
@ ePSGS_NewCassandraMappingAccepted
Definition: alerts.hpp:63
@ ePSGS_ConfigSNPHealthTimeout
Definition: alerts.hpp:74
@ ePSGS_ConfigTcpMaxConn
Definition: alerts.hpp:76
@ ePSGS_ConfigHttpWorkers
Definition: alerts.hpp:49
@ ePSGS_ConfigStatScaleType
Definition: alerts.hpp:57
@ ePSGS_ConfigMaxConnections
Definition: alerts.hpp:51
@ ePSGS_NewCassandraSatNamesMapping
Definition: alerts.hpp:64
@ ePSGS_ConfigLMDBHealthTimeout
Definition: alerts.hpp:71
@ ePSGS_OpenCassandra
Definition: alerts.hpp:60
@ ePSGS_ConfigCassandraHealthTimeout
Definition: alerts.hpp:70
@ ePSGS_ConfigTcpMaxConnSoftLimit
Definition: alerts.hpp:75
@ ePSGS_ConfigStatNBins
Definition: alerts.hpp:59
@ ePSGS_ConfigExcludeCacheInactivity
Definition: alerts.hpp:56
@ ePSGS_ConfigWGSHealthTimeout
Definition: alerts.hpp:73
@ ePSGS_ConfigTimeout
Definition: alerts.hpp:52
@ ePSGS_InvalidCassandraMapping
Definition: alerts.hpp:62
@ ePSGS_NoIPGKeyspace
Definition: alerts.hpp:68
@ ePSGS_ConfigHealthTimeout
Definition: alerts.hpp:69
@ ePSGS_ConfigStatMinMaxVal
Definition: alerts.hpp:58
@ ePSGS_NoCassandraPublicCommentsMapping
Definition: alerts.hpp:67
@ ePSGS_ConfigCDDHealthTimeout
Definition: alerts.hpp:72
@ ePSGS_ConfigExcludeCacheSize
Definition: alerts.hpp:54
@ ePSGS_Unknown
Definition: alerts.hpp:47
@ ePSGS_OpenCache
Definition: alerts.hpp:66
@ ePSGS_NewCassandraPublicCommentMapping
Definition: alerts.hpp:65
JSON node abstraction.
static CJsonNode NewObjectNode()
Create a new JSON object node.
map< EPSGS_AlertType, SPSGAlertAttributes > m_Alerts
Definition: alerts.hpp:121
void Register(EPSGS_AlertType alert_type, const string &message)
Definition: alerts.cpp:101
CJsonNode SerializeActive(void) const
Definition: alerts.cpp:195
CJsonNode Serialize(void) const
Definition: alerts.cpp:180
string x_TypeToId(EPSGS_AlertType type) const
Definition: alerts.cpp:169
EPSGS_AlertAckResult Acknowledge(const string &alert_id, const string &user)
Definition: alerts.cpp:126
EPSGS_AlertType x_IdToType(const string &alert_id) const
Definition: alerts.cpp:159
mutex m_Lock
Definition: alerts.hpp:120
Include a standard set of the NCBI C++ Toolkit most basic headers.
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
string FormatPreciseTime(const chrono::system_clock::time_point &t_point)
string id
Definition: alerts.cpp:41
EPSGS_AlertType type
Definition: alerts.cpp:40
CJsonNode Serialize(void) const
Definition: alerts.cpp:80
chrono::system_clock::time_point m_LastDetectedTimestamp
Definition: alerts.hpp:88
size_t m_CountSinceAck
Definition: alerts.hpp:92
chrono::system_clock::time_point m_AcknowledgedTimestamp
Definition: alerts.hpp:89
Definition: type.c:6
else result
Definition: token2.c:20
Modified on Fri Sep 20 14:58:09 2024 by modify_doxy.py rev. 669887