Skip to content

Instantly share code, notes, and snippets.

@pedrolopez
Created March 23, 2011 17:06
Show Gist options
  • Save pedrolopez/883490 to your computer and use it in GitHub Desktop.
Save pedrolopez/883490 to your computer and use it in GitHub Desktop.
Retrieve last 24 hours 'creative impressions'
def last_impressions(offset, id)
impressions = Impression.where(:creative_id => id, :updated_at.gte => (Time.now - offset.hour)).sum(:impressions)
if impressions.nil? || impressions == 0
then "none"
end
end
def current_allocation(order)
current = 0
if order.flights.size > 0
order.flights.each do |flight|
flight.creatives.each do |creative|
[:impressions,
:leaderboard_impressions,
:skyscraper_impressions,
:box_impressions,
:thirdpartycode_leaderboard_impressions,
:thirdpartycode_skyscraper_impressions,
:thirdpartycode_box_impressions].each do |key|
current += creative[key] unless creative[key].blank?
end
end
end
end
total = order.total_impressions
if !total.blank? && total > 0 && current > 0
total = total * 1000
current_ratio = ( current / (total)).to_i
if current_ratio > 1
msg = "Over Impressions"
Resque.enqueue_at(1.hour.from_now, Alerts::Alert_impressions, { :user_id => current_user.id,
:order_id => order.id,
:type => "Order_over_imprressions" })
elsif current_ratio < 0.1
msg = "Low Impressions"
Resque.enqueue_at(1.hour.from_now, Alerts::Alert_impressions, { :user_id => current_user.id,
:order_id => order.id,
:type => "Order_less_imprressions" })
elsif current_ratio > 0.9
msg = "Impressions near Limit!"
Resque.enqueue_at(1.hour.from_now, Alerts::Alert_impressions, { :user_id => current_user.id,
:order_id => order.id,
:type => "Order_near_limit_imprressions" })
else
msg = "Impressions"
end
[current, total, current_ratio, msg]
elsif total.blank? && current > 0
msg = "Limit not set!"
current_ratio = 1
total = current
[current, total, current_ratio, msg]
else
msg = "No impressions!"
current = 0
current_ratio = 0
total = 0
[current, total, current_ratio, msg]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment