Skip to content

Commit 2b9ee8c

Browse files
author
Enric Sala
committed
Allow to configure a database and precision epoch for data queries
1 parent f6789cd commit 2b9ee8c

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

influxdb-client/InfluxDB.m

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,21 @@
5353
end
5454

5555
% Execute a query string
56-
function result = runQuery(obj, query)
56+
function result = runQuery(obj, query, database, epoch)
57+
if nargin < 3 || isempty(database)
58+
database = obj.Database;
59+
end
60+
if nargin < 4 || isempty(epoch)
61+
epoch = 'ms';
62+
else
63+
assert(any(strcmp(epoch, {'ns', 'u', 'ms', 's', 'm', 'h'})), ...
64+
'epoch:unknown', '"%s" is not a valid epoch', epoch);
65+
end
5766
url = [obj.Url '/query'];
5867
opts = weboptions('Timeout', obj.ReadTimeout, ...
5968
'Username', obj.User, 'Password', obj.Password);
60-
response = webread(url, 'db', obj.Database, 'epoch', 'ms', 'q', query, opts);
61-
result = QueryResult.from(response);
69+
response = webread(url, 'db', database, 'epoch', epoch, 'q', query, opts);
70+
result = QueryResult.from(response, epoch);
6271
end
6372

6473
% Obtain a query builder

influxdb-client/QueryBuilder.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
properties(Access = private)
44
InfluxDB = []
5+
Database = []
6+
Epoch = []
57
Series = {}
68
Fields = {'*'}
79
Tags = {}
@@ -29,6 +31,16 @@
2931
obj.InfluxDB = influxdb;
3032
end
3133

34+
% Configure the database
35+
function obj = database(obj, database)
36+
obj.Database = database;
37+
end
38+
39+
% Configure the epoch
40+
function obj = epoch(obj, epoch)
41+
obj.Epoch = epoch;
42+
end
43+
3244
% Configure which series to query
3345
function obj = series(obj, varargin)
3446
if nargin > 2
@@ -205,7 +217,7 @@
205217
assert(~isempty(obj.InfluxDB), 'execute:clientNotSet', ...
206218
'the influxdb client is not set for this builder');
207219
query = obj.build();
208-
result = obj.InfluxDB.runQuery(query);
220+
result = obj.InfluxDB.runQuery(query, obj.Database, obj.Epoch);
209221
end
210222
end
211223

influxdb-client/QueryResult.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@
4949

5050
methods(Static)
5151
% Convert a response to objects
52-
function objs = from(response)
52+
function objs = from(response, epoch)
53+
if nargin < 2, epoch = []; end
5354
assert(~isempty(response.results), ...
5455
'from:empty', 'the response contains no results');
55-
objs = arrayfun(@(x) QueryResult.wrap(x), ...
56+
objs = arrayfun(@(x) QueryResult.wrap(x, epoch), ...
5657
response.results, 'UniformOutput', false);
5758
nonempty = cellfun(@(x) ~isempty(x), objs);
5859
objs = cellfun(@(x) x, objs(nonempty));
@@ -61,12 +62,12 @@
6162

6263
methods(Static, Access = private)
6364
% Wrap series results in a query result
64-
function obj = wrap(result)
65+
function obj = wrap(result, epoch)
6566
if isfield(result, 'error')
6667
error('query:error', 'query error: %s', result.error);
6768
end
6869
if isfield(result, 'series')
69-
series = arrayfun(@(x) SeriesResult.from(x), result.series);
70+
series = arrayfun(@(x) SeriesResult.from(x, epoch), result.series);
7071
obj = QueryResult(series);
7172
else
7273
obj = [];

influxdb-client/SeriesResult.m

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080

8181
methods(Static)
8282
% Convert a series result to an object
83-
function obj = from(serie)
83+
function obj = from(serie, epoch)
8484
fields = serie.columns;
8585
values = serie.values;
8686

@@ -126,7 +126,7 @@
126126
% Check if the first field is the time
127127
if strcmp('time', fields{1})
128128
timestamps = cell2mat(celled(:, 1));
129-
time = SeriesResult.toDatetime(timestamps);
129+
time = SeriesResult.toDatetime(timestamps, epoch);
130130
fields = fields(2:end);
131131
celled = celled(:, 2:end);
132132
else
@@ -166,11 +166,33 @@
166166
end
167167

168168
methods(Static, Access = private)
169-
% Convert a unix timestamp in millis to a datetime
170-
function [time] = toDatetime(timestamp)
171-
time = datetime(timestamp / 1000, ...
169+
% Convert a unix timestamp to a datetime
170+
function [time] = toDatetime(timestamp, epoch)
171+
scale = SeriesResult.timeScale(epoch);
172+
time = datetime(timestamp / scale, ...
172173
'ConvertFrom', 'posixtime', 'TimeZone', 'local');
173174
end
175+
176+
% Obtain the scale for an epoch
177+
function scale = timeScale(epoch)
178+
switch epoch
179+
case 'ns'
180+
scale = 1000000000;
181+
case 'u'
182+
scale = 1000000;
183+
case 'ms'
184+
scale = 1000;
185+
case 's'
186+
scale = 1;
187+
case 'm'
188+
scale = 1 / 60;
189+
case 'h'
190+
scale = 1 / 3600;
191+
otherwise
192+
error('epoch:unknown', ...
193+
'"%s" is not a valid epoch', epoch);
194+
end
195+
end
174196
end
175197

176198
end

0 commit comments

Comments
 (0)