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

Go to the SVN repository for this file.

1 /* $Id: alerts.cpp 99908 2023-05-19 12:51:11Z 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 };
68 const size_t kAlertToIdMapSize = sizeof(kAlertToIdMap) / sizeof(SAlertToId);
69 
70 
71 
73 {
75 
76  if (m_AcknowledgedTimestamp.time_since_epoch().count() == 0)
77  result.SetString("AcknowledgedTime", "n/a");
78  else
79  result.SetString("AcknowledgedTime",
81 
82  result.SetString("LastDetectedTime",
84  result.SetBoolean("On", m_On);
85  result.SetInteger("Count", m_Count);
86  result.SetInteger("CountSinceAcknowledge", m_CountSinceAck);
87  result.SetString("User", m_User);
88  result.SetString("Message", m_Message);
89  return result;
90 }
91 
92 
94  const string & message)
95 {
96  map<EPSGS_AlertType,
97  SPSGAlertAttributes>::iterator found;
98  lock_guard<mutex> guard(m_Lock);
99 
100  found = m_Alerts.find(alert_type);
101  if (found != m_Alerts.end()) {
102  // Alert has already been there
103  found->second.m_LastDetectedTimestamp = chrono::system_clock::now();
104  found->second.m_On = true;
105  ++found->second.m_Count;
106  ++found->second.m_CountSinceAck;
107  found->second.m_Message = message;
108  return;
109  }
110 
111  // Brand new alert
112  SPSGAlertAttributes attrs;
113  attrs.m_Message = message;
114  m_Alerts[alert_type] = attrs;
115 }
116 
117 
119  const string & user)
120 {
121  EPSGS_AlertType type = x_IdToType(alert_id);
123  return ePSGS_AlertNotFound;
124 
125  return Acknowledge(type, user);
126 }
127 
128 
130  const string & user)
131 {
132  map<EPSGS_AlertType,
133  SPSGAlertAttributes>::iterator found;
134  lock_guard<mutex> guard(m_Lock);
135 
136  found = m_Alerts.find(alert_type);
137  if (found == m_Alerts.end())
138  return ePSGS_AlertNotFound;
139 
140  if (!found->second.m_On)
142 
143  found->second.m_AcknowledgedTimestamp = chrono::system_clock::now();
144  found->second.m_On = false;
145  found->second.m_User = user;
146  found->second.m_CountSinceAck = 0;
148 }
149 
150 
151 EPSGS_AlertType CPSGAlerts::x_IdToType(const string & alert_id) const
152 {
153  for (size_t k = 0; k < kAlertToIdMapSize; ++k) {
154  if (NStr::CompareNocase(alert_id, kAlertToIdMap[k].id) == 0)
155  return kAlertToIdMap[k].type;
156  }
158 }
159 
160 
162 {
163  for (size_t k = 0; k < kAlertToIdMapSize; ++k) {
164  if (kAlertToIdMap[k].type == type)
165  return kAlertToIdMap[k].id;
166  }
167  return "unknown";
168 }
169 
170 
171 // Provides the alerts
173 {
175  map<EPSGS_AlertType,
176  SPSGAlertAttributes>::const_iterator k;
177  lock_guard<mutex> guard(m_Lock);
178 
179  for (k = m_Alerts.begin(); k != m_Alerts.end(); ++k) {
180  result.SetByKey(x_TypeToId(k->first), k->second.Serialize());
181  }
182  return result;
183 }
184 
185 
186 // Provides only the active alerts
188 {
190  map<EPSGS_AlertType,
191  SPSGAlertAttributes>::const_iterator k;
192  lock_guard<mutex> guard(m_Lock);
193 
194  for (k = m_Alerts.begin(); k != m_Alerts.end(); ++k) {
195  if (k->second.m_On) {
196  result.SetByKey(x_TypeToId(k->first), k->second.Serialize());
197  }
198  }
199  return result;
200 }
201 
const size_t kAlertToIdMapSize
Definition: alerts.cpp:68
const SAlertToId kAlertToIdMap[]
Definition: alerts.cpp:45
EPSGS_AlertAckResult
Definition: alerts.hpp:71
@ ePSGS_AlertAcknowledged
Definition: alerts.hpp:74
@ ePSGS_AlertNotFound
Definition: alerts.hpp:72
@ ePSGS_AlertAlreadyAcknowledged
Definition: alerts.hpp:73
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_ConfigHttpWorkers
Definition: alerts.hpp:49
@ ePSGS_ConfigStatScaleType
Definition: alerts.hpp:57
@ ePSGS_ConfigMaxConnections
Definition: alerts.hpp:51
@ ePSGS_NewCassandraSatNamesMapping
Definition: alerts.hpp:64
@ ePSGS_OpenCassandra
Definition: alerts.hpp:60
@ ePSGS_ConfigStatNBins
Definition: alerts.hpp:59
@ ePSGS_ConfigExcludeCacheInactivity
Definition: alerts.hpp:56
@ ePSGS_ConfigTimeout
Definition: alerts.hpp:52
@ ePSGS_InvalidCassandraMapping
Definition: alerts.hpp:62
@ ePSGS_NoIPGKeyspace
Definition: alerts.hpp:68
@ ePSGS_ConfigStatMinMaxVal
Definition: alerts.hpp:58
@ ePSGS_NoCassandraPublicCommentsMapping
Definition: alerts.hpp:67
@ 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:113
void Register(EPSGS_AlertType alert_type, const string &message)
Definition: alerts.cpp:93
CJsonNode SerializeActive(void) const
Definition: alerts.cpp:187
CJsonNode Serialize(void) const
Definition: alerts.cpp:172
string x_TypeToId(EPSGS_AlertType type) const
Definition: alerts.cpp:161
EPSGS_AlertAckResult Acknowledge(const string &alert_id, const string &user)
Definition: alerts.cpp:118
EPSGS_AlertType x_IdToType(const string &alert_id) const
Definition: alerts.cpp:151
mutex m_Lock
Definition: alerts.hpp:112
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:72
chrono::system_clock::time_point m_LastDetectedTimestamp
Definition: alerts.hpp:80
size_t m_CountSinceAck
Definition: alerts.hpp:84
chrono::system_clock::time_point m_AcknowledgedTimestamp
Definition: alerts.hpp:81
Definition: type.c:6
else result
Definition: token2.c:20
Modified on Wed Dec 06 07:13:37 2023 by modify_doxy.py rev. 669887