mirror of
https://code.castopod.org/adaures/castopod
synced 2025-04-19 13:01:19 +00:00

This removes computing latency when retrieving episodes list with download count in admin. The more analytics records, the more it took to calculate the sum of hits to get the downloads count for each episode.
51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Commands;
|
|
|
|
use App\Models\EpisodeModel;
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
|
|
class EpisodesComputeDownloads extends BaseCommand
|
|
{
|
|
/**
|
|
* The Command's Group
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $group = 'Episodes';
|
|
|
|
/**
|
|
* The Command's Name
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $name = 'episodes:compute-downloads';
|
|
|
|
/**
|
|
* The Command's Description
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = "Calculates all episodes downloads and stores results in episodes' downloads_count field.";
|
|
|
|
/**
|
|
* Actually execute a command.
|
|
*/
|
|
public function run(array $params): void
|
|
{
|
|
$episodesModel = new EpisodeModel();
|
|
$query = $episodesModel->builder()
|
|
->select('episodes.id as id, IFNULL(SUM(ape.hits),0) as downloads_count')
|
|
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
|
|
->groupBy('episodes.id');
|
|
|
|
$episodeModel2 = new EpisodeModel();
|
|
$episodeModel2->builder()
|
|
->setQueryAsData($query)
|
|
->onConstraint('id')
|
|
->updateBatch();
|
|
}
|
|
}
|