|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created for IG Monitoring. |
|
4
|
|
|
* User: jakim <[email protected]> |
|
5
|
|
|
* Date: 19.06.2018 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace app\components\stats; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use app\components\stats\base\AccountDiff; |
|
12
|
|
|
use Carbon\Carbon; |
|
13
|
|
|
use yii\db\Expression; |
|
14
|
|
|
use yii\helpers\ArrayHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @inheritdoc |
|
18
|
|
|
* |
|
19
|
|
|
* @property array|\app\models\Account[] $models |
|
20
|
|
|
*/ |
|
21
|
|
|
class AccountDailyDiff extends AccountDiff |
|
22
|
|
|
{ |
|
23
|
|
|
public function initLastDiff() |
|
24
|
|
|
{ |
|
25
|
|
|
$accountIds = $this->getModelIds(); |
|
26
|
|
|
$max1Ids = $this->getLastStatsIds(null, null, $accountIds); |
|
27
|
|
|
$max2Ids = $this->getLastStatsBeforeIds(null, null, $accountIds, 'account_stats.account_id', 1, $max1Ids); |
|
28
|
|
|
|
|
29
|
|
|
$accountsStats = $this->getAccountsStats($accountIds, ArrayHelper::merge($max1Ids, $max2Ids)); |
|
30
|
|
|
$this->lastDiffCache = $this->prepareCache($accountsStats, 'account_id'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The difference between the data from subsequent days from a given date range. |
|
35
|
|
|
* |
|
36
|
|
|
* @param $olderDate |
|
37
|
|
|
* @param null $newerDate |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
|
|
public function initDiff($olderDate, $newerDate = null) |
|
40
|
|
|
{ |
|
41
|
|
|
$olderDate = (new Carbon($olderDate))->subDay()->startOfDay()->toDateTimeString(); |
|
42
|
|
|
$newerDate = (new Carbon($newerDate))->endOfDay()->toDateTimeString(); |
|
43
|
|
|
|
|
44
|
|
|
if (empty($this->diffCache)) { |
|
45
|
|
|
$accountIds = $this->getModelIds(); |
|
46
|
|
|
|
|
47
|
|
|
//group by DATE Y-m-d |
|
48
|
|
|
$ids = $this->getLastStatsIds($olderDate, $newerDate, $accountIds, [ |
|
49
|
|
|
'account_id', |
|
50
|
|
|
new Expression('DATE(created_at)'), |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$accountsStats = $this->getAccountsStats($accountIds, $ids); |
|
54
|
|
|
|
|
55
|
|
|
$this->diffCache = $this->prepareCache($accountsStats, 'account_id'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |