From 26e085982f47bfea5fc3cd4f2693654f4bdf4061 Mon Sep 17 00:00:00 2001 From: Ube <57785974+radical-ube@users.noreply.github.com> Date: Mon, 16 Mar 2026 13:25:52 -0400 Subject: [PATCH 1/3] Add nil guard and zero default to downloads by season --- app/models/concerns/podcast_metrics.rb | 19 +++++++++++-- config/locales/en.yml | 39 +------------------------- 2 files changed, 18 insertions(+), 40 deletions(-) diff --git a/app/models/concerns/podcast_metrics.rb b/app/models/concerns/podcast_metrics.rb index 20b30311d..90caba1d4 100644 --- a/app/models/concerns/podcast_metrics.rb +++ b/app/models/concerns/podcast_metrics.rb @@ -66,18 +66,33 @@ def season_download_rollups end.sort { |a, b| b[1] <=> a[1] } end + def season_label(season_number) + I18n.t(".helpers.label.metrics.downloads_by_season", season_number: season_number) + end + def downloads_by_season(season_number:) + return nil unless published_seasons.include?(season_number) + default = {"#{season_label(season_number)}": 0} + season_episodes_guids = episodes.published.where(season_number: season_number).pluck(:guid) + return default if season_episodes_guids.blank? + expiration = (season_number == latest_season) ? 1.hour : 1.month - metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: expiration) do + results = metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: expiration) do Rollups::HourlyDownload .where(podcast_id: id) .where(episode_id: season_episodes_guids) .group(:podcast_id) .final .sum(:count) - end.transform_keys { |k| "Season #{season_number}" } + end + + if results.blank? + default + else + results.transform_keys { |k| season_label(season_number) } + end end def top_countries_downloads diff --git a/config/locales/en.yml b/config/locales/en.yml index 605119483..3bd363b7e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -462,44 +462,7 @@ en: caption: Caption credit: Credit metrics: - chart: - all_episodes: All Episodes - interval: - hour: Hourly - day: Daily - week: Weekly - month: Monthly - date_presets: - 7_days_last: Last 7 Days - 14_days_last: Last 14 Days - 28_days_last: Last 28 Days - 1_month_previous: Previous Month - 3_months_previous: Previous 3 Months - 6_months_previous: Previous 6 Months - 1_year_previous: Previous Year - date_week: Week to Date - date_month: Month to Date - date_year: Year to Date - 7_days_drop: 7 days since drop - 14_days_drop: 14 days since drop - 28_days_drop: 28 days since drop - 1_month_drop: 1 month since drop - 3_months_drop: 3 months since drop - all_time: All-time - dropdays: - "7": 7 Days - "14": 14 Days - "28": 28 Days - "30": 30 Days - "60": 60 Days - "90": 90 Days - "24": 24 Hours - "48": 48 Hours - uniques: - last_7_rolling: Daily (7-Day Window) - last_28_rolling: Daily (28-Day Window) - calendar_week: Weekly - calendar_month: Monthly + downloads_by_season: "Season %{season_number}" person: href: Link name: Name From 4d4f189e506b10d8edbe555ed611c277d1bce33b Mon Sep 17 00:00:00 2001 From: Ube <57785974+radical-ube@users.noreply.github.com> Date: Mon, 16 Mar 2026 13:31:47 -0400 Subject: [PATCH 2/3] Remove nils from season_download_rollups before sorting --- app/models/concerns/podcast_metrics.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/podcast_metrics.rb b/app/models/concerns/podcast_metrics.rb index 90caba1d4..f423844df 100644 --- a/app/models/concerns/podcast_metrics.rb +++ b/app/models/concerns/podcast_metrics.rb @@ -62,8 +62,10 @@ def latest_season def season_download_rollups published_seasons.map do |season| - downloads_by_season(season_number: season).to_a.flatten - end.sort { |a, b| b[1] <=> a[1] } + downloads_by_season(season_number: season).to_a.flatten.presence + end + .compact + .sort { |a, b| b[1] <=> a[1] } end def season_label(season_number) From 635b4bd943ab4053a99898c02dd8019dbee7f75c Mon Sep 17 00:00:00 2001 From: Ube <57785974+radical-ube@users.noreply.github.com> Date: Mon, 16 Mar 2026 13:57:54 -0400 Subject: [PATCH 3/3] Change cache expiration --- app/models/concerns/podcast_metrics.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/app/models/concerns/podcast_metrics.rb b/app/models/concerns/podcast_metrics.rb index f423844df..263fd5878 100644 --- a/app/models/concerns/podcast_metrics.rb +++ b/app/models/concerns/podcast_metrics.rb @@ -56,10 +56,6 @@ def published_seasons episodes.published.dropdate_desc.pluck(:season_number).uniq.compact end - def latest_season - published_seasons.first - end - def season_download_rollups published_seasons.map do |season| downloads_by_season(season_number: season).to_a.flatten.presence @@ -79,9 +75,7 @@ def downloads_by_season(season_number:) season_episodes_guids = episodes.published.where(season_number: season_number).pluck(:guid) return default if season_episodes_guids.blank? - expiration = (season_number == latest_season) ? 1.hour : 1.month - - results = metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: expiration) do + results = metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: 1.hour) do Rollups::HourlyDownload .where(podcast_id: id) .where(episode_id: season_episodes_guids)