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