8#include "MantidJson/Json.h"
17#include <Poco/ActiveResult.h>
18#include <Poco/String.h>
20#include <boost/algorithm/string/join.hpp>
25constexpr auto SEPARATOR =
"->";
37 : type(type), name(
std::move(name)), internal(internal), application(
std::move(application)) {}
79 retVal[
"name"] =
name;
90 : m_timer(), m_timerTicks(0), m_timerTicksTarget(0), m_FeatureQueue(), m_FeatureQueueSizeThreshold(50),
91 m_isEnabled(false), m_mutex(), m_application(
"python"), m_startTime(Types::Core::DateAndTime::getCurrentTime()),
96 if (!url.is_initialized()) {
97 g_log.
debug() <<
"Failed to load usage report url\n";
112 m_timer.setStartInterval((seconds * 1000));
113 m_timer.setPeriodicInterval((seconds * 1000));
125 const bool internal) {
127 std::lock_guard<std::mutex> _lock(
m_mutex);
129 using boost::algorithm::join;
136 std::lock_guard<std::mutex> _lock(
m_mutex);
142 const bool internal) {
161 std::queue<FeatureUsage> empty;
175 return Types::Core::DateAndTime::getCurrentTime() -
m_startTime;
184 }
catch (std::exception &ex) {
185 g_log.
error() <<
"Error during the shutdown of the UsageService. " << ex.what();
194 }
catch (std::exception &ex) {
195 g_log.
debug() <<
"Send startup usage failure. " << ex.what() <<
'\n';
202 if (!message.empty()) {
210 }
catch (std::exception &ex) {
211 g_log.
debug() <<
"sendFeatureUsageReport failure. " << ex.what() <<
'\n';
233 ::Json::Value header;
245 ::Json::Value message;
259 message[
"ParaView"] = 0;
266 message[
"dateTime"] =
m_startTime.toISO8601String();
270 return Mantid::JsonHelpers::jsonToString(message);
275 std::map<FeatureUsage, int> featureCountMap;
279 std::lock_guard<std::mutex> _lock(
m_mutex);
284 if (featureCountMap.find(featureUsage) == featureCountMap.end()) {
285 featureCountMap[featureUsage] = 1;
287 featureCountMap[featureUsage]++;
292 if (!featureCountMap.empty()) {
293 ::Json::Value features;
295 for (
auto const &featureItem : featureCountMap) {
296 ::Json::Value thisFeature = featureItem.first.asJson();
297 thisFeature[
"count"] = featureItem.second;
298 features.append(thisFeature);
300 if (!features.empty()) {
301 message[
"features"] = features;
302 return Mantid::JsonHelpers::jsonToString(message);
329 std::stringstream responseStream;
335 g_log.
information() <<
"Call to \"" << url <<
"\" responded with " <<
static_cast<int>(status) <<
"\n"
Exception thrown when error occurs accessing an internet resource.
const char * what() const noexcept override
Overloaded reporting method.
const int & errorCode() const
Writes out the range and limits.
UsageReporter : The Usage reporter is responsible for collating, and sending all usage data.
std::string featureTypeToString() const
Convert the stored feature type enum to a string.
::Json::Value asJson() const
bool operator<(const FeatureUsage &r) const
FeatureUsage(const FeatureType &type, std::string name, const bool internal, std::string application)
Constructor.
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
void setBody(const std::string &body)
Sets the body & content length for future requests, this will also set the method to POST is the body...
void setTimeout(int seconds)
Sets the timeout in seconds.
virtual HTTPStatus sendRequest(const std::string &url, std::ostream &responseStream)
Performs a request using http or https depending on the url.
The Logger class is in charge of the publishing messages from the framework through various channels.
void debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
void information(const std::string &msg)
Logs at information level.
static const char * version()
The full version number.
static const char * versionShort()
The version number of the last full version.
static const char * revisionFull()
The full SHA-1 of the last commit.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Poco::Timer m_timer
a timer
InternetHelper::HTTPStatus sendFeatureAsyncImpl(const std::string &message)
Async method for sending feature messages.
Poco::ActiveMethod< InternetHelper::HTTPStatus, std::string, UsageServiceImpl > m_featureActiveMethod
Async method for sending feature notifications.
void setInterval(const uint32_t seconds=60)
Sets the interval that the timer checks for tasks.
UsageServiceImpl()
Constructor.
void registerFeatureUsage(const FeatureType &type, const std::vector< std::string > &name, const bool internal)
registerFeatureUsage registers the use of a feature in mantid.
void setApplicationName(const std::string &name)
Sets the application name that has invoked Mantid.
void setEnabled(const bool enabled)
Sets whether the UsageReporter is enabled.
std::queue< FeatureUsage > m_FeatureQueue
void registerStartup()
Registers the Startup of Mantid.
void sendStartupReport()
Send startup Report.
uint32_t m_timerTicksTarget
The number of timer ticks at which to reset.
std::string getApplicationName() const
Returns the application name that has invoked Mantid.
InternetHelper::HTTPStatus sendStartupAsyncImpl(const std::string &message)
Asynchronous execution.
Types::Core::DateAndTime m_startTime
void timerCallback(Poco::Timer &)
A method to handle the timerCallbacks.
virtual std::string generateFeatureUsageMessage()
generates the message body for a feature usage message
::Json::Value generateFeatureHeader()
This puts together the system information for the json document.
uint32_t m_timerTicks
The number of timer ticks since the last reset.
Poco::ActiveMethod< InternetHelper::HTTPStatus, std::string, UsageServiceImpl > m_startupActiveMethod
Async method for sending startup notifications.
void sendFeatureUsageReport(const bool synchronous)
Send featureUsageReport.
std::string m_application
std::string m_url
Stores the base url of the usage system.
size_t m_FeatureQueueSizeThreshold
void flush()
flushes any buffers and sends any outstanding usage reports
bool isEnabled() const
Returns true if usage reporting is enabled.
Types::Core::time_duration getUpTime()
gets the uptime of this mantid instance
virtual Kernel::InternetHelper::HTTPStatus sendReport(const std::string &message, const std::string &url)
sends a report over the internet
void clear()
clear any buffers without sending any outstanding usage reports
virtual std::string generateStartupMessage()
generates the message body for a startup message
MANTID_KERNEL_DLL std::string md5FromString(const std::string &input)
create a md5 checksum from a string
Logger g_log("DateAndTime")
FeatureType
An enum specifying the 4 possible features types that can be logged in the usage service.