From 96b46ed28a44662687008133acc243dbdd27b0ef Mon Sep 17 00:00:00 2001 From: Alex Legler Date: Fri, 25 Dec 2015 19:40:43 +0100 Subject: Ruby style updates --- .rubocop.yml | 20 +++++++++++++ infra-status.rb | 7 ++--- lib/helpers.rb | 80 ++++++++++++++++++++++++------------------------- lib/notice_store.rb | 47 +++++++++++++---------------- lib/service_registry.rb | 43 +++++++++++++------------- 5 files changed, 106 insertions(+), 91 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..82899f3 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,20 @@ +Style/FileName: + Enabled: false + +Style/FormatString: + Enabled: false + +Style/Documentation: + Enabled: false + +Style/PerlBackrefs: + Enabled: false + +Metrics/LineLength: + Max: 120 + +Metrics/MethodLength: + Max: 20 + +Metrics/ModuleLength: + Max: 200 diff --git a/infra-status.rb b/infra-status.rb index 291bc6d..2a3ae43 100644 --- a/infra-status.rb +++ b/infra-status.rb @@ -13,7 +13,7 @@ require_relative 'lib/notice_store' require_relative 'lib/service_registry' require_relative 'lib/helpers' -MY_URL = 'http://infra-status.gentoo.org/' +MY_URL = 'https://infra-status.gentoo.org/' configure do NoticeStore.instance.update! @@ -32,12 +32,12 @@ get '/notice/:id' do if notice.nil? status 404 - erb :layout, :layout => false do + erb :layout, layout: false do '

No such notice

The notice you have requested does not exist or has been removed as it was resolved long ago.

' end else @title = notice['title'] - erb :notice, :locals => { :notice => notice } + erb :notice, locals: { notice: notice } end end @@ -69,4 +69,3 @@ get '/force_update' do ServiceRegistry.instance.update! redirect '/#ok' end - diff --git a/lib/helpers.rb b/lib/helpers.rb index 99180e8..55fffea 100644 --- a/lib/helpers.rb +++ b/lib/helpers.rb @@ -4,13 +4,13 @@ helpers do end def markdown(text) - markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true) + markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, space_after_headers: true) markdown.render(text) end def get_forced_state(notices) notices.each do |notice| - next unless notice.has_key? 'force_state' + next unless notice.key? 'force_state' return notice['force_state'] end @@ -38,7 +38,7 @@ helpers do _service_info = service_info(service) #service_name = ServiceRegistry.instance.services[service][:name] service_array = ServiceRegistry.instance.services[service] - service_name = service_array.nil? ? 'UNKNOWN NAME' : service_array[:name] + service_name = service_array.nil? ? 'UNKNOWN NAME' : service_array[:name] data_service_name = service_name.gsub('"', "'") content << " ' - when 'down' - return '' - when 'warning' - return '' - when 'maintenance' - return '' - else - return '' + when 'up' + return '' + when 'down' + return '' + when 'warning' + return '' + when 'maintenance' + return '' + else + return '' end end def status_text(status) case status.to_s - when 'up' - return 'The service is up and running.' - when 'down' - return 'There are indications the service is down.' - when 'warning' - return 'There are issues with the service.' - when 'maintenance' - return 'The service is undergoing scheduled maintenance.' - else - return 'No data available.' + when 'up' + return 'The service is up and running.' + when 'down' + return 'There are indications the service is down.' + when 'warning' + return 'There are issues with the service.' + when 'maintenance' + return 'The service is undergoing scheduled maintenance.' + else + return 'No data available.' end end def item_icon(type) case type.to_s - when 'maintenance' - return '' - when 'outage' - return '' - when 'information' - return '' + when 'maintenance' + return '' + when 'outage' + return '' + when 'information' + return '' end end @@ -143,11 +143,11 @@ helpers do end def humanize(secs) - [[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map{ |count, name| + [[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map do |count, name| if secs > 0 secs, n = secs.divmod(count) "#{n.to_i} #{name}" unless name == :seconds end - }.compact.reverse.join(' ') + end.compact.reverse.join(' ') end end diff --git a/lib/notice_store.rb b/lib/notice_store.rb index 379ab2a..2305686 100644 --- a/lib/notice_store.rb +++ b/lib/notice_store.rb @@ -37,27 +37,26 @@ class NoticeStore def visible_notices notices.select do |notice| is_active = notice['active'] - is_active &= notice['expire_at'] >= DateTime.now if notice.has_key? 'expire_at' - is_active &= notice['created_at'] <= DateTime.now if notice.has_key? 'created_at' + is_active &= notice['expire_at'] >= DateTime.now if notice.key? 'expire_at' + is_active &= notice['created_at'] <= DateTime.now if notice.key? 'created_at' is_active end - end def active_notices notices.select do |notice| is_active = notice['active'] - is_active &= notice['expire_at'] >= DateTime.now if notice.has_key? 'expire_at' - is_active &= notice['created_at'] <= DateTime.now if notice.has_key? 'created_at' - is_active &= notice['starts_at'] <= DateTime.now if notice.has_key? 'starts_at' + is_active &= notice['expire_at'] >= DateTime.now if notice.key? 'expire_at' + is_active &= notice['created_at'] <= DateTime.now if notice.key? 'created_at' + is_active &= notice['starts_at'] <= DateTime.now if notice.key? 'starts_at' is_active end end def notice_affects_service(notice, service) - return (notice.has_key? 'affects' and not notice['affects'].nil? and notice['affects'].include? service) + notice.key?('affects') && !notice['affects'].nil? && notice['affects'].include?(service) end def active_notices_for(service) @@ -81,6 +80,7 @@ class NoticeStore end private + def update? if ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS update! @@ -89,6 +89,8 @@ class NoticeStore end class Notice + attr_reader :content + def self.from_file(filename) content = File.read(filename) metadata = YAML.load(content) || {} @@ -96,13 +98,13 @@ class Notice description = 'missing description' description_splitpos = nil - lines = content.split("\n").map { |l| l.strip } - if lines[0] == '---' and lines.grep('---').length() >= 2 - description_splitpos = 2 - elsif lines.grep('---').length() >= 1 - description_splitpos = 1 + lines = content.split("\n").map(&:strip) + if lines[0] == '---' && lines.grep('---').length >= 2 + description_splitpos = 2 + elsif lines.grep('---').length >= 1 + description_splitpos = 1 else - description_splitpos = 0 + description_splitpos = 0 end description = content.split('---')[description_splitpos].strip @@ -110,19 +112,11 @@ class Notice end def [](what) - if @metadata.has_key? what - @metadata[what] - else - nil - end - end - - def has_key?(what) - @metadata.has_key? what + @metadata[what] end - def get_content - @content + def key?(what) + @metadata.key? what end def url @@ -130,11 +124,12 @@ class Notice end private + def initialize(id, metadata, content) @metadata = metadata - %w[created_at eta expire_at starts_at].each do |key| - @metadata[key] = DateTime.parse(@metadata[key]) if @metadata.has_key? key + %w(created_at eta expire_at starts_at).each do |key| + @metadata[key] = DateTime.parse(@metadata[key]) if @metadata.key? key end @metadata['id'] = id diff --git a/lib/service_registry.rb b/lib/service_registry.rb index 1f9e0d7..38aad87 100644 --- a/lib/service_registry.rb +++ b/lib/service_registry.rb @@ -4,10 +4,10 @@ require 'date' # Defining state constants module State - UP=1 - DOWN=2 - WARNING=3 - NA=0 + UP = 1 + DOWN = 2 + WARNING = 3 + NA = 0 end module HelperMethods @@ -15,42 +15,42 @@ module HelperMethods def all_hosts_up?(hosts) hosts.each do |host| return false unless - status_data['hosts'].has_key?(host) and - status_data['hosts'][host]['current_state'] == 0 + status_data['hosts'].key?(host) && + status_data['hosts'][host]['current_state'] == 0 end true end def host_up?(host) - status_data['hosts'].has_key?(host) and - status_data['hosts'][host]['current_state'] == 0 + status_data['hosts'].key?(host) && + status_data['hosts'][host]['current_state'] == 0 end def host_flapping?(host) - status_data['hosts'].has_key?(host) and - status_data['hosts'][host]['is_flapping'] != 0 + status_data['hosts'].key?(host) && + status_data['hosts'][host]['is_flapping'] != 0 end # Checks if the service is up def service_up?(host, service) - status_data['services'].has_key?(host) and - status_data['services'][host].has_key?(service) and - status_data['services'][host][service]['current_state'] == 0 + status_data['services'].key?(host) && + status_data['services'][host].key?(service) && + status_data['services'][host][service]['current_state'] == 0 end def service_flapping?(host, service) - status_data['services'].has_key?(host) and - status_data['services'][host].has_key?(service) and - status_data['services'][host][service]['is_flapping'] != 0 + status_data['services'].key?(host) && + status_data['services'][host].key?(service) && + status_data['services'][host][service]['is_flapping'] != 0 end def has_service?(host, service) - status_data['services'][host].has_key?(service) + status_data['services'][host].key?(service) end def default(host, service = nil) - if service == nil + if service.nil? if host_flapping? host State::WARNING elsif host_up? host @@ -98,7 +98,7 @@ class ServiceRegistry @services[name][:status] = block.call rescue Exception => e @services[name][:status] = State::NA - $stderr.puts e + $stderr.puts "Processing #{name}: #{e}" end @next_name = nil @@ -139,7 +139,7 @@ class ServiceRegistry @cache_locked = true @services = {} @categories = {} - @columns = {1 => [], 2 => [], 3 => []} + @columns = { 1 => [], 2 => [], 3 => [] } @status_data = JSON.parse(File.read(StatusSource)) load(File.join(File.dirname(__FILE__), '..', 'data', 'services.rb')) @load_date = DateTime.now @@ -154,8 +154,9 @@ class ServiceRegistry end private + def update? - if not @load_date.nil? and not @cache_locked and ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS + if !@load_date.nil? && !@cache_locked && ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS update! end end -- cgit v1.2.3-65-gdbad