]> git.openstreetmap.org Git - chef.git/blobdiff - cookbooks/munin/files/default/plugins/api_calls_num
Update rubocop and fix new warnings
[chef.git] / cookbooks / munin / files / default / plugins / api_calls_num
index b0c23664b4ef5c304eeb6218b189cf7bee798b9f..1c3a28e05536c1ceec46c453830e18983199bb0b 100755 (executable)
@@ -8,7 +8,7 @@ require 'apache_log_regex'
 NUM_LINES = 10000
 
 def uris_from_logs
-  lines = Array.new
+  lines = []
   max_time = nil
   min_time = nil
   parser = ApacheLogRegex.new('%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %x')
@@ -20,7 +20,7 @@ def uris_from_logs
       min_time = [min_time, t].compact.min
       max_time = [max_time, t].compact.max
       lines << uri
-    rescue ApacheLogRegex::ParseError => e
+    rescue ApacheLogRegex::ParseError
       # nil
     end
   end
@@ -28,27 +28,27 @@ def uris_from_logs
 end
 
 CALL_TYPES = {
-  :map => "Map API calls", 
-  :upload => "Changeset diff uploads", 
-  :amf => "AMF API calls", 
-  :history => "Element history fetches", 
+  :map => "Map API calls",
+  :upload => "Changeset diff uploads",
+  :amf => "AMF API calls",
+  :history => "Element history fetches",
   :full => "Full element fetches",
   :trkpts => "GPX trackpoints calls",
-  :web => "Web site traffic", 
+  :web => "Web site traffic",
   :other => "Other API calls"
 }
 
 def categorise_uri(line)
   uri = line.split(" ")[1]
-  
+
   case uri
-  when /api\/0\.6\/map/ then :map
-  when /api\/0\.6\/changeset\/[0-9]*\/upload/ then :upload
-  when /api\/0\.6\/amf/ then :amf
-  when /api\/0\.6\/(node|way|relation)\/[0-9]*\/history/ then :history
-  when /api\/0\.6\/(node|way|relation)\/[0-9]*\/full/ then :full
-  when /api\/0\.6\/trackpoints/ then :trkpts
-  when /api\/0\.6\// then :other
+  when %r{api/0\.6/map} then :map
+  when %r{api/0\.6/changeset/[0-9]*/upload} then :upload
+  when %r{api/0\.6/amf} then :amf
+  when %r{api/0\.6/(node|way|relation)/[0-9]*/history} then :history
+  when %r{api/0\.6/(node|way|relation)/[0-9]*/full} then :full
+  when %r{api/0\.6/trackpoints} then :trkpts
+  when %r{api/0\.6/} then :other
   else :web
   end
 end
@@ -62,17 +62,16 @@ if ARGV[0] == 'config'
 else
   min_time, max_time, lines = uris_from_logs
   delta_t = (max_time - min_time).to_f * 24 * 60
-  counts = lines.
-      collect {|x| categorise_uri(x)}.
-      inject(Hash.new) do |h, e|
-    if h.has_key? e
+  counts = lines
+           .collect { |x| categorise_uri(x) }
+           .each_with_object({}) do |h, e|
+    if h.key? e
       h[e] += 1
     else
       h[e] = 1
     end
-    h
   end
-  
+
   CALL_TYPES.keys.each do |type|
     count = counts[type] || 0
     puts "#{type}.value #{count / delta_t}"