|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace AdysTech.InfluxDB.Client.Net |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Represents a single Point (collection of fields) in a series. |
| 11 | + /// Each point is uniquely identified by its series and timestamp. |
| 12 | + /// </summary> |
| 13 | + /// <typeparam name="T">Type of the filed value, Allowed - boolean, string, integer, decimal and double </typeparam> |
| 14 | + public class InfluxDatapoint<T> : IInfluxDatapoint, IInfluxDatapoint<T> where T : IComparable, IComparable<T> |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Gets/Sets the InfluxDB measurement name |
| 18 | + /// </summary> |
| 19 | + public String MeasurementName { get; set; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Dictionary storing all Tag/Value combinations |
| 23 | + /// </summary> |
| 24 | + public Dictionary<string, string> Tags { get; private set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// The key-value pair in InfluxDB’s data structure that records metadata and the actual data value. |
| 28 | + /// Fields are required in InfluxDB’s data structure and they are not indexed. |
| 29 | + /// </summary> |
| 30 | + public Dictionary<string, T> Fields { get; private set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Timestamp for the point, will be converted to Epoch, expcted to be in UTC |
| 34 | + /// </summary> |
| 35 | + public DateTime UtcTimestamp { get; set; } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Time precision of the point, allowed are Hour->Nano second |
| 39 | + /// </summary> |
| 40 | + public TimePrecision Precision { get; set; } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Indicates whether a point got saved to InfluxDB successfully |
| 44 | + /// </summary> |
| 45 | + public bool Saved { get; set; } |
| 46 | + |
| 47 | + public IInfluxRetentionPolicy Retention { get; set; } |
| 48 | + |
| 49 | + public InfluxDatapoint() |
| 50 | + { |
| 51 | + Type itemType = typeof(T); |
| 52 | + if (itemType == typeof(long) || |
| 53 | + itemType == typeof(decimal) || |
| 54 | + itemType == typeof(double) || |
| 55 | + itemType == typeof(int) || |
| 56 | + itemType == typeof(bool) || |
| 57 | + itemType == typeof(string) || |
| 58 | + itemType == typeof(InfluxValueField)) |
| 59 | + { |
| 60 | + Tags = new Dictionary<string, string>(); |
| 61 | + Fields = new Dictionary<string, T>(); |
| 62 | + Saved = false; |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + throw new ArgumentException(itemType.Name + " is not supported by InfluxDB!"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Initializes tags with a preexisitng dictionary |
| 72 | + /// </summary> |
| 73 | + /// <param name="tags">Dictionary of tag/value pairs</param> |
| 74 | + /// <returns>True:Success, False:Failure</returns> |
| 75 | + public bool InitializeTags(IDictionary<string, string> tags) |
| 76 | + { |
| 77 | + if (Tags.Count > 0) |
| 78 | + throw new InvalidOperationException("Tags can be initialized only when the collection is empty"); |
| 79 | + try |
| 80 | + { |
| 81 | + Tags = new Dictionary<string, string>(tags); |
| 82 | + return true; |
| 83 | + } |
| 84 | + catch (Exception) |
| 85 | + { |
| 86 | + Tags = new Dictionary<string, string>(); |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Initializes fields with a preexisting dictionary |
| 93 | + /// </summary> |
| 94 | + /// <param name="fields">Dictionary of Field/Value pairs</param> |
| 95 | + /// <returns>True:Success, False:Failure</returns> |
| 96 | + public bool InitializeFields(IDictionary<string, T> fields) |
| 97 | + { |
| 98 | + if (Fields.Count > 0) |
| 99 | + throw new InvalidOperationException("Fields can be initialized only when the collection is empty"); |
| 100 | + try |
| 101 | + { |
| 102 | + Fields = new Dictionary<string, T>(fields); |
| 103 | + return true; |
| 104 | + } |
| 105 | + catch (Exception) |
| 106 | + { |
| 107 | + Fields = new Dictionary<string, T>(); |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Returns the string representing the point in InfluxDB Line protocol |
| 114 | + /// </summary> |
| 115 | + /// <returns></returns> |
| 116 | + /// <see cref="https://docs.influxdata.com/influxdb/v0.12/write_protocols/line/"/> |
| 117 | + public string ConvertToInfluxLineProtocol() |
| 118 | + { |
| 119 | + if (Fields.Count == 0) |
| 120 | + throw new InvalidOperationException("InfluxDB needs atleast one field in a line"); |
| 121 | + if (String.IsNullOrWhiteSpace(MeasurementName)) |
| 122 | + throw new InvalidOperationException("InfluxDB needs a measurement name to accept a point"); |
| 123 | + |
| 124 | + var line = new StringBuilder(); |
| 125 | + line.AppendFormat("{0}", MeasurementName); |
| 126 | + |
| 127 | + if (Tags.Count > 0) |
| 128 | + line.AppendFormat(",{0}", String.Join(",", Tags.Select(t => new StringBuilder().AppendFormat("{0}={1}", t.Key.EscapeChars(), t.Value.EscapeChars())))); |
| 129 | + |
| 130 | + var tType = typeof(T); |
| 131 | + string fields; |
| 132 | + |
| 133 | + if (tType == typeof(string)) |
| 134 | + //string needs escaping, but = is allowed in value |
| 135 | + fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}=\"{1}\"", v.Key.EscapeChars(), v.Value.ToString().EscapeChars(false)))); |
| 136 | + else if (tType == typeof(long) || tType == typeof(int)) |
| 137 | + //int needs i suffix |
| 138 | + fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}i", v.Key.EscapeChars(), v.Value))); |
| 139 | + else if (tType == typeof(bool)) |
| 140 | + //bool is okay with True or False |
| 141 | + fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}", v.Key.EscapeChars(), v.Value))); |
| 142 | + else if (tType == typeof(double)) |
| 143 | + ////double has to have a . as decimal seperator for Influx |
| 144 | + fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}", v.Key.EscapeChars(), String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0}", v.Value)))); |
| 145 | + else if (tType == typeof(InfluxValueField)) |
| 146 | + fields = String.Join(",", Fields.Select(v => new StringBuilder().AppendFormat("{0}={1}", v.Key.EscapeChars(), v.Value.ToString()))); |
| 147 | + else |
| 148 | + throw new ArgumentException(tType + " is not supported by this library at this point"); |
| 149 | + |
| 150 | + line.AppendFormat(" {0} {1}", fields, UtcTimestamp != DateTime.MinValue ? UtcTimestamp.ToEpoch(Precision) : DateTime.UtcNow.ToEpoch(Precision)); |
| 151 | + |
| 152 | + return line.ToString(); |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments