Skip to content

Commit

Permalink
RDK-51273: Fix deactivation and time correction
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianM27 committed Sep 18, 2024
1 parent 63ad130 commit c2ac695
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 27 deletions.
2 changes: 2 additions & 0 deletions Analytics/Analytics.config
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
4 changes: 1 addition & 3 deletions Analytics/Analytics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Analytics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
47 changes: 37 additions & 10 deletions Analytics/Implementation/AnalyticsImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(mQueueMutex);
mActionQueue.push({ACTION_TYPE_SHUTDOWN, nullptr});
mQueueMutex.unlock();
lock.unlock();
mQueueCondition.notify_one();
mThread.join();
}
Expand Down Expand Up @@ -161,14 +164,16 @@ namespace Plugin {
{
action = {ACTION_POPULATE_TIME_INFO, nullptr};
}
else
else if (!mActionQueue.empty())
{
action = mActionQueue.front();
mActionQueue.pop();
}

lock.unlock();

LOGINFO("Action: %d, time valid: %s", action.type, mSysTimeValid? "true" : "false");

switch (action.type) {
case ACTION_POPULATE_TIME_INFO:

Expand All @@ -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();
}
}
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand Down
1 change: 0 additions & 1 deletion Analytics/Implementation/Backend/AnalyticsBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Analytics/Implementation/Backend/Sift/SiftBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ namespace WPEFramework
, mUploaderPtr(nullptr)
{
mThread = std::thread(&SiftBackend::ActionLoop, this);
mThread.detach();
}

SiftBackend::~SiftBackend()
Expand All @@ -53,6 +52,7 @@ namespace WPEFramework
mActionQueue.push(action);
}
mQueueCondition.notify_one();
mThread.join();
}

/* virtual */ uint32_t SiftBackend::Configure(PluginHost::IShell *shell)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -181,8 +183,6 @@ namespace WPEFramework
}
lock.unlock();

LOGINFO("Action %d", action.type);

switch (action.type)
{
case ACTION_TYPE_POPULATE_CONFIG:
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 35 additions & 4 deletions Analytics/Implementation/Backend/Sift/SiftConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace WPEFramework
, Schema2(false)
, CommonSchema()
, Env()
, ProductName("rdk")
, ProductName()
, LoggerName()
, LoggerVersion()
, MaxRandomisationWindowTime(300)
Expand All @@ -61,7 +61,7 @@ namespace WPEFramework
, MinRetryPeriod(1)
, MaxRetryPeriod(30)
, ExponentialPeriodicFactor(2)
, StorePath("/opt/persistent/sky/AnalyticsSiftStore")
, StorePath()
, EventsLimit(1000)
, Url()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -484,7 +484,6 @@ namespace WPEFramework
void SiftConfig::TriggerInitialization()
{
mInitializationThread = std::thread(&SiftConfig::Initialize, this);
mInitializationThread.detach();
}

void SiftConfig::InitializeKeysMap()
Expand Down Expand Up @@ -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<JsonObject, JsonObject>(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<JsonObject, JsonObject>(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());
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Analytics/Implementation/Backend/Sift/SiftUploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ namespace WPEFramework
, mEventStartIndex(0)
{
mThread = std::thread(&SiftUploader::Run, this);
mThread.detach();
}

SiftUploader::~SiftUploader()
Expand Down Expand Up @@ -89,6 +88,7 @@ namespace WPEFramework
[this] () { return mStop; } );
if (mStop)
{
LOGINFO("SiftUploader exit");
return;
}

Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion Analytics/Implementation/LocalStore/DatabaseConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
**/
#pragma once

#include "Module.h"
#include "../../Module.h"
#include "DatabaseInterface.h"

#include <mutex>
Expand Down
2 changes: 1 addition & 1 deletion Analytics/Implementation/LocalStore/LocalStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <map>
#include <memory>

#include "Module.h"
#include "../../Module.h"
#include "DatabaseConnection.h"

namespace WPEFramework
Expand Down

0 comments on commit c2ac695

Please sign in to comment.