Permalink
Browse files

Clean up Gemfile, move chart/date helper code to lib'

  • Loading branch information...
mbrookes
mbrookes committed Nov 17, 2014
1 parent c615482 commit bbb33cdeb4478d2f42b09e83150ab717ee82a819
Showing with 97 additions and 99 deletions.
  1. +2 −4 Gemfile
  2. +5 −90 app/helpers/upmin/dashboard_helper.rb
  3. +2 −2 app/views/upmin/dashboard/_chart.html.haml
  4. +1 −3 lib/upmin/admin.rb
  5. +87 −0 lib/upmin/dashboard.rb
View
@@ -1,7 +1,5 @@
source "https://rubygems.org"
gemspec
# TODO (matt): Move to gemspec
#gem 'dateslices', path: '../../gems/dateslices', branch: 'output_format'
#gem 'by_star'
#gem 'groupdate', github:'mieko/groupdate', branch: 'sqlite3'
@@ -3,98 +3,13 @@ module DashboardHelper
# call the by_<time> method according to the time range of the model
def by_best_fit(model, limit = 30)
@model = model
return send "by_#{group_by model, limit}"
model = Upmin::DashboardData.new(model)
return model.send "by_#{model.group_by_range limit}"
end
# Selects the time period with no more than <limit> entries
def group_by(model, limit = 30)
@model = model
seconds = date_range_in_seconds
# if seconds < limit
# return 'second'
# elsif seconds/1.minute < limit
# return 'minute'
# elsif seconds/1.hour < limit
# return 'hour'
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 date_range_in_seconds
return last_date - first_date
end
def date_range
return first_date..last_date
end
def first_date
return @model.order('date(created_at) ASC').first.try(:created_at) || Time.now
end
def last_date
return @model.order('date(created_at) ASC').last.try(:created_at) || Time.now
end
#
# Group by
#
def by_day
dates = @model.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 by_week
result = Hash.new(0)
by_day.each_with_object(result) { |i, a| a[i[0].beginning_of_week.strftime] += i[1] }
return result
end
def by_month
return group_by_strftime('%b %Y')
end
def by_year
return group_by_strftime('%Y')
end
#
# Aggregate by
#
def by_day_of_week
template = Hash[Date::ABBR_DAYNAMES.map {|x| [x, 0]}]
return group_by_strftime('%a', template)
end
def by_week_of_year
return group_by_strftime('%W')
end
def 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))
by_day.each_with_object(result) { |i, a| a[i[0].strftime(filter)] += i[1] }
return result
def grouped_by(model, limit = 30)
model = Upmin::DashboardData.new(model)
return model.group_by_range limit
end
end
@@ -2,8 +2,8 @@
.col-sm-6.col-md-4
.chart-wrapper
.chart-title
#{model.humanized_name} created per #{group_by model.model_class, limit}
#{model.humanized_name} created per #{grouped_by model.model_class, limit}
.chart-stage
= column_chart by_best_fit(model.model_class, limit)
.chart-notes
Number of #{model.model_name.plural} created per #{group_by model.model_class, limit}, for all #{model.model_class.count} records.
Number of #{model.model_name.plural} created per #{grouped_by model.model_class, limit}, for all #{model.model_class.count} records.
View
@@ -12,6 +12,7 @@
require "upmin/action"
require "upmin/parameter"
require "upmin/query"
require "upmin/dashboard"
# ActiveRecord Specifics.
if defined?(ActiveRecord)
@@ -37,9 +38,6 @@
require "haml"
require "sass-rails"
require "chartkick"
#require 'dateslices'
#require 'by_star'
require "groupdate"
require "ostruct"
View
@@ -0,0 +1,87 @@
module Upmin
class DashboardData
def initialize(model)
@model = model
end
# Selects the time period with no more than <limit> entries
def group_by_range(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
return @model.order('date(created_at) ASC').first.try(:created_at) || Time.now
end
def last_date
return @model.order('date(created_at) ASC').last.try(:created_at) || Time.now
end
#
# Group by
#
def by_day
dates = @model.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 by_week
result = Hash.new(0)
by_day.each_with_object(result) { |i, a| a[i[0].beginning_of_week.strftime] += i[1] }
return result
end
def by_month
return group_by_strftime('%b %Y')
end
def by_year
return group_by_strftime('%Y')
end
#
# Aggregate by
#
def by_day_of_week
template = Hash[Date::ABBR_DAYNAMES.map {|x| [x, 0]}]
return group_by_strftime('%a', template)
end
def by_week_of_year
return group_by_strftime('%W')
end
def 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))
by_day.each_with_object(result) { |i, a| a[i[0].strftime(filter)] += i[1] }
return result
end
end
end

0 comments on commit bbb33cd

Please sign in to comment.