diff --git a/app/models/concerns/podcast_metrics.rb b/app/models/concerns/podcast_metrics.rb index ffda09721..b83c71d74 100644 --- a/app/models/concerns/podcast_metrics.rb +++ b/app/models/concerns/podcast_metrics.rb @@ -58,28 +58,39 @@ def has_seasons_chart? published_seasons.length > 1 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 - 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) + 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) - expiration = (season_number == latest_season) ? 1.hour : 1.month + return default if season_episodes_guids.blank? - 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) .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