Commit f9819376 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

fix: add downloads_count to episodes table, computed every hour

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.
parent f288a750
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
<?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();
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -51,5 +51,9 @@ class Tasks extends BaseConfig
        $schedule->command('podcast:import')
            ->everyMinute()
            ->named('podcast-import');

        $schedule->command('episodes:compute-downloads')
            ->everyHour()
            ->named('episodes:compute-downloads');
    }
}
+29 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddEpisodeDownloadsCount extends Migration
{
    public function up(): void
    {
        $fields = [
            'downloads_count' => [
                'type'     => 'INT',
                'unsigned' => true,
                'default'  => 0,
                'after'    => 'is_published_on_hubs',
            ],
        ];

        $this->forge->addColumn('episodes', $fields);
    }

    public function down(): void
    {
        $this->forge->dropColumn('episodes', 'downloads_count');
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -72,9 +72,9 @@ use RuntimeException;
 * @property string|null $location_geo
 * @property string|null $location_osm
 * @property bool $is_published_on_hubs
 * @property int $downloads_count
 * @property int $posts_count
 * @property int $comments_count
 * @property int $downloads
 * @property EpisodeComment[]|null $comments
 * @property bool $is_premium
 * @property int $created_by
@@ -112,8 +112,6 @@ class Episode extends Entity

    protected ?Chapters $chapters = null;

    protected int $downloads = 0;

    /**
     * @var Person[]|null
     */
@@ -171,6 +169,7 @@ class Episode extends Entity
        'location_geo'          => '?string',
        'location_osm'          => '?string',
        'is_published_on_hubs'  => 'boolean',
        'downloads_count'       => 'integer',
        'posts_count'           => 'integer',
        'comments_count'        => 'integer',
        'is_premium'            => 'boolean',
+1 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ class EpisodeModel extends UuidModel
        'location_geo',
        'location_osm',
        'is_published_on_hubs',
        'downloads_count',
        'posts_count',
        'comments_count',
        'is_premium',
Loading