Skip to content

Commit

Permalink
[admin] Add daily subs chart
Browse files Browse the repository at this point in the history
  • Loading branch information
jerodsanto committed Oct 31, 2024
1 parent 19246f7 commit 4926360
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@

<div class="ui basic segment">
<div class="ui twelve wide column">
<div class="chart" data-chart={chart_data(@podcast)}></div>
<div class="chart" data-chart={day_chart_data(@podcast)}></div>
</div>
</div>

<div class="ui basic segment">
<div class="ui twelve wide column">
<div class="chart" data-chart={month_chart_data(@podcast)}></div>
</div>
</div>

Expand All @@ -59,7 +65,7 @@
<%= for sub <- @subscriptions do %>
<tr>
<td>
<%= link to: Routes.admin_person_path(@conn, :show, sub.person) do %>
<%= link to: ~p"/admin/people/#{sub.person}" do %>
<%= if Changelog.Faker.name_fake?(sub.person.name) do %>
<%= sub.person.email %>
<% else %>
Expand Down
36 changes: 33 additions & 3 deletions lib/changelog_web/views/admin/podcast_subscription_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,37 @@ defmodule ChangelogWeb.Admin.PodcastSubscriptionView do
alias ChangelogWeb.PersonView
alias ChangelogWeb.Admin.SharedView

def chart_data(podcast) do
def day_chart_data(podcast) do
stats =
Enum.map(30..0, fn i ->
start_date = Timex.today() |> Timex.shift(days: -i)
start_time = start_date |> Timex.to_datetime() |> Timex.beginning_of_day()
end_time = start_date |> Timex.to_datetime() |> Timex.end_of_day()

subs = Subscription.subscribed_count(podcast, start_time, end_time)
unsubs = Subscription.unsubscribed_count(podcast, start_time, end_time)

%{date: start_date, subs: subs, unsubs: unsubs}
end)

data = %{
title: "Subs by Day",
categories: Enum.map(stats, &day_chart_category/1),
series: [
%{name: "Subs", data: Enum.map(stats, &round(&1.subs))},
%{name: "Unsubs", data: Enum.map(stats, & &1.unsubs)}
]
}

Jason.encode!(data)
end

defp day_chart_category(stat) do
{:ok, date} = Timex.format(stat.date, "{M}/{D}")
date
end

def month_chart_data(podcast) do
this_month = Timex.today() |> Timex.beginning_of_month()

stats =
Expand All @@ -22,7 +52,7 @@ defmodule ChangelogWeb.Admin.PodcastSubscriptionView do

data = %{
title: "Subs by Month",
categories: Enum.map(stats, &chart_category/1),
categories: Enum.map(stats, &month_chart_category/1),
series: [
%{name: "Subs", data: Enum.map(stats, & &1.subs)},
%{name: "Unsubs", data: Enum.map(stats, & &1.unsubs)}
Expand All @@ -32,7 +62,7 @@ defmodule ChangelogWeb.Admin.PodcastSubscriptionView do
Jason.encode!(data)
end

defp chart_category(stat) do
defp month_chart_category(stat) do
{:ok, date} = Timex.format(stat.date, "{Mshort} {YY}")
date
end
Expand Down

0 comments on commit 4926360

Please sign in to comment.