<commit_before><?php
namespace InfluxDB\Adapter;

use DateTime;

function message_to_line_protocol(array $message)
{
    if (!array_key_exists("points", $message)) {
        return;
    }

    $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 = array_key_exists("tags", $message) ? $message["tags"] : [];
        if (array_key_exists("tags", $point)) {
            $tags = array_replace_recursive($tags, $point["tags"]);
        }

        if (!$tags) {
            $lines[] = trim(
                sprintf(
                    "%s %s %d",
                    $point["measurement"], list_to_string($point["fields"], true), $unixepoch
                )
            );
        } else {
            $lines[] = trim(
                sprintf(
                    "%s,%s %s %d",
                    $point["measurement"], list_to_string($tags), list_to_string($point["fields"], true), $unixepoch
                )
            );
        }
    }

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

function list_to_string(array $elements, $escape = false)
{
    array_walk($elements, function(&$value, $key) use ($escape) {
        if ($escape && is_string($value)) {
            $value = "\"{$value}\"";
        }

        if (is_bool($value)) {
            $value = ($value) ? "true" : "false";
        }

        $value = "{$key}={$value}";
    });

    return implode(",", $elements);
}<commit_msg>Removed duplicated code<commit_after><?php
namespace InfluxDB\Adapter;

use DateTime;

function message_to_line_protocol(array $message)
{
    if (!array_key_exists("points", $message)) {
        return;
    }

    $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 = array_key_exists("tags", $message) ? $message["tags"] : [];
        if (array_key_exists("tags", $point)) {
            $tags = array_replace_recursive($tags, $point["tags"]);
        }

        $tagLine = "";
        if ($tags) {
            $tagLine = sprintf(",%s", list_to_string($tags));
        }

        $lines[] = sprintf(
            "%s%s %s %d", $point["measurement"], $tagLine, list_to_string($point["fields"], true), $unixepoch
        );
    }

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

function list_to_string(array $elements, $escape = false)
{
    array_walk($elements, function(&$value, $key) use ($escape) {
        if ($escape && is_string($value)) {
            $value = "\"{$value}\"";
        }

        if (is_bool($value)) {
            $value = ($value) ? "true" : "false";
        }

        $value = "{$key}={$value}";
    });

    return implode(",", $elements);
}
{'fraction_non_alphanumeric': 0.19046019482700705, 'fraction_numerical': 0.0026872690628149142, 'mean_word_length': 2.2264355362946913, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7213454', 'n_tokens_mistral': 974, 'n_tokens_neox': 955, 'n_words': 178}