1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created for IG Monitoring. |
4
|
|
|
* User: jakim <[email protected]> |
5
|
|
|
* Date: 2019-01-24 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace app\components\stats\diffs; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use app\components\stats\base\BaseMultiDiff; |
12
|
|
|
use app\models\AccountStats; |
13
|
|
|
use yii\base\InvalidConfigException; |
14
|
|
|
use yii\db\Expression; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class MultiAccountsDiffDataProvider |
19
|
|
|
* |
20
|
|
|
* @package app\components\stats\providers |
21
|
|
|
* |
22
|
|
|
* @property \Carbon\Carbon $from |
23
|
|
|
* @property \Carbon\Carbon $to |
24
|
|
|
*/ |
25
|
|
|
class MultiAccountsDiff extends BaseMultiDiff |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \app\models\Account[]|array |
29
|
|
|
*/ |
30
|
|
|
public $accounts; |
31
|
|
|
|
32
|
|
|
public $accountIds = []; |
33
|
|
|
|
34
|
|
|
public function setAccounts(array $accounts) |
35
|
|
|
{ |
36
|
|
|
$this->accounts = $accounts; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function init() |
42
|
|
|
{ |
43
|
|
|
parent::init(); |
44
|
|
|
if ($this->accounts === null) { |
45
|
|
|
throw new InvalidConfigException('Property \'accounts\' can not be empty.'); |
46
|
|
|
} |
47
|
|
|
$this->accountIds = $this->accountIds ?: ArrayHelper::getColumn($this->accounts, 'id'); |
48
|
|
|
$this->throwExceptionIfFromToAreNotSet(); |
49
|
|
|
$this->throwExceptionIfStatsAttributesIsNotSet(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function findStatsIds(?string $date, array $ignoredStatsIds = []) |
53
|
|
|
{ |
54
|
|
|
return AccountStats::find() |
55
|
|
|
->cache() |
56
|
|
|
->select(new Expression('MAX(id) as id')) |
57
|
|
|
->indexBy('account_id') |
58
|
|
|
->andWhere(['account_id' => $this->accountIds]) |
59
|
|
|
->andWhere(['<=', 'created_at', $date]) |
60
|
|
|
->andFilterWhere(['not', ['id' => $ignoredStatsIds]]) |
61
|
|
|
->groupBy('account_id') |
62
|
|
|
->column(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function findDataModels(array $statsIds) |
66
|
|
|
{ |
67
|
|
|
$columns = array_map(function ($attr) { |
68
|
|
|
return "account_stats.{$attr}"; |
69
|
|
|
}, $this->statsAttributes); |
70
|
|
|
$columns[] = 'id'; |
71
|
|
|
$columns[] = 'account_id'; |
72
|
|
|
$columns[] = new Expression('DATE(created_at) as created_at'); |
73
|
|
|
|
74
|
|
|
return AccountStats::find() |
75
|
|
|
->cache() |
76
|
|
|
->select($columns) |
77
|
|
|
->indexBy('account_id') |
78
|
|
|
->andWhere(['id' => $statsIds]) |
79
|
|
|
->asArray() |
80
|
|
|
->all(); |
81
|
|
|
} |
82
|
|
|
} |