Skip to content

Instantly share code, notes, and snippets.

@bobzoller
Created November 5, 2013 18:06
Show Gist options
  • Select an option

  • Save bobzoller/7323385 to your computer and use it in GitHub Desktop.

Select an option

Save bobzoller/7323385 to your computer and use it in GitHub Desktop.
# Encoding: utf-8
require 'tzinfo'
ADMIN_URL = ENV['ADMIN_URL'] || 'https://admin.goodeggs.dev:4000'
DAY_IN_MS = 1000 * 60 * 60 * 24
TZ_PACIFIC = 'America/Los_Angeles'
TZ_CENTRAL = 'America/Chicago'
TZ_EASTERN = 'America/New_York'
require 'httparty'
class GoodEggs
include HTTParty
base_uri "#{ADMIN_URL}/time_series"
def time_series(name, query)
self.class.get "/#{name}", :query => query
end
end
good_eggs = GoodEggs.new
# :first_in sets how long it takes before the job is first run. In this case, it is run immediately
SCHEDULER.every '1h', :first_in => 0 do |job|
{
'repeat_vendor_orders_pickupDate' => TZ_PACIFIC,
}.each do |name, tzid|
tz = TZInfo::Timezone.get(tzid)
thirty_days_ago = tz.utc_to_local(Time.now.utc() - (30 * 24 * 60 * 60))
startAt = tz.local_to_utc(Time.local(thirty_days_ago.year, thirty_days_ago.month, thirty_days_ago.day, 0, 0, 0))
tomorrow = tz.utc_to_local(Time.now.utc() + (1 * 24 * 60 * 60))
endAt = tz.local_to_utc(Time.local(tomorrow.year, tomorrow.month, tomorrow.day, 0, 0, 0))
time_series = good_eggs.time_series name, :tzid => tzid, :startAt => startAt.iso8601(3), :endAt => endAt.iso8601(3), :resolution => DAY_IN_MS
send_event name, :points => time_series['points']
end
end
SCHEDULER.every '1m', :first_in => 0 do |job|
{
'repeat_vendor_orders_createdAt' => TZ_PACIFIC,
'repeat_vendor_orders_createdAt_sfbay' => TZ_PACIFIC,
'repeat_vendor_orders_createdAt_la' => TZ_PACIFIC,
'repeat_vendor_orders_createdAt_nola' => TZ_CENTRAL,
'repeat_vendor_orders_createdAt_nyc' => TZ_EASTERN,
}.each do |name, tzid|
tz = TZInfo::Timezone.get(tzid)
today = tz.utc_to_local(Time.now.utc())
last_today = today - (7 * 24 * 60 * 60)
sunday = today - (today.wday * 24 * 60 * 60)
last_sunday = sunday - (7 * 24 * 60 * 60)
startAt = tz.local_to_utc(Time.local(last_sunday.year, last_sunday.month, last_sunday.day, 0, 0, 0))
endAt = tz.local_to_utc(last_today)
time_series = good_eggs.time_series name, :tzid => tzid, :startAt => startAt.iso8601(3), :endAt => endAt.iso8601(3), :resolution => DAY_IN_MS
last = time_series['points'].reduce(0) {|memo, point| memo + point['y']}
startAt = tz.local_to_utc(Time.local(sunday.year, sunday.month, sunday.day, 0, 0, 0))
endAt = tz.local_to_utc(today)
time_series = good_eggs.time_series name, :tzid => tzid, :startAt => startAt.iso8601(3), :endAt => endAt.iso8601(3), :resolution => DAY_IN_MS
current = time_series['points'].reduce(0) {|memo, point| memo + point['y']}
send_event name, :last => last, :current => current
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment