-
Notifications
You must be signed in to change notification settings - Fork 93
/
PointData.cs
718 lines (640 loc) · 23.7 KB
/
PointData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using NodaTime;
namespace InfluxDB.Client.Writes
{
/// <summary>
/// Point defines the values that will be written to the database.
/// <a href="http://bit.ly/influxdata-point">See Go Implementation</a>.
/// </summary>
public partial class PointData : IEquatable<PointData>
{
private static readonly DateTime EpochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private readonly string _measurementName;
private readonly ImmutableSortedDictionary<string, string> _tags = ImmutableSortedDictionary<string, string>
.Empty;
private readonly ImmutableSortedDictionary<string, object> _fields =
ImmutableSortedDictionary<string, object>.Empty;
public readonly WritePrecision Precision;
private readonly BigInteger? _time;
private PointData(string measurementName)
{
Arguments.CheckNonEmptyString(measurementName, "Measurement name");
_measurementName = measurementName;
Precision = WritePrecision.Ns;
}
/// <summary>
/// Create a new Point withe specified a measurement name.
/// </summary>
/// <param name="measurementName">the measurement name</param>
/// <returns>the new Point</returns>
public static PointData Measurement(string measurementName)
{
return new PointData(measurementName);
}
private PointData(string measurementName,
WritePrecision precision,
BigInteger? time,
ImmutableSortedDictionary<string, string> tags,
ImmutableSortedDictionary<string, object> fields)
{
_measurementName = measurementName;
Precision = precision;
_time = time;
_tags = tags;
_fields = fields;
}
/// <summary>
/// Adds or replaces a tag value for a point.
/// </summary>
/// <param name="name">the tag name</param>
/// <param name="value">the tag value</param>
/// <returns>this</returns>
public PointData Tag(string name, string value)
{
var isEmptyValue = string.IsNullOrEmpty(value);
var tags = _tags;
if (isEmptyValue)
{
if (tags.ContainsKey(name))
{
Trace.TraceWarning(
$"Empty tags will cause deletion of, tag [{name}], measurement [{_measurementName}]");
}
else
{
Trace.TraceWarning($"Empty tags has no effect, tag [{name}], measurement [{_measurementName}]");
return this;
}
}
if (tags.ContainsKey(name))
{
tags = tags.Remove(name);
}
if (!isEmptyValue)
{
tags = tags.Add(name, value);
}
return new PointData(_measurementName,
Precision,
_time,
tags,
_fields);
}
/// <summary>
/// Add a field with a <see cref="byte"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, byte value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="float"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, float value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="double"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, double value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="decimal"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, decimal value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="long"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, long value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="ulong"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, ulong value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="uint"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, uint value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="string"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, string value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with a <see cref="bool"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, bool value)
{
return PutField(name, value);
}
/// <summary>
/// Add a field with an <see cref="object"/> value.
/// </summary>
/// <param name="name">the field name</param>
/// <param name="value">the field value</param>
/// <returns>this</returns>
public PointData Field(string name, object value)
{
return PutField(name, value);
}
/// <summary>
/// Updates the timestamp for the point.
/// </summary>
/// <param name="timestamp">the timestamp</param>
/// <param name="timeUnit">the timestamp precision</param>
/// <returns></returns>
public PointData Timestamp(long timestamp, WritePrecision timeUnit)
{
return new PointData(_measurementName,
timeUnit,
timestamp,
_tags,
_fields);
}
/// <summary>
/// Updates the timestamp for the point represented by <see cref="TimeSpan"/>.
/// </summary>
/// <param name="timestamp">the timestamp</param>
/// <param name="timeUnit">the timestamp precision</param>
/// <returns></returns>
public PointData Timestamp(TimeSpan timestamp, WritePrecision timeUnit)
{
var time = TimeSpanToBigInteger(timestamp, timeUnit);
return new PointData(_measurementName,
timeUnit,
time,
_tags,
_fields);
}
/// <summary>
/// Updates the timestamp for the point represented by <see cref="DateTime"/>.
/// </summary>
/// <param name="timestamp">the timestamp</param>
/// <param name="timeUnit">the timestamp precision</param>
/// <returns></returns>
public PointData Timestamp(DateTime timestamp, WritePrecision timeUnit)
{
var utcTimestamp = timestamp.Kind switch
{
DateTimeKind.Local => timestamp.ToUniversalTime(),
DateTimeKind.Unspecified => DateTime.SpecifyKind(timestamp, DateTimeKind.Utc),
var _ => timestamp
};
var timeSpan = utcTimestamp.Subtract(EpochStart);
return Timestamp(timeSpan, timeUnit);
}
/// <summary>
/// Updates the timestamp for the point represented by <see cref="DateTime"/>.
/// </summary>
/// <param name="timestamp">the timestamp</param>
/// <param name="timeUnit">the timestamp precision</param>
/// <returns></returns>
public PointData Timestamp(DateTimeOffset timestamp, WritePrecision timeUnit)
{
return Timestamp(timestamp.UtcDateTime, timeUnit);
}
/// <summary>
/// Updates the timestamp for the point represented by <see cref="Instant"/>.
/// </summary>
/// <param name="timestamp">the timestamp</param>
/// <param name="timeUnit">the timestamp precision</param>
/// <returns></returns>
public PointData Timestamp(Instant timestamp, WritePrecision timeUnit)
{
var time = InstantToBigInteger(timestamp, timeUnit);
return new PointData(_measurementName,
timeUnit,
time,
_tags,
_fields);
}
/// <summary>
/// Has point any fields?
/// </summary>
/// <returns>true, if the point contains any fields, false otherwise.</returns>
public bool HasFields()
{
return _fields.Count > 0;
}
/// <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 PointData PutField(string name, object value)
{
Arguments.CheckNonEmptyString(name, "Field name");
var fields = _fields;
if (fields.ContainsKey(name))
{
fields = fields.Remove(name);
}
fields = fields.Add(name, value);
return new PointData(_measurementName,
Precision,
_time,
_tags,
fields);
}
private static BigInteger TimeSpanToBigInteger(TimeSpan timestamp, WritePrecision timeUnit)
{
BigInteger time;
switch (timeUnit)
{
case WritePrecision.Ns:
time = timestamp.Ticks * 100;
break;
case WritePrecision.Us:
time = (BigInteger)(timestamp.Ticks * 0.1);
break;
case WritePrecision.Ms:
time = (BigInteger)timestamp.TotalMilliseconds;
break;
case WritePrecision.S:
time = (BigInteger)timestamp.TotalSeconds;
break;
default:
throw new ArgumentOutOfRangeException(nameof(timeUnit), timeUnit,
"WritePrecision value is not supported");
}
return time;
}
private static BigInteger InstantToBigInteger(Instant timestamp, WritePrecision timeUnit)
{
BigInteger time;
switch (timeUnit)
{
case WritePrecision.S:
time = timestamp.ToUnixTimeSeconds();
break;
case WritePrecision.Ms:
time = timestamp.ToUnixTimeMilliseconds();
break;
case WritePrecision.Us:
time = (long)(timestamp.ToUnixTimeTicks() * 0.1);
break;
case WritePrecision.Ns:
time = (timestamp - NodaConstants.UnixEpoch).ToBigIntegerNanoseconds();
break;
default:
throw new ArgumentOutOfRangeException(nameof(timeUnit), timeUnit,
"WritePrecision value is not supported");
}
return time;
}
/// <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
{
var 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>
/// Appends the fields.
/// </summary>
/// <param name="sb">The sb.</param>
/// <returns></returns>
private bool AppendFields(StringBuilder sb)
{
var appended = false;
foreach (var keyValue in _fields)
{
var key = keyValue.Key;
var value = keyValue.Value;
if (IsNotDefined(value))
{
continue;
}
EscapeKey(sb, key);
sb.Append('=');
if (value is double || value is float)
{
sb.Append(((IConvertible)value).ToString(CultureInfo.InvariantCulture));
}
else if (value is uint || value is ulong || value is ushort)
{
sb.Append(((IConvertible)value).ToString(CultureInfo.InvariantCulture));
sb.Append('u');
}
else if (value is byte || value is int || value is long || value is sbyte || value is short)
{
sb.Append(((IConvertible)value).ToString(CultureInfo.InvariantCulture));
sb.Append('i');
}
else if (value is bool b)
{
sb.Append(b ? "true" : "false");
}
else if (value is string s)
{
sb.Append('"');
EscapeValue(sb, s);
sb.Append('"');
}
else if (value is IConvertible c)
{
sb.Append(c.ToString(CultureInfo.InvariantCulture));
}
else
{
sb.Append('"');
EscapeValue(sb, value.ToString());
sb.Append('"');
}
sb.Append(',');
appended = true;
}
if (appended)
{
sb.Remove(sb.Length - 1, 1);
}
return appended;
}
/// <summary>
/// Appends the time.
/// </summary>
/// <param name="sb">The sb.</param>
private void AppendTime(StringBuilder sb)
{
if (_time == null)
{
return;
}
sb.Append(' ');
sb.Append(((BigInteger)_time).ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Escapes the key.
/// </summary>
/// <param name="sb">The sb.</param>
/// <param name="key">The key.</param>
/// <param name="escapeEqual">Configure to escaping equal.</param>
private void EscapeKey(StringBuilder sb, string key, bool escapeEqual = true)
{
foreach (var c in key)
{
switch (c)
{
case '\n':
sb.Append("\\n");
continue;
case '\r':
sb.Append("\\r");
continue;
case '\t':
sb.Append("\\t");
continue;
case ' ':
case ',':
sb.Append("\\");
break;
case '=':
if (escapeEqual)
{
sb.Append("\\");
}
break;
}
sb.Append(c);
}
}
/// <summary>
/// Escapes the value.
/// </summary>
/// <param name="sb">The sb.</param>
/// <param name="value">The value.</param>
private void EscapeValue(StringBuilder sb, string value)
{
foreach (var c in value)
{
switch (c)
{
case '\\':
case '\"':
sb.Append("\\");
break;
}
sb.Append(c);
}
}
/// <summary>
/// Determines whether [is not defined] [the specified value].
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// <c>true</c> if [is not defined] [the specified value]; otherwise, <c>false</c>.
/// </returns>
private bool IsNotDefined(object value)
{
return value == null
|| value is double d && (double.IsInfinity(d) || double.IsNaN(d))
|| value is float f && (float.IsInfinity(f) || float.IsNaN(f));
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
return Equals(obj as PointData);
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.
/// </returns>
public bool Equals(PointData other)
{
if (other == null)
{
return false;
}
var otherTags = other._tags;
var result = _tags.Count == otherTags.Count &&
_tags.All(pair =>
{
var key = pair.Key;
var value = pair.Value;
return otherTags.ContainsKey(key) &&
otherTags[key] == value;
});
var otherFields = other._fields;
result = result && _fields.Count == otherFields.Count &&
_fields.All(pair =>
{
var key = pair.Key;
var value = pair.Value;
return otherFields.ContainsKey(key) &&
Equals(otherFields[key], value);
});
result = result &&
_measurementName == other._measurementName &&
Precision == other.Precision &&
EqualityComparer<BigInteger?>.Default.Equals(_time, other._time);
return result;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
var hashCode = 318335609;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(_measurementName);
hashCode = hashCode * -1521134295 + Precision.GetHashCode();
hashCode = hashCode * -1521134295 + _time.GetHashCode();
foreach (var pair in _tags)
{
hashCode = hashCode * -1521134295 + pair.Key?.GetHashCode() ?? 0;
hashCode = hashCode * -1521134295 + pair.Value?.GetHashCode() ?? 0;
}
foreach (var pair in _fields)
{
hashCode = hashCode * -1521134295 + pair.Key?.GetHashCode() ?? 0;
hashCode = hashCode * -1521134295 + pair.Value?.GetHashCode() ?? 0;
}
return hashCode;
}
/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(PointData left, PointData right)
{
return EqualityComparer<PointData>.Default.Equals(left, right);
}
/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator !=(PointData left, PointData right)
{
return !(left == right);
}
}
}