Skip to content

Commit bdb945f

Browse files
author
Enric Sala
committed
Adapt the query result so it can be used with command results
1 parent 6bf5c12 commit bdb945f

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ influxdb.runCommand('SHOW TAG KEYS', 'example')
193193
194194
% Create a retention policy that keeps data for one day
195195
influxdb.runCommand('CREATE RETENTION POLICY "one_day" ON "example" DURATION 1d REPLICATION 1', true)
196-
influxdb.runCommand('SHOW RETENTION POLICIES', 'example')
196+
197+
% Convert a command result to a table
198+
result = influxdb.runCommand('SHOW RETENTION POLICIES', 'example')
199+
policies = result.series().table()
197200
```
198201

199202
See the [InfluxDB documentation][influxdb-docs] for more schema exploration and management commands.

influxdb-client/InfluxDB.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343

4444
% Show databases
4545
function databases = databases(obj)
46-
response = obj.runCommand('SHOW DATABASES');
47-
databases = [response.results.series.values{:}];
46+
result = obj.runCommand('SHOW DATABASES');
47+
databases = result.series().field('name');
4848
end
4949

5050
% Change the current database
@@ -105,7 +105,7 @@
105105
end
106106

107107
% Execute other queries or commands
108-
function response = runCommand(obj, command, varargin)
108+
function result = runCommand(obj, command, varargin)
109109
idx = find(cellfun(@(x) ischar(x), varargin), 1, 'first');
110110
database = iif(isempty(idx), '', varargin{idx});
111111
idx = find(cellfun(@(x) islogical(x), varargin), 1, 'first');
@@ -124,6 +124,7 @@
124124
opts.Timeout = obj.ReadTimeout;
125125
response = webread(url, params{:}, opts);
126126
end
127+
result = QueryResult.from(response);
127128
end
128129
end
129130

influxdb-client/SeriesResult.m

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,52 +81,62 @@
8181
methods(Static)
8282
% Convert a series result to an object
8383
function obj = from(serie)
84-
name = serie.name;
85-
columns = serie.columns;
84+
fields = serie.columns;
8685
values = serie.values;
8786

88-
% Check if empty result
89-
if isempty(columns) || isempty(values)
90-
warning(['serie ''' name ''' is empty']);
91-
obj = [];
92-
return
87+
% Obtain the name if present
88+
if isfield(serie, 'name')
89+
name = serie.name;
90+
else
91+
name = '';
9392
end
9493

95-
% Extract the tags if present
94+
% Obtain the tags if present
9695
if isfield(serie, 'tags')
9796
tags = serie.tags;
9897
else
9998
tags = struct();
10099
end
101100

101+
% Check if the series is empty
102+
if isempty(fields) || isempty(values)
103+
warning(['serie "' name '" is empty']);
104+
obj = [];
105+
return
106+
end
107+
102108
% Prepare the values in a cell format
103109
N = size(values, 1);
104-
fields = columns(2:end);
105110
if iscell(values)
106-
% Implies there are non-numeric values
107-
C = length(values{1});
108-
celled = cell(N, C);
111+
% There are non-numeric values
112+
celled = cell(N, length(values{1}));
109113
for i = 1:N
110114
row = values{i};
111115
if iscell(row)
112-
% The row contains non-numeric values
113116
celled(i, :) = row;
114117
else
115-
% The row is all numeric, or NaN
116118
celled(i, :) = num2cell(row);
117119
end
118120
end
119-
time = SeriesResult.toDatetime(cell2mat(celled(:, 1)));
120121
else
121122
% All values are numeric
122-
C = size(values, 2);
123-
time = SeriesResult.toDatetime(values(:, 1));
124123
celled = num2cell(values);
125124
end
126125

127-
% Format the values as name/value structs
128-
for i = C:-1:2
129-
field = fields{i - 1};
126+
% Check if the first field is the time
127+
if strcmp('time', fields{1})
128+
timestamps = cell2mat(celled(:, 1));
129+
time = SeriesResult.toDatetime(timestamps);
130+
fields = fields(2:end);
131+
celled = celled(:, 2:end);
132+
else
133+
time = [];
134+
end
135+
136+
% Format the fields as structs
137+
C = size(celled, 2);
138+
for i = C:-1:1
139+
field = fields{i};
130140
column = celled(:, i);
131141
if all(cellfun(@(x) isnumeric(x), column))
132142
% Convert to a numeric array
@@ -147,7 +157,7 @@
147157
% Convert to a nested char cell
148158
value = {column};
149159
end
150-
props(i - 1) = struct('field', field, 'value', value);
160+
props(i) = struct('field', field, 'value', value);
151161
end
152162

153163
% Create the series result

0 commit comments

Comments
 (0)