Ejemplo n.º 1
0
        public void TestValidateClientTs()
        {
            Assert.True(GAValidator.ValidateClientTs(GAUtilities.TimeIntervalSince1970()));

            Assert.False(GAValidator.ValidateClientTs(long.MinValue));
            Assert.False(GAValidator.ValidateClientTs(long.MaxValue));
        }
Ejemplo n.º 2
0
        public static long GetClientTsAdjusted()
        {
            long clientTs = GAUtilities.TimeIntervalSince1970();
            long clientTsAdjustedInteger = clientTs + Instance.ClientServerTimeOffset;

            if (GAValidator.ValidateClientTs(clientTsAdjustedInteger))
            {
                return(clientTsAdjustedInteger);
            }
            else
            {
                return(clientTs);
            }
        }
Ejemplo n.º 3
0
        private static void ProcessEvents(string category, bool performCleanUp)
#endif
        {
            if (!GAState.IsEventSubmissionEnabled)
            {
                return;
            }

            try
            {
                string requestIdentifier = Guid.NewGuid().ToString();

                string selectSql;
                string updateSql;
                string deleteSql  = "DELETE FROM ga_events WHERE status = '" + requestIdentifier + "'";
                string putbackSql = "UPDATE ga_events SET status = 'new' WHERE status = '" + requestIdentifier + "';";

                // Cleanup
                if (performCleanUp)
                {
                    CleanupEvents();
                    FixMissingSessionEndEvents();
                }

                // Prepare SQL
                string andCategory = "";
                if (!string.IsNullOrEmpty(category))
                {
                    andCategory = " AND category='" + category + "' ";
                }
                selectSql = "SELECT event FROM ga_events WHERE status = 'new' " + andCategory + ";";
                updateSql = "UPDATE ga_events SET status = '" + requestIdentifier + "' WHERE status = 'new' " + andCategory + ";";

                // Get events to process
                JSONArray events = GAStore.ExecuteQuerySync(selectSql);

                // Check for errors or empty
                if (events == null || events.Count == 0)
                {
                    GALogger.I("Event queue: No events to send");
                    UpdateSessionTime();
                    return;
                }

                // Check number of events and take some action if there are too many?
                if (events.Count > MaxEventCount)
                {
                    // Make a limit request
                    selectSql = "SELECT client_ts FROM ga_events WHERE status = 'new' " + andCategory + " ORDER BY client_ts ASC LIMIT 0," + MaxEventCount + ";";
                    events    = GAStore.ExecuteQuerySync(selectSql);
                    if (events == null)
                    {
                        return;
                    }

                    // Get last timestamp
                    JSONNode lastItem      = events[events.Count - 1];
                    string   lastTimestamp = lastItem["client_ts"].Value;

                    // Select again
                    selectSql = "SELECT event FROM ga_events WHERE status = 'new' " + andCategory + " AND client_ts<='" + lastTimestamp + "';";
                    events    = GAStore.ExecuteQuerySync(selectSql);
                    if (events == null)
                    {
                        return;
                    }

                    // Update sql
                    updateSql = "UPDATE ga_events SET status='" + requestIdentifier + "' WHERE status='new' " + andCategory + " AND client_ts<='" + lastTimestamp + "';";
                }

                // Log
                GALogger.I("Event queue: Sending " + events.Count + " events.");

                // Set status of events to 'sending' (also check for error)
                if (GAStore.ExecuteQuerySync(updateSql) == null)
                {
                    return;
                }

                // Create payload data from events
                List <JSONNode> payloadArray = new List <JSONNode>();

                for (int i = 0; i < events.Count; ++i)
                {
                    JSONNode ev        = events[i];
                    JSONNode eventDict = null;

                    try
                    {
                        eventDict = JSONNode.LoadFromBinaryBase64(ev["event"].Value);
                    }
                    catch (Exception)
                    {
                        //GALogger.E("ProcessEvents: Error decoding json, " + e);
                    }

                    if (eventDict != null && eventDict.Count != 0)
                    {
                        if (!eventDict["client_ts"].IsNull)
                        {
                            long clientTs = eventDict["client_ts"].AsLong;
                            if (!GAValidator.ValidateClientTs(clientTs))
                            {
                                eventDict.Remove("client_ts");
                            }
                        }

                        payloadArray.Add(eventDict);
                    }
                }

                // send events
#if WINDOWS_UWP || WINDOWS_WSA
                KeyValuePair <EGAHTTPApiResponse, JSONNode> result = await GAHTTPApi.Instance.SendEventsInArray(payloadArray);
#else
                KeyValuePair <EGAHTTPApiResponse, JSONNode> result = GAHTTPApi.Instance.SendEventsInArray(payloadArray);
#endif

                ProcessEvents(result.Key, result.Value, putbackSql, deleteSql, payloadArray.Count);
            }
            catch (Exception e)
            {
                GALogger.E("Error during ProcessEvents(): " + e);
            }
        }