|
| 1 | +# Laravel Influxdb |
| 2 | + |
| 3 | +A service made to provide, set up and use [influxdb-php](https://github.com/influxdata/influxdb-php/) in Laravel 5.6 |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Follow this steps to use this package on your Laravel installation |
| 8 | + |
| 9 | +### 1. Require it on composer |
| 10 | + |
| 11 | +```bash |
| 12 | +composer require power-data-hub/laraflux |
| 13 | +``` |
| 14 | + |
| 15 | +The package will automatically register it's service provider. |
| 16 | + |
| 17 | +For Laravel <= 5.4 add the provider manually |
| 18 | + |
| 19 | +### 2. Load service provider (optional Laravel <= 5.4 only) |
| 20 | + |
| 21 | +You need to update your `config/app.php` configuration file to register our service provider, adding this line on `providers` array: |
| 22 | + |
| 23 | +```php |
| 24 | +PowerDataHub\Laraflux\ServiceProvider::class |
| 25 | +``` |
| 26 | + |
| 27 | +``` |
| 28 | +'aliases' => [ |
| 29 | +// ... |
| 30 | + 'InfluxDB' => PowerDataHub\Laraflux\Facades\InfluxDB::class, |
| 31 | +] |
| 32 | +``` |
| 33 | + |
| 34 | +* Define `.env` variables to connect to InfluxDB |
| 35 | + |
| 36 | +```ini |
| 37 | +INFLUXDB_HOST=localhost |
| 38 | +INFLUXDB_PORT=8086 |
| 39 | +INFLUXDB_USER=some_user |
| 40 | +INFLUXDB_PASSWORD=some_password |
| 41 | +INFLUXDB_SSL=false |
| 42 | +INFLUXDB_VERIFYSSL=false |
| 43 | +INFLUXDB_TIMEOUT=0 |
| 44 | +INFLUXDB_DBNAME=some_database |
| 45 | +``` |
| 46 | + |
| 47 | +* Write this into your terminal inside your project |
| 48 | + |
| 49 | +```ini |
| 50 | +php artisan vendor:publish |
| 51 | +``` |
| 52 | + |
| 53 | +## Reading Data |
| 54 | + |
| 55 | +```php |
| 56 | +<?php |
| 57 | + |
| 58 | +// executing a query will yield a resultset object |
| 59 | +$result = InfluxDB::query('select * from test_metric LIMIT 5'); |
| 60 | + |
| 61 | +// get the points from the resultset yields an array |
| 62 | +$points = $result->getPoints(); |
| 63 | +``` |
| 64 | + |
| 65 | +## Writing Data |
| 66 | + |
| 67 | +```php |
| 68 | +<?php |
| 69 | + |
| 70 | +// create an array of points |
| 71 | +$points = array( |
| 72 | + new InfluxDB\Point( |
| 73 | + 'test_metric', // name of the measurement |
| 74 | + null, // the measurement value |
| 75 | + ['host' => 'server01', 'region' => 'us-west'], // optional tags |
| 76 | + ['cpucount' => 10], // optional additional fields |
| 77 | + time() // Time precision has to be set to seconds! |
| 78 | + ), |
| 79 | + new InfluxDB\Point( |
| 80 | + 'test_metric', // name of the measurement |
| 81 | + null, // the measurement value |
| 82 | + ['host' => 'server01', 'region' => 'us-west'], // optional tags |
| 83 | + ['cpucount' => 10], // optional additional fields |
| 84 | + time() // Time precision has to be set to seconds! |
| 85 | + ) |
| 86 | +); |
| 87 | + |
| 88 | +$result = InfluxDB::writePoints($points, \InfluxDB\Database::PRECISION_SECONDS); |
| 89 | +``` |
| 90 | + |
| 91 | +License |
| 92 | +---- |
| 93 | + |
| 94 | +This project is licensed under the MIT License |
0 commit comments