Skip to content

Instantly share code, notes, and snippets.

@jin
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jin/151c0db5cb2e3d8a717c to your computer and use it in GitHub Desktop.
Save jin/151c0db5cb2e3d8a717c to your computer and use it in GitHub Desktop.
require 'json'
require 'net/http'
require 'rubygems'
staffs = %w(weesun knmnyn wgx731 Leventhan franklingu Limy Muhammad-Muneer)
baseurl = "http://osrc.dfm.io/"
# Construct the json URLs
def construct_urls(base_url, usernames, suffix)
urls = Array.new
usernames.each do |username|
urls << base_url + username + suffix
end
urls
end
# Fetch the json with the http and json gems
all_fetched_json = Array.new
urls = construct_urls(baseurl, staffs, ".json")
urls.each do |url|
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
all_fetched_json << JSON.parse(data)
end
all_fetched_json.each do |json|
events = json["usage"]["events"]
events.each do |event|
if event["type"] == "PushEvent"
puts "#{json["username"]}: #{event["total"]} total pushes"
end
end
end
puts
hourly_top = Hash.new
daily_top = Hash.new
# Compute the user with most pushes for each hour
all_fetched_json.each do |json|
username = json["username"]
events = json["usage"]["events"]
events.each do |event|
if event["type"] == "PushEvent"
(0..23).each do |hour|
count = event["day"][hour]
hourly_top[hour] = [username, count] if hourly_top[hour].nil? || hourly_top[hour][1] < count
end
(0..6).each do |day|
count = event["week"][day]
daily_top[day] = [username, count] if daily_top[day].nil? || daily_top[day][1] < count
end
end
end
end
# Print results
hourly_top.each do |top|
puts "#{top[0]} hrs: #{top[1][0]} with #{top[1][1]} pushes"
end
puts
daily_top.each do |top|
puts "Day #{top[0]}: #{top[1][0]} with #{top[1][1]} pushes"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment