1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\helpers; |
4
|
|
|
|
5
|
|
|
use hipanel\models\Resource; |
6
|
|
|
use hiqdev\php\units\Quantity; |
7
|
|
|
use hiqdev\php\units\Unit; |
8
|
|
|
use yii\helpers\ArrayHelper; |
|
|
|
|
9
|
|
|
use Yii; |
10
|
|
|
|
11
|
|
|
class ResourceHelper |
12
|
|
|
{ |
13
|
|
|
public static function getColumns(string $objectName): array |
14
|
|
|
{ |
15
|
|
|
$map = [ |
16
|
|
|
'server' => [ |
17
|
|
|
'server_traf' => Yii::t('hipanel', 'Traffic Out'), |
18
|
|
|
'server_traf_in' => Yii::t('hipanel', 'Traffic In'), |
19
|
|
|
'server_traf95' => Yii::t('hipanel', 'Traffic 95 Out'), |
20
|
|
|
'server_traf95_in' => Yii::t('hipanel', 'Traffic 95 In'), |
21
|
|
|
], |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
return $map[$objectName]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function getFilterColumns(string $objectName): array |
28
|
|
|
{ |
29
|
|
|
$columns = []; |
30
|
|
|
foreach (self::getColumns($objectName) as $type => $label) { |
31
|
|
|
$columns['overuse,' . $type] = $label; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $columns; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Resource[] $resources |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public static function aggregateByObject(array $resources): array |
42
|
|
|
{ |
43
|
|
|
$result = []; |
44
|
|
|
foreach ($resources as $resource) { |
45
|
|
|
$object = [ |
46
|
|
|
'type' => $resource['type'], |
47
|
|
|
'unit' => 'gb', |
48
|
|
|
]; |
49
|
|
|
$object['amount'] += self::convert('byte', 'gb', $resource->getAmount()); |
50
|
|
|
$result[$resource['object_id']][$resource['type']] = $object; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function convert(string $from, string $to, $value) |
57
|
|
|
{ |
58
|
|
|
return Quantity::create(Unit::create($from), $value)->convert(Unit::create($to))->getQuantity(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Resource[] $models |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public static function groupResourcesForChart(array $models): array |
66
|
|
|
{ |
67
|
|
|
$labels = []; |
68
|
|
|
$data = []; |
69
|
|
|
ArrayHelper::multisort($models, 'date'); |
70
|
|
|
foreach ($models as $model) { |
71
|
|
|
$labels[$model->date] = $model; |
72
|
|
|
$data[$model->type][] = $model->getChartAmount(); |
73
|
|
|
} |
74
|
|
|
foreach ($labels as $date => $model) { |
75
|
|
|
$labels[$date] = $model->getDisplayDate(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return [$labels, $data]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: