Пример #1
0
        public void SummarizeEventIncrementsCounters()
        {
            var     time = UnixMillisecondTime.OfMillis(1000);
            string  flag1Key = "flag1", flag2Key = "flag2", unknownFlagKey = "badkey";
            int     flag1Version = 100, flag2Version = 200;
            int     variation1 = 1, variation2 = 2;
            LdValue value1 = LdValue.Of("value1"), value2 = LdValue.Of("value2"),
                    value99 = LdValue.Of("value99"),
                    default1 = LdValue.Of("default1"), default2 = LdValue.Of("default2"),
                    default3   = LdValue.Of("default3");
            EventSummarizer es = new EventSummarizer();

            es.SummarizeEvent(time, flag1Key, flag1Version, variation1, value1, default1);
            es.SummarizeEvent(time, flag1Key, flag1Version, variation2, value2, default1);
            es.SummarizeEvent(time, flag2Key, flag2Version, variation1, value99, default2);
            es.SummarizeEvent(time, flag1Key, flag1Version, variation1, value1, default1);
            es.SummarizeEvent(time, unknownFlagKey, null, null, default3, default3);
            EventSummary data = es.Snapshot();

            Dictionary <EventsCounterKey, EventsCounterValue> expected = new Dictionary <EventsCounterKey, EventsCounterValue>();

            Assert.Equal(new EventsCounterValue(2, value1, default1),
                         data.Counters[new EventsCounterKey(flag1Key, flag1Version, variation1)]);
            Assert.Equal(new EventsCounterValue(1, value2, default1),
                         data.Counters[new EventsCounterKey(flag1Key, flag1Version, variation2)]);
            Assert.Equal(new EventsCounterValue(1, value99, default2),
                         data.Counters[new EventsCounterKey(flag2Key, flag2Version, variation1)]);
            Assert.Equal(new EventsCounterValue(1, default3, default3),
                         data.Counters[new EventsCounterKey(unknownFlagKey, null, null)]);
        }
 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;
 }
        // Returns the current summarized event data.
        public EventSummary Snapshot()
        {
            EventSummary ret = _eventsState;

            _eventsState = new EventSummary();
            return(ret);
        }
        public string SerializeOutputEvents(object[] events, EventSummary summary, out int eventCountOut)
        {
            var jsonWriter = JWriter.New();
            var scope      = new EventOutputFormatterScope(_config, jsonWriter, _config.InlineUsersInEvents);

            eventCountOut = scope.WriteOutputEvents(events, summary);
            return(jsonWriter.GetString());
        }
        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}"));
        }
Пример #6
0
        public void SummarizeEventSetsStartAndEndDates()
        {
            var             time1 = UnixMillisecondTime.OfMillis(1000);
            var             time2 = UnixMillisecondTime.OfMillis(2000);
            var             time3 = UnixMillisecondTime.OfMillis(3000);
            EventSummarizer es    = new EventSummarizer();

            es.SummarizeEvent(time2, "flag", null, null, LdValue.Null, LdValue.Null);
            es.SummarizeEvent(time1, "flag", null, null, LdValue.Null, LdValue.Null);
            es.SummarizeEvent(time3, "flag", null, null, LdValue.Null, LdValue.Null);
            EventSummary data = es.Snapshot();

            Assert.Equal(time1, data.StartDate);
            Assert.Equal(time3, data.EndDate);
        }
        public int WriteOutputEvents(object[] events, EventSummary summary)
        {
            var eventCount = events.Length;
            var arr        = _jsonWriter.Array();

            foreach (var e in events)
            {
                WriteOutputEvent(e);
            }
            if (summary.Counters.Count > 0)
            {
                WriteSummaryEvent(summary);
                eventCount++;
            }
            arr.End();
            return(eventCount);
        }
        public void WriteSummaryEvent(EventSummary summary)
        {
            var obj = _jsonWriter.Object();

            obj.Name("kind").String("summary");
            obj.Name("startDate").Long(summary.StartDate.Value);
            obj.Name("endDate").Long(summary.EndDate.Value);

            var flagsObj = obj.Name("features").Object();

            var unprocessedCounters = summary.Counters.Select(kv => MutableKeyValuePair <EventsCounterKey, EventsCounterValue> .FromKeyValue(kv)).ToArray();

            for (var i = 0; i < unprocessedCounters.Length; i++)
            {
                var firstEntry = unprocessedCounters[i];
                if (firstEntry.Value is null)
                { // already processed
                    continue;
                }
                var flagKey     = firstEntry.Key.Key;
                var flagDefault = firstEntry.Value.Default;

                var flagObj = flagsObj.Name(flagKey).Object();
                LdValueConverter.WriteJsonValue(flagDefault, flagObj.Name("default"));
                var countersArr = flagObj.Name("counters").Array();

                for (var j = i; j < unprocessedCounters.Length; j++)
                {
                    var entry = unprocessedCounters[j];
                    var key   = entry.Key;
                    if (key.Key == flagKey && entry.Value != null)
                    {
                        var counter = entry.Value;
                        unprocessedCounters[j].Value = null; // mark as already processed

                        var counterObj = countersArr.Object();
                        if (key.Variation.HasValue)
                        {
                            counterObj.Name("variation").Int(key.Variation.Value);
                        }
                        LdValueConverter.WriteJsonValue(counter.FlagValue, counterObj.Name("value"));
                        if (key.Version.HasValue)
                        {
                            counterObj.Name("version").Int(key.Version.Value);
                        }
                        else
                        {
                            counterObj.Name("unknown").Bool(true);
                        }
                        counterObj.Name("count").Int(counter.Count);
                        counterObj.End();
                    }
                }

                countersArr.End();
                flagObj.End();
            }

            flagsObj.End();
            obj.End();
        }
 public EventSummarizer()
 {
     _eventsState = new EventSummary();
 }
 public void Clear()
 {
     _eventsState = new EventSummary();
 }