Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created June 19, 2014 16:36
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 ahoward/8cc3e67df6453f27679c to your computer and use it in GitHub Desktop.
Save ahoward/8cc3e67df6453f27679c to your computer and use it in GitHub Desktop.
class BatonPass
Fattr(:window){ Rails.env.production? ? 24.hours : 10.seconds }
def BatonPass.rollup!
#
counts = Map.new
window = BatonPass.window
#
to_rollup = BatonPass.where(:auto => true)
kinds = to_rollup.distinct(:slug).compact
kinds.each do |kind|
#
client_ids = Event.where(:kind => kind).distinct(:client_id).compact
client_ids.each do |client_id|
#
sliding_window = index = offset = nil
#
sliding_window_reset = proc do |e|
index = e.created_at.to_time
offset = index + window
sliding_window = (index ... offset)
end
#
sliding_window_include = proc do |e|
sliding_window_reset[e] unless sliding_window
a = sliding_window.first.to_i
b = sliding_window.last.to_i
t = e.created_at.to_time.to_i
(a ... b).include?(t)
end
#
sliding_window_count = proc do |e|
counts[kind] ||= {}
counts[kind][client_id] ||= {}
counts[kind][client_id][sliding_window] ||= 0
counts[kind][client_id][sliding_window] += 1
end
#
events = Event.where(:kind => kind, :client_id => client_id).order_by(:created_at.asc)
events.each do |event|
if sliding_window_include[event]
sliding_window_count[event]
else
sliding_window_reset[event]
end
end
end
end
#
rollup = Map.new
counts.each do |kind, counts_per_client_id|
count = 0
counts_per_client_id.each do |client_id, counts_per_sliding_window|
count += counts_per_sliding_window.size
end
baton_pass = BatonPass[kind]
baton_pass.update_attributes!(:count => count)
rollup[kind] = count
end
#
[counts, rollup]
end
=begin
@ 24.hours
[{"web-app-click-to-pass"=>
{"1EA45EBB-E655-43A6-9E83-818B9D4764EB"=>
{2014-06-10 21:50:16 UTC...2014-06-11 21:50:16 UTC=>2},
"1E0F5002-F43A-460F-A595-687F975B7CE9"=>
{2014-06-18 16:40:24 UTC...2014-06-19 16:40:24 UTC=>7},
"FB4B1D5D-B5D9-47CA-9C9E-11A73C8B749F"=>
{2014-06-18 20:46:42 UTC...2014-06-19 20:46:42 UTC=>23}},
"web-app-twitter-share"=>
{"1EA45EBB-E655-43A6-9E83-818B9D4764EB"=>
{2014-06-10 21:09:54 UTC...2014-06-11 21:09:54 UTC=>4},
"C88434F2-553C-4B86-B8E2-0F04EE2409A6"=>
{2014-06-18 14:33:08 UTC...2014-06-19 14:33:08 UTC=>1},
"CEE4708D-E4FD-40B3-AB2C-D3B37BBBD8E6"=>
{2014-06-18 14:33:29 UTC...2014-06-19 14:33:29 UTC=>1},
"B35DCA61-7107-4EEF-A1D0-A01B50CEFC82"=>
{2014-06-18 18:31:25 UTC...2014-06-19 18:31:25 UTC=>1},
"146A9C54-00CC-4DC5-BD47-6A27480334F7"=>
{2014-06-18 18:31:57 UTC...2014-06-19 18:31:57 UTC=>1}}}
, {"web-app-click-to-pass"=>3, "web-app-twitter-share"=>5}
]
@ 10.seconds
[{"web-app-click-to-pass"=>
{"1EA45EBB-E655-43A6-9E83-818B9D4764EB"=>
{2014-06-10 21:50:16 UTC...2014-06-10 21:50:26 UTC=>1},
"1E0F5002-F43A-460F-A595-687F975B7CE9"=>
{2014-06-18 16:40:24 UTC...2014-06-18 16:40:34 UTC=>1},
"FB4B1D5D-B5D9-47CA-9C9E-11A73C8B749F"=>
{2014-06-18 20:46:42 UTC...2014-06-18 20:46:52 UTC=>6,
2014-06-18 20:47:59 UTC...2014-06-18 20:48:09 UTC=>3,
2014-06-18 21:32:24 UTC...2014-06-18 21:32:34 UTC=>1}},
"web-app-twitter-share"=>
{"1EA45EBB-E655-43A6-9E83-818B9D4764EB"=>
{2014-06-10 21:09:54 UTC...2014-06-10 21:10:04 UTC=>1},
"C88434F2-553C-4B86-B8E2-0F04EE2409A6"=>
{2014-06-18 14:33:08 UTC...2014-06-18 14:33:18 UTC=>1},
"CEE4708D-E4FD-40B3-AB2C-D3B37BBBD8E6"=>
{2014-06-18 14:33:29 UTC...2014-06-18 14:33:39 UTC=>1},
"B35DCA61-7107-4EEF-A1D0-A01B50CEFC82"=>
{2014-06-18 18:31:25 UTC...2014-06-18 18:31:35 UTC=>1},
"146A9C54-00CC-4DC5-BD47-6A27480334F7"=>
{2014-06-18 18:31:57 UTC...2014-06-18 18:32:07 UTC=>1}}}
, {"web-app-click-to-pass"=>5, "web-app-twitter-share"=>5}
]
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment