From e32f5d5adbd60f515cc3e37c4b71e3b6dc594819 Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Tue, 22 Jul 2014 13:41:11 -0400 Subject: [PATCH] Mutate implmeentation --- TimberWinR.ServiceHost/sampleconf.xml | 3 +- TimberWinR/Configuration.cs | 7 ++- TimberWinR/Filters/MutateFilter.cs | 73 +++++++++++++++++++++------ 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/TimberWinR.ServiceHost/sampleconf.xml b/TimberWinR.ServiceHost/sampleconf.xml index 39351ab..8d1d794 100644 --- a/TimberWinR.ServiceHost/sampleconf.xml +++ b/TimberWinR.ServiceHost/sampleconf.xml @@ -18,8 +18,7 @@ - - + MMM d HH:mm:ss diff --git a/TimberWinR/Configuration.cs b/TimberWinR/Configuration.cs index e32274e..0afdc6d 100644 --- a/TimberWinR/Configuration.cs +++ b/TimberWinR/Configuration.cs @@ -474,9 +474,7 @@ namespace TimberWinR IEnumerable filters = from el in config.Root.Elements("Filters") - select el; - - MutateFilter.Parse(_filters, config.Root); + select el; foreach (XElement e in filters.Elements()) { @@ -487,7 +485,8 @@ namespace TimberWinR GrokFilter grok = new GrokFilter(args); _filters.Add(grok); break; - case "Mutate": + case MutateFilter.TagName: + MutateFilter.Parse(_filters, e); break; } } diff --git a/TimberWinR/Filters/MutateFilter.cs b/TimberWinR/Filters/MutateFilter.cs index a1cf1b0..b58baf7 100644 --- a/TimberWinR/Filters/MutateFilter.cs +++ b/TimberWinR/Filters/MutateFilter.cs @@ -4,24 +4,22 @@ using System.IO; using System.Linq; using System.Text; using System.Xml.Linq; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace TimberWinR.Filters { - public class AddField - { - public string Target { get; set; } - public string Value { get; set; } - } + public class MutateFilter : FilterBase { - public List Renames { get; set; } + public const string TagName = "Mutate"; - public static void Parse(List filters, XElement rootElement) + public List Renames { get; private set; } + + public static void Parse(List filters, XElement mutateElement) { - foreach (var e in rootElement.Elements("Filters").Elements("Mutate")) - filters.Add(parseMutate(e)); + filters.Add(parseMutate(mutateElement)); } static MutateFilter parseMutate(XElement e) @@ -31,8 +29,7 @@ namespace TimberWinR.Filters MutateFilter(XElement parent) { - Renames = new List(); - + Renames = new List(); ParseRenames(parent); } @@ -40,14 +37,60 @@ namespace TimberWinR.Filters { foreach (var e in parent.Elements("Rename")) { - Pair p = new Pair(e.Attribute("oldName").Value, e.Attribute("newName").Value); - Renames.Add(p); + Rename r = new Rename(e.Attribute("oldName").Value, e.Attribute("newName").Value); + Renames.Add(r); } } public override void Apply(JObject json) { - - } + foreach (var r in Renames) + r.Apply(json); + } + + public class Rename + { + public string OldName { get; set; } + public string NewName { get; set; } + + public Rename(string oldName, string newName) + { + OldName = oldName; + NewName = newName; + } + public void Apply(JObject json) + { + json = RenameProperty(json, name => name == OldName ? NewName : name) as JObject; + } + + private static JToken RenameProperty(JToken json, Dictionary map) + { + return RenameProperty(json, name => map.ContainsKey(name) ? map[name] : name); + } + + private static JToken RenameProperty(JToken json, Func map) + { + JProperty prop = json as JProperty; + if (prop != null) + { + return new JProperty(map(prop.Name), RenameProperty(prop.Value, map)); + } + + JArray arr = json as JArray; + if (arr != null) + { + var cont = arr.Select(el => RenameProperty(el, map)); + return new JArray(cont); + } + + JObject o = json as JObject; + if (o != null) + { + var cont = o.Properties().Select(el => RenameProperty(el, map)); + return new JObject(cont); + } + return json; + } + } } }