Skip to content

Commit c69e778

Browse files
authored
Merge pull request #1452 from PRX/fix/downloads_by_season_nil_bug
Add nil guard and zero default to downloads by season
2 parents 4b6e023 + 4e9e3d0 commit c69e778

File tree

2 files changed

+21
-47
lines changed

2 files changed

+21
-47
lines changed

app/models/concerns/podcast_metrics.rb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,39 @@ def has_seasons_chart?
5858
published_seasons.length > 1
5959
end
6060

61-
def latest_season
62-
published_seasons.first
63-
end
64-
6561
def season_download_rollups
6662
published_seasons.map do |season|
67-
downloads_by_season(season_number: season).to_a.flatten
68-
end.sort { |a, b| b[1] <=> a[1] }
63+
downloads_by_season(season_number: season).to_a.flatten.presence
64+
end
65+
.compact
66+
.sort { |a, b| b[1] <=> a[1] }
67+
end
68+
69+
def season_label(season_number)
70+
I18n.t(".helpers.label.metrics.downloads_by_season", season_number: season_number)
6971
end
7072

7173
def downloads_by_season(season_number:)
74+
return nil unless published_seasons.include?(season_number)
75+
default = {"#{season_label(season_number)}": 0}
76+
7277
season_episodes_guids = episodes.published.where(season_number: season_number).pluck(:guid)
73-
expiration = (season_number == latest_season) ? 1.hour : 1.month
78+
return default if season_episodes_guids.blank?
7479

75-
metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: expiration) do
80+
results = metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: 1.hour) do
7681
Rollups::HourlyDownload
7782
.where(podcast_id: id)
7883
.where(episode_id: season_episodes_guids)
7984
.group(:podcast_id)
8085
.final
8186
.sum(:count)
82-
end.transform_keys { |k| "Season #{season_number}" }
87+
end
88+
89+
if results.blank?
90+
default
91+
else
92+
results.transform_keys { |k| season_label(season_number) }
93+
end
8394
end
8495

8596
def top_countries_downloads

config/locales/en.yml

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -462,44 +462,7 @@ en:
462462
caption: Caption
463463
credit: Credit
464464
metrics:
465-
chart:
466-
all_episodes: All Episodes
467-
interval:
468-
hour: Hourly
469-
day: Daily
470-
week: Weekly
471-
month: Monthly
472-
date_presets:
473-
7_days_last: Last 7 Days
474-
14_days_last: Last 14 Days
475-
28_days_last: Last 28 Days
476-
1_month_previous: Previous Month
477-
3_months_previous: Previous 3 Months
478-
6_months_previous: Previous 6 Months
479-
1_year_previous: Previous Year
480-
date_week: Week to Date
481-
date_month: Month to Date
482-
date_year: Year to Date
483-
7_days_drop: 7 days since drop
484-
14_days_drop: 14 days since drop
485-
28_days_drop: 28 days since drop
486-
1_month_drop: 1 month since drop
487-
3_months_drop: 3 months since drop
488-
all_time: All-time
489-
dropdays:
490-
"7": 7 Days
491-
"14": 14 Days
492-
"28": 28 Days
493-
"30": 30 Days
494-
"60": 60 Days
495-
"90": 90 Days
496-
"24": 24 Hours
497-
"48": 48 Hours
498-
uniques:
499-
last_7_rolling: Daily (7-Day Window)
500-
last_28_rolling: Daily (28-Day Window)
501-
calendar_week: Weekly
502-
calendar_month: Monthly
465+
downloads_by_season: "Season %{season_number}"
503466
person:
504467
href: Link
505468
name: Name

0 commit comments

Comments
 (0)