Skip to content

Commit

Permalink
Add yearly/per.. graphs + fix chartkick
Browse files Browse the repository at this point in the history
  • Loading branch information
forsbergplustwo committed Sep 19, 2023
1 parent 5afb33a commit a82bb99
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 124 deletions.
8 changes: 8 additions & 0 deletions app/helpers/metrics_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module MetricsHelper
def revenue_per_partial_for(charge_type)
if charge_type.blank?
"metrics/revenue_per"
else
"metrics/#{charge_type}/revenue_per"
end
end

def metric_display_value(selected_chart_display, value)
case selected_chart_display
when :number
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "chartkick"
import "./controllers"
import "chartkick"

// Polaris
import { registerPolarisControllers } from "polaris-view-components"
Expand Down
22 changes: 0 additions & 22 deletions app/javascript/controllers/chartkick_controller.js

This file was deleted.

3 changes: 0 additions & 3 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

import { application } from "./application"

import ChartkickController from "./chartkick_controller"
application.register("chartkick", ChartkickController)

import DatefieldController from "./datefield_controller"
application.register("datefield", DatefieldController)

Expand Down
11 changes: 11 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,15 @@ def total_revenue_per_app(date:, charge_type: nil)
metrics.where(metric_date: from_date..date)
end.group(:app_title).sum(:revenue)
end

def total_revenue_per_plan(date:, charge_type:, period: nil, app_title: nil)
per_plan_payments = payments.where(charge_type: charge_type)
per_plan_payments = per_plan_payments.where(app_title: app_title) if app_title.present?
per_plan_payments = if period.present?
per_plan_payments.where(payment_date: (date - period.days)..date)
else
per_plan_payments.where(payment_date: ..date)
end
per_plan_payments.group(:revenue).order("revenue DESC").count
end
end
10 changes: 6 additions & 4 deletions app/views/metrics/_revenue_per.html.erb
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
<%= polaris_divider %>

<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per app (12 Months)") do %>
<%= pie_chart current_user.yearly_revenue_per_product(date: filter.date) %>
<%= pie_chart current_user.yearly_revenue_per_product(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-app" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per country (12 Months)") do %>
<%= pie_chart current_user.yearly_revenue_per_country(date: filter.date) %>
<%= pie_chart current_user.yearly_revenue_per_country(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-country" %>
<% end %>
<% end %>
<% end %>

<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per charge type (12 Months)") do %>
<%= column_chart current_user.yearly_revenue_per_charge_type(date: filter.date) %>
<%= column_chart current_user.yearly_revenue_per_charge_type(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-charge-type-12-months" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per charge type (All time)") do %>
<%= column_chart current_user.total_revenue_per_charge_type(date: filter.date) %>
<%= column_chart current_user.total_revenue_per_charge_type(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-charve-type" %>
<% end %>
<% end %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/metrics/_tile.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<%= polaris_card(
title: tile.handle.to_s.humanize,
title: t("metrics.tiles.#{tile.handle}", default: tile.handle.to_s.humanize),
actions: [{
content: polaris_icon(name: "ViewMinor", color: :subdued),
title: "View chart",
Expand Down
8 changes: 2 additions & 6 deletions app/views/metrics/_tile_with_chart.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= polaris_card(title: tile.handle.to_s.humanize) do |card| %>
<%= polaris_card(title: t("metrics.tiles.#{tile.handle}", default: tile.handle.to_s.humanize)) do |card| %>

<%= polaris_vertical_stack(gap: "5") do %>

Expand All @@ -19,11 +19,7 @@

<% end %>

<div id="chart-1" style="height: 300px; width: 100%;" data-chartkick-target="chart">
<canvas style="box-sizing: border-box;" width="300"></canvas>
</div>
<%= tag.script type: "application/json", data: { chartkick_target: "data"} do %><%= tile.chart_data.to_json.html_safe %><% end %>
<%= tag.script type: "application/json", data: { chartkick_target: "options"} do %><%= metrics_chart_options.to_json.html_safe %><% end %>
<%= area_chart tile.chart_data, **metrics_chart_options %>

<%= polaris_divider %>

Expand Down
Empty file.
18 changes: 18 additions & 0 deletions app/views/metrics/affiliate_revenue/_revenue_per.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= polaris_divider %>

<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per plan (All time)") do %>
<%= column_chart current_user.total_revenue_per_plan(
date: filter.date,
charge_type: filter.charge_type,
app_title: filter.app
), id: "revenue-per-plan" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per country (12 Months)") do %>
<%= pie_chart current_user.yearly_revenue_per_country(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-country" %>
<% end %>
<% end %>
<% end %>
File renamed without changes.
Empty file.
27 changes: 27 additions & 0 deletions app/views/metrics/onetime_revenue/_revenue_per.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= polaris_divider %>

<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per app (12 Months)") do %>
<%= pie_chart current_user.yearly_revenue_per_product(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-app" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per country (12 Months)") do %>
<%= pie_chart current_user.yearly_revenue_per_country(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-country" %>
<% end %>
<% end %>
<% end %>

<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per app (12 Months)") do %>
<%= column_chart current_user.yearly_revenue_per_product(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-app-column" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per app (All time)") do %>
<%= column_chart current_user.total_revenue_per_app(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-app-total" %>
<% end %>
<% end %>
<% end %>
File renamed without changes.
31 changes: 0 additions & 31 deletions app/views/metrics/recurring/_revenue_per.html.erb

This file was deleted.

49 changes: 49 additions & 0 deletions app/views/metrics/recurring_revenue/_revenue_per.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Users per plan and app (#{filter.period} days)") do %>
<%= column_chart current_user.total_revenue_per_plan(
date: filter.date,
charge_type: filter.charge_type,
period: filter.period,
app_title: filter.app
), id: "revenue-per-plan-last-x-days" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Users per plan and app (All time)") do %>
<%= pie_chart current_user.total_revenue_per_plan(
date: filter.date,
charge_type: filter.charge_type,
app_title: filter.app
), id: "revenue-per-plan" %>
<% end %>
<% end %>
<% end %>

<%= polaris_divider %>

<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per app (12 Months)") do %>
<%= pie_chart current_user.yearly_revenue_per_product(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-app" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per country (12 Months)") do %>
<%= pie_chart current_user.yearly_revenue_per_country(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-country" %>
<% end %>
<% end %>
<% end %>

<%= polaris_layout do |layout| %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per app (12 Months)") do %>
<%= column_chart current_user.yearly_revenue_per_product(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-app-column" %>
<% end %>
<% end %>
<% layout.with_section(one_half: true) do %>
<%= polaris_card(title: "Revenue per app (All time)") do %>
<%= column_chart current_user.total_revenue_per_app(charge_type: filter.charge_type, date: filter.date), id: "revenue-per-app-total" %>
<% end %>
<% end %>
<% end %>
8 changes: 5 additions & 3 deletions app/views/metrics/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
<%= render "shared/page_actions", page: page %>

<%= turbo_frame_tag :metrics, data: {
controller: "chartkick",
action: "turbo:load@window->chartkick#createChart turbo:frame-load->chartkick#createChart",
#controller: "chartkick",
#action: "turbo:frame-load->chartkick#createChart",
} do %>

<%= polaris_vertical_stack(gap: "5") do %>

<%= @filter.charge_type %>

<%= polaris_stack(alignment: :center) do |stack| %>

<% stack.with_item(fill:true) do %>
Expand All @@ -36,7 +38,7 @@
<% end %>
<% end %>

<%= render "#{params[:controller]}/revenue_per", filter: @filter %>
<%= render revenue_per_partial_for(@filter.charge_type), filter: @filter %>

<% else %>

Expand Down
Loading

0 comments on commit a82bb99

Please sign in to comment.