1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* src/InfluxDbFacade.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 Illuminate\Support\Facades\Facade; |
13
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
14
|
|
|
use AustinHeap\Database\InfluxDb\Jobs\Write; |
15
|
|
|
use AustinHeap\Database\InfluxDb\Jobs\WritePoints; |
16
|
|
|
use AustinHeap\Database\InfluxDb\Jobs\WritePayload; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class InfluxDbFacade. |
20
|
|
|
*/ |
21
|
|
|
class InfluxDbFacade extends Facade |
22
|
|
|
{ |
23
|
|
|
use InteractsWithQueue; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
protected static function getFacadeAccessor(): string |
29
|
|
|
{ |
30
|
|
|
return 'InfluxDb'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $method |
35
|
|
|
* @param array $arguments |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public static function __callStatic($method, $arguments) |
40
|
|
|
{ |
41
|
|
|
switch ($method) { |
42
|
|
|
case 'write': |
43
|
|
|
case 'writePoints': |
44
|
|
|
case 'writePayload': |
45
|
|
|
return static::$method(...$arguments); |
46
|
|
|
default: |
47
|
|
|
return static::getFacadeRoot() |
48
|
|
|
->$method(...$arguments); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $parameters |
54
|
|
|
* @param string|array $payload |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public static function write(array $parameters, $payload): bool |
59
|
|
|
{ |
60
|
|
|
if (config('influxdb.queue.enable', false) === true) { |
61
|
|
|
Write::dispatch($parameters, $payload) |
62
|
|
|
->onQueue(config('influxdb.queue.name', 'default')); |
63
|
|
|
} else { |
64
|
|
|
return static::getFacadeRoot() |
65
|
|
|
->write($parameters, $payload); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string|array $payload |
73
|
|
|
* @param string $precision |
74
|
|
|
* @param string|null $retentionPolicy |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public static function writePayload( |
79
|
|
|
$payload, |
80
|
|
|
$precision = WritePayload::PRECISION_SECONDS, |
81
|
|
|
$retentionPolicy = null |
82
|
|
|
): bool { |
83
|
|
|
if (config('influxdb.queue.enable', false) === true) { |
84
|
|
|
WritePayload::dispatch($payload, $precision, $retentionPolicy) |
85
|
|
|
->onQueue(config('influxdb.queue.name', 'default')); |
86
|
|
|
} else { |
87
|
|
|
return static::getFacadeRoot() |
88
|
|
|
->writePayload($payload, $precision, $retentionPolicy); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param \InfluxDB\Point[] $points |
96
|
|
|
* @param string $precision |
97
|
|
|
* @param string|null $retentionPolicy |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public static function writePoints( |
102
|
|
|
array $points, |
103
|
|
|
$precision = WritePoints::PRECISION_SECONDS, |
104
|
|
|
$retentionPolicy = null |
105
|
|
|
): bool { |
106
|
|
|
if (config('influxdb.queue.enable', false) === true) { |
107
|
|
|
WritePoints::dispatch($points, $precision, $retentionPolicy) |
108
|
|
|
->onQueue(config('influxdb.queue.name', 'default')); |
109
|
|
|
} else { |
110
|
|
|
return static::getFacadeRoot() |
111
|
|
|
->writePoints($points, $precision, $retentionPolicy); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|