From c2ac695c6ede241c184b6a1cf47966a295a657b6 Mon Sep 17 00:00:00 2001 From: Adrian Muzyka Date: Wed, 18 Sep 2024 14:20:53 +0200 Subject: [PATCH] RDK-51273: Fix deactivation and time correction --- Analytics/Analytics.config | 2 + Analytics/Analytics.cpp | 4 +- Analytics/CMakeLists.txt | 2 +- .../AnalyticsImplementation.cpp | 47 +++++++++++++++---- .../Backend/AnalyticsBackend.cpp | 1 - .../Backend/Sift/SiftBackend.cpp | 8 ++-- .../Backend/Sift/SiftConfig.cpp | 39 +++++++++++++-- .../Backend/Sift/SiftUploader.cpp | 4 +- .../LocalStore/DatabaseConnection.h | 2 +- .../Implementation/LocalStore/LocalStore.h | 2 +- 10 files changed, 84 insertions(+), 27 deletions(-) diff --git a/Analytics/Analytics.config b/Analytics/Analytics.config index 0d57afeb1a..55d1e98150 100644 --- a/Analytics/Analytics.config +++ b/Analytics/Analytics.config @@ -27,6 +27,8 @@ if(PLUGIN_ANALYTICS_SIFT_BACKEND_ENABLED) kv(maxretryperiod, ${PLUGIN_ANALYTICS_SIFT_MAX_RETRY_PERIOD}) kv(exponentialperiodicfactor, ${PLUGIN_ANALYTICS_SIFT_EXPONENTIAL_PERIODIC_FACTOR}) kv(storepath, ${PLUGIN_ANALYTICS_SIFT_STORE_PATH}) + kv(eventslimit, ${PLUGIN_ANALYTICS_SIFT_STORE_EVENTS_LIMIT}) + kv(url, ${PLUGIN_ANALYTICS_SIFT_URL}) end() ans(siftobject) map_append(${configuration} sift ${siftobject}) diff --git a/Analytics/Analytics.cpp b/Analytics/Analytics.cpp index 007241fa63..03595602d5 100644 --- a/Analytics/Analytics.cpp +++ b/Analytics/Analytics.cpp @@ -69,13 +69,11 @@ namespace Plugin { /* virtual */ void Analytics::Deinitialize(PluginHost::IShell* service) { TRACE(Trace::Information, (_T("Analytics::Deinitialize"))); - ASSERT(service != nullptr); + ASSERT(service == mService); if (mAnalytics != nullptr) { - UnregisterAll(); RPC::IRemoteConnection *connection(service->RemoteConnection(mConnectionId)); - VARIABLE_IS_NOT_USED uint32_t result = mAnalytics->Release(); mAnalytics = nullptr; diff --git a/Analytics/CMakeLists.txt b/Analytics/CMakeLists.txt index 72814e243f..cb6f149f36 100644 --- a/Analytics/CMakeLists.txt +++ b/Analytics/CMakeLists.txt @@ -50,7 +50,7 @@ set(PLUGIN_ANALYTICS_SIFT_MAX_RETRY_PERIOD 30 CACHE STRING "Sift max retry perio 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_EVENTS_LIMIT 1000 CACHE STRING "Sift store events limit") -set(PLUGIN_ANALYTICS_SIFT_URL "https://sift.rdkcloud.com/v1/events" CACHE STRING "Sift URL") +set(PLUGIN_ANALYTICS_SIFT_URL "" CACHE STRING "Sift URL") message("Setup ${MODULE_NAME} v${MODULE_VERSION}") diff --git a/Analytics/Implementation/AnalyticsImplementation.cpp b/Analytics/Implementation/AnalyticsImplementation.cpp index 1d39da461f..4a2d2e211c 100644 --- a/Analytics/Implementation/AnalyticsImplementation.cpp +++ b/Analytics/Implementation/AnalyticsImplementation.cpp @@ -34,17 +34,20 @@ namespace Plugin { AnalyticsImplementation::AnalyticsImplementation(): mQueueMutex(), mQueueCondition(), - mBackends(IAnalyticsBackendAdministrator::Instances()) + mActionQueue(), + mEventQueue(), + mBackends(IAnalyticsBackendAdministrator::Instances()), + mSysTimeValid(false), + mShell(nullptr) { mThread = std::thread(&AnalyticsImplementation::ActionLoop, this); - mThread.detach(); } AnalyticsImplementation::~AnalyticsImplementation() { - mQueueMutex.lock(); + std::unique_lock lock(mQueueMutex); mActionQueue.push({ACTION_TYPE_SHUTDOWN, nullptr}); - mQueueMutex.unlock(); + lock.unlock(); mQueueCondition.notify_one(); mThread.join(); } @@ -161,7 +164,7 @@ namespace Plugin { { action = {ACTION_POPULATE_TIME_INFO, nullptr}; } - else + else if (!mActionQueue.empty()) { action = mActionQueue.front(); mActionQueue.pop(); @@ -169,6 +172,8 @@ namespace Plugin { lock.unlock(); + LOGINFO("Action: %d, time valid: %s", action.type, mSysTimeValid? "true" : "false"); + switch (action.type) { case ACTION_POPULATE_TIME_INFO: @@ -179,7 +184,14 @@ namespace Plugin { // Send the events from the queue, if there are any. while ( !mEventQueue.empty() ) { - SendEventToBackend( mEventQueue.front() ); + AnalyticsImplementation::Event event = mEventQueue.front(); + // convert uptime to epoch timestamp + if (event.epochTimestamp == 0) + { + event.epochTimestamp = ConvertUptimeToTimestampInMs(event.uptimeTimestamp); + } + + SendEventToBackend( event ); mEventQueue.pop(); } } @@ -189,7 +201,7 @@ namespace Plugin { if (mSysTimeValid) { // Add epoch timestamp if needed - // It should have at least uptime already + // It should have at least uptime already if (action.payload->epochTimestamp == 0) { action.payload->epochTimestamp = ConvertUptimeToTimestampInMs(action.payload->uptimeTimestamp); @@ -206,8 +218,9 @@ namespace Plugin { } else { - // Store the event in the queue + // Store the event in the queue with uptime only mEventQueue.push(*action.payload); + LOGINFO("SysTime not ready, event awaiting in queue: %s", action.payload->eventName.c_str()); } } break; @@ -216,6 +229,19 @@ namespace Plugin { 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; @@ -228,7 +254,8 @@ namespace Plugin { bool AnalyticsImplementation::IsSysTimeValid() { bool ret = false; - //TODO: Add system time validation + //TODO: Add system time validationm + // For now, relay on setTimeReady call return ret; } @@ -251,7 +278,7 @@ namespace Plugin { } else if (mBackends.find(IAnalyticsBackend::SIFT) != mBackends.end()) { - LOGINFO("Sending event to Sift backend"); + LOGINFO("Sending event to Sift backend: %s", event.eventName.c_str()); mBackends.at(IAnalyticsBackend::SIFT).SendEvent(backendEvent); } } diff --git a/Analytics/Implementation/Backend/AnalyticsBackend.cpp b/Analytics/Implementation/Backend/AnalyticsBackend.cpp index b75a26fe76..9e43324792 100644 --- a/Analytics/Implementation/Backend/AnalyticsBackend.cpp +++ b/Analytics/Implementation/Backend/AnalyticsBackend.cpp @@ -16,7 +16,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "Module.h" #include "AnalyticsBackend.h" #ifdef ANALYTICS_SIFT_BACKEND diff --git a/Analytics/Implementation/Backend/Sift/SiftBackend.cpp b/Analytics/Implementation/Backend/Sift/SiftBackend.cpp index a653958dab..5a028fb74a 100644 --- a/Analytics/Implementation/Backend/Sift/SiftBackend.cpp +++ b/Analytics/Implementation/Backend/Sift/SiftBackend.cpp @@ -42,7 +42,6 @@ namespace WPEFramework , mUploaderPtr(nullptr) { mThread = std::thread(&SiftBackend::ActionLoop, this); - mThread.detach(); } SiftBackend::~SiftBackend() @@ -53,6 +52,7 @@ namespace WPEFramework mActionQueue.push(action); } mQueueCondition.notify_one(); + mThread.join(); } /* virtual */ uint32_t SiftBackend::Configure(PluginHost::IShell *shell) @@ -126,6 +126,8 @@ namespace WPEFramework mActionQueue.pop(); } + LOGINFO("Action %d", action.type); + //Always get the most recent attributes bool attributesValid = false; bool storeConfigValid = (mStorePtr != nullptr); @@ -181,8 +183,6 @@ namespace WPEFramework } lock.unlock(); - LOGINFO("Action %d", action.type); - switch (action.type) { case ACTION_TYPE_POPULATE_CONFIG: @@ -329,7 +329,7 @@ namespace WPEFramework } else { - std::cout << "Sift: Account ID, Device ID, or Partner ID is empty for: " << event.eventName << std::endl; + LOGWARN(" Account ID, Device ID, or Partner ID is empty for: %s", event.eventName.c_str()); } eventJson["app_name"] = attributes.deviceAppName; diff --git a/Analytics/Implementation/Backend/Sift/SiftConfig.cpp b/Analytics/Implementation/Backend/Sift/SiftConfig.cpp index eadc80c084..ab8f23de51 100644 --- a/Analytics/Implementation/Backend/Sift/SiftConfig.cpp +++ b/Analytics/Implementation/Backend/Sift/SiftConfig.cpp @@ -52,7 +52,7 @@ namespace WPEFramework , Schema2(false) , CommonSchema() , Env() - , ProductName("rdk") + , ProductName() , LoggerName() , LoggerVersion() , MaxRandomisationWindowTime(300) @@ -61,7 +61,7 @@ namespace WPEFramework , MinRetryPeriod(1) , MaxRetryPeriod(30) , ExponentialPeriodicFactor(2) - , StorePath("/opt/persistent/sky/AnalyticsSiftStore") + , StorePath() , EventsLimit(1000) , Url() @@ -331,7 +331,7 @@ namespace WPEFramework uint32_t result = interface->Unregister(&mMonitorKeys); LOGINFO("IStore status %d", result); interface->Release(); - } + } } bool SiftConfig::GetAttributes(SiftConfig::Attributes &attributes) @@ -484,7 +484,6 @@ namespace WPEFramework void SiftConfig::TriggerInitialization() { mInitializationThread = std::thread(&SiftConfig::Initialize, this); - mInitializationThread.detach(); } void SiftConfig::InitializeKeysMap() @@ -633,6 +632,38 @@ namespace WPEFramework std::transform(mAttributes.env.begin(), mAttributes.env.end(), mAttributes.env.begin(), [](unsigned char c){ return std::tolower(c); }); mMutex.unlock(); + LOGINFO("Got env %s", mAttributes.env.c_str()); + } + + // Get deviceFriendlyName from System.1.getFriendlyName[friendlyName] + result = systemLink->Invoke(JSONRPC_THUNDER_TIMEOUT, "getFriendlyName", params, response); + if (result == Core::ERROR_NONE && response.HasLabel("friendlyName")) + { + mMutex.lock(); + mAttributes.deviceFriendlyName = response["friendlyName"].String(); + mMutex.unlock(); + LOGINFO("Got deviceFriendlyName %s", mAttributes.deviceFriendlyName.c_str()); + } + + // Get country from System.1.getTerritory[territory] + // Get region from System.1.getTerritory[region] + result = systemLink->Invoke(JSONRPC_THUNDER_TIMEOUT, "getTerritory", params, response); + if (result == Core::ERROR_NONE) + { + if (response.HasLabel("territory")) + { + mMutex.lock(); + mAttributes.country = response["territory"].String(); + mMutex.unlock(); + LOGINFO("Got country %s", mAttributes.country.c_str()); + } + if (response.HasLabel("region")) + { + mMutex.lock(); + mAttributes.region = response["region"].String(); + mMutex.unlock(); + LOGINFO("Got region %s", mAttributes.region.c_str()); + } } } diff --git a/Analytics/Implementation/Backend/Sift/SiftUploader.cpp b/Analytics/Implementation/Backend/Sift/SiftUploader.cpp index 971ab88e11..e3a839bd7d 100644 --- a/Analytics/Implementation/Backend/Sift/SiftUploader.cpp +++ b/Analytics/Implementation/Backend/Sift/SiftUploader.cpp @@ -53,7 +53,6 @@ namespace WPEFramework , mEventStartIndex(0) { mThread = std::thread(&SiftUploader::Run, this); - mThread.detach(); } SiftUploader::~SiftUploader() @@ -89,6 +88,7 @@ namespace WPEFramework [this] () { return mStop; } ); if (mStop) { + LOGINFO("SiftUploader exit"); return; } @@ -345,7 +345,7 @@ namespace WPEFramework JsonObject responseEvent(eventsArray[i].Object()); if (responseEvent.HasLabel("EventId")) { - if (responseEvent["event_id"].String() == eventId) + if (responseEvent["EventId"].String() == eventId) { found = true; if (responseEvent.HasLabel("Status") && responseEvent["Status"].String() != "valid") diff --git a/Analytics/Implementation/LocalStore/DatabaseConnection.h b/Analytics/Implementation/LocalStore/DatabaseConnection.h index 67d36b54ee..5b42fa1be8 100644 --- a/Analytics/Implementation/LocalStore/DatabaseConnection.h +++ b/Analytics/Implementation/LocalStore/DatabaseConnection.h @@ -18,7 +18,7 @@ **/ #pragma once -#include "Module.h" +#include "../../Module.h" #include "DatabaseInterface.h" #include diff --git a/Analytics/Implementation/LocalStore/LocalStore.h b/Analytics/Implementation/LocalStore/LocalStore.h index f940ccde11..e38a9c458c 100644 --- a/Analytics/Implementation/LocalStore/LocalStore.h +++ b/Analytics/Implementation/LocalStore/LocalStore.h @@ -25,7 +25,7 @@ #include #include -#include "Module.h" +#include "../../Module.h" #include "DatabaseConnection.h" namespace WPEFramework