forked from influxdata/influxdb-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteApi.php
172 lines (149 loc) · 5.73 KB
/
WriteApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace InfluxDB2;
/**
* Write time series data into InfluxDB.
* @package InfluxDB2
*/
class WriteApi extends DefaultApi implements Writer
{
public $writeOptions;
public $pointSettings;
/** @var Worker */
private $worker;
public $closed = false;
/**
* WriteApi constructor.
* @param $options
* @param array|null $writeOptions
* @param array|null $pointSettings
*/
public function __construct($options, array $writeOptions = null, array $pointSettings = null)
{
parent::__construct($options);
$this->writeOptions = new WriteOptions($writeOptions) ?: new WriteOptions();
$this->pointSettings = new PointSettings($pointSettings) ?: new PointSettings();
if (array_key_exists('tags', $options)) {
foreach (array_keys($options['tags']) as $key) {
$this->pointSettings->addDefaultTag($key, $options['tags'][$key]);
}
}
}
/**
* Write data into specified bucket
*
* Example write data in array
* $writeApi->write([
* ['name' => 'cpu','tags' => ['host' => 'server_nl', 'region' => 'us'],
* 'fields' => ['internal' => 5, 'external' => 6],
* 'time' => 1422568543702900257],
* ['name' => 'gpu', 'fields' => ['value' => 0.9999]]],
* WritePrecision::NS,
* 'my-bucket',
* 'my-org'
* )
*
* Example write data in line protocol
* $writeApi->write('h2o,location=west value=33i 15')
*
* Example write data using Point structure
* $point = new Point("h2o).
*
*
* @param string|Point|array $data DataPoints to write into InfluxDB. The data could be represent by
* array, Point, string
* @param string|null $precision The precision for the unix timestamps within the body line-protocol @see \InfluxDB2\Model\WritePrecision
* @param string|null $bucket specifies the destination bucket for writes
* @param string|null $org specifies the destination organization for writes
* @throws ApiException
*/
public function write($data, string $precision = null, string $bucket = null, string $org = null)
{
$precisionParam = $this->getOption("precision", $precision);
$bucketParam = $this->getOption("bucket", $bucket);
$orgParam = $this->getOption("org", $org);
$this->check("precision", $precisionParam);
$this->check("bucket", $bucketParam);
$this->check("org", $orgParam);
$this->addDefaultTags($data);
$payload = WritePayloadSerializer::generatePayload($data, $precisionParam, $bucketParam, $orgParam, $this->writeOptions->writeType);
if ($payload == null) {
return;
}
if (WriteType::BATCHING == $this->writeOptions->writeType) {
$this->worker()->push($payload);
} else {
$this->writeRaw($payload, $precisionParam, $bucketParam, $orgParam);
}
}
private function addDefaultTags(&$data)
{
$defaultTags = $this->pointSettings->getDefaultTags();
if (is_array($data)) {
if (array_key_exists('name', $data)) {
foreach (array_keys($defaultTags) as $key) {
$data['tags'][$key] = PointSettings::getValue($defaultTags[$key]);
}
} else {
foreach ($data as &$item) {
$this->addDefaultTags($item);
}
}
} elseif ($data instanceof Point) {
foreach (array_keys($defaultTags) as $key) {
$data->addTag($key, PointSettings::getValue($defaultTags[$key]));
}
}
}
/**
* Writes data using line protocol.
*
* @param string $data payload data as string (in line protocol format)
* @param string|null $precision The precision for the unix timestamps within the body line-protocol
* @param string|null $bucket specifies the destination bucket for writes
* @param string|null $org specifies the destination organization for writes
* @throws ApiException
*
* @see \InfluxDB2\Model\WritePrecision
*/
public function writeRaw(string $data, string $precision = null, string $bucket = null, string $org = null)
{
$precisionParam = $this->getOption("precision", $precision);
$bucketParam = $this->getOption("bucket", $bucket);
$orgParam = $this->getOption("org", $org);
$this->check("precision", $precisionParam);
$this->check("bucket", $bucketParam);
$this->check("org", $orgParam);
$queryParams = ["org" => $orgParam, "bucket" => $bucketParam, "precision" => $precisionParam];
$retry = new WriteRetry(
$this->writeOptions->maxRetries,
$this->writeOptions->retryInterval,
$this->writeOptions->maxRetryDelay,
$this->writeOptions->exponentialBase,
$this->writeOptions->maxRetryTime,
$this->writeOptions->jitterInterval,
$this->options['logFile'] ?? "php://output"
);
$retry->retry(function () use ($data, $queryParams) {
$this->post($data, "/api/v2/write", $queryParams);
});
}
public function close()
{
$this->closed = true;
if (isset($this->worker)) {
$this->worker()->flush();
}
unset($this->worker);
}
private function worker(): Worker
{
if (!isset($this->worker)) {
$this->worker = new Worker($this);
}
return $this->worker;
}
private function getOption(string $optionName, string $precision = null): string
{
return isset($precision) ? $precision : $this->options["$optionName"];
}
}