Skip to content

Commit f517699

Browse files
author
Enric Sala
committed
Allow to configure the db, precision, retention, consistency on writes
1 parent bcda020 commit f517699

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

influxdb-client/InfluxDB.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,27 @@
5555
end
5656

5757
% Execute a write of a line protocol string
58-
function [] = runWrite(obj, lines)
59-
url = [obj.Host '/write?db=' obj.Database '&precision=ms'];
58+
function [] = runWrite(obj, lines, database, precision, retention, consistency)
59+
params = {};
60+
if nargin > 2 && ~isempty(database)
61+
params{end + 1} = ['db=' urlencode(database)];
62+
else
63+
params{end + 1} = ['db=' urlencode(obj.Database)];
64+
end
65+
if nargin > 3 && ~isempty(precision)
66+
assert(any(strcmp(precision, {'ns', 'u', 'ms', 's', 'm', 'h'})), ...
67+
'precision:unknown', '"%s" is not a valid precision', precision);
68+
params{end + 1} = ['precision=' precision];
69+
end
70+
if nargin > 4 && ~isempty(retention)
71+
params{end + 1} = ['rp=' urlencode(retention)];
72+
end
73+
if nargin > 5 && ~isempty(consistency)
74+
assert(any(strcmp(consistency, {'any', 'one', 'quorum', 'all'})), ...
75+
'consistency:unknown', '"%s" is not a valid consistency', consistency);
76+
params{end + 1} = ['consistency=' consistency];
77+
end
78+
url = [obj.Host '/write?' strjoin(params, '&')];
6079
opts = weboptions('Username', obj.User, 'Password', obj.Password);
6180
webwrite(url, lines, opts);
6281
end

influxdb-client/WriteBuilder.m

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,56 @@
11
classdef WriteBuilder < handle
22

33
properties(Access = private)
4-
InfluxDB, Points;
4+
InfluxDB = []
5+
Items = {}
6+
Database = []
7+
Precision = 'ms'
8+
Consistency = []
9+
RetentionPolicy = []
510
end
611

712
methods
8-
% Constructor
9-
function obj = WriteBuilder()
10-
obj.InfluxDB = [];
11-
obj.Points = {};
12-
end
13-
1413
% Set the client instance used for execution
1514
function obj = influxdb(obj, influxdb)
1615
obj.InfluxDB = influxdb;
1716
end
1817

19-
% Append points
18+
% Configure the database
19+
function obj = database(obj, database)
20+
obj.Database = database;
21+
end
22+
23+
% Configure the precision
24+
function obj = precision(obj, precision)
25+
obj.Precision = precision;
26+
end
27+
28+
% Configure the retention policy
29+
function obj = retention(obj, retention)
30+
obj.RetentionPolicy = retention;
31+
end
32+
33+
% Configure the consistency
34+
function obj = consistency(obj, consistency)
35+
obj.Consistency = consistency;
36+
end
37+
38+
% Append points or series
2039
function obj = append(obj, varargin)
2140
for i = 1:length(varargin)
2241
item = varargin{i};
2342
for j = 1:length(item)
24-
obj.Points{end + 1} = item(j);
43+
obj.Items{end + 1} = item(j);
2544
end
2645
end
2746
end
2847

2948
% Build line protocol
3049
function str = build(obj)
3150
builder = java.lang.StringBuilder();
32-
for i = 1:length(obj.Points)
33-
point = obj.Points{i};
34-
builder.append(point.toLine());
51+
for i = 1:length(obj.Items)
52+
item = obj.Items{i};
53+
builder.append(item.toLine(obj.Precision));
3554
builder.append(newline);
3655
end
3756
builder.deleteCharAt(int32(builder.length() - 1));
@@ -43,7 +62,8 @@
4362
assert(~isempty(obj.InfluxDB), 'execute:clientNotSet', ...
4463
'the influxdb client is not set for this builder');
4564
lines = obj.build();
46-
obj.InfluxDB.runWrite(lines);
65+
obj.InfluxDB.runWrite(lines, obj.Database, ...
66+
obj.Precision, obj.RetentionPolicy, obj.Consistency);
4767
end
4868
end
4969

0 commit comments

Comments
 (0)