1 | <?php |
||
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 | ); |
||
27 | } |
||
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 | } |
||
37 | |||
38 | 32 | if (!$unixepoch) { |
|
39 | 32 | $unixepoch = (int)(microtime(true) * 1e9); |
|
40 | } |
||
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 | } |
||
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 | } |
||
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 | } |
||
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 | } |
||
76 | 52 | }); |
|
77 | 52 | $elements = array_filter($elements); |
|
78 | 52 | return implode(",", $elements); |
|
79 | } |
||
80 | |||
81 | 6 | protected function convertSerializable($value) |
|
85 | |||
86 | 23 | protected function convertString($value) |
|
90 | |||
91 | 15 | protected function convertInteger($value) |
|
95 | |||
96 | 25 | protected function convertDouble($value) |
|
100 | |||
101 | 7 | protected function convertBoolean($value) |
|
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) |
|
124 | } |
||
125 |