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 17, 2024
1 parent 6f35ad1 commit 49e01fc
Show file tree
Hide file tree
Showing 17 changed files with 776 additions and 141 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
7 changes: 6 additions & 1 deletion Analytics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ 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")
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
54 changes: 18 additions & 36 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,6 +46,7 @@ namespace Plugin {

AnalyticsImplementation::~AnalyticsImplementation()
{
LOGINFO("AnalyticsImplementation::~AnalyticsImplementation()");
std::unique_lock<std::mutex> lock(mQueueMutex);
mActionQueue.push({ACTION_TYPE_SHUTDOWN, nullptr});
lock.unlock();
Expand Down Expand Up @@ -100,23 +102,11 @@ namespace Plugin {

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;
return Core::ERROR_NONE;
}

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;
}

Expand All @@ -127,10 +117,16 @@ namespace Plugin {
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 +173,7 @@ namespace Plugin {
switch (action.type) {
case ACTION_POPULATE_TIME_INFO:

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

if ( mSysTimeValid )
{
Expand Down Expand Up @@ -225,24 +221,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 +234,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 +261,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
2 changes: 2 additions & 0 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 @@ -107,6 +108,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
5 changes: 4 additions & 1 deletion Analytics/Implementation/Backend/Sift/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ endif (CURL_FOUND)
target_include_directories(${TARGET_LIB} PUBLIC "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(${TARGET_LIB} PRIVATE ../../../../helpers)
target_include_directories(${TARGET_LIB} PRIVATE ../../LocalStore)
target_include_directories(${TARGET_LIB} PRIVATE ../../SystemTime)
set_property(TARGET ${TARGET_LIB} PROPERTY POSITION_INDEPENDENT_CODE ON)
set_target_properties(${TARGET_LIB} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_link_libraries(${TARGET_LIB} PRIVATE ${NAMESPACE}Plugins::${NAMESPACE}Plugins ${NAMESPACE}${PLUGIN_NAME}LocalStore)
target_link_libraries(${TARGET_LIB} PRIVATE ${NAMESPACE}Plugins::${NAMESPACE}Plugins)
target_link_libraries(${TARGET_LIB} PRIVATE ${NAMESPACE}${PLUGIN_NAME}LocalStore)
target_link_libraries(${TARGET_LIB} PRIVATE ${NAMESPACE}${PLUGIN_NAME}SystemTime)
44 changes: 20 additions & 24 deletions Analytics/Implementation/Backend/Sift/SiftBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ namespace WPEFramework
, mConfigPtr(nullptr)
, mStorePtr(nullptr)
, mUploaderPtr(nullptr)
, mSessionId()
{
mThread = std::thread(&SiftBackend::ActionLoop, this);
}

SiftBackend::~SiftBackend()
{
LOGINFO("SiftBackend::~SiftBackend");
Action action = {ACTION_TYPE_SHUTDOWN, nullptr};
{
std::lock_guard<std::mutex> lock(mQueueMutex);
Expand All @@ -55,12 +57,12 @@ namespace WPEFramework
mThread.join();
}

/* virtual */ uint32_t SiftBackend::Configure(PluginHost::IShell *shell)
/* virtual */ uint32_t SiftBackend::Configure(PluginHost::IShell *shell, SystemTimePtr sysTime)
{
ASSERT(shell != nullptr);
std::unique_lock<std::mutex> lock(mQueueMutex);
mShell = shell;
mConfigPtr = std::unique_ptr<SiftConfig>(new SiftConfig(shell));
mConfigPtr = std::unique_ptr<SiftConfig>(new SiftConfig(shell, sysTime));
return Core::ERROR_NONE;
}

Expand All @@ -76,17 +78,6 @@ namespace WPEFramework
return Core::ERROR_NONE;
}

/* virtual */ uint32_t SiftBackend::SetSessionId(const std::string &sessionId)
{
std::unique_lock<std::mutex> lock(mQueueMutex);
if (mConfigPtr != nullptr)
{
mConfigPtr->SetSessionId(sessionId);
return Core::ERROR_NONE;
}
return Core::ERROR_GENERAL;
}

void SiftBackend::ActionLoop()
{
std::unique_lock<std::mutex> lock(mQueueMutex);
Expand Down Expand Up @@ -153,7 +144,6 @@ namespace WPEFramework
{
mUploaderPtr = std::unique_ptr<SiftUploader>(new SiftUploader(mStorePtr,
uploaderConfig.url,
uploaderConfig.apiKey,
uploaderConfig.maxRandomisationWindowTime,
uploaderConfig.maxEventsInPost,
uploaderConfig.maxRetries,
Expand All @@ -172,9 +162,9 @@ namespace WPEFramework
configValid = true;
// For Sift 1.0 update uploader with auth values if avaliable
// So they will be added to the events if missing
if (!attributes.schema2Enabled && !attributes.accountId.empty() && !attributes.deviceId.empty() && !attributes.partnerId.empty())
if (!attributes.schema2Enabled && !attributes.xboAccountId.empty() && !attributes.xboDeviceId.empty() && !attributes.partnerId.empty())
{
mUploaderPtr->setDeviceInfoRequiredFields(attributes.accountId, attributes.deviceId, attributes.partnerId);
mUploaderPtr->setDeviceInfoRequiredFields(attributes.xboAccountId, attributes.xboDeviceId, attributes.partnerId);
}
}
else
Expand Down Expand Up @@ -212,6 +202,7 @@ namespace WPEFramework
}
break;
case ACTION_TYPE_SHUTDOWN:
LOGINFO("Shutting down SiftBackend");
return;
default:
break;
Expand All @@ -223,6 +214,11 @@ namespace WPEFramework

bool SiftBackend::SendEventInternal(const Event &event, const SiftConfig::Attributes &attributes)
{
if (mSessionId.empty())
{
mSessionId = GenerateRandomUUID();
}

JsonObject eventJson = JsonObject();
if (attributes.schema2Enabled)
{
Expand Down Expand Up @@ -260,13 +256,13 @@ namespace WPEFramework
}
eventJson["device_model"] = attributes.deviceModel;
eventJson["device_type"] = attributes.deviceType;
eventJson["device_timezone"] = std::stoi(attributes.deviceTimeZone);
eventJson["device_timezone"] = attributes.deviceTimeZone;
eventJson["device_os_name"] = attributes.deviceOsName;
eventJson["device_os_version"] = attributes.deviceOsVersion;
eventJson["platform"] = attributes.platform;
eventJson["device_manufacturer"] = attributes.deviceManufacturer;
eventJson["authenticated"] = attributes.authenticated;
eventJson["session_id"] = attributes.sessionId;
eventJson["session_id"] = mSessionId;
eventJson["proposition"] = attributes.proposition;
if (!attributes.retailer.empty())
{
Expand Down Expand Up @@ -318,13 +314,13 @@ namespace WPEFramework
eventJson["event_name"] = event.eventName;
eventJson["event_schema"] = attributes.productName + "/" + event.eventName + "/" + event.eventVersion;
eventJson["event_payload"] = JsonObject(event.eventPayload);
eventJson["session_id"] = attributes.sessionId;
eventJson["session_id"] = mSessionId;
eventJson["event_id"] = GenerateRandomUUID();

if (!attributes.accountId.empty() && !attributes.deviceId.empty() && !attributes.partnerId.empty())
if (!attributes.xboAccountId.empty() && !attributes.xboDeviceId.empty() && !attributes.partnerId.empty())
{
eventJson["account_id"] = attributes.accountId;
eventJson["device_id"] = attributes.deviceId;
eventJson["account_id"] = attributes.xboAccountId;
eventJson["device_id"] = attributes.xboDeviceId;
eventJson["partner_id"] = attributes.partnerId;
}
else
Expand All @@ -335,9 +331,9 @@ namespace WPEFramework
eventJson["app_name"] = attributes.deviceAppName;
eventJson["app_ver"] = attributes.deviceAppVersion;
eventJson["device_model"] = attributes.deviceModel;
eventJson["device_timezone"] = std::stoi(attributes.deviceTimeZone);
eventJson["device_timezone"] = attributes.deviceTimeZone;
eventJson["platform"] = attributes.platform;
eventJson["os_ver"] = attributes.deviceSoftwareVersion;
eventJson["os_ver"] = attributes.deviceOsVersion;
eventJson["device_language"] = ""; // Empty for now
eventJson["timestamp"] = event.epochTimestamp;
eventJson["device_type"] = attributes.deviceType;
Expand Down
4 changes: 2 additions & 2 deletions Analytics/Implementation/Backend/Sift/SiftBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ namespace Plugin {
SiftBackend();
~SiftBackend();
uint32_t SendEvent(const Event& event) override;
uint32_t Configure(PluginHost::IShell* shell) override;
uint32_t SetSessionId(const std::string& sessionId) override;
uint32_t Configure(PluginHost::IShell* shell, SystemTimePtr sysTime) override;

private:

Expand Down Expand Up @@ -83,6 +82,7 @@ namespace Plugin {
SiftConfigPtr mConfigPtr;
SiftStorePtr mStorePtr;
SiftUploaderPtr mUploaderPtr;
std::string mSessionId;
};

}
Expand Down
Loading

0 comments on commit 49e01fc

Please sign in to comment.