Created
October 29, 2012 19:10
-
-
Save knowuh/3975805 to your computer and use it in GitHub Desktop.
activiy_launch_log_parser.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'uri' | |
require 'nokogiri' | |
require 'net/http' | |
require 'pry' | |
require 'active_support/inflector' | |
LAUNCH_REGEX = /"(?<url>.*offerings\/(?<offering_id>[0-9]+)\.jnlp[^"]*)" for\s+(?<ip>[0-9|.]*)\s+at\s+(?<date>\d{4}-\d{2}\-\d{2})\s+(?<time>\d{1,2}:\d{1,2}:\d{1,2})/ | |
@host_url = "http://rites-investigations.staging.concord.org" | |
class Launch | |
attr_accessor :date | |
attr_accessor :time | |
attr_accessor :ip | |
attr_accessor :offering_id | |
attr_accessor :url | |
attr_accessor :activity_name | |
def self.from_match_data(hash) | |
results = self.new() | |
results.date = hash[:date] | |
results.url = hash[:url] | |
results.ip = hash[:ip] | |
results.time = hash[:time] | |
results.offering_id = hash[:offering_id] | |
results.activity_name = Launch.activity_name(results.offering_id) | |
return results | |
end | |
def self.activity_name(id) | |
@@cache ||= {} | |
return @@cache[id] if @@cache[id] | |
return @@cache[id] = find_activity(id) | |
end | |
def self.find_activity(id) | |
@host_url = "http://rites-investigations.staging.concord.org" | |
info_url = "#{@host_url}/portal/offerings/#{id}.xml" | |
puts info_url | |
uri = URI.parse(info_url) | |
response = Net::HTTP.get_response(uri) | |
body = response.body | |
xml_doc = Nokogiri::XML(body) | |
offering_id = xml_doc.xpath('//runnable-id').text | |
offering_type = xml_doc.xpath('//runnable-type').text.downcase.pluralize | |
offering_url = "#{@host_url}/#{offering_type}/#{offering_id}.xml" | |
uri = URI.parse(offering_url) | |
response = Net::HTTP.get_response(uri) | |
body = response.body | |
xml_doc = Nokogiri::XML(body) | |
return xml_doc.xpath('//name').text | |
end | |
def to_log_format | |
['url','ip','date','time','activity_name'].map { |field| "#{field}='#{self.send(field)}'"}.join(',') + "\n" | |
end | |
end | |
def parse_log(filename) | |
file = File.new(filename) | |
launches = [] | |
file.each do |line| | |
matches = line.match(LAUNCH_REGEX) | |
if matches | |
l = Launch.from_match_data(matches) | |
launches.push(l) | |
end | |
end | |
return launches | |
end | |
@launches = parse_log("production.log") | |
puts @launches.size | |
binding.pry | |
@launches.each { |l| puts l.to_log_format } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment