Skip to content

Commit

Permalink
Introducing quick graphs! Migration needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Koopmann committed Dec 25, 2009
1 parent 0da0d02 commit a59bcfb
Show file tree
Hide file tree
Showing 18 changed files with 322 additions and 16 deletions.
53 changes: 52 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ApplicationController < ActionController::Base

helper_method :getServiceAlarmMessage
helper_method :getHostAlarmMessage

helper_method :getTopGraphs
helper_method :getTopGraphJS

before_filter :login_required
before_filter :update_last_online
Expand Down Expand Up @@ -298,5 +301,53 @@ def getServiceAlarmMessage status, ms
def getHostAlarmMessage sensor, value
return "Sensor \"#{h(sensor)}\" had value \"#{h(value)}\""
end


def getTopGraphs
#begin
returnage = String.new
graphs = Topgraph.find_all_by_user_id current_user.id

i = 0
graphs.each do |graph|
returnage << "<span class=\"topgraph-title"
returnage << " first" if i == 0
returnage << "\">#{graph.name}:</span> <span id=\"topgraph#{i}\"></span>"
i += 1
end

return returnage
#rescue
# return ""
#end
end

def getTopGraphJS
begin
returnage = "var graph_width = 100;"
graphs = Topgraph.find_all_by_user_id current_user.id

i = 0
graphs.each do |graph|
values = Servicerecord.find :all, :conditions => [ "serviceid = ?", graph.target_id ], :order => "timestamp DESC", :limit => 120
value_array = "["
values = values.reverse
values.each do |value|
value_array << "#{value.ms},"
end
value_array << "]"

returnage << "var values#{i} = #{value_array};"
returnage << "jQuery('#topgraph#{i}').sparkline(values#{i}, { width: graph_width, lineColor: \"#EC6600\" });";

i += 1
end

return returnage
rescue
return ""
end

return ""
end

end
27 changes: 27 additions & 0 deletions app/controllers/topgraphs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class TopgraphsController < ApplicationController
def create
graph = Topgraph.new params[:topgraph]
graph.user_id = current_user.id

if graph.save
flash[:notice] = "Quick graph has been created."
else
flash[:error] = "Could not create quick graph!"
end

redirect_to :controller => "users", :action => :settings
end

def destroy
graph = Topgraph.find params[:id]

if graph.delete
flash[:notice] = "Quick graph has been deleted."
else
flash[:error] = "Could not delete quick graph!"
end

redirect_to :controller => "users", :action => :settings
end

end
3 changes: 3 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def settings
else
@gravatar_allowed = false
end

@new_topgraph = Topgraph.new
@topgraphs = Topgraph.find_all_by_user_id current_user.id
end

def saveusersettings
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/topgraphs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TopgraphsHelper
end
6 changes: 6 additions & 0 deletions app/models/topgraph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Topgraph < ActiveRecord::Base
validates_presence_of :graph_type, :target_id, :name, :minutes
validates_numericality_of :graph_type, :target_id, :minutes

validates_length_of :name, :maximum => 5
end
28 changes: 18 additions & 10 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
<html>
<head>
<title>ScopePort Web Interface</title>
<%= stylesheet_link_tag "core" %>
<%= stylesheet_link_tag "sections" %>
<%= stylesheet_link_tag "jquery.gritter" %>
<%= javascript_include_tag "prototype" %>
<%= javascript_include_tag "jquery-1.3.2.min" %>
<%= javascript_include_tag "jquery.gritter.min" %>
<%= javascript_include_tag "jquery.tablesorter.min.js" %>
<%= javascript_include_tag "scopeport" %>
<%= javascript_include_tag "scopeport-popups" %>
<%= stylesheet_link_tag "core" %>
<%= stylesheet_link_tag "sections" %>
<%= stylesheet_link_tag "jquery.gritter" %>
<%= javascript_include_tag "prototype" %>
<%= javascript_include_tag "jquery-1.3.2.min" %>
<%= javascript_include_tag "jquery.gritter.min" %>
<%= javascript_include_tag "jquery.tablesorter.min.js" %>
<%= javascript_include_tag "jquery.sparkline.min.js" %>
<%= javascript_include_tag "scopeport" %>
<%= javascript_include_tag "scopeport-popups" %>
<script type="text/javascript">
jQuery.noConflict();
</script>
<%= javascript_include_tag "effects" %>
<%= javascript_include_tag "effects" %>
</head>
<body onclick="clearSearch()">
<div id="maintop">
Expand Down Expand Up @@ -125,6 +126,9 @@
</script>

<div id="maincontent-top">
<div id="maincontent-top-graphs">
<%= getTopGraphs %>
</div>
<div id="maincontent-top-search">
<form id="searchform">
<div>
Expand All @@ -149,5 +153,9 @@
</div>
</div>

<script type="text/javascript">
<%= getTopGraphJS %>
</script>

</body>
</html>
48 changes: 47 additions & 1 deletion app/views/users/settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,53 @@
<p><%= submit_tag "Save my settings", :disable_with => "Please wait" %></p>
<% end %>

<h1>Quick graphs</h1>
You can configure up to three graphs that are displayed left of the search bar on every page.

<% form_for @new_topgraph, :url => { :controller => "topgraphs", :action => "create" } do |f| %>
<div class="topgraph-new-form-part first">
<%= f.label :graph_type, "Type" %>
<%= f.select :graph_type, options_for_select({ "Service" => "0" }) %>
</div>

<div class="topgraph-new-form-part">
<%= f.label :target_id, "Service" %>
<%= f.select :target_id, Service.all.collect {|s| [ s.name, s.id ] }, { :include_blank => true } %>
</div>

<div class="topgraph-new-form-part">
<%= f.label :name, "Name" %>
<%= f.text_field :name, :maxlength => 5 %>
</div>

<div class="topgraph-new-form-part">
<%= f.label :minutes, "Timespan (Minutes)" %>
<%= f.text_field :minutes, :value => "180" %>
</div>

<div class="topgraph-new-form-part topgraph-new-form-submit">
<% if @topgraphs.count >= 3 %>
<%= submit_tag "Maximum reached", :disabled => true %>
<% else %>
<%= submit_tag "Create" %>
<% end %>
</div>

<br style="clear: both;" />
<% end %>

<br/>

<h3>Configured graphs</h3>

<ul>
<%= @topgraphs.blank? ? "<li>No quick graphs configured</li>" : "" %>
<% @topgraphs.each do |graph| %>
<li><%=h graph.name %> [<%= link_to "Delete", { :controller => "topgraphs", :action => "destroy", :id => graph.id }, :confirm => "Do you really want to delete this quick graph?" %>]</li>
<% end %>
</ul>

<h1>OAuth</h1>
<p>
<%= link_to ("Configure OAuth Client Applications", :controller => "oauth_clients") %>
</p>
</p>
2 changes: 1 addition & 1 deletion config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# Check if RRDTool is installed
rrd_parts = `rrdtool`.split

if rrd_parts[0].blank? or rrd_parts[0] != "RRDtool"
if rrd_parts[0] == nil or rrd_parts[0] != "RRDtool"
puts "ERROR: YOU NEED TO INSTALL RRDTOOL! (i.e. aptitude install rrdtool)"
exit
end
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20091224233503_create_topgraphs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateTopgraphs < ActiveRecord::Migration
def self.up
create_table :topgraphs do |t|
t.integer :type
t.integer :target_id
t.string :target_sensor
t.string :name
t.integer :minutes
t.timestamps
end
end

def self.down
drop_table :topgraphs
end
end
9 changes: 9 additions & 0 deletions db/migrate/20091225001520_change_topgraph_type_column_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ChangeTopgraphTypeColumnName < ActiveRecord::Migration
def self.up
rename_column :topgraphs, :type, :graph_type
end

def self.down
rename_column :topgraphs, :graph_type, :type
end
end
9 changes: 9 additions & 0 deletions db/migrate/20091225002328_add_user_id_to_topgraphs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddUserIdToTopgraphs < ActiveRecord::Migration
def self.up
add_column :topgraphs, :user_id, :integer
end

def self.down
remove_column :topgraphs, :user_id
end
end
Loading

0 comments on commit a59bcfb

Please sign in to comment.