/// <summary> /// Appends the tags. /// </summary> /// <param name="writer">The writer.</param> /// <param name="pointSettings">The point settings.</param> private void AppendTags(StringBuilder writer, PointSettings pointSettings) { IReadOnlyDictionary <string, string> entries; if (pointSettings == null) { entries = _tags; } else { IReadOnlyDictionary <string, string> defaultTags = pointSettings.GetDefaultTags(); try { entries = _tags.AddRange(defaultTags); } catch (ArgumentException) { // Most cases don't expect to override existing content // override don't consider as best practice // therefore it a trade-off between being less efficient // on the default behavior or on the override scenario var builder = _tags.ToBuilder(); foreach (var item in defaultTags) { var name = item.Key; if (!builder.ContainsKey(name)) // existing tags overrides { builder.Add(name, item.Value); } } entries = builder; } } foreach (var keyValue in entries) { var key = keyValue.Key; var value = keyValue.Value; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) { continue; } writer.Append(','); EscapeKey(writer, key); writer.Append('='); EscapeKey(writer, value); } writer.Append(' '); }
/// <summary> /// The Line Protocol /// </summary> /// <param name="pointSettings">with the default values</param> /// <returns></returns> public string ToLineProtocol(PointSettings pointSettings = null) { var sb = new StringBuilder(); EscapeKey(sb, _measurementName, false); AppendTags(sb, pointSettings); var appendedFields = AppendFields(sb); if (!appendedFields) { return(""); } AppendTime(sb); return(sb.ToString()); }
private void AppendTags(StringBuilder writer, PointSettings pointSettings) { IDictionary <string, string> entries = _tags; if (pointSettings != null) { var defaultTags = pointSettings.GetDefaultTags(); if (defaultTags.Count > 0) { var list = new List <IDictionary <string, string> > { _tags, defaultTags }; entries = list.SelectMany(dict => dict) .Where(pair => !string.IsNullOrEmpty(pair.Value)) .ToLookup(pair => pair.Key, pair => pair.Value) .ToDictionary(group => group.Key, group => { var first = group.First(); return(string.IsNullOrEmpty(first) ? group.Last() : first); }) .ToSortedDictionary(StringComparer.Ordinal); } } foreach (var keyValue in entries) { var key = keyValue.Key; var value = keyValue.Value; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) { continue; } writer.Append(','); EscapeKey(writer, key); writer.Append('='); EscapeKey(writer, value); } writer.Append(' '); }