Skip to content

Commit 612740b

Browse files
author
Enric Sala
committed
Ignore non-finite values, they are not supported in line protocol
1 parent edb8dea commit 612740b

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

influxdb-client/Point.m

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
% Add a field value
2727
function obj = field(obj, key, value)
28-
if isempty(value)
29-
error('field:emptyValue', 'value of field "%s" is empty', key);
30-
elseif isfloat(value)
31-
obj.Fields{end + 1} = sprintf('%s=%.8g', key, value);
28+
if isfloat(value)
29+
if ~isempty(value) && isfinite(value)
30+
obj.Fields{end + 1} = sprintf('%s=%.8g', key, value);
31+
end
3232
elseif isinteger(value)
3333
obj.Fields{end + 1} = sprintf('%s=%ii', key, value);
3434
elseif ischar(value)
@@ -58,15 +58,18 @@
5858

5959
% Format to Line Protocol
6060
function line = toLine(obj, precision)
61-
assert(~isempty(obj.Name), 'toLine:emptyName', 'series name cannot be empty');
62-
assert(~isempty(obj.Fields), 'toLine:emptyFields', 'must define at least one field');
63-
start = strjoin([{obj.Name}, obj.Tags], ',');
64-
fields = strjoin(obj.Fields, ',');
65-
if isempty(obj.Time)
66-
line = [start, ' ', fields];
61+
if isempty(obj.Fields)
62+
line = '';
6763
else
68-
if nargin < 2, precision = 'ms'; end
69-
line = [start, ' ', fields, ' ', obj.timeFmt(precision)];
64+
assert(~isempty(obj.Name), 'toLine:emptyName', 'series name cannot be empty');
65+
start = strjoin([{obj.Name}, obj.Tags], ',');
66+
fields = strjoin(obj.Fields, ',');
67+
if isempty(obj.Time)
68+
line = [start, ' ', fields];
69+
else
70+
if nargin < 2, precision = 'ms'; end
71+
line = [start, ' ', fields, ' ', obj.timeFmt(precision)];
72+
end
7073
end
7174
end
7275
end

influxdb-client/Series.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@
8585
end
8686
point.time(obj.Time(i));
8787
if nargin < 2
88-
builder.append(point.toLine());
88+
line = point.toLine();
8989
else
90-
builder.append(point.toLine(precision))
90+
line = point.toLine(precision);
91+
end
92+
if ~isempty(line)
93+
builder.append(line);
94+
builder.append(newline);
9195
end
92-
builder.append(newline);
9396
end
9497
builder.deleteCharAt(int32(builder.length() - 1));
9598
lines = char(builder.toString());

tests/PointTest.m

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@
22

33
methods(Test)
44
function build_fails_when_empty_name(test)
5-
f = @() Point('').toLine();
5+
f = @() Point('').fields('temperature', 24.3).toLine();
66
test.verifyError(f, 'toLine:emptyName');
77
end
88

9-
function build_fails_when_empty_fields(test)
10-
f = @() Point('weather').toLine();
11-
test.verifyError(f, 'toLine:emptyFields');
9+
function single_field(test)
10+
p = Point('weather').fields('temperature', 24.3);
11+
exp = 'weather temperature=24.3';
12+
test.verifyEqual(p.toLine(), exp);
1213
end
1314

14-
function field_with_empty_value_fails(test)
15-
f = @() Point('weather').fields('temperature', []);
16-
test.verifyError(f, 'field:emptyValue');
15+
function when_empty_fields_return_empty(test)
16+
p = Point('weather');
17+
test.verifyEqual(p.toLine(), '');
1718
end
1819

19-
function single_field(test)
20-
p = Point('weather').fields('temperature', 24.3);
20+
function empty_fields_are_ignored(test)
21+
p = Point('weather').fields('temperature', [], 'humidity', 60.7);
22+
exp = 'weather humidity=60.7';
23+
test.verifyEqual(p.toLine(), exp);
24+
end
25+
26+
function nonfinite_fields_are_ignored(test)
27+
p = Point('weather').fields('temperature', 24.3) ...
28+
.fields('rain', Inf, 'humidity', NaN, 'sun', -Inf);
2129
exp = 'weather temperature=24.3';
2230
test.verifyEqual(p.toLine(), exp);
2331
end

tests/SeriesTest.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ function every_property_is_used(test)
180180
'weather,city=barcelona temperature=-3.5 1529933581618'];
181181
test.verifyEqual(s.toLine(), exp);
182182
end
183+
184+
function skips_nonfinite_samples(test)
185+
s = Series('weather') ...
186+
.time(test.Time) ...
187+
.fields('temperature', [24.3, Inf]) ...
188+
.fields('humidity', [-Inf, NaN]);
189+
exp = 'weather temperature=24.3 1529933525520';
190+
test.verifyEqual(s.toLine(), exp);
191+
end
183192
end
184193

185194
end

0 commit comments

Comments
 (0)