Skip to content

Instantly share code, notes, and snippets.

@ono
Created March 25, 2014 23:45
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 ono/9773928 to your computer and use it in GitHub Desktop.
Save ono/9773928 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# A copy of script that gets API usage from Librato.
# Some variables are replaced for security reasons - so it won't work as it is for you.
# It's a reference of my blog post on geckoboard blog.
class LibratoMetric
def initialize
@user = APP_CONFIG["librato"]["user"]
@api_token = APP_CONFIG["librato"]["api_token"]
@metric_name = "METRIC-NAME"
end
def url(date)
url = "https://metrics-api.librato.com/v1/metrics/#{@metric_name}"
url += "?count=24&resolution=3600&summarize_time=1"
url += "&source=SOURCE"
url += "&start_time=#{date.to_time.to_i}"
end
def make_http(date)
uri = URI(url(date))
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new uri.request_uri
request.basic_auth @user, @api_token
http.request(request)
end
end
def get_count(date)
response = make_http date
count = 0
json = JSON.parse response.body
json["measurements"].each do |key,value|
count += value[0]["count"]
end
count
end
end
class RedisUniqueServiceAccounts
def initialize
@redis = Redis.new(
host: APP_CONFIG["redis"]["host"],
port: APP_CONFIG["redis"]["port"],
timeout: 0.5
)
end
def key(date)
"acme-service-account:#{date.to_time.strftime('%Y-%m-%d')}"
end
def get_count(date)
@redis.hlen key(date)
end
end
class GeckoboardClient
def initialize
@api_key = APP_CONFIG["geckoboard"]["api_key"]
end
def url(widget_key)
"https://push.geckoboard.com/v1/send/#{widget_key}"
end
def push(widget_key, number)
uri = URI(url(widget_key))
data = {
api_key: @api_key,
data: {
item: [{value: number}]
}
}
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Post.new uri.request_uri
request['Content-Type'] = 'application/json'
request.body = data.to_json
http.request(request)
end
end
end
def last_working_day
if Time.now.monday?
3.days.ago.midnight
elsif Time.now.sunday?
2.days.ago.midnight
else
1.days.ago.midnight
end
end
day_before_any_fix = Time.new(2014, 3, 1)
while true do
gecko = GeckoboardClient.new
metric = LibratoMetric.new
redis = RedisUniqueServiceAccounts.new
puts "--- START GET/SEND METRICS"
# Show number of API calls per day (before fix)
fetch = metric.get_count day_before_any_fix
gecko.push "WIDGET-KEY-1", fetch
# Show number of API calls/account per day (before fix)
acc = redis.get_count day_before_any_fix
gecko.push "WIDGET-KEY-2", (fetch / acc.to_f).round(2)
fetch = metric.get_count last_working_day
gecko.push "WIDGET-KEY-3", fetch
acc = redis.get_count last_working_day
gecko.push "WIDGET-KEY-4", (fetch / acc.to_f).round(2)
fetch = metric.get_count 0.day.ago.midnight
gecko.push "WIDGET-KEY-5", fetch
acc = redis.get_count 0.day.ago.midnight
gecko.push "WIDGET-KEY-6", (fetch / acc.to_f).round(2)
puts "--- METRICS SENT"
sleep 600
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment