-
Notifications
You must be signed in to change notification settings - Fork 47
/
QueryApi.php
133 lines (111 loc) · 3.59 KB
/
QueryApi.php
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
<?php
namespace InfluxDB2;
use InfluxDB2\Model\Dialect;
use InfluxDB2\Model\Query;
use Psr\Http\Message\ResponseInterface;
/**
* The client of the InfluxDB 2.x that implement Query HTTP API endpoint.
*
* @package InfluxDB2
*/
class QueryApi extends DefaultApi
{
private $DEFAULT_DIALECT;
/**
* QueryApi constructor.
* @param array $options
*/
public function __construct(array $options)
{
parent::__construct($options);
$this->DEFAULT_DIALECT = new Dialect([
'header' => true,
'delimiter' => ',',
'comment_prefix' => '#',
'annotations' => ['datatype', 'group', 'default']
]);
}
/**
* Executes the Flux query and returns the unparsed raw result
*
* @param string|Query $query flux query to execute. The data could be represent by string, Query
* @param string|null $org specifies the source organization
* @param Dialect|null $dialect csv dialect
* @return string
*/
public function queryRaw($query, ?string $org = null, ?Dialect $dialect = null): ?string
{
$result = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT);
if ($result == null) {
return null;
}
return $result->getBody()->getContents();
}
/**
* Executes the Flux query against the InfluxDB 2.x and synchronously map the whole response to FluxTable[]
* NOTE: This method is not intended for large query results.
*
* @param string|Query $query
* @param string|null $org
* @param Dialect|null $dialect
* @return FluxTable[]
*/
public function query($query, ?string $org = null, ?Dialect $dialect = null): ?array
{
if ($query instanceof Query) {
$query->setDialect($this->DEFAULT_DIALECT);
}
$response = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT);
if ($response == null) {
return null;
}
$parser = new FluxCsvParser($response->getBody());
$parser->parse();
return $parser->tables;
}
/**
* Executes the Flux query against the InfluxDB 2.x and returns generator to stream the result.
*
* @param string| Query $query
* @param string|null $org
* @param Dialect|null $dialect
*
* @return FluxCsvParser generator
*/
public function queryStream($query, ?string $org = null, ?Dialect $dialect = null): ?FluxCsvParser
{
if ($query instanceof Query) {
$query->setDialect($this->DEFAULT_DIALECT);
}
$response = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT);
if ($response == null) {
return null;
}
return new FluxCsvParser($response->getBody(), true);
}
private function postQuery($query, $org, $dialect): ?ResponseInterface
{
$orgParam = $org ?: $this->options["org"];
$this->check("org", $orgParam);
$payload = $this->generatePayload($query, $dialect);
$queryParams = ["org" => $orgParam];
if ($payload == null) {
return null;
}
return $this->post($payload->__toString(), "/api/v2/query", $queryParams);
}
private function generatePayload($query, $dialect): ?Query
{
if ((!isset($query) || trim($query) === '')) {
return null;
}
if ($query instanceof Query) {
return $query;
}
return new Query([
'query' => $query,
'dialect' => $dialect,
'type' => null
]);
}
}