From 782e6500059aa4a39037ce0dfd3799c43547b425 Mon Sep 17 00:00:00 2001 From: Zahid Zafar Date: Thu, 10 Jun 2021 18:19:30 +0500 Subject: [PATCH] [sdk 396] Event Q emptying into RQ issue fixed (#110) * Production Issue fixed * Update CHANGELOG.md * Update CHANGELOG.md Co-authored-by: ArtursKadikis --- .../Plugins/CountlySDK/Helpers/Constants.cs | 2 +- .../Helpers/RequestCountlyHelper.cs | 16 +++----- .../Services/ConsentCountlyService.cs | 5 ++- .../Services/CrashReportsCountlyService.cs | 5 ++- .../Services/DeviceIdCountlyService.cs | 7 +++- .../Services/EventCountlyService.cs | 41 +++++-------------- .../CountlySDK/Services/LocationService.cs | 8 ++-- .../CountlySDK/Services/PushCountlyService.cs | 5 ++- .../Services/SessionCountlyService.cs | 17 ++++---- .../Services/UserDetailsCountlyService.cs | 8 ++-- CHANGELOG.md | 3 ++ 11 files changed, 54 insertions(+), 63 deletions(-) diff --git a/Assets/Plugins/CountlySDK/Helpers/Constants.cs b/Assets/Plugins/CountlySDK/Helpers/Constants.cs index 0cabac86..7a2917ec 100644 --- a/Assets/Plugins/CountlySDK/Helpers/Constants.cs +++ b/Assets/Plugins/CountlySDK/Helpers/Constants.cs @@ -3,7 +3,7 @@ namespace Plugins.CountlySDK.Helpers { internal class Constants { - public const string SdkVersion = "20.11.2"; + public const string SdkVersion = "20.11.3"; #if UNITY_EDITOR public const string SdkName = "csharp-unity-editor"; diff --git a/Assets/Plugins/CountlySDK/Helpers/RequestCountlyHelper.cs b/Assets/Plugins/CountlySDK/Helpers/RequestCountlyHelper.cs index a89fdaa9..b9c687b7 100644 --- a/Assets/Plugins/CountlySDK/Helpers/RequestCountlyHelper.cs +++ b/Assets/Plugins/CountlySDK/Helpers/RequestCountlyHelper.cs @@ -30,7 +30,7 @@ internal RequestCountlyHelper(CountlyConfiguration config, CountlyLogHelper log, _requestRepo = requestRepo; } - private async Task AddRequestToQueue(CountlyRequestModel request) + private void AddRequestToQueue(CountlyRequestModel request) { Log.Verbose("[RequestCountlyHelper] AddRequestToQueue: " + request.ToString()); @@ -47,8 +47,6 @@ private async Task AddRequestToQueue(CountlyRequestModel request) } _requestRepo.Enqueue(request); - - await ProcessQueue(); } internal async Task ProcessQueue() @@ -151,12 +149,10 @@ internal string BuildPostRequest(Dictionary queryParams) } /// - /// Uses GetAsync/PostAsync method to make request to the Countly server and returns the response. + /// An internal function to add a request to request queue. /// - /// - /// - /// - internal async Task GetResponseAsync(Dictionary queryParams) + + internal void AddToRequestQueue(Dictionary queryParams) { CountlyRequestModel requestModel; string data = BuildPostRequest(queryParams); @@ -166,7 +162,7 @@ internal async Task GetResponseAsync(Dictionary queryParams) requestModel = new CountlyRequestModel(true, BuildGetRequest(queryParams), null, DateTime.UtcNow); } - await AddRequestToQueue(requestModel); + AddRequestToQueue(requestModel); } /// @@ -244,4 +240,4 @@ private async Task PostAsync(string uri, string data) } } -} \ No newline at end of file +} diff --git a/Assets/Plugins/CountlySDK/Services/ConsentCountlyService.cs b/Assets/Plugins/CountlySDK/Services/ConsentCountlyService.cs index e899fc91..e342d58b 100644 --- a/Assets/Plugins/CountlySDK/Services/ConsentCountlyService.cs +++ b/Assets/Plugins/CountlySDK/Services/ConsentCountlyService.cs @@ -245,7 +245,8 @@ internal async Task SendConsentChanges(List consents, bool value) } }; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } #endregion @@ -349,4 +350,4 @@ private void NotifyListeners(List updatedConsents, bool newConsentValu internal override void DeviceIdChanged(string deviceId, bool merged) { } #endregion } -} \ No newline at end of file +} diff --git a/Assets/Plugins/CountlySDK/Services/CrashReportsCountlyService.cs b/Assets/Plugins/CountlySDK/Services/CrashReportsCountlyService.cs index c032422f..b192391d 100644 --- a/Assets/Plugins/CountlySDK/Services/CrashReportsCountlyService.cs +++ b/Assets/Plugins/CountlySDK/Services/CrashReportsCountlyService.cs @@ -89,7 +89,8 @@ internal async Task SendCrashReportInternal(CountlyExceptionDetailModel model) } }; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } @@ -170,4 +171,4 @@ internal override void DeviceIdChanged(string deviceId, bool merged) } #endregion } -} \ No newline at end of file +} diff --git a/Assets/Plugins/CountlySDK/Services/DeviceIdCountlyService.cs b/Assets/Plugins/CountlySDK/Services/DeviceIdCountlyService.cs index ef96811b..7160b893 100644 --- a/Assets/Plugins/CountlySDK/Services/DeviceIdCountlyService.cs +++ b/Assets/Plugins/CountlySDK/Services/DeviceIdCountlyService.cs @@ -104,7 +104,7 @@ public async Task ChangeDeviceIdWithoutMerge(string deviceId) } //Add currently recorded events to request queue----------------------------------- - await _eventCountlyService.AddEventsToRequestQueue(); + _eventCountlyService.AddEventsToRequestQueue(); //Ends current session //Do not dispose timer object @@ -122,6 +122,8 @@ public async Task ChangeDeviceIdWithoutMerge(string deviceId) } NotifyListeners(false); + + await _requestCountlyHelper.ProcessQueue(); } /// @@ -176,7 +178,8 @@ public async Task ChangeDeviceIdWithMerge(string deviceId) { "old_device_id", oldDeviceId } }; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); NotifyListeners(true); } diff --git a/Assets/Plugins/CountlySDK/Services/EventCountlyService.cs b/Assets/Plugins/CountlySDK/Services/EventCountlyService.cs index 0e3f953f..a22e04ba 100644 --- a/Assets/Plugins/CountlySDK/Services/EventCountlyService.cs +++ b/Assets/Plugins/CountlySDK/Services/EventCountlyService.cs @@ -27,16 +27,16 @@ internal EventCountlyService(CountlyConfiguration configuration, CountlyLogHelpe /// /// Add all recorded events to request queue /// - internal async Task AddEventsToRequestQueue() + internal void AddEventsToRequestQueue() { - Log.Debug("[EventCountlyService] AddEventsToRequestQueue"); + Log.Debug("[EventCountlyService] AddEventsToRequestQueue Start"); if (_eventRepo.Models.Count == 0) { return; } - + int count = _eventRepo.Models.Count; //Send all at once Dictionary requestParams = new Dictionary @@ -47,10 +47,11 @@ internal async Task AddEventsToRequestQueue() } }; - await _requestCountlyHelper.GetResponseAsync(requestParams); - - _eventRepo.Clear(); + _requestCountlyHelper.AddToRequestQueue(requestParams); + for (int i = 0; i < count; ++i) { + _eventRepo.Dequeue(); + } } /// @@ -70,7 +71,8 @@ internal async Task RecordEventAsync(CountlyEventModel @event) _eventRepo.Enqueue(@event); if (_eventRepo.Count >= _configuration.EventQueueThreshold) { - await AddEventsToRequestQueue(); + AddEventsToRequestQueue(); + await _requestCountlyHelper.ProcessQueue(); } } @@ -145,29 +147,6 @@ public async Task RecordEventAsync(string key, IDictionary segme await RecordEventAsync(@event); } - /// - /// Sends multiple events to the countly server. It expects a list of events as input. - /// - /// a list of events - /// - internal async Task ReportMultipleEventsAsync(List events) - { - if (events == null || events.Count == 0) { - return; - } - - Dictionary requestParams = - new Dictionary - { - { - "events", JsonConvert.SerializeObject(events, Formatting.Indented, - new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore}) - } - }; - - await _requestCountlyHelper.GetResponseAsync(requestParams); - } - /// /// Reports a custom event to the Countly server. /// @@ -230,4 +209,4 @@ internal override void ConsentChanged(List updatedConsents, bool newCo } #endregion } -} \ No newline at end of file +} diff --git a/Assets/Plugins/CountlySDK/Services/LocationService.cs b/Assets/Plugins/CountlySDK/Services/LocationService.cs index 304bd8c1..7bf5e64b 100644 --- a/Assets/Plugins/CountlySDK/Services/LocationService.cs +++ b/Assets/Plugins/CountlySDK/Services/LocationService.cs @@ -47,7 +47,8 @@ internal async Task SendRequestWithEmptyLocation() { "location", string.Empty } }; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } /// @@ -85,7 +86,8 @@ internal async Task SendIndependantLocationRequest() } if (requestParams.Count > 0) { - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } } @@ -183,4 +185,4 @@ internal override void ConsentChanged(List updatedConsents, bool newCo } #endregion } -} \ No newline at end of file +} diff --git a/Assets/Plugins/CountlySDK/Services/PushCountlyService.cs b/Assets/Plugins/CountlySDK/Services/PushCountlyService.cs index c72c549a..f92e9eb2 100644 --- a/Assets/Plugins/CountlySDK/Services/PushCountlyService.cs +++ b/Assets/Plugins/CountlySDK/Services/PushCountlyService.cs @@ -93,7 +93,8 @@ private async Task PostToCountlyAsync(TestMode? mode, string token) { $"{Constants.UnityPlatform}_token", token }, }; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } /// @@ -147,4 +148,4 @@ public IDictionary ToDictionary() } } -} \ No newline at end of file +} diff --git a/Assets/Plugins/CountlySDK/Services/SessionCountlyService.cs b/Assets/Plugins/CountlySDK/Services/SessionCountlyService.cs index c7e5f45a..26d6bd32 100644 --- a/Assets/Plugins/CountlySDK/Services/SessionCountlyService.cs +++ b/Assets/Plugins/CountlySDK/Services/SessionCountlyService.cs @@ -83,7 +83,7 @@ private async void SessionTimerOnElapsedAsync(object sender, ElapsedEventArgs el { Log.Debug("[SessionCountlyService] SessionTimerOnElapsedAsync"); - await _eventService.AddEventsToRequestQueue(); + _eventService.AddEventsToRequestQueue(); await _requestCountlyHelper.ProcessQueue(); if (!_configuration.IsAutomaticSessionTrackingDisabled) { @@ -142,7 +142,8 @@ the SDK adds an empty location entry to every "begin_session" request. */ requestParams.Add("metrics", JsonConvert.SerializeObject(CountlyMetricModel.Metrics, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } /// @@ -163,7 +164,7 @@ internal async Task EndSessionAsync() IsSessionInitiated = false; - await _eventService.AddEventsToRequestQueue(); + _eventService.AddEventsToRequestQueue(); Dictionary requestParams = new Dictionary @@ -171,9 +172,10 @@ internal async Task EndSessionAsync() {"end_session", 1}, {"session_duration", (DateTime.Now - _lastSessionRequestTime).TotalSeconds} }; - - await _requestCountlyHelper.GetResponseAsync(requestParams); + + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } @@ -203,7 +205,8 @@ internal async Task ExtendSessionAsync() _lastSessionRequestTime = DateTime.Now; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } @@ -225,4 +228,4 @@ internal override async void ConsentChanged(List updatedConsents, bool } #endregion } -} \ No newline at end of file +} diff --git a/Assets/Plugins/CountlySDK/Services/UserDetailsCountlyService.cs b/Assets/Plugins/CountlySDK/Services/UserDetailsCountlyService.cs index 86274140..16ec5c24 100644 --- a/Assets/Plugins/CountlySDK/Services/UserDetailsCountlyService.cs +++ b/Assets/Plugins/CountlySDK/Services/UserDetailsCountlyService.cs @@ -90,7 +90,8 @@ public async Task SetUserDetailsAsync(CountlyUserDetailsModel userDetailsModel) new JsonSerializerSettings{ NullValueHandling = NullValueHandling.Ignore }) }, }; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } /// @@ -129,7 +130,8 @@ public async Task SetCustomUserDetailsAsync(CountlyUserDetailsModel userDetailsM }) } }; - await _requestCountlyHelper.GetResponseAsync(requestParams); + _requestCountlyHelper.AddToRequestQueue(requestParams); + await _requestCountlyHelper.ProcessQueue(); } /// @@ -309,4 +311,4 @@ internal override void ConsentChanged(List updatedConsents, bool newCo } #endregion } -} \ No newline at end of file +} diff --git a/CHANGELOG.md b/CHANGELOG.md index d40d4e83..c9965bbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 20.11.3 +* Fixed potential timing issues in situations where events are combined into a request. + ## 20.11.2 * Added Consent feature * Added data type checking for segmentation