Skip to content

Commit

Permalink
Merge a96ef06 into efcae9c
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Pikesley committed Jan 2, 2016
2 parents efcae9c + a96ef06 commit fdc52eb
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 13 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
* Check for cache expiry (timecop!)
* Give things an expiry, change colour / move them up the list when they're due
* Individual URLs per dataset, maybe iframe the dashboards ala Sir Handel
* Front page with high-priority things?
Expand Down
2 changes: 2 additions & 0 deletions config/lookups.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
catface:
url: /catface
title: Catface
repo: pikesley/catface

snake-data:
url: /snake
title: Snake
repo: pikesley/snake-data

datasets:
length:
Expand Down
11 changes: 11 additions & 0 deletions features/json.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Get JSON

Background:
Given I send and accept JSON

@vcr
Scenario: Get JSON for a dataset
When I send a GET request to "catface/flea-treatment"
Then the response status should be "200"
And the JSON response should have "$.title" with the text "Flea Treatment"
And the JSON response should have "$.data[0].Date" with the text "2015-12-03"
21 changes: 21 additions & 0 deletions features/support/vcr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'vcr'
require 'webmock/cucumber'

VCR.configure do |c|
c.default_cassette_options = { :record => :once }

c.cassette_library_dir = 'features/support/vcr'
c.hook_into :webmock
c.allow_http_connections_when_no_cassette = false

[
'GITHUB_CLIENT_ID',
'GITHUB_CLIENT_SECRET'
].each do |env_var|
c.filter_sensitive_data("<#{env_var}>") { ENV[env_var] }
end
end

VCR.cucumber_tags do |t|
t.tag '@vcr', use_scenario_name: true
end
10 changes: 5 additions & 5 deletions lib/assets/css/application.scss
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//@import 'bootstrap';

#numbers {
#data {
text-align: center;
}

#numbers h1, h2, h3, h4 {
#data h1, h2, h3, h4 {
padding: 0px;
margin: 0px;
}

#numbers h1 {
#data h1 {
font-size: 1.5em;
}

#numbers h3 {
#data h3 {
font-size: 1em;
}

#numbers div {
#data div {
padding: 5px;
}
9 changes: 2 additions & 7 deletions lib/assets/javascripts/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
function cellContent(id, data) {
fixedDate = moment(data, 'YYYY-MM-DD').format('dddd Do MMMM')
subject = id.replace('-', ' ')

content = '<p>Last ' + subject + ' was on</p><h2>' + fixedDate + '</h2>'

return "<div class='col-md-4' id='#" + id + "'>" + content + "</div>"
function githubLink(json) {
return '<a href="' + json['url'] + '">Source</a>'
}
21 changes: 21 additions & 0 deletions lib/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative 'dashboard/fetcher'
require_relative 'dashboard/cleaner'
require_relative 'dashboard/assets'
require_relative 'dashboard/helpers'
require_relative 'dashboard/racks'
require_relative 'dashboard/version'

Expand All @@ -22,6 +23,10 @@ module Dashboard
class App < Sinatra::Base
set :views, 'lib/views'

helpers do
include Dashboard::Helpers
end

get '/' do
@content = '<h1>Home Dashboard</h1>'
title = 'Dashboard'
Expand All @@ -44,6 +49,20 @@ class App < Sinatra::Base
end
end

get '/:repo/:dataset' do
respond_to do |wants|
headers 'Vary' => 'Accept'

wants.json do
Cleaner.sanitized_data("https://api.github.com/repos/#{repo_map[params[:repo]]}/contents/#{params[:dataset]}.csv?ref=master").to_json
end

wants.html do
erb :dataset, layout: :default
end
end
end

get '/snake' do
respond_to do |wants|
headers 'Vary' => 'Accept'
Expand All @@ -63,4 +82,6 @@ class App < Sinatra::Base
# start the server if ruby file executed directly
run! if app_file == $0
end


end
15 changes: 15 additions & 0 deletions lib/dashboard/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Dashboard
module Helpers
def repo_map
l = Cleaner.lookups.keys.map { |k| { Cleaner.lookups[k]['url'][1..-1] => Cleaner.lookups[k]['repo'] } }
map = {}
l.each do |m|
m.each_pair do |k, v|
map[k] = v
end
end

map
end
end
end
80 changes: 80 additions & 0 deletions lib/views/dataset.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<div class='row' id='data'>
</div>

<script>
$.getJSON(document.URL, function(json) {
console.log(json['type'])
if (json['type'] === 'latest') {
$('#data').append(
latestCell(json)
)
}
else if (json['type'] === 'graph') {
graph(json)
}
})

function graph(json) {
ecks = Object.keys(json['data'][0])[0]
why = Object.keys(json['data'][0])[1]
var points = [
{
x: json['data'].map(function(item){ return item[ecks] }),
y: json['data'].map(function(item){ return item[why] }),
type: 'scatter'
}
]

var layout = {
title: json['title'] + ' (' + githubLink(json) + ')',
xaxis: {
tickformat: "%b %Y",
tickangle: 90
},
yaxis: {
title: why,
titlefont: {
size: 14,
color: '#7f7f7f'
}
},
margin: {
l: 50, r: 10
}
}

$('#data').html("<div id='" + json['id'] + "' class='col-md-12' style='height: 400px'></div>")
Plotly.newPlot(json['id'], points, layout)
}

function latestCell(json) {
d = json['data'][json['data'].length -1][json['date-field']]
fixedDate = moment(d, 'YYYY-MM-DD').format('Do MMMM')
age = moment(d, 'YYYY-MM-DD').fromNow()
content = '<h1><small>Last</small> '
content += json['title'] + ':</h1>'
if(json['special_fields']) {
content += '<h4>'

json['special_fields'].forEach(function(field) {
content += json['data'][json['data'].length -1][field]
content += ' '
})

content += '</h4>'
}
content += '<h2>' + fixedDate + '</h2>'
content += '<h3>(' + age + ')</h3>'
content += githubLink(json)

return cellContent(json['id'], content)
}

function githubLink(json) {
return '<a href="' + json['url'] + '">Source</a>'
}

function cellContent(id, content) {
return "<div class='col-md-12' id='#" + id + "'>" + content + "</div>"
}
</script>

0 comments on commit fdc52eb

Please sign in to comment.