Skip to content

Instantly share code, notes, and snippets.

@all4miller
Created April 1, 2021 08:51
Show Gist options
  • Save all4miller/097655de25b554664677b18c61d7cc5b to your computer and use it in GitHub Desktop.
Save all4miller/097655de25b554664677b18c61d7cc5b to your computer and use it in GitHub Desktop.
def process_past_48_hours
biz_ids = Businesss.where.not(id: 10).where(flag: true).pluck(:id)
biz_ids += Cards.where(state: 1).pluck(:biz_id)
biz_ids = biz_ids.uniq
# 4720 biz_ids recrds
h = {}
st = (DateTime.now - 48.hours).beginning_of_hour
en = ((st + 48.hours) - 1.second)
Businesss.where(id: biz_ids).order(:plan).each do |business|
# only 47/4720 biz_ids meet the random_enum?
if business.random_enum?
h.each do |k,v|
h[k] += (business.fee_in_dollars / 510.0).round()
end
else
feed_ids = Feed.where(business_id: business.id).pluck(:id)
# feed table has 873,273 records
views = Views.include_that.where(feed_id: feed_ids).where(hit:st..en).group("date_trunc('hour', hit_at)").count(:id)
# views table has 8,414,271 records *and* is on a differnt database but in this
# current design that doesnt really matter as they not joinng
# ever qiuery here returns an avg of 54 records
views.each do |hour, count|
h[hour] = 0 if h[hour].nil?
h[hour] += ((count || 0) * (business.cpc_in_dollars || 0.03)).round()
end
end
end
h = h.sort_by{|k,v| k}
y = {}
h.each do |arr|
at = DateTime.parse(arr[0].to_s).in_time_zone("timezone")
at = at + 1.hour
y[at] = arr[1]
end
y
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment