summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/influxdb/src/LineProtocol.php
blob: b3b5f4a7e6444dc094d86b7338d1b9a089266b3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

namespace gipfl\InfluxDb;

use function ksort;
use function strlen;

abstract class LineProtocol
{
    public static function renderMeasurement($measurement, $tags = [], $fields = [], $timestamp = null)
    {
        return Escape::measurement($measurement)
            . static::renderTags($tags)
            . static::renderFields($fields)
            . static::renderTimeStamp($timestamp)
            . "\n";
    }

    public static function renderTags($tags)
    {
        ksort($tags);
        $string = '';
        foreach ($tags as $key => $value) {
            if ($value === null || strlen($value) === 0) {
                continue;
            }
            $string .= ',' . static::renderTag($key, $value);
        }

        return $string;
    }

    public static function renderFields($fields)
    {
        $string = '';
        foreach ($fields as $key => $value) {
            $string .= ',' . static::renderField($key, $value);
        }
        $string[0] = ' ';

        return $string;
    }

    public static function renderTimeStamp($timestamp)
    {
        if ($timestamp === null) {
            return '';
        } else {
            return " $timestamp";
        }
    }

    public static function renderTag($key, $value)
    {
        return Escape::key($key) . '=' . Escape::tagValue($value);
    }

    public static function renderField($key, $value)
    {

        return Escape::key($key) . '=' . Escape::fieldValue($value);
    }
}