fix(analytics): update service management so that it works with new OPAWG slug values

This commit is contained in:
Benjamin Bellamy 2020-11-30 11:45:41 +00:00
parent 8b3c689f86
commit 7fe9d42500
10 changed files with 441 additions and 150 deletions

View File

@ -23,22 +23,24 @@ class Feed extends Controller
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
$service = null;
$serviceSlug = '';
try {
$service = \Opawg\UserAgentsPhp\UserAgentsRSS::find(
$_SERVER['HTTP_USER_AGENT']
);
if ($service) {
$serviceSlug = $service['slug'];
}
} catch (\Exception $e) {
// If things go wrong the show must go on and the user must be able to download the file
log_message('critical', $e);
}
$cacheName =
"podcast{$podcast->id}_feed" .
($service ? "_{$service['slug']}" : '');
"podcast{$podcast->id}_feed" . ($service ? "_{$serviceSlug}" : '');
if (!($found = cache($cacheName))) {
$found = get_rss_feed($podcast, $service ? $service['name'] : '');
$found = get_rss_feed($podcast, $serviceSlug);
// The page cache is set to expire after next episode publication or a decade by default so it is deleted manually upon podcast update
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(

View File

@ -78,7 +78,7 @@ class FakePodcastsAnalyticsSeeder extends Seeder
$service =
$jsonRSSUserAgents[
rand(1, count($jsonRSSUserAgents) - 1)
]['name'];
]['slug'];
$app = isset($player['app']) ? $player['app'] : '';
$device = isset($player['device'])
? $player['device']

View File

@ -14,6 +14,11 @@ use CodeIgniter\Entity;
class AnalyticsPodcastsByCountry extends Entity
{
/**
* @var string
*/
protected $labels;
protected $casts = [
'podcast_id' => 'integer',
'country_code' => 'string',

View File

@ -0,0 +1,38 @@
<?php
/**
* Class AnalyticsPodcastsByService
* Entity for AnalyticsPodcastsByService
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Entities;
use CodeIgniter\Entity;
class AnalyticsPodcastsByService extends Entity
{
/**
* @var string
*/
protected $labels;
protected $casts = [
'podcast_id' => 'integer',
'app' => '?string',
'device' => '?string',
'os' => '?string',
'is_bot' => 'boolean',
'date' => 'datetime',
'hits' => 'integer',
];
public function getLabels()
{
return \Opawg\UserAgentsPhp\UserAgentsRSS::getName(
$this->attributes['labels']
) ?? $this->attributes['labels'];
}
}

View File

@ -17,7 +17,7 @@ use Config\Mimes;
* @param string $service The name of the service that fetches the RSS feed for future reference when the audio file is eventually downloaded
* @return string rss feed as xml
*/
function get_rss_feed($podcast, $serviceName = '')
function get_rss_feed($podcast, $serviceSlug = '')
{
$episodes = $podcast->episodes;
@ -185,7 +185,7 @@ function get_rss_feed($podcast, $serviceName = '')
$enclosure->addAttribute(
'url',
$episode->enclosure_url .
(empty($serviceName) ? '' : '?_from=' . urlencode($serviceName))
(empty($serviceSlug) ? '' : '?_from=' . urlencode($serviceSlug))
);
$enclosure->addAttribute('length', $episode->enclosure_filesize);
$enclosure->addAttribute('type', $episode->enclosure_mimetype);

View File

@ -23,41 +23,6 @@ class AnalyticsPodcastByPlayerModel extends Model
protected $useTimestamps = false;
/**
* Gets service data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataByServiceWeekly(int $podcastId): array
{
if (
!($found = cache(
"{$podcastId}_analytics_podcasts_by_player_by_service_weekly"
))
) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`service` as `labels`')
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`service` !=' => '',
'`is_bot`' => 0,
'`date` >' => $oneWeekAgo,
])
->groupBy('`labels`')
->orderBy('`values`', 'DESC')
->findAll();
cache()->save(
"{$podcastId}_analytics_podcasts_by_player_by_service_weekly",
$found,
600
);
}
return $found;
}
/**
* Gets player data for a podcast
*

View File

@ -0,0 +1,60 @@
<?php
/**
* Class AnalyticsPodcastByServiceModel
* 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/
*/
namespace App\Models;
use CodeIgniter\Model;
class AnalyticsPodcastByServiceModel extends Model
{
protected $table = 'analytics_podcasts_by_player';
protected $allowedFields = [];
protected $returnType = \App\Entities\AnalyticsPodcastsByService::class;
protected $useSoftDeletes = false;
protected $useTimestamps = false;
/**
* Gets service data for a podcast
*
* @param int $podcastId
*
* @return array
*/
public function getDataByServiceWeekly(int $podcastId): array
{
if (
!($found = cache(
"{$podcastId}_analytics_podcasts_by_service_weekly"
))
) {
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
$found = $this->select('`service` as `labels`')
->selectSum('`hits`', '`values`')
->where([
'`podcast_id`' => $podcastId,
'`service` !=' => '',
'`is_bot`' => 0,
'`date` >' => $oneWeekAgo,
])
->groupBy('`labels`')
->orderBy('`values`', 'DESC')
->findAll();
cache()->save(
"{$podcastId}_analytics_podcasts_by_service_weekly",
$found,
600
);
}
return $found;
}
}

View File

@ -26,7 +26,7 @@
<div class="chart-pie" id="by-service-weekly-pie" data-chart-type="pie-chart" data-chart-url="<?= route_to(
'analytics-data',
$podcast->id,
'PodcastByPlayer',
'PodcastByService',
'ByServiceWeekly'
) ?>"></div>
</div>

View File

@ -15,8 +15,8 @@
"league/commonmark": "^1.5",
"vlucas/phpdotenv": "^5.2",
"league/html-to-markdown": "^4.10",
"opawg/user-agents-php": "*",
"podlibre/ipcat": "*"
"opawg/user-agents-php": "^1.0",
"podlibre/ipcat": "^1.0"
},
"require-dev": {
"mikey179/vfsstream": "1.6.*",

431
composer.lock generated

File diff suppressed because it is too large Load Diff