Mutate implmeentation
This commit is contained in:
@@ -18,8 +18,7 @@
|
||||
<Match field="Text" value="%{SYSLOGLINE}" />
|
||||
</Grok>
|
||||
<Mutate>
|
||||
<Rename oldName="foo" newName="bar"/>
|
||||
<Rename oldName="foo" newName="bar"/>
|
||||
<Rename oldName="timestamp" newName="xtimestamp"/>
|
||||
</Mutate>
|
||||
<Date field="timestamp" target="@timestamp" convertToUTC="true">
|
||||
<Pattern>MMM d HH:mm:ss</Pattern>
|
||||
|
||||
@@ -474,9 +474,7 @@ namespace TimberWinR
|
||||
|
||||
IEnumerable<XElement> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Pair> Renames { get; set; }
|
||||
public const string TagName = "Mutate";
|
||||
|
||||
public static void Parse(List<FilterBase> filters, XElement rootElement)
|
||||
public List<Rename> Renames { get; private set; }
|
||||
|
||||
public static void Parse(List<FilterBase> 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<Pair>();
|
||||
|
||||
Renames = new List<Rename>();
|
||||
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<string, string> map)
|
||||
{
|
||||
return RenameProperty(json, name => map.ContainsKey(name) ? map[name] : name);
|
||||
}
|
||||
|
||||
private static JToken RenameProperty(JToken json, Func<string, string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user