Created
April 1, 2021 09:42
-
-
Save all4miller/8494d9574a98dc1d78c447a97609e88c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_past_48_hours_fast | |
st = (DateTime.now - 48.hours).beginning_of_hour | |
en = ((st + 48.hours) - 1.second) | |
Business.connection.select_all(" | |
with biz as ( | |
select businesses.id, | |
coalesce(businesses.cpc_in_dollars, 0.03) cpc_in_dollars, | |
businesses.fee_in_dollars, | |
businesses.plan | |
from businesses | |
where id in ( | |
select id | |
from businesses | |
where flag = true | |
and businesses.id <> 4 | |
union all | |
select biz_id | |
from cards | |
where state = 1) | |
), | |
data as ( | |
select biz.id, | |
biz.cpc_in_dollars, | |
date_trunc('hour', views.hit_at) timestmp, | |
count(views.id) view_count | |
from biz | |
join feeds | |
on feeds.biz_id = biz.id | |
join stats.views | |
on feeds.id = views.feed_id | |
and views.bot = 1 | |
and views.hit_at between '#{st}' and '#{en}' | |
where biz.plan <> 3 | |
group by 1,2,3 | |
), random_enum as ( | |
select sum(round((fee_in_dollars / 510.0)::numeric)) fee_in_dollars | |
from biz | |
where biz.plan = 3 | |
) | |
select timestmp, | |
sum(round((view_count * cpc_in_dollars)::numeric)) + random_enum.fee_in_dollars cnt | |
from data, | |
random_enum | |
group by timestmp, | |
random_enum.fee_in_dollars | |
order by timestmp | |
").map do |g| | |
[ | |
(DateTime.parse(g['timestmp'].to_s).in_time_zone('timezone') + 1.hour), g['cnt'].to_i | |
] | |
end.to_h | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment