Skip to content

Commit

Permalink
Show history of word and page count
Browse files Browse the repository at this point in the history
  • Loading branch information
dahoo committed Jun 8, 2015
1 parent 5208ff9 commit 5b9ffa3
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 32 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ gem 'jquery-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

# Versioning
gem 'paper_trail', '~> 4.0.0.rc'

gem "highcharts-rails", "~> 4.1.5"

group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ GEM
erubis (2.7.0)
execjs (1.4.0)
multi_json (~> 1.0)
highcharts-rails (4.1.5)
railties (>= 3.1)
hike (1.2.3)
i18n (0.6.4)
jbuilder (1.5.0)
Expand All @@ -70,6 +72,10 @@ GEM
mime-types (1.23)
minitest (4.7.5)
multi_json (1.7.8)
paper_trail (4.0.0.rc1)
activerecord (>= 3.0, < 6.0)
activesupport (>= 3.0, < 6.0)
request_store (~> 1.1.0)
pg (0.15.1)
polyglot (0.3.3)
postmark (1.0.1)
Expand Down Expand Up @@ -103,6 +109,7 @@ GEM
rdoc (3.12.2)
json (~> 1.4)
ref (1.0.5)
request_store (1.1.0)
sass (3.2.10)
sass-rails (4.0.0)
railties (>= 4.0.0.beta, < 5.0)
Expand Down Expand Up @@ -141,9 +148,11 @@ PLATFORMS

DEPENDENCIES
coffee-rails (~> 4.0.0)
highcharts-rails (~> 4.1.5)
jbuilder (~> 1.2)
jquery-rails
less-rails
paper_trail (~> 4.0.0.rc)
pg
postmark-rails
rails (= 4.0.0)
Expand All @@ -154,3 +163,6 @@ DEPENDENCIES
therubyracer
twitter-bootstrap-rails!
uglifier (>= 1.3.0)

BUNDLED WITH
1.10.1
3 changes: 3 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require highcharts
//= require highcharts/highcharts-more
//= require highcharts/themes/dark-unica
//= require_tree .
119 changes: 93 additions & 26 deletions app/assets/javascripts/papers.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,33 @@ paper_etag = undefined
for sketch of sketches
updateSketch(sketch, data)

if @chart
updateTimeline(data["history"])
else
initTimeline(data["history"])
paper_etag = jqXHR.getResponseHeader("Etag")


@updateSketch = (sketch, data) ->
# Sketches aren't loaded when document is ready
# --> wait for them to load, since Processing.js provides no callback
timer = 0
timeout = 2000
clearInterval(mem)
mem = setInterval ->
instance = Processing.getInstanceById(sketch);
if instance
if not sketches[sketch]
# Load this sketch for the first time
initSketch(sketch, instance)
instance.update($.parseJSON(data["stats"]));
clearInterval(mem);
else
timer += 10
if timer > timeout
console.log("WARNING: Failed to load sketch");
clearInterval(mem);
, 10
# Sketches aren't loaded when document is ready
# --> wait for them to load, since Processing.js provides no callback
timer = 0
timeout = 2000
clearInterval(mem)
mem = setInterval ->
instance = Processing.getInstanceById(sketch);
if instance
if not sketches[sketch]
# Load this sketch for the first time
initSketch(sketch, instance)
instance.update(data["stats"]);
clearInterval(mem);
else
timer += 10
if timer > timeout
console.log("WARNING: Failed to load sketch");
clearInterval(mem);
, 10


@initSketch = (sketch, instance) ->
Expand All @@ -68,14 +72,77 @@ paper_etag = undefined
instance.setLevels(distinctLevels);
else # Do nothing

sketches[sketch] = 1
sketches[sketch] = 1

updateTimeline = (data) ->
words = []
pages = []

$.each data, (key,value) ->
words.push([Date.parse(value.time), value.words])
pages.push([Date.parse(value.time), value.pages])

@chart.series[0].update
data: words
@chart.series[1].update
data: pages

initTimeline = (data) ->
words = []
pages = []

$.each data, (key,value) ->
words.push([Date.parse(value.time), value.words])
pages.push([Date.parse(value.time), value.pages])

@chart = $('#timeline').highcharts
chart:
type: 'spline'
zoomType: 'x'
backgroundColor:'black'
title: text: ''
xAxis:
type: 'datetime'
dateTimeLabelFormats:
month: '%e. %b'
year: '%Y'
title: text: 'Date'
yAxis: [
{
title: text: 'Words'
min: 0
}
{
title: text: 'Pages'
min: 0
}
]
tooltip:
headerFormat: '<b>{series.name}</b><br>'
pointFormat: '{point.x:%e. %b}: {point.y:.2f}'
plotOptions: spline: marker: enabled: true
series: [
{
name: 'Words'
data: words
yAxis: 0
}
{
name: 'Pages'
data: pages
yAxis: 1
}
]

$ ->
if $('.visualization').length > 0 # See if we're on a paper_show page
paper_id = $('#paper_id').html()
updateSketches(paper_id)
if $('.visualization').length > 0 # See if we're on a paper_show page
Highcharts.setOptions
global:
useUTC: false

paper_id = $('#paper_id').html()
updateSketches(paper_id)

setInterval ->
updateSketches(paper_id)
, update_interval
setInterval ->
updateSketches(paper_id)
, update_interval
17 changes: 17 additions & 0 deletions app/models/paper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
class Paper < ActiveRecord::Base
has_many :applause

has_paper_trail skip: [:created_at, :updated_at]

def history
versions.map do |version|
{
time: version.created_at,
words: JSON.parse(version.reify.stats)['num_words'],
pages: JSON.parse(version.reify.stats)['pages']
}
end +
[{
time: updated_at,
words: JSON.parse(stats)['num_words'],
pages: JSON.parse(stats)['pages']
}]
end
end
6 changes: 5 additions & 1 deletion app/views/papers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= render "applause" %>

<p id="backbutton"><%= link_to 'BACK TO ALL PAPERS', papers_path %></p>
<% if @paper.alive %>
<% if @paper.alive %>
<p id="typing">I'm currently typing ... !</p>
<% end %>
<div class="container">
Expand All @@ -31,6 +31,10 @@
<canvas id="paragraphs" data-processing-sources=<%=asset_path 'paragraphs.pde'%> ></canvas>
</div>
</div>
<div class="span12 text-center">
<h3>History</h3>
<div id="timeline"/>
</div>
<div class="row">
<div class="span4 text-center">
<h3>Oxford 3000+ coverage</h3>
Expand Down
3 changes: 2 additions & 1 deletion app/views/papers/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
json.extract! @paper, :title, :stats, :created_at, :updated_at
json.extract! @paper, :title, :created_at, :updated_at, :history
json.stats JSON.parse(@paper.stats)
16 changes: 12 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131031130746) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
ActiveRecord::Schema.define(version: 20150607235217) do

create_table "applauses", force: true do |t|
t.integer "paper_id"
Expand All @@ -33,4 +30,15 @@
t.boolean "alive"
end

create_table "versions", force: true do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
end

add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id", using: :btree

end

0 comments on commit 5b9ffa3

Please sign in to comment.