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.
This commit is contained in:
Yassine Doghri 2024-12-28 15:23:02 +00:00
commit 5182d5d67a
7 changed files with 91 additions and 17 deletions

View file

@ -75,34 +75,25 @@ class EpisodeController extends BaseController
// Use LIKE operator as a fallback.
if (strlen($query) < 4) {
$episodes = $episodeModel
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
->where('episodes.podcast_id', $this->podcast->id)
->where('podcast_id', $this->podcast->id)
->like('title', $episodeModel->db->escapeLikeString($query))
->orLike('description_markdown', $episodeModel->db->escapeLikeString($query))
->orLike('slug', $episodeModel->db->escapeLikeString($query))
->orLike('location_name', $episodeModel->db->escapeLikeString($query))
->groupBy('episodes.id')
->orderBy('-`published_at`', '', false)
->orderBy('created_at', 'desc');
} else {
$episodes = $episodeModel
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
->where('episodes.podcast_id', $this->podcast->id)
->where('podcast_id', $this->podcast->id)
->where(
"MATCH (title, description_markdown, slug, location_name) AGAINST ('{$episodeModel->db->escapeString(
$query
)}')"
)
->groupBy('episodes.id');
);
}
} else {
$episodes = $episodeModel
->select('episodes.*, IFNULL(SUM(ape.hits),0) as downloads')
->join('analytics_podcasts_by_episode ape', 'episodes.id=ape.episode_id', 'left')
->where('episodes.podcast_id', $this->podcast->id)
->groupBy('episodes.id')
->where('podcast_id', $this->podcast->id)
->orderBy('-`published_at`', '', false)
->orderBy('created_at', 'desc');
}