2020-06-12 22:41:09 +02:00
|
|
|
<?php
|
2020-08-04 13:25:22 +02:00
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-12 22:41:09 +02:00
|
|
|
/**
|
2021-05-19 18:35:13 +02:00
|
|
|
* Class AnalyticsPodcastByPlayerModel Model for analytics_podcasts_by_player table in database
|
|
|
|
*
|
2020-06-12 22:41:09 +02:00
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-08-04 13:25:22 +02:00
|
|
|
|
2021-04-14 17:58:40 +02:00
|
|
|
namespace Analytics\Models;
|
2020-06-12 22:41:09 +02:00
|
|
|
|
2021-05-06 16:00:48 +02:00
|
|
|
use Analytics\Entities\AnalyticsPodcastsByPlayer;
|
2020-06-12 22:41:09 +02:00
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
2020-10-08 16:45:46 +02:00
|
|
|
class AnalyticsPodcastByPlayerModel extends Model
|
2020-06-12 22:41:09 +02:00
|
|
|
{
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-06-12 22:41:09 +02:00
|
|
|
protected $table = 'analytics_podcasts_by_player';
|
|
|
|
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = AnalyticsPodcastsByPlayer::class;
|
2021-05-19 18:35:13 +02:00
|
|
|
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-06-12 22:41:09 +02:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2021-05-06 16:00:48 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-06-12 22:41:09 +02:00
|
|
|
protected $useTimestamps = false;
|
2020-10-06 17:39:27 +02:00
|
|
|
|
2020-10-21 18:04:18 +02:00
|
|
|
/**
|
2020-10-08 16:45:46 +02:00
|
|
|
* Gets player data for a podcast
|
2020-10-06 17:39:27 +02:00
|
|
|
*
|
2021-05-06 16:00:48 +02:00
|
|
|
* @return AnalyticsPodcastsByPlayer[]
|
2020-10-06 17:39:27 +02:00
|
|
|
*/
|
2020-10-14 12:38:48 +02:00
|
|
|
public function getDataByAppWeekly(int $podcastId): array
|
2020-10-06 17:39:27 +02:00
|
|
|
{
|
|
|
|
if (
|
2021-06-09 14:40:22 +02:00
|
|
|
! ($found = cache("{$podcastId}_analytics_podcasts_by_player_by_app_weekly"))
|
2020-10-06 17:39:27 +02:00
|
|
|
) {
|
2020-11-03 15:14:30 +01:00
|
|
|
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
|
2021-04-02 19:20:02 +02:00
|
|
|
$found = $this->select('app as labels')
|
|
|
|
->selectSum('hits', 'values')
|
2020-10-06 17:39:27 +02:00
|
|
|
->where([
|
2021-04-02 19:20:02 +02:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'app !=' => '',
|
|
|
|
'is_bot' => 0,
|
|
|
|
'date >' => $oneWeekAgo,
|
2020-10-06 17:39:27 +02:00
|
|
|
])
|
2021-04-02 19:20:02 +02:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('values', 'DESC')
|
2020-11-03 15:14:30 +01:00
|
|
|
->findAll();
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
2021-06-08 11:52:11 +02:00
|
|
|
->save("{$podcastId}_analytics_podcasts_by_player_by_app_weekly", $found, 600);
|
2020-10-06 17:39:27 +02:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-14 12:38:48 +02:00
|
|
|
* Gets player data for a podcast
|
2020-10-06 17:39:27 +02:00
|
|
|
*
|
2021-05-06 16:00:48 +02:00
|
|
|
* @return AnalyticsPodcastsByPlayer[]
|
2020-10-06 17:39:27 +02:00
|
|
|
*/
|
2020-10-14 12:38:48 +02:00
|
|
|
public function getDataByAppYearly(int $podcastId): array
|
2020-10-06 17:39:27 +02:00
|
|
|
{
|
|
|
|
if (
|
2021-06-09 14:40:22 +02:00
|
|
|
! ($found = cache("{$podcastId}_analytics_podcasts_by_player_by_app_yearly"))
|
2020-10-06 17:39:27 +02:00
|
|
|
) {
|
2020-11-03 15:14:30 +01:00
|
|
|
$oneYearAgo = date('Y-m-d', strtotime('-1 year'));
|
2021-04-02 19:20:02 +02:00
|
|
|
$found = $this->select('app as labels')
|
|
|
|
->selectSum('hits', 'values')
|
2020-10-06 17:39:27 +02:00
|
|
|
->where([
|
2021-04-02 19:20:02 +02:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'app !=' => '',
|
|
|
|
'is_bot' => 0,
|
|
|
|
'date >' => $oneYearAgo,
|
2020-10-06 17:39:27 +02:00
|
|
|
])
|
2021-04-02 19:20:02 +02:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('values', 'DESC')
|
2020-11-03 15:14:30 +01:00
|
|
|
->findAll();
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
2021-06-08 11:52:11 +02:00
|
|
|
->save("{$podcastId}_analytics_podcasts_by_player_by_app_yearly", $found, 600);
|
2020-10-14 12:38:48 +02:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets os data for a podcast
|
|
|
|
*
|
2021-05-06 16:00:48 +02:00
|
|
|
* @return AnalyticsPodcastsByPlayer[]
|
2020-10-14 12:38:48 +02:00
|
|
|
*/
|
|
|
|
public function getDataByOsWeekly(int $podcastId): array
|
|
|
|
{
|
|
|
|
if (
|
2021-06-09 14:40:22 +02:00
|
|
|
! ($found = cache("{$podcastId}_analytics_podcasts_by_player_by_os_weekly"))
|
2020-10-14 12:38:48 +02:00
|
|
|
) {
|
2020-11-03 15:14:30 +01:00
|
|
|
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
|
2021-04-02 19:20:02 +02:00
|
|
|
$found = $this->select('os as labels')
|
|
|
|
->selectSum('hits', 'values')
|
2020-10-06 17:39:27 +02:00
|
|
|
->where([
|
2021-04-02 19:20:02 +02:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'app !=' => '',
|
|
|
|
'os !=' => '',
|
|
|
|
'is_bot' => 0,
|
|
|
|
'date >' => $oneWeekAgo,
|
2020-10-06 17:39:27 +02:00
|
|
|
])
|
2021-04-02 19:20:02 +02:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('values', 'DESC')
|
2020-11-03 15:14:30 +01:00
|
|
|
->findAll();
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
2021-06-08 11:52:11 +02:00
|
|
|
->save("{$podcastId}_analytics_podcasts_by_player_by_os_weekly", $found, 600);
|
2020-10-14 12:38:48 +02:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
2020-10-06 17:39:27 +02:00
|
|
|
|
2020-10-14 12:38:48 +02:00
|
|
|
/**
|
|
|
|
* Gets player data for a podcast
|
|
|
|
*
|
2021-05-06 16:00:48 +02:00
|
|
|
* @return AnalyticsPodcastsByPlayer[]
|
2020-10-14 12:38:48 +02:00
|
|
|
*/
|
|
|
|
public function getDataByDeviceWeekly(int $podcastId): array
|
|
|
|
{
|
|
|
|
if (
|
2021-06-09 14:40:22 +02:00
|
|
|
! ($found = cache("{$podcastId}_analytics_podcasts_by_player_by_device_weekly"))
|
2020-10-14 12:38:48 +02:00
|
|
|
) {
|
2020-11-03 15:14:30 +01:00
|
|
|
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
|
2021-04-02 19:20:02 +02:00
|
|
|
$found = $this->select('device as labels')
|
|
|
|
->selectSum('hits', 'values')
|
2020-10-06 17:39:27 +02:00
|
|
|
->where([
|
2021-04-02 19:20:02 +02:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'device !=' => '',
|
|
|
|
'is_bot' => 0,
|
|
|
|
'date >' => $oneWeekAgo,
|
2020-10-06 17:39:27 +02:00
|
|
|
])
|
2021-04-02 19:20:02 +02:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('values', 'DESC')
|
2020-11-03 15:14:30 +01:00
|
|
|
->findAll();
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
2021-06-08 11:52:11 +02:00
|
|
|
->save("{$podcastId}_analytics_podcasts_by_player_by_device_weekly", $found, 600);
|
2020-10-14 12:38:48 +02:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
2020-10-06 17:39:27 +02:00
|
|
|
|
2020-10-14 12:38:48 +02:00
|
|
|
/**
|
|
|
|
* Gets bots data for a podcast
|
|
|
|
*
|
2021-05-06 16:00:48 +02:00
|
|
|
* @return AnalyticsPodcastsByPlayer[]
|
2020-10-14 12:38:48 +02:00
|
|
|
*/
|
|
|
|
public function getDataBots(int $podcastId): array
|
|
|
|
{
|
|
|
|
if (
|
2021-05-19 18:35:13 +02:00
|
|
|
! ($found = cache("{$podcastId}_analytics_podcasts_by_player_bots"))
|
2020-10-14 12:38:48 +02:00
|
|
|
) {
|
2020-11-03 15:14:30 +01:00
|
|
|
$oneYearAgo = date('Y-m-d', strtotime('-1 year'));
|
2021-04-02 19:20:02 +02:00
|
|
|
$found = $this->select('DATE_FORMAT(date,"%Y-%m-01") as labels')
|
|
|
|
->selectSum('hits', 'values')
|
2020-10-06 17:39:27 +02:00
|
|
|
->where([
|
2021-04-02 19:20:02 +02:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'is_bot' => 1,
|
|
|
|
'date >' => $oneYearAgo,
|
2020-10-06 17:39:27 +02:00
|
|
|
])
|
2021-04-02 19:20:02 +02:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('labels', 'ASC')
|
2020-11-03 15:14:30 +01:00
|
|
|
->findAll();
|
2020-10-06 17:39:27 +02:00
|
|
|
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
2021-06-08 11:52:11 +02:00
|
|
|
->save("{$podcastId}_analytics_podcasts_by_player_bots", $found, 600);
|
2020-10-06 17:39:27 +02:00
|
|
|
}
|
|
|
|
return $found;
|
|
|
|
}
|
2020-06-12 22:41:09 +02:00
|
|
|
}
|