From ec8c41eb8339a04a975b5813cfedc74a9b787e56 Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Tue, 22 Jul 2014 11:39:32 -0400 Subject: [PATCH] Implemented DateFilter, added License --- LICENSE.txt | 13 ++++++++ TimberWinR.ServiceHost/Program.cs | 2 +- TimberWinR/Filters/DateFilter.cs | 51 ++++++++++++++++++++++++++++--- TimberWinR/Filters/GrokFilter.cs | 36 +++++++++++----------- 4 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..a6099db --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,13 @@ +Copyright 2014 Vistaprint + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/TimberWinR.ServiceHost/Program.cs b/TimberWinR.ServiceHost/Program.cs index 5dc2755..56a2355 100644 --- a/TimberWinR.ServiceHost/Program.cs +++ b/TimberWinR.ServiceHost/Program.cs @@ -20,7 +20,7 @@ namespace TimberWinR.ServiceHost { private static void Main(string[] args) { - Arguments arguments = new Arguments(); + Arguments arguments = new Arguments(); HostFactory.Run(hostConfigurator => { diff --git a/TimberWinR/Filters/DateFilter.cs b/TimberWinR/Filters/DateFilter.cs index af0b404..7e851f7 100644 --- a/TimberWinR/Filters/DateFilter.cs +++ b/TimberWinR/Filters/DateFilter.cs @@ -1,7 +1,10 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text; +using Newtonsoft.Json.Linq; namespace TimberWinR.Filters { @@ -10,11 +13,51 @@ namespace TimberWinR.Filters public string Field { get; private set; } public string Target { get; private set; } public bool ConvertToUTC { get; private set; } - public List Pattern { get; private set; } + public List Patterns { get; private set; } - public override void Apply(Newtonsoft.Json.Linq.JObject json) + public DateFilter() { - throw new NotImplementedException(); + Patterns = new List(); + } + + public override void Apply(JObject json) + { + JToken token = null; + if (json.TryGetValue(Field, StringComparison.OrdinalIgnoreCase, out token)) + { + string text = token.ToString(); + if (!string.IsNullOrEmpty(text)) + { + DateTime ts; + if (Patterns == null || Patterns.Count == 0) + { + if (DateTime.TryParse(text, out ts)) + { + if (ConvertToUTC) + ts = ts.ToUniversalTime(); + + if (json[Target] == null) + json.Add(Target, ts); + else + json[Target] = ts; + } + } + else + { + if (DateTime.TryParseExact(text, Patterns.ToArray(), CultureInfo.InvariantCulture, + DateTimeStyles.None, out ts)) + { + if (ConvertToUTC) + ts = ts.ToUniversalTime(); + + if (json[Target] == null) + json.Add(Target, ts); + else + json[Target] = ts; + } + } + } + } } } } diff --git a/TimberWinR/Filters/GrokFilter.cs b/TimberWinR/Filters/GrokFilter.cs index 287fad4..68278ad 100644 --- a/TimberWinR/Filters/GrokFilter.cs +++ b/TimberWinR/Filters/GrokFilter.cs @@ -60,24 +60,24 @@ namespace TimberWinR.Filters foreach (string fieldName in namedCaptures.Keys) { - if (fieldName == "timestamp") - { - string value = namedCaptures[fieldName]; - DateTime ts; - if (DateTime.TryParse(value, out ts)) - json.Add(fieldName, ts.ToUniversalTime()); - else if (DateTime.TryParseExact(value, new string[] - { - "MMM dd hh:mm:ss", - "MMM dd HH:mm:ss", - "MMM dd h:mm", - "MMM dd hh:mm", - }, CultureInfo.InvariantCulture, DateTimeStyles.None, out ts)) - json.Add(fieldName, ts.ToUniversalTime()); - else - json.Add(fieldName, (JToken) namedCaptures[fieldName]); - } - else + //if (fieldName == "timestamp") + //{ + // string value = namedCaptures[fieldName]; + // DateTime ts; + // if (DateTime.TryParse(value, out ts)) + // json.Add(fieldName, ts.ToUniversalTime()); + // else if (DateTime.TryParseExact(value, new string[] + // { + // "MMM dd hh:mm:ss", + // "MMM dd HH:mm:ss", + // "MMM dd h:mm", + // "MMM dd hh:mm", + // }, CultureInfo.InvariantCulture, DateTimeStyles.None, out ts)) + // json.Add(fieldName, ts.ToUniversalTime()); + // else + // json.Add(fieldName, (JToken) namedCaptures[fieldName]); + //} + //else json.Add(fieldName, (JToken) namedCaptures[fieldName]); } }