Skip to content

Commit 26e0859

Browse files
committed
Add nil guard and zero default to downloads by season
1 parent d52de61 commit 26e0859

File tree

2 files changed

+18
-40
lines changed

2 files changed

+18
-40
lines changed

app/models/concerns/podcast_metrics.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,33 @@ def season_download_rollups
6666
end.sort { |a, b| b[1] <=> a[1] }
6767
end
6868

69+
def season_label(season_number)
70+
I18n.t(".helpers.label.metrics.downloads_by_season", season_number: season_number)
71+
end
72+
6973
def downloads_by_season(season_number:)
74+
return nil unless published_seasons.include?(season_number)
75+
default = {"#{season_label(season_number)}": 0}
76+
7077
season_episodes_guids = episodes.published.where(season_number: season_number).pluck(:guid)
78+
return default if season_episodes_guids.blank?
79+
7180
expiration = (season_number == latest_season) ? 1.hour : 1.month
7281

73-
metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: expiration) do
82+
results = metrics_cache_fetch("#{metrics_cache_key}/downloads_by_season/#{season_number}", expires_in: expiration) do
7483
Rollups::HourlyDownload
7584
.where(podcast_id: id)
7685
.where(episode_id: season_episodes_guids)
7786
.group(:podcast_id)
7887
.final
7988
.sum(:count)
80-
end.transform_keys { |k| "Season #{season_number}" }
89+
end
90+
91+
if results.blank?
92+
default
93+
else
94+
results.transform_keys { |k| season_label(season_number) }
95+
end
8196
end
8297

8398
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)