From 508ea875abd6f6519bbf0c0321d486570a66f34a Mon Sep 17 00:00:00 2001 From: Eric Fontana Date: Wed, 23 Jul 2014 06:57:10 -0400 Subject: [PATCH] Intermediate 1 --- TimberWinR.ServiceHost/sampleconf.xml | 3 +- TimberWinR/Filters/MutateFilter.cs | 84 ++++++++++++++++++---- TimberWinR/Inputs/TailFileInputListener.cs | 10 ++- 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/TimberWinR.ServiceHost/sampleconf.xml b/TimberWinR.ServiceHost/sampleconf.xml index 8d1d794..c5a5bee 100644 --- a/TimberWinR.ServiceHost/sampleconf.xml +++ b/TimberWinR.ServiceHost/sampleconf.xml @@ -18,7 +18,8 @@ - + + MMM d HH:mm:ss diff --git a/TimberWinR/Filters/MutateFilter.cs b/TimberWinR/Filters/MutateFilter.cs index b58baf7..c5204f1 100644 --- a/TimberWinR/Filters/MutateFilter.cs +++ b/TimberWinR/Filters/MutateFilter.cs @@ -8,14 +8,12 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace TimberWinR.Filters -{ - - +{ public class MutateFilter : FilterBase { public const string TagName = "Mutate"; - public List Renames { get; private set; } + public List Operations { get; private set; } public static void Parse(List filters, XElement mutateElement) { @@ -29,27 +27,82 @@ namespace TimberWinR.Filters MutateFilter(XElement parent) { - Renames = new List(); - ParseRenames(parent); + Operations = new List(); + + ParseOperations(parent); } - private void ParseRenames(XElement parent) + private void ParseOperations(XElement parent) { - foreach (var e in parent.Elements("Rename")) + foreach (var e in parent.Elements()) { - Rename r = new Rename(e.Attribute("oldName").Value, e.Attribute("newName").Value); - Renames.Add(r); - } - } + switch(e.Name.ToString()) + { + case Rename.TagName: + ParseRename(e); + break; + case Remove.TagName: + ParseRemove(e); + break; + } + } + } public override void Apply(JObject json) { - foreach (var r in Renames) + foreach (var r in Operations) r.Apply(json); } - public class Rename + public abstract class MutateOperation { + public abstract void Apply(JObject json); + } + + private void ParseRemove(XElement e) + { + var o = new Remove(e.Attribute("field").Value); + Operations.Add(o); + } + + class Remove : MutateOperation + { + public const string TagName = "Remove"; + public string FieldName { get; set; } + + public Remove(string fieldName) + { + FieldName = fieldName; + } + + public override void Apply(JObject json) + { + var fieldName = FieldName; + if (fieldName.Contains('%')) + fieldName = ReplaceTokens(fieldName, json); + json.Remove(fieldName); + } + + private string ReplaceTokens(string fieldName, JObject json) + { + foreach(var token in json.Children()) + { + string replaceString = "%{" + token.Path + "}"; + fieldName = fieldName.Replace(replaceString, json[token.Path].ToString()); + } + return fieldName; + } + } + + private void ParseRename(XElement e) + { + var o = new Rename(e.Attribute("oldName").Value, e.Attribute("newName").Value); + Operations.Add(o); + } + + class Rename : MutateOperation + { + public const string TagName = "Rename"; public string OldName { get; set; } public string NewName { get; set; } @@ -58,7 +111,8 @@ namespace TimberWinR.Filters OldName = oldName; NewName = newName; } - public void Apply(JObject json) + + public override void Apply(JObject json) { json = RenameProperty(json, name => name == OldName ? NewName : name) as JObject; } diff --git a/TimberWinR/Inputs/TailFileInputListener.cs b/TimberWinR/Inputs/TailFileInputListener.cs index 6b1c275..583b813 100644 --- a/TimberWinR/Inputs/TailFileInputListener.cs +++ b/TimberWinR/Inputs/TailFileInputListener.cs @@ -36,7 +36,7 @@ namespace TimberWinR.Inputs private void FileWatcher() { - var oLogQuery = new LogQuery(); + var checkpointFileName = Path.Combine(System.IO.Path.GetTempPath(), string.Format("{0}.lpc", Guid.NewGuid().ToString())); @@ -56,8 +56,9 @@ namespace TimberWinR.Inputs // Execute the query while (!CancelToken.IsCancellationRequested) { + var oLogQuery = new LogQuery(); try - { + { var rs = oLogQuery.Execute(query, iFmt); Dictionary colMap = new Dictionary(); for (int col=0; col