1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\models; |
4
|
|
|
|
5
|
|
|
use hipanel\base\Model; |
6
|
|
|
use hipanel\base\ModelTrait; |
7
|
|
|
use hipanel\modules\server\models\Server; |
8
|
|
|
use hiqdev\hiart\ActiveQuery; |
9
|
|
|
use hiqdev\php\units\Quantity; |
10
|
|
|
use hiqdev\php\units\Unit; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\db\QueryInterface; |
13
|
|
|
|
14
|
|
|
class Resource extends Model |
15
|
|
|
{ |
16
|
|
|
use ModelTrait; |
17
|
|
|
|
18
|
|
|
public static function tableName(): string |
19
|
|
|
{ |
20
|
|
|
return 'use'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function rules() |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
[['id', 'object_id', 'type_id'], 'integer'], |
27
|
|
|
[['last', 'total'], 'number'], |
28
|
|
|
[['type', 'aggregation'], 'string'], |
29
|
|
|
[['time_from', 'time_till', 'date'], 'datetime', 'format' => 'php:Y-m-d'], |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getServer(): ActiveQuery |
34
|
|
|
{ |
35
|
|
|
return $this->hasOne(Server::class, ['object_id' => 'id']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
* @return QueryInterface |
41
|
|
|
*/ |
42
|
|
|
public static function find(array $options = []): QueryInterface |
43
|
|
|
{ |
44
|
|
|
return new ResourceQuery(get_called_class(), [ |
45
|
|
|
'options' => $options, |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getAmount() |
50
|
|
|
{ |
51
|
|
|
if (in_array($this->type, $this->getBandwidthTypes(), true)) { |
|
|
|
|
52
|
|
|
return $this->last; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
if (in_array($this->type, $this->getTrafficTypes(), true)) { |
|
|
|
|
55
|
|
|
return $this->total; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->total; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getConvertedAmount(string $from, string $to) |
62
|
|
|
{ |
63
|
|
|
return Quantity::create(Unit::create($from), $this->getAmount())->convert(Unit::create($to))->getQuantity(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getChartAmount() |
67
|
|
|
{ |
68
|
|
|
if (in_array($this->type, $this->getBandwidthTypes(), true)) { |
|
|
|
|
69
|
|
|
return round($this->getAmount() / (10 ** 6), 2); |
70
|
|
|
} |
71
|
|
|
if (in_array($this->type, $this->getTrafficTypes(), true)) { |
|
|
|
|
72
|
|
|
return round($this->getAmount() / (10 ** 9), 2); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->getAmount(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getDisplayDate(): string |
79
|
|
|
{ |
80
|
|
|
if ($this->aggregation === 'month') { |
|
|
|
|
81
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'LLL y'); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
if ($this->aggregation === 'week') { |
|
|
|
|
84
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'dd LLL y'); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
if ($this->aggregation === 'day') { |
|
|
|
|
87
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date), 'dd LLL y'); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return Yii::$app->formatter->asDate(strtotime($this->date)); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getTrafficTypes(): array |
94
|
|
|
{ |
95
|
|
|
return ['server_traf_in', 'server_traf_max', 'server_traf']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getBandwidthTypes(): array |
99
|
|
|
{ |
100
|
|
|
return ['server_traf95_in', 'server_traf95_max', 'server_traf95']; |
101
|
|
|
} |
102
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.