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

Go to the SVN repository for this file.

1 /* $Id: usage_report_job.cpp 45523 2020-08-24 21:36:49Z evgeniev $
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: Vladislav Evgeniev
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 
32 #include <sstream>
33 
34 #include <corelib/ncbidiag.hpp>
35 
37 
41 
43 
45 
46 CReportParams& CReportParams::Add(const string& name, const string& value)
47 {
48 
50  return *this;
51 }
52 
53 CReportParams& CReportParams::Add(const string& name, const char* value)
54 {
55  return Add(name,string(value));
56 }
57 
58 CReportParams& CReportParams::Add(const string& name, int value)
59 {
60  return Add(name, NStr::IntToString(value));
61 }
62 
63 CReportParams& CReportParams::Add(const string& name, unsigned int value)
64 {
65  return Add(name, NStr::UIntToString(value));
66 }
67 
68 CReportParams& CReportParams::Add(const string& name, long value)
69 {
70  return Add(name, NStr::LongToString(value));
71 }
72 
73 CReportParams& CReportParams::Add(const string& name, unsigned long value)
74 {
75  return Add(name, NStr::ULongToString(value));
76 }
77 
78 CReportParams& CReportParams::Add(const string& name, char value)
79 {
80  return Add(name, string(1, value));
81 }
82 
83 CReportParams& CReportParams::Add(const string& name, double value)
84 
85 {
86  return Add(name, NStr::DoubleToString(value));
87 }
88 
89 CReportParams& CReportParams::Add(const string& name, bool value)
90 
91 {
92  return Add(name, NStr::BoolToString(value));
93 }
94 
96 {
97  std::stringstream result;
98  for (auto const &param : m_Params) {
99  result << '&' << param.first << '=' << param.second;
100  }
101  return result.str();
102 }
103 
104 ///////////////////////////////////////////////////////////////////////////////
105 /// CUsageReportJob
106 
107 static string s_GetUrl()
108 {
109  return "https://www.ncbi.nlm.nih.gov/stat?ncbi_app=GenomeWorkbench2";
110 }
111 
112 bool CUsageReportJob::m_IsEnabled = true;
113 volatile unsigned CUsageReportJob::m_FailedAttempts = 0;
115 
117  : m_Canceled(false)
118  , m_URL(s_GetUrl())
119 {
120  m_URL += params.ToString();
121 }
122 
124 {
125  CRef<CHttpSession> session(new CHttpSession);
126  CHttpResponse response = session->Get(m_URL);
127 
128  if (response.GetStatusCode() != 200) {
130  return IAppJob::eFailed;
131  }
132  m_FailedAttempts = 0;
133  return IAppJob::eCompleted;
134 }
135 
137 {
138  return (m_IsEnabled && (m_FailedAttempts < 10));
139 }
140 
142 {
143  m_IsEnabled = enable;
144 }
145 
147 {
149  params.Add("os", x_GetOS());
150  params.Add("version", x_GetGbenchVersion());
151  CIRef<IAppJob> job(new CUsageReportJob(params));
152  try {
153  disp.StartJob(*job, "ThreadPool");
154  }
155  catch (const CException& e) {
157  LOG_POST(Warning << "CUsageReportJob::ReportUsage failed to start job");
158  LOG_POST(Warning << e.ReportAll());
159  }
160 }
161 
162 static const char* kRegistryKey = "GBENCH.Application.ReportUsage.Enabled";
163 
165 {
167  gui_reg.Set(kRegistryKey, m_IsEnabled);
168 }
169 
171 {
173  m_IsEnabled = gui_reg.GetBool(kRegistryKey, true);
174 }
175 
177 {
178 #if defined(NCBI_OS_MSWIN)
179  return string("Windows");
180 #elif defined(NCBI_OS_DARWIN)
181  return string("MacOSX");
182 #elif defined(NCBI_OS_UNIX)
183  return string("Linux");
184 #else
185  return string("unknown");
186 #endif
187 }
188 
190 {
191  std::lock_guard<std::mutex> guard(m_VersionStringMutex);
192  static string ver_str;
193  if (ver_str.empty()) {
194  stringstream ver_strm;
196  ver_str = ver_strm.str();
197  }
198  return ver_str;
199 }
200 
CAppJobDispatcher.
@ eMajor
absolute (in)compatibility
@ eMinor
backward (in)compatibility
@ ePatchLevel
patch level
static CGuiRegistry & GetInstance()
access the application-wide singleton
Definition: registry.cpp:400
void Set(const string &key, int val)
set specific values
Definition: registry.cpp:178
bool GetBool(const string &key, bool default_val=false) const
Definition: registry.cpp:143
HTTP response.
Temporary object for holding extra report arguments.
CReportParams & Add(const string &name, const string &value)
CReportParams & Add(const string &name, const char *value)
string ToString() const
TStringMap m_Params
static bool IsEnabled()
Indicates whether usage statistics collection is enabled.
static volatile unsigned m_FailedAttempts
static void SetEnabled(bool enable=true)
Switch usage statistics collection on/off.
virtual EJobState Run()
Function that does all the useful work, called by the Engine.
static void ReportUsage(CReportParams &params)
static string x_GetOS()
static std::mutex m_VersionStringMutex
version string access mutex
static string x_GetGbenchVersion()
static bool m_IsEnabled
static void SaveSettings()
static void LoadSettings()
#define false
Definition: bool.h:36
string
Definition: cgiapp.hpp:687
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
string ReportAll(TDiagPostFlags flags=eDPF_Exception) const
Report all exceptions.
Definition: ncbiexpt.cpp:370
static CAppJobDispatcher & GetInstance()
EJobState
Job states (describe FSM)
Definition: app_job.hpp:86
TJobID StartJob(IAppJob &job, const string &engine_name, IEngineParams *params=NULL)
Starts a Job on the specified engine in "passive mode" - no notifications or progress reports will be...
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
CHttpResponse Get(const CUrl &url, const CTimeout &timeout=CTimeout(CTimeout::eDefault), THttpRetries retries=null)
Shortcut for GET requests.
int GetStatusCode(void) const
Get response status code.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
CUsageReportJob(void)
Default constructor.
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
static string LongToString(long value, TNumToStringFlags flags=0, int base=10)
Convert Int to string.
Definition: ncbistr.hpp:5141
static const string BoolToString(bool value)
Convert bool to string.
Definition: ncbistr.cpp:2815
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static string UIntToString(unsigned int value, TNumToStringFlags flags=0, int base=10)
Convert UInt to string.
Definition: ncbistr.hpp:5109
static string ULongToString(unsigned long value, TNumToStringFlags flags=0, int base=10)
Convert unsigned long to string.
Definition: ncbistr.hpp:5150
static string URLEncode(const CTempString str, EUrlEncode flag=eUrlEnc_SkipMarkChars)
URL-encode string.
Definition: ncbistr.cpp:6062
@ eUrlEnc_URIQueryValue
Encode query part of an URI, arg value.
Definition: ncbistr.hpp:3151
@ eUrlEnc_URIQueryName
Encode query part of an URI, arg name.
Definition: ncbistr.hpp:3150
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Defines NCBI C++ diagnostic APIs, classes, and macros.
else result
Definition: token2.c:20
USING_SCOPE(objects)
static string s_GetUrl()
CUsageReportJob.
static const char * kRegistryKey
Modified on Sun Apr 21 03:42:29 2024 by modify_doxy.py rev. 669887