Skip to content

Commit 567fd94

Browse files
feat: add FluxRecord.row with response data stored in Array (#131)
1 parent 8b21fdc commit 567fd94

File tree

6 files changed

+117
-5
lines changed

6 files changed

+117
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 3.1.0 [unreleased]
22

3+
### Features
4+
1. [#131](https://github.com/influxdata/influxdb-client-php/pull/131): Added `FluxRecord.row` which stores response data in a array
5+
6+
37
## 3.0.0 [2022-09-30]
48

59
:warning: This release drops strong couple to [Guzzle HTTP client](https://github.com/guzzle/guzzle).

examples/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
## Others
2020
- [InfluxDB_18_Example.php](InfluxDB_18_Example.php) - How to use forward compatibility APIs from InfluxDB 1.8
2121
- [DeleteDataExample.php](DeleteDataExample.php) - How to delete data from InfluxDB by client
22-
- [InvokableScripts.php](InvokableScripts.php) - How to use Invokable scripts Cloud API to create custom endpoints that query data
22+
- [InvokableScripts.php](InvokableScripts.php) - How to use Invokable scripts Cloud API to create custom endpoints that query data
23+
- [RecordRowExample.php](RecordRowExample.php) - How to use `FluxRecord.row` instead of `FluxRecord.values`,
24+
in case of duplicity column names

examples/RecordRowExample.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* How to use `FluxRecord.row` instead of `FluxRecord.values`
5+
*/
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
use InfluxDB2\Client;
10+
11+
$org = 'my-org';
12+
$bucket = 'my-bucket';
13+
$token = 'my-token';
14+
15+
//
16+
// Creating client
17+
//
18+
$client = new Client([
19+
"url" => "http://localhost:8086",
20+
"token" => $token,
21+
"bucket" => $bucket,
22+
"org" => $org,
23+
"precision" => InfluxDB2\Model\WritePrecision::S
24+
]);
25+
26+
//
27+
// Write test data into InfluxDB
28+
//
29+
$writeApi = $client->createWriteApi();
30+
31+
foreach (range(1, 5) as $i) {
32+
$writeApi->write("point,table=my-table result=$i", InfluxDB2\Model\WritePrecision::MS, $bucket, $org);
33+
}
34+
35+
$writeApi->close();
36+
37+
//
38+
// Query data with pivot
39+
//
40+
$queryApi = $client->createQueryApi();
41+
42+
$result = $queryApi->query(
43+
"from(bucket: \"$bucket\") |> range(start: -1m) |> filter(fn: (r) => (r[\"_measurement\"] == \"point\"))
44+
|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"
45+
);
46+
47+
//
48+
// Write data to output
49+
//
50+
printf("\n--------------------------------------- FluxRecord.values ----------------------------------------\n");
51+
foreach ($result as $table) {
52+
foreach ($table->records as $record) {
53+
$values = implode(", ", $record->values);
54+
print "$values\n";
55+
}
56+
}
57+
58+
printf("\n----------------------------------------- FluxRecord.row -----------------------------------------\n");
59+
foreach ($result as $table) {
60+
foreach ($table->records as $record) {
61+
$row = implode(", ", $record->row);
62+
print "$row\n";
63+
}
64+
}
65+
66+
$client->close();

src/InfluxDB2/FluxCsvParser.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ private function parseRecord(int $tableIndex, FluxTable $table, array $csv)
147147
foreach ($table->columns as $fluxColumn) {
148148
$columnName = $fluxColumn->label;
149149
$strVal = $csv[$fluxColumn->index + 1];
150-
$record->values[$columnName] = $this->toValue($strVal, $fluxColumn);
150+
$value = $this->toValue($strVal, $fluxColumn);
151+
$record->values[$columnName] = $value;
152+
$record->row[] = $value;
151153
}
152154
return $record;
153155
}
@@ -183,10 +185,24 @@ private function addDefaultEmptyValues(FluxTable $table, $defaultValues)
183185
private function addColumnNamesAndTags(FluxTable $table, array $csv)
184186
{
185187
$i = 1;
186-
foreach ($table->columns as &$column) {
188+
189+
foreach ($table->columns as $column) {
187190
$column->label = $csv[$i];
188191
$i++;
189192
}
193+
194+
$duplicates = array();
195+
foreach (array_count_values($csv) as $label => $count) {
196+
if ($count > 1) {
197+
$duplicates[] = $label;
198+
}
199+
}
200+
201+
if (count($duplicates) > 0) {
202+
$duplicatesStr = implode(", ", $duplicates);
203+
print "The response contains columns with duplicated names: {$duplicatesStr}\n";
204+
print "You should use the 'FluxRecord.row' to access your data instead of 'FluxRecord.values'.";
205+
}
190206
}
191207

192208

src/InfluxDB2/FluxRecord.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ class FluxRecord implements ArrayAccess
1515
{
1616
public $table;
1717
public $values;
18+
public $row;
1819

1920
/**
2021
* FluxRecord constructor.
2122
* @param $table int table index
2223
* @param $values array array with record values, key is the column name
2324
*/
24-
public function __construct($table, $values=null)
25+
public function __construct($table, $values = null, $row = null)
2526
{
2627
$this->table = $table;
2728
$this->values = $values;
29+
$this->row = $row;
2830
}
2931

3032
/**
@@ -70,7 +72,7 @@ public function getField()
7072
/**
7173
* @return mixed record value for column named '_measurement'
7274
*/
73-
public function getMeasurement():string
75+
public function getMeasurement(): string
7476
{
7577
return $this->getRecordValue('_measurement');
7678
}

tests/FluxCsvParserTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,28 @@ public function testParseInfinity()
442442
$this->assertEquals(-INF, $tables[0]->records[11]->values['le']);
443443
}
444444

445+
public function testParseDuplicateColumnNames()
446+
{
447+
$data = "#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,string,string,double
448+
#group,false,false,true,true,false,true,true,false
449+
#default,_result,,,,,,,
450+
,result,table,_start,_stop,_time,_measurement,location,result
451+
,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:33.746Z,my_measurement,Prague,25.3
452+
,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:39.299Z,my_measurement,Prague,25.3
453+
,,0,2022-09-13T06:14:40.469404272Z,2022-09-13T06:24:40.469404272Z,2022-09-13T06:24:40.454Z,my_measurement,Prague,25.3
454+
";
455+
456+
$parser = new FluxCsvParser($data);
457+
$tables = $parser->parse()->tables;
458+
459+
$this->assertEquals(1, sizeof($tables));
460+
$this->assertEquals(3, sizeof($tables[0]->records));
461+
$this->assertEquals(8, sizeof($tables[0]->columns));
462+
$this->assertEquals(7, sizeof($tables[0]->records[0]->values));
463+
$this->assertEquals(8, sizeof($tables[0]->records[0]->row));
464+
$this->assertEquals(25.3, $tables[0]->records[0]->row[7]);
465+
}
466+
445467
private function assertColumns(array $columnHeaders, array $values)
446468
{
447469
$i = 0;

0 commit comments

Comments
 (0)