From fe2080aff9a79bd7081ddb9051f19ceddd5ce03f Mon Sep 17 00:00:00 2001 From: Eloy Gomez Date: Sat, 8 Aug 2015 14:33:44 +0200 Subject: [PATCH] Display some stats --- app/controllers/api/api_controller.rb | 4 ++ app/controllers/api/stats_controller.rb | 10 +++++ app/controllers/stats_controller.rb | 5 +++ app/views/layouts/application.html.erb | 3 +- app/views/stats/show.html.erb | 9 ++++ config/routes.rb | 5 +++ spec/controllers/api/stats_controller_spec.rb | 44 +++++++++++++++++++ spec/factories.rb | 7 ++- 8 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 app/controllers/api/api_controller.rb create mode 100644 app/controllers/api/stats_controller.rb create mode 100644 app/controllers/stats_controller.rb create mode 100644 app/views/stats/show.html.erb create mode 100644 spec/controllers/api/stats_controller_spec.rb diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb new file mode 100644 index 000000000..0534444c0 --- /dev/null +++ b/app/controllers/api/api_controller.rb @@ -0,0 +1,4 @@ +class Api::ApiController < ApplicationController + before_action :authenticate_user! + protect_from_forgery with: :null_session +end diff --git a/app/controllers/api/stats_controller.rb b/app/controllers/api/stats_controller.rb new file mode 100644 index 000000000..26fb373fb --- /dev/null +++ b/app/controllers/api/stats_controller.rb @@ -0,0 +1,10 @@ +class Api::StatsController < Api::ApiController + def show + event_type = params[:event] + unless event_type.present? + return render json: {}, status: :bad_request + end + + render json: Ahoy::Event.where(name: event_type).group_by_day(:time).count + end +end diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb new file mode 100644 index 000000000..c33f6843e --- /dev/null +++ b/app/controllers/stats_controller.rb @@ -0,0 +1,5 @@ +class StatsController < ApplicationController + def show + @event_types = Ahoy::Event.select(:name).uniq.pluck(:name) + end +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 36a3c44cc..5c6324e31 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,6 +8,7 @@ <%= stylesheet_link_tag "application" %> <%= javascript_include_tag "vendor/modernizr" %> <%= javascript_include_tag "application", 'data-turbolinks-track' => true %> + <%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %> <%= csrf_meta_tags %> @@ -27,4 +28,4 @@ <%= render 'layouts/footer' %> - \ No newline at end of file + diff --git a/app/views/stats/show.html.erb b/app/views/stats/show.html.erb new file mode 100644 index 000000000..0671b15ad --- /dev/null +++ b/app/views/stats/show.html.erb @@ -0,0 +1,9 @@ +

Stats

+ +

Visits

+<%= line_chart Visit.group_by_day(:started_at).count %> + +<% @event_types.each do |event_type| %> +

<%= event_type.titleize %>

+ <%= line_chart api_stats_path(event: event_type) %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 73a77f60e..6cb8dcba6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,11 @@ Rails.application.routes.draw do end resource :account, controller: "account", only: [:show, :update] + resource :stats, only: [:show] + + namespace :api do + resource :stats, only: [:show] + end # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/spec/controllers/api/stats_controller_spec.rb b/spec/controllers/api/stats_controller_spec.rb new file mode 100644 index 000000000..e2f4cd02b --- /dev/null +++ b/spec/controllers/api/stats_controller_spec.rb @@ -0,0 +1,44 @@ +require 'rails_helper' + +describe Api::StatsController do + + # GET index + #---------------------------------------------------------------------- + + describe 'GET index' do + let(:user) { create :user } + + context 'event not present' do + it 'should respond with bad_request' do + sign_in user + get :show + expect(response.status).to eq 400 + end + end + + context 'event present' do + it 'should return event counts' do + time_1 = DateTime.yesterday.to_datetime + time_2 = DateTime.now + + event_1 = create :ahoy_event, name: 'foo', time: time_1 + event_2 = create :ahoy_event, name: 'foo', time: time_1 + event_3 = create :ahoy_event, name: 'foo', time: time_2 + event_4 = create :ahoy_event, name: 'bar' + + sign_in user + get :show, event: 'foo' + + expect(response).to be_ok + + data = JSON.parse(response.body) + dates = data.keys + + expect(DateTime.parse dates.first).to eq time_1.utc.beginning_of_day + expect(DateTime.parse dates.last).to eq time_2.utc.beginning_of_day + expect(data[dates.first]).to eq 2 + expect(data[dates.last]).to eq 1 + end + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 8e1a2ea6c..18b65b7f3 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -31,4 +31,9 @@ FactoryGirl.define do debate end -end \ No newline at end of file + factory :ahoy_event, :class => Ahoy::Event do + id { SecureRandom.uuid } + time DateTime.now + sequence(:name) {|n| "Event #{n} type"} + end +end