1
|
|
|
<?php |
2
|
|
|
namespace InfluxDB\Adapter; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
|
6
|
|
|
trait WriterTrait |
7
|
|
|
{ |
8
|
33 |
|
public function messageToLineProtocol(array $message, array $tags = []) |
9
|
|
|
{ |
10
|
33 |
|
if (!array_key_exists("points", $message)) { |
11
|
1 |
|
return; |
12
|
|
|
} |
13
|
|
|
|
14
|
32 |
|
$message = $this->prepareMessageSection($message); |
15
|
32 |
|
$message["tags"] = array_replace_recursive($tags, $message["tags"]); |
16
|
|
|
|
17
|
32 |
|
$lines = []; |
18
|
32 |
|
foreach ($message["points"] as $point) { |
19
|
32 |
|
$point = $this->prepareMessageSection($point, $message["time"]); |
20
|
32 |
|
$tags = array_replace_recursive($message["tags"], $point["tags"]); |
21
|
|
|
|
22
|
32 |
|
$tagLine = $this->tagsToString($tags); |
23
|
|
|
|
24
|
32 |
|
$lines[] = sprintf( |
25
|
32 |
|
"%s%s %s %d", $point["measurement"], $tagLine, $this->pointsToString($point["fields"]), $point["time"] |
26
|
32 |
|
); |
27
|
32 |
|
} |
28
|
|
|
|
29
|
32 |
|
return implode("\n", $lines); |
30
|
|
|
} |
31
|
|
|
|
32
|
32 |
|
private function prepareMessageSection(array $message, $unixepoch = false) |
33
|
|
|
{ |
34
|
32 |
|
if (!array_key_exists("tags", $message)) { |
35
|
30 |
|
$message["tags"] = []; |
36
|
30 |
|
} |
37
|
|
|
|
38
|
32 |
|
if (!$unixepoch) { |
39
|
32 |
|
$unixepoch = (int)(microtime(true) * 1e9); |
40
|
32 |
|
} |
41
|
|
|
|
42
|
32 |
|
if (array_key_exists("time", $message)) { |
43
|
12 |
|
$dt = new DateTime($message["time"]); |
44
|
12 |
|
$unixepoch = (int)($dt->format("U") * 1e9); |
45
|
12 |
|
} |
46
|
32 |
|
$message["time"] = $unixepoch; |
47
|
|
|
|
48
|
32 |
|
return $message; |
49
|
|
|
} |
50
|
|
|
|
51
|
32 |
|
protected function tagsToString(array $tags) |
52
|
|
|
{ |
53
|
32 |
|
$tagLine = ""; |
54
|
32 |
|
if (count($tags) > 0) { |
55
|
|
|
array_walk($tags, function (&$value, $key) { |
56
|
11 |
|
$value = "{$this->addSlashes($key)}={$this->addSlashes($value)}"; |
57
|
11 |
|
}); |
58
|
11 |
|
$tagLine = sprintf(",%s", implode(",", $tags)); |
59
|
11 |
|
} |
60
|
|
|
|
61
|
32 |
|
return $tagLine; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function pointsToString(array $elements) |
65
|
|
|
{ |
66
|
52 |
|
array_walk($elements, function (&$value, $key) { |
67
|
52 |
|
$dataType = gettype($value); |
68
|
52 |
|
if (!in_array($dataType, ["string", "double", "boolean", "integer"])) { |
69
|
6 |
|
$dataType = "serializable"; |
70
|
6 |
|
} |
71
|
52 |
|
$dataType = ucfirst($dataType); |
72
|
52 |
|
if ($dataType!='Null') { |
73
|
52 |
|
$value = call_user_func([$this, "convert{$dataType}"], $value); |
74
|
52 |
|
$value = "{$this->addSlashes($key)}={$value}"; |
75
|
52 |
|
} |
76
|
52 |
|
}); |
77
|
52 |
|
$elements = array_filter($elements); |
78
|
52 |
|
return implode(",", $elements); |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
protected function convertSerializable($value) |
82
|
|
|
{ |
83
|
6 |
|
return "{$value}"; |
84
|
|
|
} |
85
|
|
|
|
86
|
23 |
|
protected function convertString($value) |
87
|
|
|
{ |
88
|
23 |
|
return "\"{$value}\""; |
89
|
|
|
} |
90
|
|
|
|
91
|
15 |
|
protected function convertInteger($value) |
92
|
|
|
{ |
93
|
15 |
|
return "{$value}i" ; |
94
|
|
|
} |
95
|
|
|
|
96
|
25 |
|
protected function convertDouble($value) |
97
|
|
|
{ |
98
|
25 |
|
return $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
protected function convertBoolean($value) |
102
|
|
|
{ |
103
|
7 |
|
return (($value) ? "true" : "false"); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns strings with space, comma, or equals sign characters backslashed per Influx write protocol syntax |
108
|
|
|
* |
109
|
|
|
* @param string $value |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
52 |
|
private function addSlashes($value) |
113
|
|
|
{ |
114
|
52 |
|
return str_replace([ |
115
|
52 |
|
' ', |
116
|
52 |
|
',', |
117
|
|
|
'=' |
118
|
52 |
|
], [ |
119
|
52 |
|
'\ ', |
120
|
52 |
|
'\,', |
121
|
|
|
'\=' |
122
|
52 |
|
], $value); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|