AddEvent() public method

Add a single event to the specified collection.
public AddEvent ( string collection, object eventInfo, IEnumerable addOns = null ) : void
collection string Collection name
eventInfo object An object representing the event to be added.
addOns IEnumerable Optional collection of Data Enhancement Add-ons
return void
Example #1
0
        public override void Setup()
        {
            base.Setup();

            // If not using mocks, set up conditions on the server
            if (!UseMocks)
            {
                var client = new KeenClient(settingsEnv);
                //client.DeleteCollection(testCol);
                client.AddEvent(testCol, new { field1 = "99999999" });
            }
        }
Example #2
0
        public override void Setup()
        {
            base.Setup();

            // If not using mocks, set up conditions on the server
            if (!UseMocks)
            {
                var client = new KeenClient(SettingsEnv);

                client.DeleteCollection(FunnelColA);
                client.DeleteCollection(FunnelColB);
                client.DeleteCollection(FunnelColC);

                client.AddEvent(FunnelColA, new { id = 1, name = new { first = "sam", last = "w" } });
                client.AddEvent(FunnelColA, new { id = 2, name = new { first = "dean", last = "w" } });
                client.AddEvent(FunnelColA, new { id = 3, name = new { first = "crowly", last = "" } });

                client.AddEvent(FunnelColB, new { id = 1, name = new { first = "sam", last = "w" } });
                client.AddEvent(FunnelColB, new { id = 2, name = new { first = "dean", last = "w" } });

                client.AddEvent(FunnelColC, new { id = 1, name = new { first = "sam", last = "w" } });

                Thread.Sleep(8000); // Give it a moment to show up. Queries will fail if run too soon.
            }
        }
Example #3
0
        public void No_AddOn_Success()
        {
            var client = new KeenClient(SettingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(SettingsEnv,
                    addEvent: (c, e, p) =>
                    {
                        if (e["keen"].ToString().Contains("keen:ip_to_geo"))
                            throw new Exception("Unexpected values");
                    });

            Assert.DoesNotThrow(() => client.AddEvent("AddOnTest", new { an_ip = "70.187.8.97" }));
        }
Example #4
0
        public void IpToGeo_Send_Success()
        {
            var client = new KeenClient(SettingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(SettingsEnv,
                    addEvent: (c, e, p) =>
                    {
                        if (!e["keen"].ToString().Contains("keen:ip_to_geo"))
                            throw new Exception("Unexpected values");
                    });

            var a = AddOn.IpToGeo("an_ip", "geocode");

            Assert.DoesNotThrow(() => client.AddEvent("AddOnTest", new {an_ip = "70.187.8.97"}, new List<AddOn> {a}));
        }
Example #5
0
        public void UserAgentParser_Send_Success()
        {
            var client = new KeenClient(SettingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(SettingsEnv,
                    addEvent: (c, e, p) =>
                    {
                        if (!e["keen"].ToString().Contains("keen:ua_parser"))
                            throw new Exception("Unexpected values");
                    });

            var a = AddOn.UserAgentParser("user_agent_string", "user_agent_parsed");

            Assert.DoesNotThrow(() => client.AddEvent("AddOnTest", new { user_agent_string = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" }, new List<AddOn> { a }));
        }
Example #6
0
        public void IpToGeo_MissingInput_Throws()
        {
            var client = new KeenClient(SettingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(SettingsEnv,
                    addEvent: (c, e, p) =>
                    {
                        if (!e["keen"].ToString().Contains("\"ip\": \"an_ip\""))
                            throw new KeenException("Unexpected values");
                    });

            var a = AddOn.IpToGeo("wrong_field", "geocode");

            Assert.Throws<KeenException>(() => client.AddEvent("AddOnTest", new { an_ip = "70.187.8.97" }, new List<AddOn> { a }));
        }
        static void Main (string[] args)
        {
            // Loading Keen.IO Keys and Misc. from Config File
            _keenIOProjectID = ConfigurationManager.AppSettings["keenIOProjectID"];
            _keenIOMasterKey = ConfigurationManager.AppSettings["keenIOMasterKey"];
            _keenIOWriteKey  = ConfigurationManager.AppSettings["keenIOWriteKey"];
            _keenIOReadKey   = ConfigurationManager.AppSettings["keenIOReadKey"];
            _bucketName      = ConfigurationManager.AppSettings["keenIOBucketName"];

            // Configuring MongoDB Wrapper for connection and queries
            MongoDBWrapper mongoDB   = new MongoDBWrapper ();
            string fullServerAddress = String.Join (":", Consts.MONGO_SERVER, Consts.MONGO_PORT);
            mongoDB.ConfigureDatabase (Consts.MONGO_USER, Consts.MONGO_PASS, Consts.MONGO_AUTH_DB, fullServerAddress, Consts.MONGO_TIMEOUT, Consts.MONGO_DATABASE, Consts.MONGO_COLLECTION);

            // Creating Keen.IO Variables
            var projectSettings = new ProjectSettingsProvider (_keenIOProjectID, _keenIOMasterKey, _keenIOWriteKey, _keenIOReadKey);
            var keenClient      = new KeenClient (projectSettings);

            // From This point on, you can change your code to reflect your own "Reading" logic
            // What I've done is simply read the records from the MongoDB database and Upload them to Keen.IO
            foreach (var currentApp in mongoDB.FindMatch<AppModel> (Query.NE ("Uploaded", true)))
            {
                try
                {
                    // Adding Event to Keen.IO
                    keenClient.AddEvent ("PlayStore2014", currentApp);

                    // Incrementing Counter
                    _appsCounter++;

                    // Console feedback Every 100 Processed Apps
                    if (_appsCounter % 100 == 0)
                    {
                        Console.WriteLine ("Uploaded : " + _appsCounter);
                    }

                    mongoDB.SetUpdated (currentApp.Url);
                }
                catch (Exception ex)
                {
                    Console.WriteLine ("\n\t" + ex.Message);
                }
            }
        }
Example #8
0
        public async Task Async_Caching_SendEvents_Success()
        {
            var client = new KeenClient(settingsEnv, new EventCacheMemory());
            if (UseMocks)
                client.Event = new EventMock(settingsEnv,
                    AddEvents: new Func<JObject, IProjectSettings, IEnumerable<CachedEvent>>((e, p) =>
                    {
                        Assert.AreEqual(p, settingsEnv, "Unexpected settings object");
                        Assert.AreEqual(e.Property("CachedEventTest").Value.AsEnumerable().Count(), 3, "Expected exactly 3 collection members");
                        return new List<CachedEvent>();
                    }));

            client.AddEvent("CachedEventTest", new { AProperty = "AValue" });
            client.AddEvent("CachedEventTest", new { AProperty = "AValue" });
            client.AddEvent("CachedEventTest", new { AProperty = "AValue" });

            await client.SendCachedEventsAsync();
            Assert.Null(await client.EventCache.TryTake(), "Cache should be empty");
        }
Example #9
0
        public void Caching_SendInvalidEvents_Throws()
        {
            var client = new KeenClient(settingsEnv, new EventCacheMemory());
            if (UseMocks)
                client.Event = new EventMock(settingsEnv,
                    AddEvents: new Func<JObject, IProjectSettings, IEnumerable<CachedEvent>>((e, p) =>
                    {
                        if (p == settingsEnv)
                            throw new KeenBulkException("Mock exception", 
                                new List<CachedEvent>(){new CachedEvent("CachedEventTest", e, new Exception())});
                        else
                            throw new Exception("Unexpected value");
                    }));

            var anEvent = new JObject();
            anEvent.Add("AProperty", "AValue");
            client.AddEvent("CachedEventTest", anEvent);
            
            anEvent.Add("keen", JObject.FromObject(new {invalidPropName = "value"} ));
            client.AddEvent("CachedEventTest", anEvent);
            Assert.DoesNotThrow(() => 
                {
                    try
                    {
                        client.SendCachedEvents();
                    } 
                    catch (KeenBulkException ex)
                    {
                        if (ex.FailedEvents.Count() != 1)
                            throw new Exception("Expected 1 failed event.");
                    }
                });
        }
Example #10
0
        public async void Caching_SendManyEvents_Success()
        {
            var client = new KeenClient(settingsEnv, new EventCacheMemory());
            var total = 0;
            if (UseMocks)
                client.Event = new EventMock(settingsEnv,
                    AddEvents: new Func<JObject, IProjectSettings, IEnumerable<CachedEvent>>((e, p) =>
                    {
                        if ((p == settingsEnv)
                            && (null != e.Property("CachedEventTest"))
                            && ((e.Property("CachedEventTest").Value.Children().Count() == KeenConstants.BulkBatchSize)))
                        {
                            total += e.Property("CachedEventTest").Value.Children().Count();
                            return new List<CachedEvent>();
                        }
                        else
                            throw new Exception("Unexpected value");
                    }));

            for (int i = 0; i < KeenConstants.BulkBatchSize; i++)
                client.AddEvent("CachedEventTest", new { AProperty = "AValue" });

            Assert.DoesNotThrow(() => client.SendCachedEvents());
            Assert.Null(await client.EventCache.TryTake(), "Cache is empty");
            Assert.True( !UseMocks || ( total == KeenConstants.BulkBatchSize));
        }
Example #11
0
        public async void Caching_SendEvents_Success()
        {
            var client = new KeenClient(settingsEnv, new EventCacheMemory());
            if (UseMocks)
                client.Event = new EventMock(settingsEnv,
                    AddEvents: new Func<JObject, IProjectSettings, IEnumerable<CachedEvent>>((e, p) =>
                    {
                        if ((p == settingsEnv) 
                            && (null!=e.Property("CachedEventTest"))
                            && (e.Property("CachedEventTest").Value.Children().Count()==3))
                            return new List<CachedEvent>();
                        else
                            throw new Exception("Unexpected value");
                    }));

            client.AddEvent("CachedEventTest", new { AProperty = "AValue" });
            client.AddEvent("CachedEventTest", new { AProperty = "AValue" });
            client.AddEvent("CachedEventTest", new { AProperty = "AValue" });

            Assert.DoesNotThrow(() => client.SendCachedEvents());
            Assert.Null(await client.EventCache.TryTake(), "Cache is empty");
        }
Example #12
0
        public void Caching_AddEvents_Success()
        {
            var client = new KeenClient(settingsEnv, new EventCacheMemory());

            Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" }));
            Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" }));
            Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" }));
        }
Example #13
0
        public void AddGlobalProperty_DelegateValueProviderNullReturn_Throws()
        {
            var client = new KeenClient(settingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(settingsEnv,
                    AddEvent: new Action<string, JObject, IProjectSettings>((c, e, p) =>
                    {
                        if ((p == settingsEnv) && (c == "AddEventTest") 
                            && (e["AProperty"].Value<string>() == "AValue")
                            && (e["AGlobal"].Value<string>() == "value"))
                            return;
                        else
                            throw new Exception("Unexpected value");
                    }));

            var i = 0;
            // return valid for the first two tests, then null
            client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => i++ > 1 ? null : "value"));
            // This is the second test (AddGlobalProperty runs the first)
            Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", new { AProperty = "AValue" }));
            // Third test should fail.
            Assert.Throws<KeenException>(() => { client.AddEvent("AddEventTest", new { AProperty = "AValue" }); });
        }
Example #14
0
        public void CachingPCL_AddEvents_Success(IEventCache cache)
        {
            var client = new KeenClient(SettingsEnv, cache);

            Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" }));
            Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" }));
            Assert.DoesNotThrow(() => client.AddEvent("CachedEventTest", new { AProperty = "AValue" }));
        }
Example #15
0
        public void UrlParser_Send_Success()
        {
            var client = new KeenClient(SettingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(SettingsEnv,
                    addEvent: (c, e, p) =>
                    {
                        if (!e["keen"].ToString().Contains("keen:url_parser"))
                            throw new Exception("Unexpected values");
                    });

            var a = AddOn.UrlParser("url", "url_parsed");

            Assert.DoesNotThrow(() => client.AddEvent("AddOnTest", new { url = "https://keen.io/docs/data-collection/data-enrichment/#anchor" }, new List<AddOn> { a }));
        }
Example #16
0
        public void AddEvent_InvalidKeenNamespaceProperty_Throws()
        {
            var client = new KeenClient(settingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(settingsEnv,
                    AddEvent: new Action<string,JObject,IProjectSettings>((c,e,p) => 
                    {
                        if ((p == settingsEnv) 
                            && (c == "X") 
                            && (null != e.Property("keen"))
                            && (e.Property("keen").Value.GetType()==typeof(JObject))
                            && (null!=((JObject)e.Property("keen").Value).Property("AProperty")))
                                throw new KeenInvalidKeenNamespacePropertyException();
                        else
                            throw new Exception("Unexpected value");
                    }));

            Assert.Throws<KeenInvalidKeenNamespacePropertyException>(() => client.AddEvent("X", new { keen = new { AProperty = "AValue" } }));
        }
Example #17
0
 public void AddEvent_InvalidCollectionNameTooLong_Throws()
 {
     var client = new KeenClient(settingsEnv);
     Assert.Throws<KeenException>(() => client.AddEvent(new String('X', 257), new { AProperty = "AValue" }));
 }
Example #18
0
 public void AddEvent_InvalidCollectionNameDollarSign_Throws()
 {
     var client = new KeenClient(settingsEnv);
     Assert.Throws<KeenException>(() => client.AddEvent("$Invalid", new { AProperty = "AValue" }));
 }
Example #19
0
 public void AddEvent_ValidProjectIdInvalidWriteKey_Throws()
 {
     var settings = new ProjectSettingsProvider(projectId: settingsEnv.ProjectId, writeKey: "X");
     var client = new KeenClient(settings);
     if (UseMocks)
         client.EventCollection = new EventCollectionMock(settings,
             AddEvent: new Action<string, JObject, IProjectSettings>((c, e, p) =>
             {
                 if ((p == settings) && (c == "X") && (e["X"].Value<string>() == "X"))
                     throw new KeenInvalidApiKeyException(c);
             }));
     Assert.Throws<KeenInvalidApiKeyException>(() => client.AddEvent("X", new { X = "X" }));
 }
Example #20
0
        public async void CachingPCL_SendEventsParallel_Success(IEventCache cache)
        {
            await cache.Clear();
            var client = new KeenClient(settingsEnv, cache);
            if (UseMocks)
                client.Event = new EventMock(settingsEnv,
                    AddEvents: new Func<JObject, IProjectSettings, IEnumerable<CachedEvent>>((e, p) =>
                    {
                        return new List<CachedEvent>();
                    }));

            (from i in Enumerable.Range(1,100)
            select new { AProperty = "AValue" })
            .AsParallel()
            .ForAll((e)=>client.AddEvent("CachedEventTest", e));

            await client.SendCachedEventsAsync();
            Assert.Null(await client.EventCache.TryTake(), "Cache is empty");
        }
Example #21
0
        public void AddGlobalProperty_DelegateSimpleValue_Success()
        {
            var client = new KeenClient(SettingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(SettingsEnv,
                    addEvent: new Action<string, JObject, IProjectSettings>((c, e, p) =>
                    {
                        if ((p == SettingsEnv) && (c == "AddEventTest")
                            && (e["AProperty"].Value<string>() == "AValue")
                            && (e["AGlobal"]!=null))
                            return;
                        else
                            throw new Exception("Unexpected value");
                    }));

            Assert.DoesNotThrow(() =>
            {
                client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => DateTime.Now.Millisecond));
                client.AddEvent("AddEventTest", new { AProperty = "AValue" });
                client.AddEvent("AddEventTest", new { AProperty = "AValue" });
            });
        }
Example #22
0
 public void AddEvent_Success()
 {
     var client = new KeenClient(settingsEnv);
     if (UseMocks)
         client.EventCollection = new EventCollectionMock(settingsEnv,
             AddEvent: new Action<string, JObject, IProjectSettings>((c, e, p) =>
             {
                 if ((p == settingsEnv) && (c == "AddEventTest") && (e["AProperty"].Value<string>()=="AValue"))
                     return;
                 else
                     throw new Exception("Unexpected values");
             }));
     Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", new { AProperty = "AValue" }));
 }
Example #23
0
        public async Task CachingPCL_SendEventsParallel_Success(IEventCache cache)
        {
            await cache.Clear();
            var client = new KeenClient(SettingsEnv, cache);
            if (UseMocks)
                client.Event = new EventMock(SettingsEnv,
                    addEvents: (e, p) => new List<CachedEvent>());

            (from i in Enumerable.Range(1,100)
            select new { AProperty = "AValue" })
            .AsParallel()
            .ForAll(e=>client.AddEvent("CachedEventTest", e));

            await client.SendCachedEventsAsync();
            Assert.Null(await client.EventCache.TryTake(), "Cache is empty");
        }
Example #24
0
        public void AddEvent_ScopedKeyWrite_Success()
        {
            var scope = "{\"timestamp\": \"2014-02-25T22:09:27.320082\", \"allowed_operations\": [\"write\"]}";
            var scopedKey = ScopedKey.EncryptString(settingsEnv.MasterKey, scope);
            var settings = new ProjectSettingsProvider(masterKey: settingsEnv.MasterKey, projectId: settingsEnv.ProjectId, writeKey: scopedKey);

            var client = new KeenClient(settings);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(settings,
                    AddEvent: new Action<string, JObject, IProjectSettings>((c, e, p) =>
                    {
                        var key = JObject.Parse(ScopedKey.Decrypt(p.MasterKey, p.WriteKey));
                        if ((key["allowed_operations"].Values<string>().First()=="write") && (p == settings) && (c == "AddEventTest") && (e["AProperty"].Value<string>()=="CustomKey"))
                            return;
                        else
                            throw new Exception("Unexpected value");
                    }));

            Assert.DoesNotThrow(() => client.AddEvent("AddEventTest", new { AProperty = "CustomKey" }));
        }
Example #25
0
        public void AddGlobalProperty_CollectionValue_Success()
        {
            var client = new KeenClient(settingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(settingsEnv,
                    AddEvent: new Action<string, JObject, IProjectSettings>((c, e, p) =>
                    {
                        if ((p == settingsEnv) && (c == "AddEventTest")
                            && (e["AProperty"].Value<string>() == "AValue")
                            && (e["AGlobal"].Values<int>().All((x) => (x == 1) || (x == 2) || (x == 3))))
                            return;
                        else
                            throw new Exception("Unexpected value");
                    }));

            Assert.DoesNotThrow(() =>
            {
                client.AddGlobalProperty("AGlobal", new[] { 1, 2, 3, });
                client.AddEvent("AddEventTest", new { AProperty = "AValue" });
            });
        }
Example #26
0
        public void ReferrerParser_Send_Success()
        {
            var client = new KeenClient(SettingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(SettingsEnv,
                    addEvent: (c, e, p) =>
                    {
                        if (!e["keen"].ToString().Contains("keen:referrer_parser"))
                            throw new Exception("Unexpected values");
                    });

            var a = AddOn.ReferrerParser("referrer", "page", "referrer_parsed");

            Assert.DoesNotThrow(() => client.AddEvent("AddOnTest", new { page = "", referrer = "" }, new List<AddOn> { a }));
        }
Example #27
0
        public void AddGlobalProperty_DelegateObjectValue_Success()
        {
            var client = new KeenClient(settingsEnv);
            if (UseMocks)
                client.EventCollection = new EventCollectionMock(settingsEnv,
                    AddEvent: new Action<string, JObject, IProjectSettings>((c, e, p) =>
                    {
                        if ((p == settingsEnv) && (c == "AddEventTest") 
                            && (e["AProperty"].Value<string>() == "AValue") 
                            && (e["AGlobal"].Value<JObject>()["SubProp1"].Value<string>() == "Value"))
                            return;
                        else
                            throw new Exception("Unexpected value");
                    }));

            Assert.DoesNotThrow(() =>
            {
                client.AddGlobalProperty("AGlobal", new DynamicPropertyValue(() => new { SubProp1 = "Value", SubProp2 = "Value" }));
                client.AddEvent("AddEventTest", new { AProperty = "AValue" });
            });
        }
Example #28
0
        public async void Funnel_ValidTimeframe_Success()
        {
            var client = new KeenClient(settingsEnv);
            var filters = new List<QueryFilter>() { new QueryFilter("field1", QueryFilter.FilterOperator.GreaterThan(), "1") };

            var funnelColA = "FunnelTestA";
            var funnelColB = "FunnelTestB";
            var funnelColC = "FunnelTestC";

            try
            {
                if (!UseMocks)
                {
                    client.DeleteCollection(funnelColA);
                    client.DeleteCollection(funnelColB);
                    client.DeleteCollection(funnelColC);
                }
            }
            catch (Exception)
            {
            }

            var timeframe = new QueryAbsoluteTimeframe(DateTime.Now, DateTime.Now.AddSeconds(2));

            if (!UseMocks)
            {
                client.AddEvent(funnelColA, new { id = "1" });
                client.AddEvent(funnelColA, new { id = "2" });
                client.AddEvent(funnelColA, new { id = "3" });

                client.AddEvent(funnelColB, new { id = "1" });
                client.AddEvent(funnelColB, new { id = "2" });

                client.AddEvent(funnelColC, new { id = "1" });
            }
            IEnumerable<FunnelStep> funnelsteps = new List<FunnelStep>()
            {
                new FunnelStep(){ EventCollection = funnelColA, ActorProperty = "id" },
                new FunnelStep(){ EventCollection = funnelColB, ActorProperty = "id" },
                new FunnelStep(){ EventCollection = funnelColC, ActorProperty = "id" },
            };
            IEnumerable<int> result = new List<int>() { 3, 2, 1 };


            Mock<IQueries> queryMock = null;
            if (UseMocks)
            {
                queryMock = new Mock<IQueries>();
                queryMock.Setup(m => m.Funnel(
                        It.IsAny<string>(),
                        It.Is<IEnumerable<FunnelStep>>(f => f == funnelsteps),
                        It.Is<QueryTimeframe>(t => t == null),
                        It.Is<string>(t => t == "")
                      ))
                  .Returns(Task.FromResult(result));

                client.Queries = queryMock.Object;
            }

            var reply = (await client.QueryFunnelAsync(testCol, funnelsteps, null)).ToList();

            if (null != queryMock)
                queryMock.VerifyAll();
        }
Example #29
0
 public void AddEvent_InvalidCollectionNameNull_Throws()
 {
     var client = new KeenClient(SettingsEnv);
     Assert.Throws<KeenException>(() => client.AddEvent(null, new { AProperty = "AValue" }));
 }