influxdata / influxdata/influxdb-client-php

Support for InfluxQL (1.X style queries)

Open
#138 2 comments 2 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
166
Forks
52
PR merge metrics
No merged PRs in 30d

Description

__Proposal:__
Would it be possible to add support for the "old-style" InfluxQL queries?

__Current behavior:__
I am testing the following query:

`SELECT count/518400 from (SELECT count("status") FROM "system" WHERE status = \'running\' ) WHERE time > now()-60d AND count > 129600 GROUP BY "host"'`

In PHP, my code looks something like the following:
```
// Load the InfluxDB library
use InfluxDB2\Client;
use InfluxDB2\Model\WritePrecision;
use InfluxDB2\Query\Builder;

$client = new Client([
"url" => "my_URL:8086",
"token" => "my_token",
"bucket" => "my_bucket",
"org" => "my_org",
"precision" => WritePrecision::NS,
]);
$queryApi = $client->createQueryApi();

$queryQL = 'SELECT count/518400 from (SELECT count("status") FROM "system" WHERE status = \'running\' ) WHERE time > now()-60d AND count > 129600 GROUP BY "host"';

// Execute the query
$result = $queryApi->query($queryQL);
```

__Desired behavior:__
I would expect an output array. Instead, this throws the following error
`AH01071: Got error 'PHP message: PHP Fatal error: Uncaught InfluxDB2\\ApiException: [400] Error connecting to the API (http://my_url:8086/api/v2/query?org=my_org)(: compilation failed: error @1:21-1:90: expected comma in property list, got IDENT\n\nerror @1:21-1:90: expected comma in property list, got LPAREN\n\nerror @1:39-1:89: invalid expression @1:87-1:88: '\n\nerror @1:39-1:89: unexpected token for property key: LPAREN (()) in /public_html/vendor/influxdata/influxdb-client-php/src/InfluxDB2/DefaultApi.php:157\nStack trace:\n#0 /public_html/vendor/influxdata/influxdb-client-php/src/InfluxDB2/DefaultApi.php(214): InfluxDB2\\DefaultApi->sendRequest()\n#1 /public_html/vendor/influxdata/influxdb-client-php/src/InfluxDB2/DefaultApi.php(79): InfluxDB2\\DefaultApi->request()\n#2 /public_html/vendor/influxdata/influxdb-client-php/src/InfluxDB2/QueryApi.php(115): InfluxDB2\\DefaultApi->post()\n#3 /public_html/vendor...'`

__Alternatives considered:__
I tried "converting" the query to Flux as best as I could, but it returns an empty array:
```
$queryFlux = 'from(bucket: "my_bucket")
|> range(start: -60d)
|> filter(fn: (r) => r["status"] == "running")
|> count(column: "status")
|> filter(fn: (r) => r["_value"] > 129600)
|> map(fn: (r) => ({r with _value: r["_value"] / 518400}))
|> group(columns: ["host"])';

// Execute the query
$result = $queryApi->query($queryFlux);
```

__Use case:__
I have a lot of queries that I've built in Grafana that would be nice to more easily copy over, rather than re-formulate to the Flux queries. Plus would make it a lot easer to debug with a graphical UI such as Grafana that I can plug the queries into.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with InfluxDB2/QueryApi.php, especially the query entry point, and trace the request through InfluxDB2/DefaultApi.php as shown in the error. Determine the client changes needed for the supplied InfluxQL query, then verify that query() returns the expected output array rather than the compilation error.

Written by the indexing model from the issue text.

Assessment

Tech stack
grafana, php
Domain
api, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.