Permalink
Browse files

Merge pull request #114 from obfuscurity/chartroulette

Chartroulette view
  • Loading branch information...
obfuscurity committed May 13, 2013
2 parents be2e5f7 + ad69829 commit cdefa9dfb5413637622741bb43d598e08b193b77
@@ -33,6 +33,10 @@ def self.find_or_create_by_uid(user)
User.filter(:uid => user['uid']).first or User.new(:uid => user['uid'], :email => user['email']).save
end
def favorites
JSON.parse(self.preferences)['favorites']
end
def add_favorite(uuid)
preferences = JSON.parse(self.preferences)
preferences['favorites'].push(uuid).uniq!
@@ -0,0 +1,89 @@
var myDashboards = [];
var myDashboardIndex = 0;
var myDashboardRefreshInterval = 10000;
// Render our page title
var renderChatrouletteTitle = function(dashboard) {
$('.header h1').find('span').remove();
$('.header h1').append('<span class="filter"> - <a href="/dashboards/' + dashboard.uuid + '">' + dashboard.name + '</a></span>');
}
var renderGraphs = function() {
$.ajax({
accepts: {json: 'application/json'},
cache: false,
dataType: 'json',
error: function(xhr, textStatus, errorThrown) { console.log(errorThrown); },
url: '/chartroulette'
}).done(function(d) {
if (d.length > 0) {
myDashboards = d;
renderNextDashboard();
setInterval(function() {
clearGraphs();
renderNextDashboard();
}, myDashboardRefreshInterval);
} else {
$('div.graphs').append('<h3>Oops!</h3>');
$('div.graphs').append("<p>Sorry, we don't have anything to show you yet. Why don't you favorite (star) a couple dashboards and come back again?");
console.log('no favorited dashboards to display');
}
});
}
var renderNextDashboard = function() {
return $.ajax({
accepts: {json: 'application/json'},
cache: false,
dataType: 'json',
error: function(xhr, textStatus, errorThrown) { console.log(errorThrown); },
url: '/dashboards/' + myDashboards[myDashboardIndex].uuid
}).done(function(d) {
if (d.graphs.length === 0) {
console.log('No graphs found');
}
var row = 0;
$('div.graphs').append('<div class="row"></div>');
for (var i=0; i < d.graphs.length; i++) {
var targets = [];
for (var j in $.parseJSON(d.graphs[i].configuration).target) {
targets.push({name: $.parseJSON(d.graphs[i].configuration).target[j]});
}
var c = $.extend($.parseJSON(d.graphs[i].configuration), $.parseJSON(d.graphs[i].overrides), {targets: targets});
row = Math.floor( i / myColumns );
var spanSize = ( 12 / myColumns );
if (($('div.graphs div.row').length - 1) !== row) {
$('div.graphs').append('<div class="row"></div>');
}
$($('div.graphs div.row')[row]).append('<span id="' + d.graphs[i].uuid + '" class="graph span' + spanSize + '"></div>');
var myGraphWidth = $($('div.row span.graph')[0]).width();
var graphCloseIcon = '<img class="close hidden" src="/img/close.png" />';
$('div.graphs div.row span#' + d.graphs[i].uuid).append('<label for="' + d.graphs[i].uuid + '">' + d.graphs[i].name + '</label>');
$('div.graphs div.row span#' + d.graphs[i].uuid).append('<a href="/graphs/' + d.graphs[i].uuid + '"><img src="' + constructGraphUrl(c) + '" alt="' + d.graphs[i].name + '" name="' + d.graphs[i].uuid + '" /></a>');
$('div.graphs div.row span.graph img').load(function() {
// hide spinner on successful load
$(this).parent('a').parent('span').css('background-image', 'none')
}).error(function() {
console.log('failed to load ' + $(this)[0].name);
var broken_img = $(this);
// hide spinner and set background color
broken_img.css('display', 'none');
broken_img.parent('a').parent('span').css('background-image', 'none').css('background-color', '#eee')
broken_img.parent('a').parent('span').find('label').css('margin', '10px 0 0 15px');
// change label and link to image profile
broken_img.parent('a').parent('span').find('label').html('Unable to load <a href="/graphs/' + broken_img[0].name + '">' + broken_img[0].alt + '</a>');
});
}
renderChatrouletteTitle(d.dashboard);
myDashboardIndex++;
if (myDashboardIndex == myDashboards.length) {
myDashboardIndex = 0;
}
});
}
// Not used in dashboards
var scrollNextPage = function() {
return true;
}
@@ -312,6 +312,9 @@ var disableAutoRefresh = function() {
// Populate our toolbar based on URL params
var setParamsOnLoad = function() {
if (window.location.pathname.match(/chartroulette/)) {
return;
}
if ($.url().param('interval') != undefined) {
myInterval = $.url().param('interval');
}
@@ -0,0 +1,15 @@
module Descartes
class Web < Sinatra::Base
get '/chartroulette/?' do
if request.accept.include?("application/json")
favorites = User.filter(:email => session['user']['email']).first.favorites
content_type 'application/json'
status 200
Dashboard.filter(:enabled => true).where(:uuid => favorites).to_json
else
haml :chartroulette, :locals => { :title => 'Descartes - Chartroulette' }
end
end
end
end
@@ -2,11 +2,11 @@ module Descartes
class Web < Sinatra::Base
get '/dashboards/?' do
if request.accept.include?("application/json")
if request.accept.include?('application/json')
@dashboards = []
if params[:search]
matching_dashboards = []
params[:search].split(",").each do |search|
params[:search].split(',').each do |search|
matching_dashboards << Dashboard.select('dashboards.*'.lit, 'COUNT(graph_dashboard_relations.*) AS graph_count'.lit).
from(:dashboards, :graph_dashboard_relations).
where(:dashboards__enabled => true).
@@ -48,15 +48,15 @@ class Web < Sinatra::Base
:dashboards__updated_at).
order('LOWER(dashboards.name)'.lit).all
end
content_type "application/json"
content_type 'application/json'
@dashboards.flatten.to_json
else
haml :'dashboards/list', :locals => { :title => "Descartes - Dashboard List" }
haml :'dashboards/list', :locals => { :title => 'Descartes - Dashboard List' }
end
end
post '/dashboards/?' do
if request.accept.include?("application/json") && params[:uuids] && params[:name]
if request.accept.include?('application/json') && params[:uuids] && params[:name]
owner = api_token? ? 'api@localhost' : session['user']['uid']
@dashboard = Dashboard.new({ :owner => owner, :name => params[:name] })
@dashboard.save
@@ -73,7 +73,7 @@ class Web < Sinatra::Base
@graphs = []
if params[:search]
matching_graphs = []
params[:search].split(",").each do |search|
params[:search].split(',').each do |search|
matching_graphs << Graph.select('graphs.*'.lit).from(:graphs, :graph_dashboard_relations, :dashboards, :tags).
where(:graph_dashboard_relations__graph_id => :graphs__id,
:graph_dashboard_relations__dashboard_id => @dashboard.id,
@@ -102,8 +102,8 @@ class Web < Sinatra::Base
@graphs.push(Graph[r.graph_id])
end
end
if request.accept.include?("application/json")
content_type "application/json"
if request.accept.include?('application/json')
content_type 'application/json'
{ :dashboard => @dashboard, :graphs => @graphs }.to_json
else
if @dashboard.nil?
@@ -126,7 +126,7 @@ class Web < Sinatra::Base
end
post '/dashboards/:id/favorite/?' do
if request.accept.include?("application/json")
if request.accept.include?('application/json')
if @dashboard = Dashboard.filter(:enabled => true, :uuid => params[:id]).first
User.filter(:uid => session['user']['uid']).first.add_favorite(@dashboard.uuid)
session['user']['preferences'] = User.filter(:uid => session['user']['uid']).first.preferences
@@ -140,7 +140,7 @@ class Web < Sinatra::Base
end
delete '/dashboards/:id/favorite/?' do
if request.accept.include?("application/json")
if request.accept.include?('application/json')
if @dashboard = Dashboard.filter(:enabled => true, :uuid => params[:id]).first
User.filter(:uid => session['user']['uid']).first.remove_favorite(@dashboard.uuid)
session['user']['preferences'] = User.filter(:uid => session['user']['uid']).first.preferences
@@ -154,9 +154,9 @@ class Web < Sinatra::Base
end
post '/dashboards/:id/graphs/?' do
if request.accept.include?("application/json") && params[:uuids]
if request.accept.include?('application/json') && params[:uuids]
@dashboard = Dashboard.filter(:enabled => true, :uuid => params[:id]).first
params[:uuids].split(",").each do |g_uuid|
params[:uuids].split(',').each do |g_uuid|
@graph = Graph.filter(:enabled => true, :uuid => g_uuid).first
GraphDashboardRelation.new(:dashboard_id => @dashboard.id, :graph_id => @graph.id).save
end
@@ -2,8 +2,8 @@ module Descartes
class Web < Sinatra::Base
get '/favorites/?' do
if request.accept.include?("application/json")
content_type "application/json"
if request.accept.include?('application/json')
content_type 'application/json'
status 200
JSON.parse(User.filter(:uid => session['user']['uid']).first.preferences)['favorites'].to_json
else
@@ -2,11 +2,11 @@ module Descartes
class Web < Sinatra::Base
get '/graphs/?' do
if request.accept.include?("application/json")
if request.accept.include?('application/json')
@graphs = []
if params[:search]
matching_graphs = []
params[:search].split(",").each do |search|
params[:search].split(',').each do |search|
matching_graphs << Graph.select('graphs.*'.lit).from(:graphs, :tags).
where(:graphs__enabled => true, :graphs__id => :tags__graph_id).
filter(:tags__name.like(/#{search}/i)).all
@@ -26,12 +26,12 @@ class Web < Sinatra::Base
page_count = 12
@graphs << Graph.filter(:enabled => true).order(Sequel.desc(:views), Sequel.desc(:id)).paginate(page_index, page_count).all
end
content_type "application/json"
content_type 'application/json'
status 200
@graphs.flatten.to_json
else
status 200
haml :'graphs/list', :locals => { :title => "Descartes - Graph List" }
haml :'graphs/list', :locals => { :title => 'Descartes - Graph List' }
end
end
@@ -55,7 +55,7 @@ class Web < Sinatra::Base
end
end
end
if request.accept.include?("application/json")
if request.accept.include?('application/json')
status 200
# XXX - should return tags too
@graph.to_json
@@ -73,8 +73,8 @@ class Web < Sinatra::Base
get '/graphs/:uuid/?' do
@graph = Graph.filter(:uuid => params[:uuid]).first
if request.accept.include?("application/json")
content_type "application/json"
if request.accept.include?('application/json')
content_type 'application/json'
status 200
@graph.to_json
else
@@ -86,8 +86,8 @@ class Web < Sinatra::Base
end
get '/graphs/:uuid/tags/?' do
if request.accept.include?("application/json")
content_type "application/json"
if request.accept.include?('application/json')
content_type 'application/json'
@graph = Graph.filter(:uuid => params[:uuid]).first
@tags = Tag.select(:id, :name).filter(:graph_id => @graph.id).order(:id).all
status 200
@@ -98,8 +98,8 @@ class Web < Sinatra::Base
end
post '/graphs/:uuid/tags/?' do
if request.accept.include?("application/json")
content_type "application/json"
if request.accept.include?('application/json')
content_type 'application/json'
if params[:name]
tags = []
tags.push(params[:name]).flatten!
@@ -154,8 +154,8 @@ class Web < Sinatra::Base
end
post '/graphs/:id/gists/?' do
if request.accept.include?("application/json")
content_type "application/json"
if request.accept.include?('application/json')
content_type 'application/json'
@graph = Graph.filter(:uuid => params[:id]).first
@gist = Gist.new(:owner => session['user']['uid'], :url => params[:url], :name => @graph.name, :data => params[:data], :graph_id => @graph.id)
@gist.save
@@ -167,8 +167,8 @@ class Web < Sinatra::Base
end
post '/graphs/:id/comments/?' do
if request.accept.include?("application/json")
content_type "application/json"
if request.accept.include?('application/json')
content_type 'application/json'
@graph = Graph.filter(:uuid => params[:uuid]).first
@comment = Comment.new(:owner => session['user']['uid'], :uuid => @graph.uuid)
@comment.save
@@ -0,0 +1,5 @@
.header
%h1 Chartroulette
.graphs
%script{ :type => "text/javascript", :src => "/js/chartroulette.js" }
%script{ :type => "text/javascript", :src => "/js/render-common.js" }
@@ -26,6 +26,8 @@
%a{ :href => "/graphs" } Graphs
%li.dashboards
%a{ :href => "/dashboards" } Dashboards
%li.chartroulette
%a{ :href => "/chartroulette" } Chartroulette
%li.metrics
%a{ :href => "/metrics" } Metrics
View
@@ -21,6 +21,7 @@ class Web < Sinatra::Base
require 'descartes/routes/metrics'
require 'descartes/routes/gists'
require 'descartes/routes/favorites'
require 'descartes/routes/chartroulette'
get '/' do
haml :index

0 comments on commit cdefa9d

Please sign in to comment.