summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Util/PerfDataFormat.php
blob: 1caffff081c8bb84acbb6900ce144c07ff0a7002 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Util;

class PerfDataFormat
{
    protected static $instance;

    protected static $generalBase = 1000;

    protected static $bitPrefix = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'];

    protected static $bytePrefix = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    protected static $wattHourPrefix = ['Wh', 'kWh', 'MWh', 'GWh', 'TWh', 'PWh', 'EWh', 'ZWh', 'YWh'];

    protected static $wattPrefix = [-1 => 'mW', 'W', 'kW', 'MW', 'GW'];

    protected static $amperePrefix = [-3 => 'nA', -2 => 'µA', -1 => 'mA', 'A', 'kA', 'MA', 'GA'];

    protected static $ampSecondPrefix = [-2 => 'µAs', -1 => 'mAs', 'As', 'kAs', 'MAs', 'GAs'];

    protected static $voltPrefix = [-2 => 'µV', -1 => 'mV', 'V', 'kV', 'MV', 'GV'];

    protected static $ohmPrefix = ['Ω'];

    protected static $gramPrefix = [
        -5 => 'fg',
        -4 => 'pg',
        -3 => 'ng',
        -2 => 'µg',
        -1 => 'mg',
        'g',
        'kg',
        't',
        'ktǂ',
        'Mt',
        'Gt'
    ];

    protected static $literPrefix = [
        -5 => 'fl',
        -4 => 'pl',
        -3 => 'nl',
        -2 => 'µl',
        -1 => 'ml',
        'l',
        'kl',
        'Ml',
        'Gl',
        'Tl',
        'Pl'
    ];

    protected static $secondPrefix = [-3 => 'ns', -2 => 'µs', -1 => 'ms', 's'];

    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new PerfDataFormat();
        }

        return self::$instance;
    }

    public static function bits($value): string
    {
        return self::formatForUnits($value, self::$bitPrefix, self::$generalBase);
    }

    public static function bytes($value): string
    {
        return self::formatForUnits($value, self::$bytePrefix, self::$generalBase);
    }

    public static function wattHours($value): string
    {
        return self::formatForUnits($value, self::$wattHourPrefix, self::$generalBase);
    }

    public static function watts($value): string
    {
        return self::formatForUnits($value, self::$wattPrefix, self::$generalBase);
    }

    public static function amperes($value): string
    {
        return self::formatForUnits($value, self::$amperePrefix, self::$generalBase);
    }

    public static function ampereSeconds($value): string
    {
        return self::formatForUnits($value, self::$ampSecondPrefix, self::$generalBase);
    }

    public static function volts($value): string
    {
        return self::formatForUnits($value, self::$voltPrefix, self::$generalBase);
    }

    public static function ohms($value): string
    {
        return self::formatForUnits($value, self::$ohmPrefix, self::$generalBase);
    }

    public static function grams($value): string
    {
        return self::formatForUnits($value, self::$gramPrefix, self::$generalBase);
    }

    public static function liters($value): string
    {
        return self::formatForUnits($value, self::$literPrefix, self::$generalBase);
    }

    public static function seconds($value): string
    {
        $value = (float) $value;
        $absValue = abs($value);

        if ($absValue < 60) {
            return self::formatForUnits($value, self::$secondPrefix, self::$generalBase);
        } elseif ($absValue < 3600) {
            return sprintf('%0.2f m', $value / 60);
        } elseif ($absValue < 86400) {
            return sprintf('%0.2f h', $value / 3600);
        }

        return sprintf('%0.2f d', $value / 86400);
    }

    protected static function formatForUnits(float $value, array &$units, int $base): string
    {
        $sign = '';
        if ($value < 0) {
            $value = abs($value);
            $sign = '-';
        }

        if ($value == 0) {
            $pow = $result = 0;
        } else {
            $pow = floor(log($value, $base));

            // Identify nearest unit if unknown
            while (! isset($units[$pow])) {
                if ($pow < 0) {
                    $pow++;
                } else {
                    $pow--;
                }
            }

            $result = $value / pow($base, $pow);
        }

        // 1034.23 looks better than 1.03, but 2.03 is fine:
        if ($pow > 0 && $result < 2) {
            $result = $value / pow($base, --$pow);
        }

        return sprintf(
            '%s%0.2f %s',
            $sign,
            $result,
            $units[$pow]
        );
    }
}