private LdValue SerializeOneEvent(EventOutputFormatter f, object e)
 {
     var emptySummary = new EventSummary();
     var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new object[] { e }, emptySummary, out var count)).Get(0);
     Assert.Equal(1, count);
     return outputEvent;
 }
Пример #2
0
        private async Task FlushEventsAsync(FlushPayload payload)
        {
            EventOutputFormatter formatter = new EventOutputFormatter(_config);
            string jsonEvents;
            int    eventCount;

            try
            {
                jsonEvents = formatter.SerializeOutputEvents(payload.Events, payload.Summary, out eventCount);
            }
            catch (Exception e)
            {
                LogHelpers.LogException(_logger, "Error preparing events, will not send", e);
                return;
            }

            var result = await _eventSender.SendEventDataAsync(EventDataKind.AnalyticsEvents,
                                                               jsonEvents, eventCount);

            if (result.Status == DeliveryStatus.FailedAndMustShutDown)
            {
                _disabled = true;
            }
            if (result.TimeFromServer.HasValue)
            {
                Interlocked.Exchange(ref _lastKnownPastTime,
                                     UnixMillisecondTime.FromDateTime(result.TimeFromServer.Value).Value);
            }
        }
        private void TestInlineUserSerialization(User user, LdValue expectedJsonValue, EventsConfiguration config)
        {
            config.InlineUsersInEvents = true;
            var f = new EventOutputFormatter(config);

            var evalEvent = new EvaluationEvent { FlagKey = "flag", User = user };
            var outputEvent = SerializeOneEvent(f, evalEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.Equal(user.Anonymous ? LdValue.Of("anonymousUser") : LdValue.Null, outputEvent.Get("contextKind"));

            var identifyEvent = new IdentifyEvent { User = user };
            outputEvent = SerializeOneEvent(f, identifyEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.False(outputEvent.Dictionary.ContainsKey("contextKind"));

            var customEvent = new CustomEvent { EventKey = "customkey", User = user };
            outputEvent = SerializeOneEvent(f, customEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.Equal(user.Anonymous ? LdValue.Of("anonymousUser") : LdValue.Null, outputEvent.Get("contextKind"));

            var indexEvent = new IndexEvent { User = user };
            outputEvent = SerializeOneEvent(f, indexEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(expectedJsonValue, outputEvent.Get("user"));
            Assert.False(outputEvent.Dictionary.ContainsKey("contextKind"));
        }
 public void AllUserAttributesAreSerialized()
 {
     var f = new EventOutputFormatter(new EventsConfiguration());
     var user = User.Builder("userkey")
         .Anonymous(true)
         .Avatar("http://avatar")
         .Country("US")
         .Custom("custom1", "value1")
         .Custom("custom2", "value2")
         .Email("*****@*****.**")
         .FirstName("first")
         .IPAddress("1.2.3.4")
         .LastName("last")
         .Name("me")
         .Secondary("s")
         .Build();
     var userJson = LdValue.Parse(@"{
         ""key"":""userkey"",
         ""anonymous"":true,
         ""avatar"":""http://avatar"",
         ""country"":""US"",
         ""custom"":{""custom1"":""value1"",""custom2"":""value2""},
         ""email"":""*****@*****.**"",
         ""firstName"":""first"",
         ""ip"":""1.2.3.4"",
         ""lastName"":""last"",
         ""name"":""me"",
         ""secondary"":""s""
         }");
     TestInlineUserSerialization(user, userJson, new EventsConfiguration());
 }
        public void UserKeyIsSetInsteadOfUserWhenNotInlined()
        {
            var user = User.Builder("userkey")
                .Name("me")
                .Build();
            var userJson = LdValue.Parse(@"{""key"":""userkey"",""name"":""me""}");
            var f = new EventOutputFormatter(new EventsConfiguration());

            var evalEvent = new EvaluationEvent { FlagKey = "flag", User = user };
            var outputEvent = SerializeOneEvent(f, evalEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            // user is always inlined in identify event
            var identifyEvent = new IdentifyEvent { User = user };
            outputEvent = SerializeOneEvent(f, identifyEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(userJson, outputEvent.Get("user"));

            var customEvent = new CustomEvent { EventKey = "customkey", User = user };
            outputEvent = SerializeOneEvent(f, customEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("user"));
            Assert.Equal(user.Key, outputEvent.Get("userKey").AsString);

            // user is always inlined in index event
            var indexEvent = new IndexEvent { Timestamp = _fixedTimestamp, User = user };
            outputEvent = SerializeOneEvent(f, indexEvent);
            Assert.Equal(LdValue.Null, outputEvent.Get("userKey"));
            Assert.Equal(userJson, outputEvent.Get("user"));
        }
        public void SummaryEventIsSerialized()
        {
            var summary = new EventSummary();
            summary.NoteTimestamp(UnixMillisecondTime.OfMillis(1001));

            summary.IncrementCounter("first", 1, 11, LdValue.Of("value1a"), LdValue.Of("default1"));

            summary.IncrementCounter("second", 1, 21, LdValue.Of("value2a"), LdValue.Of("default2"));

            summary.IncrementCounter("first", 1, 11, LdValue.Of("value1a"), LdValue.Of("default1"));
            summary.IncrementCounter("first", 1, 12, LdValue.Of("value1a"), LdValue.Of("default1"));

            summary.IncrementCounter("second", 2, 21, LdValue.Of("value2b"), LdValue.Of("default2"));
            summary.IncrementCounter("second", null, 21, LdValue.Of("default2"), LdValue.Of("default2")); // flag exists (has version), but eval failed (no variation)

            summary.IncrementCounter("third", null, null, LdValue.Of("default3"), LdValue.Of("default3")); // flag doesn't exist (no version)

            summary.NoteTimestamp(UnixMillisecondTime.OfMillis(1000));
            summary.NoteTimestamp(UnixMillisecondTime.OfMillis(1002));

            var f = new EventOutputFormatter(new EventsConfiguration());
            var outputEvent = LdValue.Parse(f.SerializeOutputEvents(new object[0], summary, out var count)).Get(0);
            Assert.Equal(1, count);

            Assert.Equal("summary", outputEvent.Get("kind").AsString);
            Assert.Equal(1000, outputEvent.Get("startDate").AsInt);
            Assert.Equal(1002, outputEvent.Get("endDate").AsInt);

            var featuresJson = outputEvent.Get("features");
            Assert.Equal(3, featuresJson.Count);

            var firstJson = featuresJson.Get("first");
            Assert.Equal("default1", firstJson.Get("default").AsString);
            TestUtil.AssertContainsInAnyOrder(firstJson.Get("counters").AsList(LdValue.Convert.Json),
                LdValue.Parse(@"{""value"":""value1a"",""variation"":1,""version"":11,""count"":2}"),
                LdValue.Parse(@"{""value"":""value1a"",""variation"":1,""version"":12,""count"":1}"));

            var secondJson = featuresJson.Get("second");
            Assert.Equal("default2", secondJson.Get("default").AsString);
            TestUtil.AssertContainsInAnyOrder(secondJson.Get("counters").AsList(LdValue.Convert.Json),
                LdValue.Parse(@"{""value"":""value2a"",""variation"":1,""version"":21,""count"":1}"),
                LdValue.Parse(@"{""value"":""value2b"",""variation"":2,""version"":21,""count"":1}"),
                LdValue.Parse(@"{""value"":""default2"",""version"":21,""count"":1}"));

            var thirdJson = featuresJson.Get("third");
            Assert.Equal("default3", thirdJson.Get("default").AsString);
            TestUtil.AssertContainsInAnyOrder(thirdJson.Get("counters").AsList(LdValue.Convert.Json),
                LdValue.Parse(@"{""unknown"":true,""value"":""default3"",""count"":1}"));
        }
 private void TestEventSerialization(object e, LdValue expectedJsonValue)
 {
     var f = new EventOutputFormatter(new EventsConfiguration());
     var outputEvent = SerializeOneEvent(f, e);
     Assert.Equal(expectedJsonValue, outputEvent);
 }