Skip to content

Commit

Permalink
RDK-53686 Analytics sysTime validation, generate sessionId and attrib…
Browse files Browse the repository at this point in the history
…utes source alignment
  • Loading branch information
adrianM27 committed Oct 30, 2024
1 parent 98d61f3 commit afbc538
Show file tree
Hide file tree
Showing 22 changed files with 795 additions and 232 deletions.
1 change: 1 addition & 0 deletions Analytics/Analytics.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if boolean("@PLUGIN_ANALYTICS_SIFT_BACKEND_ENABLED@"):
sift.add("productname", "@PLUGIN_ANALYTICS_SIFT_PRODUCT_NAME@")
sift.add("loggername", "@PLUGIN_ANALYTICS_SIFT_LOGGER_NAME@")
sift.add("loggerversion", "@PLUGIN_ANALYTICS_SIFT_LOGGER_VERSION@")
sift.add("platformdefault", "@PLUGIN_ANALYTICS_SIFT_PLATFORM_DEFAULT@")
sift.add("maxrandomisationwindowtime", "@PLUGIN_ANALYTICS_SIFT_MAX_RANDOMISATION_WINDOW_TIME@")
sift.add("maxeventsinpost", "@PLUGIN_ANALYTICS_SIFT_MAX_EVENTS_IN_POST@")
sift.add("maxretries", "@PLUGIN_ANALYTICS_SIFT_MAX_RETRIES@")
Expand Down
1 change: 1 addition & 0 deletions Analytics/Analytics.config
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if(PLUGIN_ANALYTICS_SIFT_BACKEND_ENABLED)
kv(productname ${PLUGIN_ANALYTICS_SIFT_PRODUCT_NAME})
kv(loggername ${PLUGIN_ANALYTICS_SIFT_LOGGER_NAME})
kv(loggerversion ${PLUGIN_ANALYTICS_SIFT_LOGGER_VERSION})
kv(platformdefault ${PLUGIN_ANALYTICS_SIFT_PLATFORM_DEFAULT})
kv(maxrandomisationwindowtime, ${PLUGIN_ANALYTICS_SIFT_MAX_RANDOMISATION_WINDOW_TIME})
kv(maxeventsinpost, ${PLUGIN_ANALYTICS_SIFT_MAX_EVENTS_IN_POST})
kv(maxretries, ${PLUGIN_ANALYTICS_SIFT_MAX_RETRIES})
Expand Down
4 changes: 0 additions & 4 deletions Analytics/Analytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ namespace WPEFramework {
END_INTERFACE_MAP

static const string ANALYTICS_METHOD_SEND_EVENT;
static const string ANALYTICS_METHOD_SET_SESSION_ID;
static const string ANALYTICS_METHOD_SET_TIME_READY;

private:
void Deactivated(RPC::IRemoteConnection* connection);
Expand All @@ -78,8 +76,6 @@ namespace WPEFramework {
void UnregisterAll();

uint32_t SendEventWrapper(const JsonObject& parameters, JsonObject& response);
uint32_t SetSessionIdWrapper(const JsonObject& parameters, JsonObject& response);
uint32_t SetTimeReadyWrapper(const JsonObject& parameters, JsonObject& response);

private:
PluginHost::IShell* mService;
Expand Down
45 changes: 0 additions & 45 deletions Analytics/AnalyticsJsonRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include "UtilsJsonRpc.h"

const string WPEFramework::Plugin::Analytics::ANALYTICS_METHOD_SEND_EVENT = "sendEvent";
// TODO: To be removed once the Analytics is capable of handling it internally
const string WPEFramework::Plugin::Analytics::ANALYTICS_METHOD_SET_SESSION_ID = "setSessionId";
const string WPEFramework::Plugin::Analytics::ANALYTICS_METHOD_SET_TIME_READY = "setTimeReady";

namespace WPEFramework {

Expand All @@ -34,15 +31,11 @@ namespace Plugin {
void Analytics::RegisterAll()
{
Register(_T(ANALYTICS_METHOD_SEND_EVENT), &Analytics::SendEventWrapper, this);
Register(_T(ANALYTICS_METHOD_SET_SESSION_ID), &Analytics::SetSessionIdWrapper, this);
Register(_T(ANALYTICS_METHOD_SET_TIME_READY), &Analytics::SetTimeReadyWrapper, this);
}

void Analytics::UnregisterAll()
{
Unregister(_T(ANALYTICS_METHOD_SEND_EVENT));
Unregister(_T(ANALYTICS_METHOD_SET_SESSION_ID));
Unregister(_T(ANALYTICS_METHOD_SET_TIME_READY));
}

// API implementation
Expand Down Expand Up @@ -93,44 +86,6 @@ namespace Plugin {
returnResponse(result == Core::ERROR_NONE);
}

// Method: setSessionId - Set the session ID
// Return codes:
// - ERROR_NONE: Success
// - ERROR_GENERAL: Failed to set the session ID
uint32_t Analytics::SetSessionIdWrapper(const JsonObject& parameters, JsonObject& response)
{
LOGINFOMETHOD();

uint32_t result = Core::ERROR_NONE;

returnIfStringParamNotFound(parameters, "sessionId");

string sessionId = parameters["sessionId"].String();

if (mAnalytics != nullptr) {
result = mAnalytics->SetSessionId(sessionId);
}

returnResponse(result == Core::ERROR_NONE);
}

// Method: setTimeReady - Set the time ready
// Return codes:
// - ERROR_NONE: Success
// - ERROR_GENERAL: Failed to set the time ready
uint32_t Analytics::SetTimeReadyWrapper(const JsonObject& parameters, JsonObject& response)
{
LOGINFOMETHOD();

uint32_t result = Core::ERROR_NONE;

if (mAnalytics != nullptr) {
result = mAnalytics->SetTimeReady();
}

returnResponse(result == Core::ERROR_NONE);
}

}

}
3 changes: 3 additions & 0 deletions Analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ All notable changes to this RDK Service will be documented in this file.

For more details, refer to versioning section under Main README.

## [1.0.1] - 2024-10-16
- Support generation of sessionID, SysTime validation and limit nbr of external attributes for Sift

## [1.0.0] - 2024-07-25
### Added
- New RDK Service Analytics to handle analytics events and send them to dedicated backends
Expand Down
11 changes: 8 additions & 3 deletions Analytics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set(MODULE_NAME ${NAMESPACE}${PLUGIN_NAME})

set(VERSION_MAJOR 1)
set(VERSION_MINOR 0)
set(VERSION_PATCH 0)
set(VERSION_PATCH 1)

add_compile_definitions(ANALYTICS_MAJOR_VERSION=${VERSION_MAJOR})
add_compile_definitions(ANALYTICS_MINOR_VERSION=${VERSION_MINOR})
Expand All @@ -42,13 +42,14 @@ set(PLUGIN_ANALYTICS_SIFT_ENV "prod" CACHE STRING "Sift environment")
set(PLUGIN_ANALYTICS_SIFT_PRODUCT_NAME "entos" CACHE STRING "Sift product name") #entos-immerse in Sift2.0
set(PLUGIN_ANALYTICS_SIFT_LOGGER_NAME "Analytics" CACHE STRING "Sift logger name")
set(PLUGIN_ANALYTICS_SIFT_LOGGER_VERSION "${MODULE_VERSION}" CACHE STRING "Sift logger version")
set(PLUGIN_ANALYTICS_SIFT_PLATFORM_DEFAULT "entos:rdk" CACHE STRING "Sift platform default value")
set(PLUGIN_ANALYTICS_SIFT_MAX_RANDOMISATION_WINDOW_TIME 300 CACHE STRING "Sift max randomisation window time of posting queued events")
set(PLUGIN_ANALYTICS_SIFT_MAX_EVENTS_IN_POST 10 CACHE STRING "Sift max events in post")
set(PLUGIN_ANALYTICS_SIFT_MAX_RETRIES 10 CACHE STRING "Sift max retries posting events")
set(PLUGIN_ANALYTICS_SIFT_MIN_RETRY_PERIOD 1 CACHE STRING "Sift min retry period seconds")
set(PLUGIN_ANALYTICS_SIFT_MAX_RETRY_PERIOD 30 CACHE STRING "Sift max retry period seconds")
set(PLUGIN_ANALYTICS_SIFT_EXPONENTIAL_PERIODIC_FACTOR 2 CACHE STRING "Sift exponential periodic factor")
set(PLUGIN_ANALYTICS_SIFT_STORE_PATH "/opt/persistent/sky/AnalyticsSiftStore" CACHE STRING "Sift store path")
set(PLUGIN_ANALYTICS_SIFT_STORE_PATH "/persistent/AnalyticsSiftStore" CACHE STRING "Sift store path")
set(PLUGIN_ANALYTICS_SIFT_STORE_EVENTS_LIMIT 1000 CACHE STRING "Sift store events limit")
set(PLUGIN_ANALYTICS_SIFT_URL "" CACHE STRING "Sift URL")

Expand All @@ -67,8 +68,11 @@ add_library(${MODULE_NAME} SHARED
Module.cpp)

target_include_directories(${MODULE_NAME} PRIVATE Implementation)
target_include_directories(${MODULE_NAME} PRIVATE Implementation/SystemTime)
target_include_directories(${MODULE_NAME} PRIVATE ../)
target_include_directories(${MODULE_NAME} PRIVATE ../helpers)

add_subdirectory(Implementation/SystemTime)
add_subdirectory(Implementation/LocalStore)
add_subdirectory(Implementation/Backend)

Expand All @@ -85,7 +89,8 @@ target_link_libraries(${MODULE_NAME}
${NAMESPACE}Definitions::${NAMESPACE}Definitions
${DS_LIBRARIES}
${IARMBUS_LIBRARIES}
${MODULE_NAME}Backends)
${MODULE_NAME}Backends
${MODULE_NAME}SystemTime)

install(TARGETS ${MODULE_NAME}
DESTINATION lib/${STORAGE_DIRECTORY}/plugins)
Expand Down
70 changes: 21 additions & 49 deletions Analytics/Implementation/AnalyticsImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "AnalyticsImplementation.h"
#include "Backend/AnalyticsBackend.h"
#include "UtilsLogging.h"
#include "SystemTime.h"

#include <fstream>
#include <streambuf>
Expand All @@ -36,7 +37,7 @@ namespace Plugin {
mQueueCondition(),
mActionQueue(),
mEventQueue(),
mBackends(IAnalyticsBackendAdministrator::Instances()),
mBackends(IAnalyticsBackendAdministrator::Create()),
mSysTimeValid(false),
mShell(nullptr)
{
Expand All @@ -45,20 +46,21 @@ namespace Plugin {

AnalyticsImplementation::~AnalyticsImplementation()
{
LOGINFO("AnalyticsImplementation::~AnalyticsImplementation()");
std::unique_lock<std::mutex> lock(mQueueMutex);
mActionQueue.push({ACTION_TYPE_SHUTDOWN, nullptr});
lock.unlock();
mQueueCondition.notify_one();
mThread.join();
}

/* virtual */ uint32_t AnalyticsImplementation::SendEvent(const string& eventName,
/* virtual */ Core::hresult AnalyticsImplementation::SendEvent(const string& eventName,
const string& eventVersion,
const string& eventSource,
const string& eventSourceVersion,
RPC::IStringIterator* const& cetList,
const uint64_t& epochTimestamp,
const uint64_t& uptimeTimestamp,
IStringIterator* const& cetList,
const uint64_t epochTimestamp,
const uint64_t uptimeTimestamp,
const string& eventPayload)
{
std::shared_ptr<Event> event = std::make_shared<Event>();
Expand Down Expand Up @@ -98,39 +100,23 @@ namespace Plugin {
return Core::ERROR_NONE;
}

uint32_t AnalyticsImplementation::SetSessionId(const string& id)
{
uint32_t ret = Core::ERROR_GENERAL;
// set session id in sift backend
if (mBackends.find(IAnalyticsBackend::SIFT) != mBackends.end())
{
ret = mBackends.at(IAnalyticsBackend::SIFT).SetSessionId(id);
}

return ret;
}

uint32_t AnalyticsImplementation::SetTimeReady()
{
// set time ready action
std::unique_lock<std::mutex> lock(mQueueMutex);
mActionQueue.push({ACTION_TYPE_SET_TIME_READY, nullptr});
lock.unlock();
mQueueCondition.notify_one();
return Core::ERROR_NONE;
}

uint32_t AnalyticsImplementation::Configure(PluginHost::IShell* shell)
{
LOGINFO("Configuring Analytics");
uint32_t result = Core::ERROR_NONE;
ASSERT(shell != nullptr);
mShell = shell;

mSysTime = std::make_shared<SystemTime>(shell);
if(mSysTime == nullptr)
{
LOGERR("Failed to create SystemTime instance");
}

for (auto &backend : mBackends)
{
LOGINFO("Configuring backend: %s", backend.first.c_str());
backend.second.Configure(shell);
backend.second->Configure(shell, mSysTime);
}

return result;
Expand Down Expand Up @@ -177,7 +163,7 @@ namespace Plugin {
switch (action.type) {
case ACTION_POPULATE_TIME_INFO:

//mSysTimeValid = IsSysTimeValid();
mSysTimeValid = IsSysTimeValid();

if ( mSysTimeValid )
{
Expand Down Expand Up @@ -225,24 +211,8 @@ namespace Plugin {
}
break;
case ACTION_TYPE_SHUTDOWN:
LOGINFO("Shutting down Analytics");
return;
case ACTION_TYPE_SET_TIME_READY:
{
mSysTimeValid = true;
// Send the events from the queue, if there are any.
while ( !mEventQueue.empty() )
{
AnalyticsImplementation::Event event = mEventQueue.front();
// convert uptime to epoch timestamp
if (event.epochTimestamp == 0)
{
event.epochTimestamp = ConvertUptimeToTimestampInMs(event.uptimeTimestamp);
}

SendEventToBackend( event );
mEventQueue.pop();
}
}break;
default:
break;
}
Expand All @@ -254,8 +224,10 @@ namespace Plugin {
bool AnalyticsImplementation::IsSysTimeValid()
{
bool ret = false;
//TODO: Add system time validationm
// For now, relay on setTimeReady call
if (mSysTime != nullptr)
{
ret = mSysTime->IsSystemTimeAvailable();
}

return ret;
}
Expand All @@ -279,7 +251,7 @@ namespace Plugin {
else if (mBackends.find(IAnalyticsBackend::SIFT) != mBackends.end())
{
LOGINFO("Sending event to Sift backend: %s", event.eventName.c_str());
mBackends.at(IAnalyticsBackend::SIFT).SendEvent(backendEvent);
mBackends.at(IAnalyticsBackend::SIFT)->SendEvent(backendEvent);
}
}

Expand Down
12 changes: 6 additions & 6 deletions Analytics/Implementation/AnalyticsImplementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <interfaces/IAnalytics.h>
#include <interfaces/IConfiguration.h>
#include "Backend/AnalyticsBackend.h"
#include "SystemTime.h"

#include <mutex>
#include <condition_variable>
Expand Down Expand Up @@ -76,16 +77,14 @@ namespace Plugin {


// IAnalyticsImplementation interface
uint32_t SendEvent(const string& eventName,
Core::hresult SendEvent(const string& eventName,
const string& eventVersion,
const string& eventSource,
const string& eventSourceVersion,
RPC::IStringIterator* const& cetList,
const uint64_t& epochTimestamp,
const uint64_t& uptimeTimestamp,
IStringIterator* const& cetList,
const uint64_t epochTimestamp,
const uint64_t uptimeTimestamp,
const string& eventPayload) override;
uint32_t SetSessionId(const string& id) override;
uint32_t SetTimeReady() override;


// IConfiguration interface
Expand All @@ -107,6 +106,7 @@ namespace Plugin {
const IAnalyticsBackends mBackends;
bool mSysTimeValid;
PluginHost::IShell* mShell;
SystemTimePtr mSysTime;
};
}
}
7 changes: 3 additions & 4 deletions Analytics/Implementation/Backend/AnalyticsBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ namespace Plugin {

const std::string IAnalyticsBackend::SIFT = "Sift";

IAnalyticsBackends& IAnalyticsBackendAdministrator::Instances()
IAnalyticsBackends IAnalyticsBackendAdministrator::Create()
{
static SiftBackend siftBackend;
static IAnalyticsBackends backendInstances = {
IAnalyticsBackends backendInstances = {
#ifdef ANALYTICS_SIFT_BACKEND
{IAnalyticsBackend::SIFT, siftBackend},
{IAnalyticsBackend::SIFT, std::make_shared<SiftBackend>()},
#endif
};
return (backendInstances);
Expand Down
10 changes: 6 additions & 4 deletions Analytics/Implementation/Backend/AnalyticsBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <map>
#include <string>
#include "../../Module.h"
#include "../SystemTime/SystemTime.h"

// Interface for Analytics Backedn
namespace WPEFramework {
Expand All @@ -42,15 +43,16 @@ namespace Plugin {

const static std::string SIFT;

virtual uint32_t Configure(PluginHost::IShell* shell) = 0;
virtual uint32_t Configure(PluginHost::IShell* shell, SystemTimePtr sysTime) = 0;
virtual uint32_t SendEvent(const Event& event) = 0;
virtual uint32_t SetSessionId(const std::string& sessionId) = 0;
};

typedef std::map<std::string, IAnalyticsBackend&> IAnalyticsBackends;
typedef std::shared_ptr<IAnalyticsBackend> IAnalyticsBackendPtr;

typedef std::map<std::string, IAnalyticsBackendPtr> IAnalyticsBackends;

struct IAnalyticsBackendAdministrator {
static IAnalyticsBackends& Instances();
static IAnalyticsBackends Create();

virtual ~IAnalyticsBackendAdministrator() = default;
};
Expand Down
Loading

0 comments on commit afbc538

Please sign in to comment.