Add a chart showing all events in admin stats

Not sure it's useful, but it makes the main admin stats page a bit more
interesting.
This commit is contained in:
Javi Martín
2024-04-25 02:11:16 +02:00
parent 14454bdd45
commit b5d12959a0
11 changed files with 101 additions and 25 deletions

View File

@@ -1,14 +1,11 @@
.stats-event-links { .stats-event-links {
@include header-font-size(h3);
font-weight: bold;
list-style-type: none;
margin-#{$global-left}: 0;
margin-top: $line-height; margin-top: $line-height;
ul { * + * {
@include header-font-size(h3); margin-top: $line-height;
font-weight: bold;
list-style-type: none;
margin-#{$global-left}: 0;
* + * {
margin-top: $line-height;
}
} }
} }

View File

@@ -1,4 +1 @@
<div class="stats-event-links"> <%= link_list(*links, class: "stats-event-links") %>
<h2><%= title %></h2>
<%= link_list(*links) %>
</div>

View File

@@ -12,10 +12,6 @@ class Admin::Stats::EventLinksComponent < ApplicationComponent
Ahoy::Chart.new(event).title Ahoy::Chart.new(event).title
end end
def title
t("admin.stats.graph.title")
end
def links def links
event_names.map do |event| event_names.map do |event|
[link_text(event), graph_admin_stats_path(event: event)] [link_text(event), graph_admin_stats_path(event: event)]

View File

@@ -0,0 +1,5 @@
<div class="stats-global-chart">
<h2><%= title %></h2>
<%= chart_tag %>
<%= render Admin::Stats::EventLinksComponent.new(event_names) %>
</div>

View File

@@ -0,0 +1,19 @@
class Admin::Stats::GlobalChartComponent < ApplicationComponent
private
def event_names
Ahoy::Chart.active_event_names
end
def chart_tag
tag.div("data-graph": data_points.to_json)
end
def data_points
Ahoy::Chart.active_events_data_points
end
def title
t("admin.stats.graph.title")
end
end

View File

@@ -1,7 +1,5 @@
class Admin::StatsController < Admin::BaseController class Admin::StatsController < Admin::BaseController
def show def show
@event_names = Ahoy::Chart.active_event_names
@visits = Visit.count @visits = Visit.count
@debates = Debate.with_hidden.count @debates = Debate.with_hidden.count
@proposals = Proposal.with_hidden.count @proposals = Proposal.with_hidden.count

View File

@@ -9,7 +9,9 @@ module Ahoy
end end
def self.active_event_names def self.active_event_names
event_names_with_collections.select { |name, collection| collection.any? }.keys event_names_with_collections.select { |name, collection| collection.any? }.keys.sort_by do |event_name|
new(event_name).title
end
end end
def self.event_names_with_collections def self.event_names_with_collections
@@ -23,10 +25,19 @@ module Ahoy
} }
end end
def self.active_events_data_points
ds = Ahoy::DataSource.new
active_event_names.map { |event_name| new(event_name) }.each do |chart|
ds.add chart.title, chart.data
end
ds.build
end
def data_points def data_points
ds = Ahoy::DataSource.new ds = Ahoy::DataSource.new
ds.add title, records_by_day.count ds.add title, data
ds.build ds.build
end end
@@ -34,6 +45,10 @@ module Ahoy
t("admin.stats.graph.#{event_name}") t("admin.stats.graph.#{event_name}")
end end
def data
records_by_day.count
end
private private
def records def records

View File

@@ -1,3 +1,7 @@
<% content_for :head do %>
<%= javascript_include_tag "stat_graphs", "data-turbolinks-track" => "reload" %>
<% end %>
<div id="stats" class="stats"> <div id="stats" class="stats">
<div class="row"> <div class="row">
<div class="small-12 column"> <div class="small-12 column">
@@ -110,7 +114,7 @@
</div> </div>
<%= render Admin::Stats::EventLinksComponent.new(@event_names) %> <%= render Admin::Stats::GlobalChartComponent.new %>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -1,12 +1,11 @@
require "rails_helper" require "rails_helper"
describe Admin::Stats::EventLinksComponent do describe Admin::Stats::EventLinksComponent do
it "renders an <h2> heading followed by a list of links" do it "renders a list of links" do
render_inline Admin::Stats::EventLinksComponent.new( render_inline Admin::Stats::EventLinksComponent.new(
%w[legislation_annotation_created legislation_answer_created] %w[legislation_annotation_created legislation_answer_created]
) )
expect(page).to have_css "h2", exact_text: "Graphs"
expect(page).to have_link count: 2 expect(page).to have_link count: 2
page.find("ul") do |list| page.find("ul") do |list|

View File

@@ -0,0 +1,18 @@
require "rails_helper"
describe Admin::Stats::GlobalChartComponent do
before do
allow(Ahoy::Chart).to receive(:active_event_names).and_return(
%i[budget_investment_created level_3_user]
)
end
it "renders an <h2> tag with a graph and links" do
render_inline Admin::Stats::GlobalChartComponent.new
expect(page).to have_css "h2", exact_text: "Graphs"
expect(page).to have_css "[data-graph]"
expect(page).to have_link "Budget investments created"
expect(page).to have_link "Level 3 users verified"
end
end

View File

@@ -1,6 +1,34 @@
require "rails_helper" require "rails_helper"
describe Ahoy::Chart do describe Ahoy::Chart do
describe ".active_events_data_points" do
it "groups several events together" do
create(:budget_investment, created_at: "2010-01-01")
create(:budget_investment, created_at: "2010-01-02")
create(:legislation_annotation, created_at: "2010-01-01")
create(:legislation_annotation, created_at: "2010-01-03")
expect(Ahoy::Chart.active_events_data_points).to eq({
x: ["2010-01-01", "2010-01-02", "2010-01-03"],
"Budget investments created" => [1, 1, 0],
"Legislation annotations created" => [1, 0, 1]
})
end
it "sorts events alphabetically" do
create(:budget_investment, created_at: "2010-01-01")
create(:legislation_annotation, created_at: "2010-01-01")
I18n.with_locale(:es) do
expect(Ahoy::Chart.active_events_data_points.keys).to eq([
:x,
"Anotaciones creadas",
"Proyectos de gasto creados"
])
end
end
end
describe "#data_points" do describe "#data_points" do
it "raises an exception for unknown events" do it "raises an exception for unknown events" do
chart = Ahoy::Chart.new(:mystery) chart = Ahoy::Chart.new(:mystery)