Newer
Older

Yassine Doghri
committed
/**
* Class AnalyticsPodcastsByPlayerModel
* Model for analytics_podcasts_by_player table in database
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/

Yassine Doghri
committed
namespace App\Models;
use CodeIgniter\Model;
class AnalyticsPodcastsByPlayerModel extends Model
{
protected $table = 'analytics_podcasts_by_player';
protected $allowedFields = [];

Yassine Doghri
committed
protected $returnType = \App\Entities\AnalyticsPodcastsByPlayer::class;
protected $useSoftDeletes = false;
protected $useTimestamps = false;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Gets all data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataByApp(int $podcastId): array
{
if (
!($found = cache(
"{$podcastId}_analytics_podcasts_by_player_by_app"
))
) {
$found = $this->select('`app` as `labels`')
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`app` !=' => null,
'`bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')),
])
->groupBy('`labels`')
->orderBy('`values``', 'DESC')
->findAll(10);
cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_app",
$found,
14400
);
}
return $found;
}
/**
* Gets all data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataByDevice(int $podcastId): array
{
if (
!($found = cache(
"{$podcastId}_analytics_podcasts_by_player_by_device"
))
) {
$foundApp = $this->select(
'CONCAT_WS("/", `device`, `os`, `app`) as `ids`, `app` as `labels`, CONCAT_WS("/", `device`, `os`) as `parents`'
)
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`app` !=' => null,
'`bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')),
])
->groupBy('`ids`')
->orderBy('`values``', 'DESC')
->findAll();
$foundOs = $this->select(
'CONCAT_WS("/", `device`, `os`) as `ids`, `os` as `labels`, `device` as `parents`'
)
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`os` !=' => null,
'`bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')),
])
->groupBy('`ids`')
->orderBy('`values``', 'DESC')
->findAll();
$foundDevice = $this->select(
'`device` as `ids`, `device` as `labels`, "" as `parents`'
)
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`device` !=' => null,
'`bot`' => 0,
'`date` >' => date('Y-m-d', strtotime('-1 week')),
])
->groupBy('`ids`')
->orderBy('`values``', 'DESC')
->findAll();
$foundBot = $this->select(
'"bots" as `ids`, "Bots" as `labels`, "" as `parents`'
)
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`bot`' => 1,
'`date` >' => date('Y-m-d', strtotime('-1 week')),
])
->groupBy('`ids`')
->orderBy('`values``', 'DESC')
->findAll();
$found = array_merge($foundApp, $foundOs, $foundDevice, $foundBot);
cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_device",
$found,
14400
);
}
return $found;
}