Skip to content

Commit

Permalink
Merge pull request #57 from corley/feature/refactor-message-to-line-p…
Browse files Browse the repository at this point in the history
…rotocol

Refactored method message to line protocol
  • Loading branch information
wdalmut committed Sep 12, 2015
2 parents a122e8d + bdc5787 commit 1fe2165
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 141 deletions.
38 changes: 23 additions & 15 deletions src/Adapter/AdapterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,43 @@ protected function messageToLineProtocol(array $message)
return;
}

if (!array_key_exists("tags", $message)) {
$message["tags"] = [];
}

$message = $this->prepareMessageSection($message);
$message["tags"] = array_replace_recursive($this->getOptions()->getTags(), $message["tags"]);

$unixepoch = (int)(microtime(true) * 1e9);
if (array_key_exists("time", $message)) {
$dt = new DateTime($message["time"]);
$unixepoch = (int)($dt->format("U") * 1e9);
}

$lines = [];
foreach ($message["points"] as $point) {
$tags = $message["tags"];
if (array_key_exists("tags", $point)) {
$tags = array_replace_recursive($tags, $point["tags"]);
}
$point = $this->prepareMessageSection($point, $message["time"]);
$tags = array_replace_recursive($message["tags"], $point["tags"]);

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

$lines[] = sprintf(
"%s%s %s %d", $point["measurement"], $tagLine, $this->pointsToString($point["fields"]), $unixepoch
"%s%s %s %d", $point["measurement"], $tagLine, $this->pointsToString($point["fields"]), $point["time"]
);
}

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

private function prepareMessageSection(array $message, $unixepoch = false)
{
if (!array_key_exists("tags", $message)) {
$message["tags"] = [];
}

if (!$unixepoch) {
$unixepoch = (int)(microtime(true) * 1e9);
}

if (array_key_exists("time", $message)) {
$dt = new DateTime($message["time"]);
$unixepoch = (int)($dt->format("U") * 1e9);
}
$message["time"] = $unixepoch;

return $message;
}

protected function tagsToString(array $tags)
{
$tagLine = "";
Expand Down
Loading

0 comments on commit 1fe2165

Please sign in to comment.