Permalink
Browse files

Merge branch 'dashboard_v2' of https://github.com/mbrookes/upmin-admi…

…n-ruby into mbrookes-dashboard_v2

Conflicts:
	app/views/layouts/upmin/application.html.haml
  • Loading branch information...
jdurand committed Apr 17, 2015
2 parents bb20ea2 + 0baef22 commit d4fbfcae6dba85e7607a4c50cb6e2e0bdab148e0
View
@@ -1,3 +1,5 @@
source "https://rubygems.org"
gemspec
@@ -13,4 +13,3 @@
*= require_tree .
*= require_self
*/
@@ -0,0 +1,16 @@
.panel .panel-heading {
border-bottom: 1px solid #d7d7d7;
color: #666;
font-size: 14px;
font-weight: 200;
padding: 7px 10px 4px;
}
.panel .panel-footer {
background: #fbfbfb;
border-top: 1px solid #e2e2e2;
color: #808080;
font-size: 12px;
padding: 8px 10px 5px;
}
@@ -0,0 +1,11 @@
require_dependency "upmin/application_controller"
module Upmin
class DashboardController < ApplicationController
def index
@models = Upmin::Model.all
end
end
end
@@ -12,9 +12,6 @@ class ModelsController < ApplicationController
before_filter :set_action, only: [:action]
before_filter :set_arguments, only: [:action]
def dashboard
end
# GET /:model_name/:id
def show
end
@@ -146,6 +143,10 @@ def set_klass
@klass = Upmin::Model.find_class(params[:klass])
end
def set_klasses
@klasses = Upmin::Model.find_class(params[:klass])
end
def set_action
action_name = params[:method]
@action = @model.actions.select{ |action| action.name.to_s == action_name }.first
@@ -9,9 +9,12 @@
= javascript_include_tag("upmin/application")
= csrf_meta_tags
%link{href: "//cdn.jsdelivr.net/bootstrap/3.2.0/css/bootstrap.min.css", rel: "stylesheet", type: "text/css"}
%link{href: "//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css", rel: "stylesheet"}
%link{href: "//cdn.jsdelivr.net/bootstrap/3.2.0/css/bootstrap.min.css", rel: "stylesheet"}
-#%link{href: "//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css", rel: "stylesheet"}
%script{src: "//cdn.jsdelivr.net/bootstrap/3.2.0/js/bootstrap.min.js"}
%script{src: "//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"}
-# TODO (matt): Only include this on the dashboard page:
= javascript_include_tag "//www.google.com/jsapi", "chartkick"
- if content_for?(:head)
= yield(:head)
@@ -0,0 +1,9 @@
-limit ||= 30
.col-sm-6.col-md-4
.panel.panel-default
.panel-heading
#{model.humanized_name} created per #{model.model_class.grouping limit}
.panel-body
= column_chart model.model_class.group_by_best_fit limit
.panel-footer
Number of #{model.model_name.plural} created per #{model.model_class.grouping limit}, for all #{model.count} records.
@@ -0,0 +1,14 @@
-if defined?(DataMapper)
.container
.row
.jumbotron
%h2
Sorry, the default dashboard doesn't support DataMapper
%p
Please create a custom dashboard view in:
%code
app/views/upmin/dashboard/index.html.[haml|erb|etc]
-else
.container-fluid
.row
= render partial: 'chart', collection: @models, as: 'model', locals: { limit: 30 }

This file was deleted.

Oops, something went wrong.
View
@@ -1,9 +1,6 @@
Upmin::Engine.routes.draw do
root to: "models#dashboard"
# TODO(jon): Add support for dashboards (or some other main page).
# TODO(jon): Move dashboards to an appropriate controller
get "/", as: :upmin_dashboard, controller: :models, action: :dashboard
root to: "dashboard#index"
get "/", as: :upmin_dashboard, controller: :dashboard, action: :index
scope "m" do
scope "/:klass" do
View
@@ -29,14 +29,15 @@
require "upmin/railties/paginator"
require "upmin/railties/render"
require "upmin/railties/render_helpers"
require "upmin/railties/dashboard"
require "upmin/railtie"
# gems and stuff we use
require "jquery-rails"
require "ransack"
require "haml"
require "sass-rails"
require "chartkick"
require "ostruct"
View
@@ -7,6 +7,7 @@ class Railtie < Rails::Railtie
ActiveSupport.on_load(:active_record) do
if defined?(ActiveRecord)
::ActiveRecord::Base.send(:include, Upmin::Railties::ActiveRecord)
::ActiveRecord::Base.send(:extend, Upmin::Railties::Dashboard)
end
if defined?(DataMapper)
@@ -0,0 +1,91 @@
module Upmin::Railties
module Dashboard
def group_by_best_fit(limit = 30)
return send "group_by_#{grouping limit}"
end
# Selects the time period with no more than <limit> entries
def grouping(limit = 30)
seconds = range_in_seconds
if seconds/1.day < limit
return 'day'
elsif seconds/1.week < limit
return 'week'
elsif seconds/1.month < limit
return 'month'
else
return 'year'
end
end
#
# Date range manipulation
#
def range_in_seconds
return last_date - first_date
end
def first_date
order('date(created_at) ASC').first.try(:created_at) || Time.now
end
def last_date
order('date(created_at) ASC').last.try(:created_at) || Time.now
end
#
# Group by
#
def group_by_day
dates = where.not('created_at' => nil).group('date(created_at)').order('date(created_at) ASC').count
# Convert sqlite String date keys to Date keys
dates.map! { |k, v| [Date.parse(k), v] } if dates.keys.first.is_a? String
return dates
end
def group_by_week
result = Hash.new(0)
group_by_day.each_with_object(result) { |i, a| a[i[0].beginning_of_week.strftime] += i[1] }
return result
end
def group_by_month
return group_by_strftime('%b %Y')
end
def group_by_year
return group_by_strftime('%Y')
end
#
# Aggregate by
#
def group_by_day_of_week
template = Hash[Date::ABBR_DAYNAMES.map {|x| [x, 0]}]
return group_by_strftime('%a', template)
end
def group_by_day_of_month
return Hash[group_by_strftime('%d').sort]
end
def group_by_week_of_year
return Hash[group_by_strftime('%W').sort]
end
def group_by_month_of_year
template = Hash[Date::ABBR_MONTHNAMES.map {|x| [x, 0]}]
template.shift
return group_by_strftime( '%b', template)
end
def group_by_strftime(filter, result = Hash.new(0))
group_by_day.each_with_object(result) { |i, a| a[i[0].strftime(filter)] += i[1] }
return result
end
end
end
View
@@ -27,6 +27,10 @@ Gem::Specification.new do |s|
s.add_dependency("sass-rails")
s.add_dependency("kaminari")
s.add_dependency("ransack")
s.add_dependency('chartkick')
s.add_dependency('groupdate')
#s.add_runtime_dependency('dateslice')
#s.add_dependency('by_star')
s.add_development_dependency("sqlite3")
s.add_development_dependency("capybara")

0 comments on commit d4fbfca

Please sign in to comment.