1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* src/InfluxDbServiceProvider.php. |
4
|
|
|
* |
5
|
|
|
* @author Austin Heap <[email protected]> |
6
|
|
|
* @version v0.1.7 |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace AustinHeap\Database\InfluxDb; |
11
|
|
|
|
12
|
|
|
use Monolog\Logger; |
13
|
|
|
use Illuminate\Log\Writer; |
14
|
|
|
use Illuminate\Support\Facades\Log; |
15
|
|
|
use InfluxDB\Client as InfluxClient; |
16
|
|
|
use Illuminate\Support\ServiceProvider; |
17
|
|
|
use AustinHeap\Database\InfluxDb\Logs\Formatter; |
18
|
|
|
use InfluxDB\Client\Exception as ClientException; |
19
|
|
|
use AustinHeap\Database\InfluxDb\Logs\MonologHandler; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class InfluxDbServiceProvider. |
23
|
|
|
*/ |
24
|
|
|
class InfluxDbServiceProvider extends ServiceProvider |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function boot(): void |
30
|
|
|
{ |
31
|
|
|
$this->publishes([ |
32
|
|
|
__DIR__.'/config/influxdb.php' => config_path('influxdb.php'), |
33
|
|
|
]); |
34
|
|
|
|
35
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/influxdb.php', 'influxdb'); |
36
|
|
|
|
37
|
|
|
if (config('influxdb.log.monolog', false) === true) { |
38
|
|
|
$handler = new MonologHandler($this->getLoggingLevel()); |
|
|
|
|
39
|
|
|
$handler->setFormatter(new Formatter()); |
40
|
|
|
|
41
|
|
|
$monolog = Log::getMonolog(); |
42
|
|
|
$monolog->pushHandler($handler); |
43
|
|
|
|
44
|
|
|
$new_log = new Writer($monolog, Log::getEventDispatcher()); |
45
|
|
|
Log::swap($new_log); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function register(): void |
53
|
|
|
{ |
54
|
|
|
$this->app->singleton('InfluxDb', function ($app) { |
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$timeout = is_int(config('influxdb.timeout', null)) ? config('influxdb.timeout') : 5; |
57
|
|
|
$verifySsl = is_bool(config('influxdb.verify_ssl', null)) ? config('influxdb.verify_ssl') : true; |
58
|
|
|
$protocol = 'influxdb'; |
59
|
|
|
|
60
|
|
|
if (in_array(config('influxdb.protocol'), ['https', 'udp'])) { |
61
|
|
|
$protocol = config('influxdb.protocol').'+'.$protocol; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$dsn = sprintf('%s://%s:%s@%s:%s/%s', |
65
|
|
|
$protocol, |
66
|
|
|
config('influxdb.user'), |
67
|
|
|
config('influxdb.pass'), |
68
|
|
|
config('influxdb.host'), |
69
|
|
|
config('influxdb.port'), |
70
|
|
|
config('influxdb.database') |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
return InfluxClient::fromDSN($dsn, $timeout, $verifySsl); |
74
|
|
|
} catch (ClientException $exception) { |
75
|
|
|
throw $exception; |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \Illuminate\Config\Repository|int|mixed|string |
82
|
|
|
*/ |
83
|
|
|
private function getLoggingLevel() |
84
|
|
|
{ |
85
|
|
|
$level = config('influxdb.log.level', 'DEBUG'); |
86
|
|
|
$level = empty($level) ? 'DEBUG' : $level; |
87
|
|
|
|
88
|
|
|
return in_array($level, [ |
89
|
|
|
'DEBUG', |
90
|
|
|
'INFO', |
91
|
|
|
'NOTICE', |
92
|
|
|
'WARNING', |
93
|
|
|
'ERROR', |
94
|
|
|
'CRITICAL', |
95
|
|
|
'ALERT', |
96
|
|
|
'EMERGENCY', |
97
|
|
|
]) ? $level : Logger::DEBUG; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return \InfluxDB\Client|\InfluxDB\Database |
102
|
|
|
*/ |
103
|
|
|
public static function getInstance() |
104
|
|
|
{ |
105
|
|
|
return app('InfluxDb'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|