Commit 7fe9d425 authored by Benjamin Bellamy's avatar Benjamin Bellamy 💬
Browse files

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

parent 8b3c689f
Pipeline #531 passed with stage
in 5 minutes and 5 seconds
......@@ -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(
......
......@@ -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']
......
......@@ -14,6 +14,11 @@ use CodeIgniter\Entity;
class AnalyticsPodcastsByCountry extends Entity
{
/**
* @var string
*/
protected $labels;
protected $casts = [
'podcast_id' => 'integer',
'country_code' => 'string',
......
<?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'];
}
}
......@@ -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);
......
......@@ -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
*
......
<?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;
}
}
......@@ -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>
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment