Skip to content

Commit

Permalink
Play with highstock
Browse files Browse the repository at this point in the history
  • Loading branch information
redox committed Dec 6, 2013
1 parent 5726c10 commit 5d4305d
Show file tree
Hide file tree
Showing 10 changed files with 479 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gem 'thin'
gem 'rails-api'
gem 'active_model_serializers', github: "rails-api/active_model_serializers"
gem 'delayed_job_active_record'
gem 'groupdate', git: 'https://github.com/mieko/groupdate.git', branch: 'sqlite3'

group :development do
gem 'sqlite3'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ GIT
active_model_serializers (0.9.0.pre)
activemodel (>= 3.2)

GIT
remote: https://github.com/mieko/groupdate.git
revision: f75a3d47a27a7351998555a5bfe532a7be9248ed
branch: sqlite3
specs:
groupdate (0.1.6)
activerecord (>= 3.0.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -212,6 +220,7 @@ DEPENDENCIES
delayed_job_active_record
factory_girl_rails
figaro
groupdate!
haml-rails
html2haml
jbuilder (~> 1.2)
Expand Down
353 changes: 353 additions & 0 deletions app/assets/javascripts/highstock.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class StatsController < ApplicationController
def index
@stories = Item.stories_per_hour_since(1.month.ago)
@comments = Item.comments_per_hour_since(1.month.ago)
end
end
12 changes: 12 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,22 @@ def resolve_parent!
self.save
end

def self.stories_per_hour_since(ago)
per_hour_since(Item.story, ago)
end

def self.comments_per_hour_since(ago)
per_hour_since(Item.comment, ago)
end

private
def after_create_tasks
self.delay(priority: 0).resolve_parent! # 0 = top priority
self.delay(priority: 1).crawl_thumbnail!
end

def self.per_hour_since(item_type, ago)
Item.where(item_type_cd: item_type).where('created_at > ?', ago).group_by_hour(:created_at).count.map { |k,v| [k.is_a?(String) ? DateTime.parse(k) : k, v] }
end

end
1 change: 1 addition & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
%ul.list-inline
%li= link_to 'About', about_path
%li= link_to 'Search', root_path
/%li= link_to 'Stats', stats_path
%li= link_to 'API', api_path
%li= link_to 'Cool Apps', cool_apps_path
%li
Expand Down
86 changes: 86 additions & 0 deletions app/views/stats/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
- content_for :head do
= javascript_include_tag "highstock"

%h1
Stats
%span.text-mini.label.label-warning BETA

%p WIP

%h3 Activity per hour
%div{id: 'stories_per_hour'}
:javascript
$('#stories_per_hour').highcharts('StockChart', {
legend: {
enabled: true
},

credits : {
enabled : false
},

exporting : {
enabled : false
},

rangeSelector : {
enabled : false,
},

scrollbar : {
enabled : false
},

navigator : {
enabled : true,
},

xAxis: {
gridLineColor : '#eee',
labels: {
style : {
color: '#AAAAAC'
}
},
lineColor: '#E0E0E0',
},

yAxis: [
{
gridLineColor : '#eee',
labels: {
style : {
color: '#AAAAAC'
},
enabled : true
},
min: 0.0,
},
{
gridLineColor : '#eee',
labels: {
style : {
color: '#AAAAAC'
},
enabled : true
},
min: 0.0,
opposite: true
},
],

series : [
{
name : '# Stories',
data : #{@stories.map { |v| [v[0].to_time.to_i * 1000, v[1]] }.inspect},
type : 'line'
},
{
name : '# Comments',
data : #{@comments.map { |v| [v[0].to_time.to_i * 1000, v[1]] }.inspect},
type : 'line',
yAxis: 1
}
],
});

1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.assets.precompile += %w(highstock.js)
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get 'about' => 'pages#about'
get 'api' => 'pages#api'
get 'cool_apps' => 'pages#cool_apps'
get 'stats' => 'stats#index'

root 'home#index'

Expand Down
9 changes: 9 additions & 0 deletions test/controllers/stats_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class StatsControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end

end

0 comments on commit 5d4305d

Please sign in to comment.