Skip to content

Commit

Permalink
URL-per-dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
pikesley committed Jan 2, 2016
1 parent b4727b5 commit a96ef06
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 14 deletions.
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
2 changes: 1 addition & 1 deletion features/json.feature
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@vcr
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"
Expand Down
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>'
}
13 changes: 12 additions & 1 deletion 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 Down Expand Up @@ -49,7 +54,11 @@ class App < Sinatra::Base
headers 'Vary' => 'Accept'

wants.json do
Cleaner.sanitized_data("https://api.github.com/repos/pikesley/#{params[:repo]}/contents/#{params[:dataset]}.csv?ref=master").to_json
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
Expand All @@ -73,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 a96ef06

Please sign in to comment.