Skip to content

Commit 84330c3

Browse files
committed
Fix bounds on sparkline query and rounding error
1 parent d6e5b62 commit 84330c3

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

app/controllers/podcast_metrics_controller.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class PodcastMetricsController < ApplicationController
22
include MetricsUtils
33

44
before_action :set_podcast
5-
before_action :check_clickhouse, except: %i[show]
5+
# before_action :check_clickhouse, except: %i[show]
66

77
def show
88
end
@@ -12,9 +12,10 @@ def episode_sparkline
1212
@prev_episode = Episode.find_by(guid: metrics_params[:prev_episode_id])
1313

1414
@episode_trend = calculate_episode_trend(@episode, @prev_episode)
15+
1516
@sparkline_downloads =
1617
Rollups::HourlyDownload
17-
.where(episode_id: @episode[:guid], hour: (@episode.first_rss_published_at..))
18+
.where(episode_id: @episode[:guid], hour: (publish_hour(@episode)..publish_hour(@episode) + 6.months))
1819
.final
1920
.select(:episode_id, "DATE_TRUNC('DAY', hour) AS hour", "SUM(count) AS count")
2021
.group(:episode_id, "DATE_TRUNC('DAY', hour) AS hour")
@@ -219,19 +220,19 @@ def metrics_params
219220
end
220221

221222
def calculate_episode_trend(episode, prev_episode)
222-
return nil unless episode.in_default_feed? && episode.first_rss_published_at.present? && prev_episode.present?
223+
return nil unless episode.first_rss_published_at.present? && prev_episode.present?
223224
return nil if (episode.first_rss_published_at + 1.day) > Time.now
224225

225226
ep_dropday_sum = episode_dropday_query(episode)
226227
previous_ep_dropday_sum = episode_dropday_query(prev_episode)
227228

228229
return nil if ep_dropday_sum <= 0 || previous_ep_dropday_sum <= 0
229230

230-
((ep_dropday_sum.to_f / previous_ep_dropday_sum.to_f) - 1).round(2)
231+
((ep_dropday_sum.to_f / previous_ep_dropday_sum.to_f) - 1).round(3)
231232
end
232233

233234
def episode_dropday_query(ep)
234-
lowerbound = ep.first_rss_published_at.beginning_of_hour
235+
lowerbound = publish_hour(ep)
235236
upperbound = lowerbound + 24.hours
236237

237238
Rollups::HourlyDownload
@@ -240,4 +241,12 @@ def episode_dropday_query(ep)
240241
.load_async
241242
.sum(:count)
242243
end
244+
245+
def publish_hour(episode)
246+
if episode.first_rss_published_at.present?
247+
episode.first_rss_published_at.beginning_of_hour
248+
else
249+
episode.published_at.beginning_of_hour
250+
end
251+
end
243252
end

app/helpers/metrics_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def parse_trend(trend)
1717

1818
if trend > 0
1919
{
20-
percent: "+#{(trend * 100).round(2)}%",
20+
percent: "+#{trend * 100}%",
2121
color: modified_trend_color(trend, "text-success"),
2222
direction: modified_trend_direction(trend, "up")
2323
}
2424
elsif trend < 0
2525
{
26-
percent: "-#{(trend * -100).round(2)}%",
26+
percent: "-#{trend * -100}%",
2727
color: modified_trend_color(trend, "text-danger"),
2828
direction: modified_trend_direction(trend, "down")
2929
}

0 commit comments

Comments
 (0)