diff --git a/app/assets/stylesheets/admin/stats/event_links.scss b/app/assets/stylesheets/admin/stats/event_links.scss
index 5375edc41..e781d41a6 100644
--- a/app/assets/stylesheets/admin/stats/event_links.scss
+++ b/app/assets/stylesheets/admin/stats/event_links.scss
@@ -1,14 +1,11 @@
.stats-event-links {
+ @include header-font-size(h3);
+ font-weight: bold;
+ list-style-type: none;
+ margin-#{$global-left}: 0;
margin-top: $line-height;
- ul {
- @include header-font-size(h3);
- font-weight: bold;
- list-style-type: none;
- margin-#{$global-left}: 0;
-
- * + * {
- margin-top: $line-height;
- }
+ * + * {
+ margin-top: $line-height;
}
}
diff --git a/app/components/admin/stats/event_links_component.html.erb b/app/components/admin/stats/event_links_component.html.erb
index 7a1181571..b0c861d71 100644
--- a/app/components/admin/stats/event_links_component.html.erb
+++ b/app/components/admin/stats/event_links_component.html.erb
@@ -1,4 +1 @@
-
-
<%= title %>
- <%= link_list(*links) %>
-
+<%= link_list(*links, class: "stats-event-links") %>
diff --git a/app/components/admin/stats/event_links_component.rb b/app/components/admin/stats/event_links_component.rb
index dbe79b26f..6f7480406 100644
--- a/app/components/admin/stats/event_links_component.rb
+++ b/app/components/admin/stats/event_links_component.rb
@@ -12,10 +12,6 @@ class Admin::Stats::EventLinksComponent < ApplicationComponent
Ahoy::Chart.new(event).title
end
- def title
- t("admin.stats.graph.title")
- end
-
def links
event_names.map do |event|
[link_text(event), graph_admin_stats_path(event: event)]
diff --git a/app/components/admin/stats/global_chart_component.html.erb b/app/components/admin/stats/global_chart_component.html.erb
new file mode 100644
index 000000000..10a1368e5
--- /dev/null
+++ b/app/components/admin/stats/global_chart_component.html.erb
@@ -0,0 +1,5 @@
+
+
<%= title %>
+ <%= chart_tag %>
+ <%= render Admin::Stats::EventLinksComponent.new(event_names) %>
+
diff --git a/app/components/admin/stats/global_chart_component.rb b/app/components/admin/stats/global_chart_component.rb
new file mode 100644
index 000000000..ae4ecec1f
--- /dev/null
+++ b/app/components/admin/stats/global_chart_component.rb
@@ -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
diff --git a/app/controllers/admin/stats_controller.rb b/app/controllers/admin/stats_controller.rb
index 4cd913f27..3313cebd0 100644
--- a/app/controllers/admin/stats_controller.rb
+++ b/app/controllers/admin/stats_controller.rb
@@ -1,7 +1,5 @@
class Admin::StatsController < Admin::BaseController
def show
- @event_names = Ahoy::Chart.active_event_names
-
@visits = Visit.count
@debates = Debate.with_hidden.count
@proposals = Proposal.with_hidden.count
diff --git a/app/models/ahoy/chart.rb b/app/models/ahoy/chart.rb
index 033a55c56..d2077390d 100644
--- a/app/models/ahoy/chart.rb
+++ b/app/models/ahoy/chart.rb
@@ -9,7 +9,9 @@ module Ahoy
end
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
def self.event_names_with_collections
@@ -23,10 +25,19 @@ module Ahoy
}
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
ds = Ahoy::DataSource.new
- ds.add title, records_by_day.count
-
+ ds.add title, data
ds.build
end
@@ -34,6 +45,10 @@ module Ahoy
t("admin.stats.graph.#{event_name}")
end
+ def data
+ records_by_day.count
+ end
+
private
def records
diff --git a/app/views/admin/stats/show.html.erb b/app/views/admin/stats/show.html.erb
index 7c06341ef..b6eccdbc0 100644
--- a/app/views/admin/stats/show.html.erb
+++ b/app/views/admin/stats/show.html.erb
@@ -1,3 +1,7 @@
+<% content_for :head do %>
+ <%= javascript_include_tag "stat_graphs", "data-turbolinks-track" => "reload" %>
+<% end %>
+
@@ -110,7 +114,7 @@
- <%= render Admin::Stats::EventLinksComponent.new(@event_names) %>
+ <%= render Admin::Stats::GlobalChartComponent.new %>
diff --git a/spec/components/admin/stats/event_links_component_spec.rb b/spec/components/admin/stats/event_links_component_spec.rb
index 847c2047a..489f22e1d 100644
--- a/spec/components/admin/stats/event_links_component_spec.rb
+++ b/spec/components/admin/stats/event_links_component_spec.rb
@@ -1,12 +1,11 @@
require "rails_helper"
describe Admin::Stats::EventLinksComponent do
- it "renders an heading followed by a list of links" do
+ it "renders a list of links" do
render_inline Admin::Stats::EventLinksComponent.new(
%w[legislation_annotation_created legislation_answer_created]
)
- expect(page).to have_css "h2", exact_text: "Graphs"
expect(page).to have_link count: 2
page.find("ul") do |list|
diff --git a/spec/components/admin/stats/global_chart_component_spec.rb b/spec/components/admin/stats/global_chart_component_spec.rb
new file mode 100644
index 000000000..8d8eb63d6
--- /dev/null
+++ b/spec/components/admin/stats/global_chart_component_spec.rb
@@ -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 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
diff --git a/spec/models/ahoy/chart_spec.rb b/spec/models/ahoy/chart_spec.rb
index 3791aff51..e69d9d13e 100644
--- a/spec/models/ahoy/chart_spec.rb
+++ b/spec/models/ahoy/chart_spec.rb
@@ -1,6 +1,34 @@
require "rails_helper"
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
it "raises an exception for unknown events" do
chart = Ahoy::Chart.new(:mystery)