Skip to content

Commit c8234a4

Browse files
committed
Add InfluxDB 2.0 support
1 parent 667f370 commit c8234a4

File tree

6 files changed

+54
-37
lines changed

6 files changed

+54
-37
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@ This package is a Laravel wrapper for the [influxdb-php](https://packagist.org/p
44

55
## Installation
66

7-
This package requires PHP 7.1+, and supports Laravel 5.6 - 8.
7+
This package requires PHP 7.4+, Laravel 8+ and works with InfluxDB 2.0. For InfluxDB 1.x, see below.
88

99
1. To install the latest version of the package, run the following command in your terminal:
1010

1111
```bash
12-
composer require ge-tracker/influxdb-laravel
12+
$ composer require ge-tracker/influxdb-laravel
1313
```
1414

1515
Laravel will auto-discover the package's service provider, located at `GeTracker\InfluxDBLaravel\InfluxDBLaravelServiceProvider`.
1616
1717
2. Next, you should publish the application's configuration file
1818

1919
```bash
20-
php artisan vendor:publish
20+
$ php artisan vendor:publish
2121
```
2222

23+
### InfluxDB 1.x
24+
25+
To install a 1.7x compatible version, please install version 1.x of this package. You can [view the 1.x configuration options on GitHub](https://github.com/ge-tracker/influxdb-laravel/tree/v1).
26+
27+
```bash
28+
$ composer require "ge-tracker/influxdb-laravel:^1.0"
29+
```
30+
2331
## Configuration
2432

2533
This package's configuration, after publishing, will be located at `config/influxdb.php`.
@@ -42,7 +50,7 @@ The underlying InfluxDB connection instance can be accessed via Facade or Depend
4250
<?php
4351
4452
// create an array of points
45-
$points = array(
53+
$points = [
4654
new InfluxDB\Point(
4755
'test_metric', // name of the measurement
4856
null, // the measurement value
@@ -57,7 +65,7 @@ $points = array(
5765
['cpucount' => 10], // optional additional fields
5866
time() // Time precision has to be set to seconds!
5967
)
60-
);
68+
];
6169
6270
$result = InfluxDB::writePoints($points, \InfluxDB\Database::PRECISION_SECONDS);
6371
```

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"illuminate/support": "^8.0",
1010
"influxdata/influxdb-client-php": "^1.11.0"
1111
},
12+
"require-dev": {
13+
"orchestra/testbench": "^6.13"
14+
},
1215
"license": "MIT",
1316
"authors": [
1417
{

config/influxdb.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@
2727
'connections' => [
2828

2929
'main' => [
30-
'host' => env('INFLUXDB_HOST'),
31-
'port' => env('INFLUXDB_PORT', 8086),
32-
'username' => env('INFLUXDB_USER'),
33-
'password' => env('INFLUXDB_PASSWORD'),
34-
'database' => env('INFLUXDB_DATABASE'),
35-
'ssl' => env('INFLUXDB_SSL', false),
36-
'verifySSL' => env('INFLUXDB_VERIFY_SSL', false),
37-
'timeout' => env('INFLUXDB_TIMEOUT', 0),
38-
'connectTimeout' => env('INFLUXDB_CONNECT_TIMEOUT', 0),
30+
'url' => env('INFLUXDB_URL'),
31+
'token' => env('INFLUXDB_TOKEN'),
32+
'bucket' => env('INFLUXDB_BUCKET'),
33+
'org' => env('INFLUXDB_ORG'),
34+
'verifySSL' => env('INFLUXDB_VERIFY_SSL', false),
35+
'precision' => env('INFLUXDB_PRECISION', 'ns'),
3936
],
4037

4138
],

src/InfluxDBFactory.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace GeTracker\InfluxDBLaravel;
44

5-
use InfluxDB\Client;
5+
use InfluxDB2\Client;
66

77
/**
88
* @author James Austen <james@ge-tracker.com>
@@ -12,21 +12,32 @@ class InfluxDBFactory
1212
/**
1313
* @param array $config
1414
*
15-
* @return \InfluxDB\Database
15+
* @return \InfluxDB2\Client
1616
*/
17-
public function make(array $config)
17+
public function make(array $config): Client
1818
{
19-
$client = new Client(
20-
$config['host'],
21-
$config['port'],
22-
$config['username'],
23-
$config['password'],
24-
$config['ssl'],
25-
$config['verifySSL'],
26-
$config['timeout'],
27-
$config['connectTimeout']
28-
);
29-
30-
return $client->selectDB($config['database']);
19+
return new Client([
20+
'url' => $config['url'],
21+
'token' => $config['token'],
22+
'bucket' => $config['bucket'],
23+
'org' => $config['org'],
24+
'verifySSL' => $config['verifySSL'],
25+
'precision' => $config['precision'],
26+
]);
3127
}
28+
29+
// /**
30+
// * @param string $precision
31+
// *
32+
// * @return string
33+
// */
34+
// private function getWritePrecision(string $precision): string
35+
// {
36+
// if (!in_array($precision, WritePrecision::getAllowableEnumValues())) {
37+
// // throw
38+
// $precision = 'ns';
39+
// }
40+
//
41+
// return constant(WritePrecision::class . '::' . strtoupper($precision));
42+
// }
3243
}

src/InfluxDBLaravelServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected function registerBindings()
7878
return $manager->connection();
7979
});
8080

81-
$this->app->alias('influxdb.connection', \InfluxDB\Database::class);
81+
$this->app->alias('influxdb.connection', \InfluxDB2\Client::class);
8282
}
8383

8484
public function provides()

src/InfluxDBManager.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66
use Illuminate\Contracts\Config\Repository;
77

88
/**
9-
* @mixin \InfluxDB\Database
9+
* @mixin \InfluxDB2\Client
1010
*
1111
* @author James Austen <james@ge-tracker.com>
1212
*/
1313
class InfluxDBManager extends AbstractManager
1414
{
1515
/**
1616
* The factory instance.
17-
*
18-
* @var InfluxDBFactory
1917
*/
20-
protected $factory;
18+
protected InfluxDBFactory $factory;
2119

2220
public function __construct(Repository $config, InfluxDBFactory $factory)
2321
{
@@ -30,9 +28,9 @@ public function __construct(Repository $config, InfluxDBFactory $factory)
3028
*
3129
* @param array $config
3230
*
33-
* @return \InfluxDB\Database
31+
* @return \InfluxDB2\Client
3432
*/
35-
protected function createConnection(array $config)
33+
protected function createConnection(array $config): \InfluxDB2\Client
3634
{
3735
return $this->factory->make($config);
3836
}
@@ -42,7 +40,7 @@ protected function createConnection(array $config)
4240
*
4341
* @return string
4442
*/
45-
protected function getConfigName()
43+
protected function getConfigName(): string
4644
{
4745
return 'influxdb';
4846
}

0 commit comments

Comments
 (0)