Skip to content

Commit 1fe2165

Browse files
committed
Merge pull request #57 from corley/feature/refactor-message-to-line-protocol
Refactored method message to line protocol
2 parents a122e8d + bdc5787 commit 1fe2165

File tree

2 files changed

+210
-141
lines changed

2 files changed

+210
-141
lines changed

src/Adapter/AdapterAbstract.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,43 @@ protected function messageToLineProtocol(array $message)
2727
return;
2828
}
2929

30-
if (!array_key_exists("tags", $message)) {
31-
$message["tags"] = [];
32-
}
33-
30+
$message = $this->prepareMessageSection($message);
3431
$message["tags"] = array_replace_recursive($this->getOptions()->getTags(), $message["tags"]);
3532

36-
$unixepoch = (int)(microtime(true) * 1e9);
37-
if (array_key_exists("time", $message)) {
38-
$dt = new DateTime($message["time"]);
39-
$unixepoch = (int)($dt->format("U") * 1e9);
40-
}
41-
4233
$lines = [];
4334
foreach ($message["points"] as $point) {
44-
$tags = $message["tags"];
45-
if (array_key_exists("tags", $point)) {
46-
$tags = array_replace_recursive($tags, $point["tags"]);
47-
}
35+
$point = $this->prepareMessageSection($point, $message["time"]);
36+
$tags = array_replace_recursive($message["tags"], $point["tags"]);
4837

4938
$tagLine = $this->tagsToString($tags);
5039

5140
$lines[] = sprintf(
52-
"%s%s %s %d", $point["measurement"], $tagLine, $this->pointsToString($point["fields"]), $unixepoch
41+
"%s%s %s %d", $point["measurement"], $tagLine, $this->pointsToString($point["fields"]), $point["time"]
5342
);
5443
}
5544

5645
return implode("\n", $lines);
5746
}
5847

48+
private function prepareMessageSection(array $message, $unixepoch = false)
49+
{
50+
if (!array_key_exists("tags", $message)) {
51+
$message["tags"] = [];
52+
}
53+
54+
if (!$unixepoch) {
55+
$unixepoch = (int)(microtime(true) * 1e9);
56+
}
57+
58+
if (array_key_exists("time", $message)) {
59+
$dt = new DateTime($message["time"]);
60+
$unixepoch = (int)($dt->format("U") * 1e9);
61+
}
62+
$message["time"] = $unixepoch;
63+
64+
return $message;
65+
}
66+
5967
protected function tagsToString(array $tags)
6068
{
6169
$tagLine = "";

0 commit comments

Comments
 (0)