Skip to content

Commit 02f26a7

Browse files
committed
Add bottom half cards
1 parent 56029db commit 02f26a7

File tree

5 files changed

+109
-44
lines changed

5 files changed

+109
-44
lines changed

app/controllers/concerns/metrics_queries.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,56 @@ def downloads_by_feed(model, slugs, date_start = Date.utc_today - 28.days)
3939
.load_async
4040
end
4141

42+
def top_countries_rollups(model, date_start = (Date.utc_today - 28.days).to_s, date_end = Date.utc_today.to_s)
43+
model_id, column = model_attrs(model)
44+
45+
Rollups::DailyGeo
46+
.where("#{column}": model_id, day: date_start..date_end)
47+
.select(:country_code, "SUM(count) AS count")
48+
.group(:country_code)
49+
.order(Arel.sql("SUM(count) AS count DESC"))
50+
.final
51+
.limit(10)
52+
.load_async
53+
end
54+
55+
def other_countries_rollups(model, top_country_codes, date_start = (Date.utc_today - 28.days).to_s, date_end = Date.utc_today.to_s)
56+
model_id, column = model_attrs(model)
57+
58+
Rollups::DailyGeo
59+
.where("#{column}": model_id, day: date_start..date_end)
60+
.where.not(country_code: top_country_codes)
61+
.select("'Other' AS country_code", "SUM(count) AS count")
62+
.final
63+
.load_async
64+
end
65+
66+
def top_agents_rollups(model, date_start = (Date.utc_today - 28.days).to_s, date_end = Date.utc_today.to_s)
67+
model_id, column = model_attrs(model)
68+
69+
Rollups::DailyAgent
70+
.where("#{column}": model_id, day: date_start..date_end)
71+
.select("agent_name_id AS code", "SUM(count) AS count")
72+
.group("agent_name_id AS code")
73+
.order(Arel.sql("SUM(count) AS count DESC"))
74+
.final
75+
.limit(10)
76+
.load_async
77+
end
78+
79+
def other_agents_rollups(model, top_agent_codes, date_start = (Date.utc_today - 28.days).to_s, date_end = Date.utc_today.to_s)
80+
model_id, column = model_attrs(model)
81+
82+
Rollups::DailyAgent
83+
.where("#{column}": model_id, day: date_start..date_end)
84+
.where.not(agent_name_id: top_agent_codes)
85+
.select("'Other' AS code", "SUM(count) AS count")
86+
.final
87+
.load_async
88+
end
89+
90+
private
91+
4292
def model_attrs(model)
4393
model_id = if model.is_a?(Enumerable)
4494
model.pluck(:guid)

app/controllers/episode_metrics_controller.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,35 @@ def feeds
5050
}
5151
end
5252

53-
def geos
53+
def countries
54+
top_countries = top_countries_rollups(@episode)
55+
56+
top_country_codes = top_countries.pluck(:country_code)
57+
58+
other_countries = other_countries_rollups(@episode, top_country_codes)
59+
60+
@country_rollups = []
61+
@country_rollups << top_countries
62+
@country_rollups << other_countries
63+
64+
render partial: "metrics/countries_card", locals: {
65+
countries: @country_rollups.flatten
66+
}
5467
end
5568

5669
def agents
70+
agent_apps = top_agents_rollups(@episode)
71+
top_apps_ids = agent_apps.pluck(:code)
72+
73+
other_apps = other_agents_rollups(@episode, top_apps_ids)
74+
75+
@agent_rollups = []
76+
@agent_rollups << agent_apps
77+
@agent_rollups << other_apps
78+
79+
render partial: "metrics/agent_apps_card", locals: {
80+
agents: @agent_rollups.flatten
81+
}
5782
end
5883

5984
private

app/controllers/podcast_metrics_controller.rb

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -175,28 +175,10 @@ def dropdays
175175
end
176176

177177
def countries
178-
date_start = (Date.utc_today - 28.days).to_s
179-
date_end = Date.utc_today.to_s
180-
181-
top_countries =
182-
Rollups::DailyGeo
183-
.where(podcast_id: @podcast.id, day: date_start..date_end)
184-
.select(:country_code, "SUM(count) AS count")
185-
.group(:country_code)
186-
.order(Arel.sql("SUM(count) AS count DESC"))
187-
.final
188-
.limit(10)
189-
.load_async
190-
178+
top_countries = top_countries_rollups(@podcast)
191179
top_country_codes = top_countries.pluck(:country_code)
192180

193-
other_countries =
194-
Rollups::DailyGeo
195-
.where(podcast_id: @podcast.id, day: date_start..date_end)
196-
.where.not(country_code: top_country_codes)
197-
.select("'Other' AS country_code", "SUM(count) AS count")
198-
.final
199-
.load_async
181+
other_countries = other_countries_rollups(@podcast, top_country_codes)
200182

201183
@country_rollups = []
202184
@country_rollups << top_countries
@@ -208,27 +190,10 @@ def countries
208190
end
209191

210192
def agents
211-
date_start = (Date.utc_today - 28.days).to_s
212-
date_end = Date.utc_today.to_s
213-
214-
agent_apps =
215-
Rollups::DailyAgent
216-
.where(podcast_id: @podcast.id, day: date_start..date_end)
217-
.select("agent_name_id AS code", "SUM(count) AS count")
218-
.group("agent_name_id AS code")
219-
.order(Arel.sql("SUM(count) AS count DESC"))
220-
.final
221-
.limit(10)
222-
.load_async
223-
193+
agent_apps = top_agents_rollups(@podcast)
224194
top_apps_ids = agent_apps.pluck(:code)
225-
other_apps =
226-
Rollups::DailyAgent
227-
.where(podcast_id: @podcast.id, day: date_start..date_end)
228-
.where.not(agent_name_id: top_apps_ids)
229-
.select("'Other' AS code", "SUM(count) AS count")
230-
.final
231-
.load_async
195+
196+
other_apps = other_agents_rollups(@podcast, top_apps_ids)
232197

233198
@agent_rollups = []
234199
@agent_rollups << agent_apps

app/views/episodes/_overview.html.erb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
<div class="row">
6767
<% if uses_multiple_feeds(episode) %>
68-
<div class="col d-grid mb-4">
68+
<div class="col-12 d-grid mb-4">
6969
<div class="card shadow">
7070
<div class="card-header d-flex justify-content-between align-items-center">
7171
<span class="h4 fw-bold mb-0">Downloads by Feed</span>
@@ -79,6 +79,31 @@
7979
</div>
8080
</div>
8181
<% end %>
82-
82+
<div class="col-lg-6 d-grid mb-4">
83+
<div class="card shadow">
84+
<div class="card-header d-flex justify-content-between align-items-center">
85+
<span class="h4 fw-bold mb-0">Downloads by Country</span>
86+
<span>Last 28 Days</span>
87+
</div>
88+
<div class="card-body p-0">
89+
<%= turbo_frame_tag "countries", src: countries_episode_metrics_path(episode_id: episode.guid), loading: "lazy" do %>
90+
<%= render "metrics/loading_card" %>
91+
<% end %>
92+
</div>
93+
</div>
94+
</div>
95+
<div class="col-lg-6 d-grid mb-4">
96+
<div class="card shadow">
97+
<div class="card-header d-flex justify-content-between align-items-center">
98+
<span class="h4 fw-bold mb-0">Downloads by App</span>
99+
<span>Last 28 Days</span>
100+
</div>
101+
<div class="card-body p-0">
102+
<%= turbo_frame_tag "agent_apps", src: agents_episode_metrics_path(episode_id: episode.guid), loading: "lazy" do %>
103+
<%= render "metrics/loading_card" %>
104+
<% end %>
105+
</div>
106+
</div>
107+
</div>
83108
</div>
84109
</div>

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
resource :metrics, only: [:show], controller: :episode_metrics do
4444
get "downloads"
4545
get "feeds"
46-
get "geos"
46+
get "countries"
4747
get "agents"
4848
end
4949
end

0 commit comments

Comments
 (0)