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\models\Account; |
12
|
|
|
use app\models\AccountStats; |
13
|
|
|
use Carbon\Carbon; |
14
|
|
|
use yii\base\Component; |
15
|
|
|
use yii\db\Expression; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
class AccountDaily extends Component |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \app\models\Account |
22
|
|
|
*/ |
23
|
|
|
public $model; |
24
|
|
|
|
25
|
|
|
private $statsAttributes = [ |
26
|
|
|
'media', |
27
|
|
|
'followed_by', |
28
|
|
|
'follows', |
29
|
|
|
'er', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
private static $cache = []; |
33
|
|
|
|
34
|
|
|
public function __construct(Account $model, array $config = []) |
35
|
|
|
{ |
36
|
|
|
$this->model = $model; |
37
|
|
|
static::$cache = []; |
|
|
|
|
38
|
|
|
parent::__construct($config); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function get() |
42
|
|
|
{ |
43
|
|
|
return static::$cache; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The difference between the data from subsequent days from a given date range. |
48
|
|
|
* |
49
|
|
|
* @param $olderDate |
50
|
|
|
* @param null $newerDate |
|
|
|
|
51
|
|
|
* @return \app\components\stats\AccountDaily |
52
|
|
|
*/ |
53
|
|
|
public function initDiff($olderDate, $newerDate = null) |
54
|
|
|
{ |
55
|
|
|
$olderDate = (new Carbon($olderDate))->startOfDay()->toDateTimeString(); |
56
|
|
|
$newerDate = (new Carbon($newerDate))->endOfDay()->toDateTimeString(); |
57
|
|
|
|
58
|
|
|
if (empty(static::$cache)) { |
|
|
|
|
59
|
|
|
//group by DATE Y-m-d |
60
|
|
|
$ids = AccountStats::find() |
61
|
|
|
->select(new Expression('MAX(id) as id')) |
62
|
|
|
->andWhere(['account_id' => $this->model->id]) |
63
|
|
|
->andWhere(['>=', 'created_at', $olderDate]) |
64
|
|
|
->andWhere(['<=', 'created_at', $newerDate]) |
65
|
|
|
->groupBy(new Expression('DATE(created_at)')) |
66
|
|
|
->column(); |
67
|
|
|
|
68
|
|
|
$stats = AccountStats::find() |
69
|
|
|
->select([ |
70
|
|
|
'account_stats.*', |
71
|
|
|
new Expression('DATE(created_at) as created_at'), |
72
|
|
|
]) |
73
|
|
|
->andWhere(['account_id' => $this->model->id]) |
74
|
|
|
->andWhere(['id' => $ids]) |
75
|
|
|
->orderBy('id ASC') |
76
|
|
|
->asArray() |
77
|
|
|
->all(); |
78
|
|
|
|
79
|
|
|
foreach ($stats as $stat) { |
80
|
|
|
foreach ($this->statsAttributes as $statsAttribute) { |
81
|
|
|
$value = ArrayHelper::getValue($stat, $statsAttribute, 0); |
82
|
|
|
static::$cache[$stat['created_at']][$statsAttribute] = $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |