1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\actions; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use hipanel\actions\Action; |
7
|
|
|
use hipanel\actions\RenderJsonAction; |
8
|
|
|
use hipanel\modules\finance\helpers\ResourceConfigurator; |
9
|
|
|
use hipanel\modules\finance\helpers\ResourceHelper; |
10
|
|
|
use hipanel\modules\finance\models\proxy\Resource; |
11
|
|
|
use http\Exception\RuntimeException; |
12
|
|
|
use yii\base\DynamicModel; |
13
|
|
|
|
14
|
|
|
class ResourceFetchDataAction extends RenderJsonAction |
15
|
|
|
{ |
16
|
|
|
public ResourceConfigurator $configurator; |
|
|
|
|
17
|
|
|
|
18
|
|
|
public function init() |
19
|
|
|
{ |
20
|
|
|
parent::init(); |
21
|
|
|
$this->return = function (Action $action) { |
22
|
|
|
$request = $action->controller->request; |
23
|
|
|
if ($request->isPost) { |
24
|
|
|
$resources = $this->getResources([ |
25
|
|
|
'object_ids' => $request->post('object_ids'), |
26
|
|
|
'time_from' => $request->post('time_from'), |
27
|
|
|
'time_till' => $request->post('time_till'), |
28
|
|
|
'groupby' => 'server_traf_month', |
29
|
|
|
]); |
30
|
|
|
|
31
|
|
|
return ResourceHelper::aggregateByObject($resources, $this->configurator); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return []; |
35
|
|
|
}; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function getResources(array $params): array |
39
|
|
|
{ |
40
|
|
|
$options = DynamicModel::validateData([ |
41
|
|
|
'object_ids' => $params['object_ids'], |
42
|
|
|
'time_from' => $params['time_from'] ?? (new DateTime())->modify('first day of this month')->format('Y-m-d'), |
43
|
|
|
'time_till' => $params['time_till'] ?? (new DateTime())->modify('last day of this month')->format('Y-m-d'), |
44
|
|
|
'groupby' => $params['groupby'], |
45
|
|
|
], [ |
46
|
|
|
[['object_ids', 'time_from', 'time_till', 'groupby'], 'required'], |
47
|
|
|
['object_ids', 'string'], |
48
|
|
|
[['time_from', 'time_till'], 'datetime', 'format' => 'php:Y-m-d'], |
49
|
|
|
['groupby', 'in', 'range' => ['server_traf_month', 'server_traf_week', 'server_traf_day']], |
50
|
|
|
]); |
51
|
|
|
if ($options->hasErrors()) { |
52
|
|
|
throw new RuntimeException($options->getErrors()[0]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return Resource::find() |
56
|
|
|
->where([ |
57
|
|
|
'object_id' => explode(',', $options->object_ids), |
58
|
|
|
'time_from' => $options->time_from, |
59
|
|
|
'time_till' => $options->time_till, |
60
|
|
|
'groupby' => $options->groupby, |
61
|
|
|
]) |
62
|
|
|
->limit(-1) |
63
|
|
|
->all(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|