Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Munin graph when in development for use. #62

Merged
merged 1 commit into from
Nov 26, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/assets/images/dummy-graph-for-development.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

def show
@service = Service.find_by_name(params[:id])
@munin = Munin.new(@service) if @service.munin_url.present?
@munin = Munin.new(@service)

respond_with @service
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/hosts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class: 'btn btn-danger' %>
</div>

<% if @munin.find_service_that_hash_munin_url_by(@host) -%>
<% if @munin.find_service_that_has_munin_url_by(@host) -%>
<div>
<h2><%= t('hosts.show.graphs') %></h2>
<p><%= link_to @munin.url_for(host: @host).to_s, @munin.url_for(host: @host).to_s %></p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/services/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<dd><%= @service.name %></dd>
<dt><strong><%= model_class.human_attribute_name(:description) %>:</strong></dt>
<dd><%= markdown(@service.description) %></dd>
<% if @service.munin_url.present? -%>
<% if @munin.activated? -%>
<dt><strong><%= model_class.human_attribute_name(:munin_url) %>:</strong></dt>
<dd>
<%= link_to @service.munin_url, @service.munin_url %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/shared/_hosts.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<table class="table table-striped">
<thead>
<tr>
<% if munin -%>
<% if munin.try(:activated?) -%>
<th class="graph"><%= model_class.human_attribute_name(:graph) %></th>
<% end -%>
<th><%= model_class.human_attribute_name(:name) %>/<%= model_class.human_attribute_name(:ip_address) %></th>
Expand All @@ -25,7 +25,7 @@
<tbody>
<% hosts.each do |host| %>
<tr>
<% if munin -%>
<% if munin.try(:activated?) -%>
<td class="graph">
<%= link_to image_tag(munin.graph_url_for(role: role, host: host).to_s), munin.url_for(role: role, host: host).to_s, target: '_blank' %>
</td>
Expand Down
41 changes: 30 additions & 11 deletions lib/munin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'uri'

class Munin
DUMMY_MUNIN_URL = 'http://munin.example.com/'
DUMMY_GRAPH_PATH = '/assets/dummy-graph-for-development.png'

class Error < StandardError; end

attr_accessor :service
Expand All @@ -9,11 +12,23 @@ def initialize (service = nil)
@service = service
end

def dummy?
Rails.env.development? && (service && service.munin_url.blank?)
end

def activated?
dummy? || (service && service.munin_url.present?)
end

def root
raise Error, 'No service defined' unless service
raise Error, 'No munin_url defined' unless service.munin_url.present?
raise Error, 'No service defined' if !dummy? && !service
raise Error, 'No munin_url defined' if !dummy? && !service.munin_url.present?

URI.parse(service.munin_url)
if dummy?
URI.parse(DUMMY_MUNIN_URL)
else
URI.parse(service.munin_url)
end
end

def service_url
Expand All @@ -25,24 +40,28 @@ def service_url
def url_for (args)
host = args[:host]
role = args[:role] || host.roles.first
self.service ||= find_service_that_hash_munin_url_by(host)
self.service ||= find_service_that_has_munin_url_by(host)

url = root
path = [service, role, host].map { |p| URI.escape(p.name) }.join('/')
url += path
end

def graph_url_for (args)
host = args[:host]
role = args[:role]
url = url_for(role: role, host: host)
options = { type: :load, span: :day}.merge(args[:options] || {})
if dummy?
URI.parse(DUMMY_GRAPH_PATH)
else
host = args[:host]
role = args[:role]
url = url_for(role: role, host: host)
options = { type: :load, span: :day}.merge(args[:options] || {})

path = [url.path, URI.escape("#{options[:type].to_s}-#{options[:span].to_s}.png")].join('/')
url += path
path = [url.path, URI.escape("#{options[:type].to_s}-#{options[:span].to_s}.png")].join('/')
url += path
end
end

def find_service_that_hash_munin_url_by (host)
def find_service_that_has_munin_url_by (host)
found = nil

if service && service.munin_url.present?
Expand Down
102 changes: 95 additions & 7 deletions spec/lib/munin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,85 @@
it { expect(munin.service).to be == service }
end

describe '#dummy?' do
context 'when `Rails.env` == development', env: :development do
context 'and service.munin_url is set' do
include_context 'init_munin_with_service'

it {
expect(munin.dummy?).to be_false
}
end

context 'and service.munin_url is not set' do
include_context 'init_munin_with_service'
before { service.munin_url = nil }

it {
expect(munin.dummy?).to be_true
}
end
end
end

describe '#activated?' do
context 'when dummy mode for develpment', env: :development do
context 'and service.munin_url is set' do
include_context 'init_munin_with_service'

it {
expect(munin.activated?).to be_true
}
end
end

context 'when normal mode' do
context 'and service.munin_url is set' do
include_context 'init_munin_with_service'

it {
expect(munin.activated?).to be_true
}
end

context 'and service.munin_url is not set'do
include_context 'init_munin_with_service'
before { service.munin_url = nil }

it {
expect(munin.activated?).to be_false
}
end
end
end

describe '#root' do
include_context 'init_munin_with_service'
it { expect(munin.root).to be == URI.parse(service.munin_url) }
context 'when dummy mode for development', env: :development do
context 'and service is set' do
include_context 'init_munin_with_service'

it {
expect(munin.root).to be == URI.parse(service.munin_url)
}
end

context 'and service.munin_url is not set' do
include_context 'init_munin_with_service'
before { service.munin_url = nil }

it {
expect(munin.root).to be == URI.parse(Munin::DUMMY_MUNIN_URL)
}
end
end

context 'when normal mode' do
include_context 'init_munin_with_service'

it {
expect(munin.root).to be == URI.parse(service.munin_url)
}
end
end

describe '#service_url' do
Expand Down Expand Up @@ -50,9 +126,9 @@
end

describe '#graph_url_for' do
include_context 'init_munin_with_service'

context 'when option is passed' do
include_context 'init_munin_with_service'

it {
url = munin.graph_url_for(
role: role,
Expand All @@ -67,24 +143,36 @@
end

context 'when option is passed' do
include_context 'init_munin_with_service'

it {
url = munin.graph_url_for(role: role, host: host)
expect(url).to be == URI.parse(
"#{service.munin_url}#{URI.escape(service.name)}/#{URI.escape(role.name)}/#{URI.escape(host.name)}/load-day.png"
)
}
end

context 'when `Rails.env` is development', env: :development do
include_context 'init_munin_with_service'
before { service.munin_url = nil }

it {
url = munin.graph_url_for(role: role, host: host)
expect(url).to be == URI.parse(Munin::DUMMY_GRAPH_PATH)
}
end
end

describe '#find_service_that_hash_munin_url_by' do
describe '#find_service_that_has_munin_url_by' do
context 'service is already set' do
include_context 'init_munin_with_service'
it { expect(munin.find_service_that_hash_munin_url_by(host)).to be == service }
it { expect(munin.find_service_that_has_munin_url_by(host)).to be == service }
end

context 'service is not set' do
include_context 'init_munin'
it { expect(munin.find_service_that_hash_munin_url_by(host)).to be == service }
it { expect(munin.find_service_that_has_munin_url_by(host)).to be == service }
end
end
end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
RSpec.configure do |config|
OmniAuth.config.test_mode = true

config.before(:each, env: :development) do
Rails.stub(:env).and_return(ActiveSupport::StringInquirer.new('development'))
end

config.after(:each, env: :development) do
Rails.unstub(:env)
end

# factory girl
FactoryGirl.find_definitions
config.include FactoryGirl::Syntax::Methods
Expand Down