コード例 #1
0
ファイル: LogEventsManager.cs プロジェクト: Osch-1/EventsCS
        public List <Event> Parse(string eventsToAdd)
        {
            List <Event> parsedEvents = new List <Event>();

            //substring getter
            SubstringBetweenFlagsGetter substringGetter = new SubstringBetweenFlagsGetter();
            //deserializer
            JsonEventParser jsonEventParser = new JsonEventParser();

            while (true)
            {
                string eventFromLogKey = substringGetter.Get(eventsToAdd, "key:", ",");
                string jsonFromLog     = substringGetter.Get(eventsToAdd, "json:", "\n");

                if ((String.IsNullOrEmpty(eventFromLogKey)) || (String.IsNullOrEmpty(jsonFromLog)))
                {
                    break;
                }

                int startingJsonIndex = eventsToAdd.IndexOf("json:");
                int endingJsonIndex   = startingJsonIndex + jsonFromLog.Length;

                eventsToAdd = eventsToAdd.Remove(0, endingJsonIndex);

                bool isTooBigEvent = jsonFromLog.Length > MaxAllowedJsonLength;

                if (!isTooBigEvent)
                {
                    Event parsedEvent = jsonEventParser.Parse(eventFromLogKey, jsonFromLog);
                    parsedEvents.Add(parsedEvent);
                }
            }
            return(parsedEvents);
        }
コード例 #2
0
        public void Parse_JsonWithListProperty_ReturnEmptyEvent()
        {
            //Arrange
            JsonEventParser jsonEventParser = new JsonEventParser();

            //Act
            Event Result = jsonEventParser.Parse("SomeEventName", "{courses: [\"html\", \"css\"]\n}");

            //Assert
            Assert.Equal("SomeEventName", Result.EventKey);
            Assert.Equal("Property name: courses\n  Property type: List\n  Sample value: \n  [\"html\",\"css\"]", Result.JsonPropertiesMetaValue[0].ToString());
        }
コード例 #3
0
        public void Parse_EmptyString_ReturnEmptyEvent()
        {
            //Arrange
            JsonEventParser jsonEventParser = new JsonEventParser();

            //Act
            Event Result = jsonEventParser.Parse("SomeEventName", "");

            //Assert
            Assert.Equal("", Result.EventKey);
            Assert.Equal(new List <JsonProperty>(), Result.JsonPropertiesMetaValue);
        }
コード例 #4
0
        public void Parse_JsonWithObjectProperty_ReturnEventWithObjectProperty()
        {
            //Arrange
            JsonEventParser jsonEventParser = new JsonEventParser();

            //Act
            Event Result = jsonEventParser.Parse("SomeEventName", "{Name: \"Jhon\",\nage: \"32\"\n,\nson: {Name: \"Jhon\",\nage: \"12\"\n}\n}");

            //Assert
            Assert.Equal("SomeEventName", Result.EventKey);
            Assert.Equal("Property name: Name\n  Property type: String\n  Sample value: \n  Jhon", Result.JsonPropertiesMetaValue[0].ToString());
            Assert.Equal("Property name: age\n  Property type: Number\n  Sample value: \n  32", Result.JsonPropertiesMetaValue[1].ToString());
            Assert.Equal("Property name: son\n  Property type: Object\n  Sample value: \n  {\"Name\":\"Jhon\",\"age\":\"12\"}", Result.JsonPropertiesMetaValue[2].ToString());
        }
コード例 #5
0
        private void ParseEventOnHandle(object model, BasicDeliverEventArgs ea)
        {
            byte[] body       = ea.Body.ToArray();
            string message    = Encoding.UTF8.GetString(body);
            string routingKey = ea.RoutingKey;

            using IServiceScope scope = _serviceProvider.CreateScope();
            JsonEventParser  jsonEventParser = new JsonEventParser();
            IEventRepository eventRepository = scope.ServiceProvider.GetService <IEventRepository>();

            if ((message == null) || (message == ""))
            {
                return;
            }


            Event parsedEvent = jsonEventParser.Parse(routingKey, message);

            string eventFromLogKey = parsedEvent.EventKey;
            Event  existingEvent   = eventRepository.GetEvent(eventFromLogKey);
            bool   doesEventExists = existingEvent != null;

            if (parsedEvent.JsonPropertiesMetaValue == null)
            {
                return;
            }

            if (doesEventExists)
            {
                eventRepository.Update(parsedEvent);
            }
            else
            {
                eventRepository.Add(parsedEvent);
            }
        }