Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
<?php
/**
* Class AnalyticsPodcastModel
* Model for analytics_podcasts table in database
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;
use CodeIgniter\Model;
class AnalyticsPodcastModel extends Model
{
protected $table = 'analytics_podcasts';
protected $allowedFields = [];
protected $returnType = \App\Entities\AnalyticsPodcasts::class;
protected $useSoftDeletes = false;
protected $useTimestamps = false;
/**
* Gets hits data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataByDay(int $podcastId): array
{
if (!($found = cache("{$podcastId}_analytics_podcast_by_day"))) {
$found = $this->select('`date` as `labels`, `hits` as `values`')
->where([
'`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 year')),
])
->orderBy('`labels`', 'ASC')
->findAll();
cache()->save("{$podcastId}_analytics_podcast_by_day", $found, 600);
}
return $found;
}
/**
* Gets hits data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataByMonth(int $podcastId): array
{
if (!($found = cache("{$podcastId}_analytics_podcast_by_month"))) {
$found = $this->select(
'concat(year(`date`),"-",month(`date`),"-01") as `labels`'
)
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 year')),
])
->groupBy('`labels`')
->orderBy('`labels`', 'ASC')
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
->findAll();
cache()->save(
"{$podcastId}_analytics_podcast_by_month",
$found,
600
);
}
return $found;
}
/**
* Gets unique listeners data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataUniqueListenersByDay(int $podcastId): array
{
if (
!($found = cache(
"{$podcastId}_analytics_podcast_unique_listeners_by_day"
))
) {
$found = $this->select(
'`date` as `labels`, `unique_listeners` as `values`'
)
->where([
'`podcast_id`' => $podcastId,
'`date` >' => date('Y-m-d', strtotime('-1 year')),
])
->orderBy('`labels`', 'ASC')
->findAll();
cache()->save(
"{$podcastId}_analytics_podcast_unique_listeners_by_day",
$found,
600
);
}
return $found;
}
/**
* Gets unique listeners data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataUniqueListenersByMonth(int $podcastId): array
{
if (
!($found = cache(
"{$podcastId}_analytics_podcast_unique_listeners_by_month"
))
) {
$found = $this->select(
'concat(year(`date`),"-",month(`date`),"-01") as `labels`'
)
->selectSum('`unique_listeners`', '`values`')
->where([
'`podcast_id`' => $podcastId,
])
->groupBy('`labels`')
->orderBy('`labels`', 'ASC')