Display some stats
This commit is contained in:
4
app/controllers/api/api_controller.rb
Normal file
4
app/controllers/api/api_controller.rb
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
class Api::ApiController < ApplicationController
|
||||||
|
before_action :authenticate_user!
|
||||||
|
protect_from_forgery with: :null_session
|
||||||
|
end
|
||||||
10
app/controllers/api/stats_controller.rb
Normal file
10
app/controllers/api/stats_controller.rb
Normal file
@@ -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
|
||||||
5
app/controllers/stats_controller.rb
Normal file
5
app/controllers/stats_controller.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class StatsController < ApplicationController
|
||||||
|
def show
|
||||||
|
@event_types = Ahoy::Event.select(:name).uniq.pluck(:name)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
<%= stylesheet_link_tag "application" %>
|
<%= stylesheet_link_tag "application" %>
|
||||||
<%= javascript_include_tag "vendor/modernizr" %>
|
<%= javascript_include_tag "vendor/modernizr" %>
|
||||||
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
|
<%= javascript_include_tag "application", 'data-turbolinks-track' => true %>
|
||||||
|
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
|
||||||
<%= csrf_meta_tags %>
|
<%= csrf_meta_tags %>
|
||||||
<script src="https://www.google.com/recaptcha/api.js?hl=<%= I18n.locale %>"></script>
|
<script src="https://www.google.com/recaptcha/api.js?hl=<%= I18n.locale %>"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
9
app/views/stats/show.html.erb
Normal file
9
app/views/stats/show.html.erb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<h1>Stats</h1>
|
||||||
|
|
||||||
|
<h3>Visits</h3>
|
||||||
|
<%= line_chart Visit.group_by_day(:started_at).count %>
|
||||||
|
|
||||||
|
<% @event_types.each do |event_type| %>
|
||||||
|
<h3><%= event_type.titleize %></h3>
|
||||||
|
<%= line_chart api_stats_path(event: event_type) %>
|
||||||
|
<% end %>
|
||||||
@@ -21,6 +21,11 @@ Rails.application.routes.draw do
|
|||||||
end
|
end
|
||||||
|
|
||||||
resource :account, controller: "account", only: [:show, :update]
|
resource :account, controller: "account", only: [:show, :update]
|
||||||
|
resource :stats, only: [:show]
|
||||||
|
|
||||||
|
namespace :api do
|
||||||
|
resource :stats, only: [:show]
|
||||||
|
end
|
||||||
|
|
||||||
# Example of regular route:
|
# Example of regular route:
|
||||||
# get 'products/:id' => 'catalog#view'
|
# get 'products/:id' => 'catalog#view'
|
||||||
|
|||||||
44
spec/controllers/api/stats_controller_spec.rb
Normal file
44
spec/controllers/api/stats_controller_spec.rb
Normal file
@@ -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
|
||||||
@@ -31,4 +31,9 @@ FactoryGirl.define do
|
|||||||
debate
|
debate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :ahoy_event, :class => Ahoy::Event do
|
||||||
|
id { SecureRandom.uuid }
|
||||||
|
time DateTime.now
|
||||||
|
sequence(:name) {|n| "Event #{n} type"}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user