public void TestAddDictFunction()
        {
            TrackerPayload payload           = new TrackerPayload();
            Dictionary <string, object> dict = new Dictionary <string, object> ();

            dict.Add("hello", "world");
            dict.Add("demo", 10);

            Assert.AreEqual(0, payload.GetDictionary().Count);
            payload.AddDict(dict);
            Assert.AreEqual(1, payload.GetDictionary().Count);
            Assert.AreEqual("world", payload.GetDictionary()["hello"]);
        }
        public void TestAddJsonFunction()
        {
            TrackerPayload payload           = new TrackerPayload();
            Dictionary <string, object> dict = new Dictionary <string, object> ();

            dict.Add("hello", "world");

            Assert.AreEqual(0, payload.GetDictionary().Count);
            payload.AddJson(dict, false, "encoded", "not_encoded");
            Assert.AreEqual(1, payload.GetDictionary().Count);
            Assert.AreEqual("{\"hello\":\"world\"}", payload.GetDictionary()["not_encoded"]);
            Assert.AreEqual(39, payload.GetByteSize());
            Assert.AreEqual("{\"not_encoded\":\"{\\\"hello\\\":\\\"world\\\"}\"}", payload.ToString());
        }
        public void TestAddFunction()
        {
            TrackerPayload payload = new TrackerPayload();

            payload.Add("demo", "application");

            Assert.AreEqual(1, payload.GetDictionary().Count);
            Assert.AreEqual("application", payload.GetDictionary()["demo"]);

            payload.Add("demo", null);
            Assert.AreEqual(1, payload.GetDictionary().Count);
            payload.Add(null, "demo");
            Assert.AreEqual(1, payload.GetDictionary().Count);
        }
Exemplo n.º 4
0
        public void TestEventStoreFunctions()
        {
            EventStore es = new EventStore();

            Assert.AreEqual(0, es.GetEventCount());

            TrackerPayload payload = new TrackerPayload();

            payload.Add("hello", "world");

            Assert.IsTrue(es.AddEvent(payload));
            Assert.IsTrue(es.AddEvent(payload));
            Assert.IsTrue(es.AddEvent(payload));
            Assert.IsTrue(es.AddEvent(payload));
            Assert.AreEqual(4, es.GetEventCount());
            Assert.IsTrue(es.DeleteEvent(4));
            Assert.AreEqual(3, es.GetEventCount());
            Assert.IsTrue(es.DeleteEvents(new List <int> {
                3, 2
            }));
            Assert.AreEqual(1, es.GetEventCount());
            Assert.IsTrue(es.DeleteAllEvents());
            Assert.AreEqual(0, es.GetEventCount());

            Assert.IsTrue(es.AddEvent(payload));
            Assert.IsTrue(es.AddEvent(payload));
            Assert.IsTrue(es.AddEvent(payload));
            Assert.IsTrue(es.AddEvent(payload));
            Assert.AreEqual(4, es.GetEventCount());
            Assert.AreEqual(4, es.GetAllEvents().Count);
            Assert.AreEqual(2, es.GetDescEventRange(2).Count);
            Assert.AreEqual(payload.GetDictionary(), es.GetEvent(1).GetPayload().GetDictionary());

            Assert.IsTrue(es.DeleteAllEvents());
        }
Exemplo n.º 5
0
        // --- Database Functions

        /// <summary>
        /// Adds an event payload to the database.
        /// </summary>
        /// <returns><c>true</c>, if event was added, <c>false</c> otherwise.</returns>
        /// <param name="payload">An event payload</param>
        public bool AddEvent(TrackerPayload payload)
        {
            bool result = false;

            if (IsDatabaseOpen())
            {
                byte[] bytes = Utils.SerializeDictionary(payload.GetDictionary());
                if (bytes != null)
                {
                    using (var someCommand = dbConnection.CreateCommand())
                    {
                        Log.Debug("Emitter: EventStore.AddEvent preparing SQL");
                        someCommand.CommandText = INSERT_EVENT + "(@payload)";
                        Log.Verbose("EventStore: Event add: Checking for null property");
                        SqliteParameter param = new SqliteParameter("@payload", System.Data.DbType.Binary);
                        param.Value = bytes;
                        someCommand.Parameters.Add(param);
                        // Check whether insert succeeded
                        result = someCommand.ExecuteNonQuery() > 0;
                        // Close and dispose of resources
                    }
                }
            }
            Log.Verbose("EventStore: Event add result: " + result);
            return(result);
        }
Exemplo n.º 6
0
        // --- Database Functions

        /// <summary>
        /// Adds an event payload to the database.
        /// </summary>
        /// <returns><c>true</c>, if event was added, <c>false</c> otherwise.</returns>
        /// <param name="payload">An event payload</param>
        public bool AddEvent(TrackerPayload payload)
        {
            bool result = false;

            if (IsDatabaseOpen())
            {
                byte[] bytes = Utils.SerializeDictionary(payload.GetDictionary());
                if (bytes != null)
                {
                    // Build Insert Command
                    IDbCommand command = dbConnection.CreateCommand();
                    command.CommandText = INSERT_EVENT + "(@payload)";
                    SqliteParameter param = new SqliteParameter("@payload", DbType.Binary);
                    param.Value = bytes;
                    command.Parameters.Add(param);

                    // Check whether insert succeeded
                    result = command.ExecuteNonQuery() > 0;

                    // Close and dispose of resources
                    command.Dispose();
                    command = null;
                }
            }
            Log.Verbose("EventStore: Event add result: " + result);
            return(result);
        }
        /// <summary>
        /// Sends all events as GET requests on background threads
        /// </summary>
        /// <returns>The get.</returns>
        /// <param name="eventRows">Event rows.</param>
        protected int HttpGet(List <EventRow> eventRows, ConcurrentQueue <RequestResult> resultQueue)
        {
            int count = eventRows.Count;

            try
            {
                // Send each row as an individual GET Request
                foreach (EventRow eRow in eventRows)
                {
                    TrackerPayload payload  = eRow.GetPayload();
                    long           byteSize = payload.GetByteSize() + POST_STM_BYTES;
                    bool           oversize = byteSize > byteLimitGet;
                    Log.Debug("Emitter: Sending GET with byte-size: " + byteSize);
                    new ReadyRequest(
                        GetGETRequest(payload.GetDictionary()),
                        new List <int> {
                        eRow.GetRowId()
                    },
                        oversize,
                        resultQueue
                        ).Send();
                }
            }
            catch (Exception e)
            {
                Log.Debug("Emitter: caught exception in HTTPGet request: " + e.Message);
                Log.Debug("Emitter: HTTPGet exception trace: " + e.StackTrace);
            }
            return(count);
        }
        public void TestInit()
        {
            TrackerPayload payload = new TrackerPayload();

            Assert.NotNull(payload);
            Assert.AreEqual(0, payload.GetDictionary().Count);
        }
Exemplo n.º 9
0
        // --- Interface Methods

        /// <summary>
        /// Gets the event payload.
        /// </summary>
        /// <returns>The event payload</returns>
        public override IPayload GetPayload()
        {
            TrackerPayload payload = new TrackerPayload();

            payload.Add(Constants.SV_NAME, this.name);
            payload.Add(Constants.SV_ID, this.id);
            return(new SelfDescribingJson(Constants.SCHEMA_SCREEN_VIEW, payload.GetDictionary()));
        }
        /// <summary>
        /// Send all event rows as POST requests on background threads
        /// </summary>
        /// <returns>The results of all the requests</returns>
        /// <param name="eventRows">Event rows.</param>
        protected int HttpPost(List <EventRow> eventRows, ConcurrentQueue <RequestResult> resultQueue)
        {
            int count = 0;

            List <int> rowIds = new List <int>();
            List <Dictionary <string, object> > payloadDicts = new List <Dictionary <string, object> >();
            long totalByteSize = 0;

            try
            {
                for (int i = 0; i < eventRows.Count; i++)
                {
                    TrackerPayload payload         = eventRows[i].GetPayload();
                    long           payloadByteSize = payload.GetByteSize() + POST_STM_BYTES;

                    if ((payloadByteSize + POST_WRAPPER_BYTES) > byteLimitPost)
                    {
                        // A single Payload has exceeded the Byte Limit
                        Log.Debug("Emitter: Single event exceeds byte limit: " + (payloadByteSize + POST_WRAPPER_BYTES) + " is > " + byteLimitPost);
                        Log.Debug("Sending POST with byte-size: " + (payloadByteSize + POST_WRAPPER_BYTES));
                        List <Dictionary <string, object> > singlePayloadPost = new List <Dictionary <string, object> > {
                            payload.GetDictionary()
                        };
                        List <int> singlePayloadId = new List <int> {
                            eventRows[i].GetRowId()
                        };
                        new ReadyRequest(GetPOSTRequest(singlePayloadPost), singlePayloadId, true, resultQueue).Send();
                        count++;
                    }
                    else if ((totalByteSize + payloadByteSize + POST_WRAPPER_BYTES + (payloadDicts.Count - 1)) > byteLimitPost)
                    {
                        Log.Debug("Emitter: Byte limit reached: " + (totalByteSize + payloadByteSize + POST_WRAPPER_BYTES + (payloadDicts.Count - 1)) +
                                  " is > " + byteLimitPost);
                        Log.Debug("Emitter: Sending POST with byte-size: " + (totalByteSize + POST_WRAPPER_BYTES + (payloadDicts.Count - 1)));
                        new ReadyRequest(GetPOSTRequest(payloadDicts), rowIds, false, resultQueue).Send();
                        count++;

                        // Reset collections
                        payloadDicts = new List <Dictionary <string, object> > {
                            payload.GetDictionary()
                        };
                        rowIds = new List <int> {
                            eventRows[i].GetRowId()
                        };
                        totalByteSize = payloadByteSize;
                    }
                    else
                    {
                        payloadDicts.Add(payload.GetDictionary());
                        rowIds.Add(eventRows[i].GetRowId());
                        totalByteSize += payloadByteSize;
                    }
                }

                if (payloadDicts.Count > 0)
                {
                    Log.Debug("Emitter: Sending POST with byte-size: " + (totalByteSize + POST_WRAPPER_BYTES + (payloadDicts.Count - 1)));
                    new ReadyRequest(GetPOSTRequest(payloadDicts), rowIds, false, resultQueue).Send();
                    count++;
                }
            }
            catch (Exception e)
            {
                Log.Debug("Emitter: caught exception in HTTPPost request: " + e.Message);
                Log.Debug("Emitter: HTTPPost exception trace: " + e.StackTrace);
            }
            return(count);
        }