Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions app/models/concerns/podcast_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added .presence to catch empty arrays and revert them to nil, then remove them before sorting.

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)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realized this didn't make it into en.yml

end

def downloads_by_season(season_number:)
return nil unless published_seasons.include?(season_number)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first guard to make sure that the season number exists.

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?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set the default at the top, and this guard checks to see if there are any published episodes given this season. if not, then just return the default.


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?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastly, if the query returns no results, return the default.

default
else
results.transform_keys { |k| season_label(season_number) }
end
end

def top_countries_downloads
Expand Down
39 changes: 1 addition & 38 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -462,44 +462,7 @@ en:
caption: Caption
credit: Credit
metrics:
chart:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i realized i missed this chunk of previously used labels that i forgot to remove during the design switch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Always tricky to track down unused translations.

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
Expand Down
Loading