-
Notifications
You must be signed in to change notification settings - Fork 10
/
point.dart
187 lines (167 loc) · 4.23 KB
/
point.dart
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
part of influxdb_client_api;
/// Point defines values of a single measurement.
class Point {
String name;
SplayTreeMap<String, String> tags = SplayTreeMap();
SplayTreeMap<String, dynamic> fields = SplayTreeMap();
//timestamp in epoch nanoseconds
dynamic timestamp;
/// Create a new Point with specified a measurement name.
Point(this.name);
/// Sets point's measurement.
static Point measurement(String name) {
var p = Point(name);
return p;
}
/// Adds a tag.
Point addTag(String name, String value) {
tags[name] = value;
return this;
}
/// Adds a tag.
Point addField(String name, dynamic value) {
fields[name] = value;
return this;
}
/// Sets point time.
///
/// A [int] or [DateTime] value can be used
/// to carry an int value of a precision that depends
/// on WriteApi, nanoseconds by default.
Point time(dynamic time) {
if (time is DateTime) {
timestamp = time;
} else if (time is num) {
timestamp = time;
} else {
throw ArgumentError('Illegal timestamp type');
}
return this;
}
/// Creates an InfluxDB protocol line out of this instance.
/// |measurement,tag_set field_set timestamp
String toLineProtocol(WritePrecision precision, {Map? defaultTags}) {
var sb = StringBuffer();
_escapeKey(sb, name, false);
_appendTags(sb, defaultTags: defaultTags);
var appendedFields = _appendFields(sb);
if (!appendedFields) {
return '';
}
_appendTime(sb, precision);
return sb.toString();
}
void _escapeKey(StringBuffer sb, String key, bool escapeEqual) {
for (var i = 0; i < key.length; i++) {
switch (key[i]) {
case '\n':
sb.write('\\n');
continue;
case '\r':
sb.write('\\r');
continue;
case '\t':
sb.write('\\t');
continue;
case ' ':
case ',':
sb.write('\\');
break;
case '=':
if (escapeEqual) {
sb.write('\\');
}
break;
default:
}
sb.write(key[i]);
}
}
void _addTag(String key, String value, StringBuffer sb) {
if (key.isEmpty || value.isEmpty) {
return;
}
sb.write(',');
_escapeKey(sb, key, true);
sb.write('=');
_escapeKey(sb, value, true);
}
void _appendTags(StringBuffer sb, {Map? defaultTags}) {
if (defaultTags != null) {
defaultTags.forEach((k, v) => _addTag(k, v, sb));
}
tags.forEach((k, v) => _addTag(k, v, sb));
sb.write(' ');
}
bool _appendFields(StringBuffer sb) {
var appended = false;
var first = true;
fields.forEach((key, value) {
if (value == null) {
return;
}
if (!first) {
sb.write(',');
}
_escapeKey(sb, key, true);
sb.write('=');
if (value is num) {
if (value is int) {
sb.write(value);
sb.write('i');
} else {
sb.write(value);
}
} else if (value is String) {
sb.write('"');
_escapeValue(sb, value);
sb.write('"');
} else {
sb.write(value);
}
first = false;
appended = true;
});
return appended;
}
void _escapeValue(StringBuffer sb, String value) {
for (var i = 0; i < value.length; i++) {
switch (value[i]) {
case '\\':
case '"':
sb.write('\\');
break;
}
sb.write(value[i]);
}
}
void _appendTime(StringBuffer sb, WritePrecision precision) {
if (timestamp == null) {
return;
}
sb.write(' ');
if (timestamp is int) {
sb.write(timestamp);
} else if (timestamp is DateTime) {
int convertedTime;
var nanos = (timestamp as DateTime).microsecondsSinceEpoch * 1000;
switch (precision.value) {
case 'ns':
convertedTime = nanos;
break;
case 'us':
convertedTime = (nanos / 1000).round();
break;
case 'ms':
convertedTime = (nanos / 1000000).round();
break;
case 's':
convertedTime = (nanos / 1000000000).round();
break;
default:
throw ArgumentError('Unsupported precision: $precision');
}
sb.write(convertedTime);
}
}
}