jam-cloud/ruby/lib/jam_ruby/models/email_batch_new_musician.rb

118 lines
3.4 KiB
Ruby
Raw Normal View History

2014-05-16 07:58:06 +00:00
module JamRuby
class EmailBatchNewMusician < EmailBatchPeriodic
2014-05-18 00:12:01 +00:00
BATCH_SIZE = 500
2014-05-17 17:38:22 +00:00
2014-05-31 09:36:10 +00:00
SINCE_DAYS = 14
2014-05-16 07:58:06 +00:00
VAR_MUSICIAN_COUNT = "@MUSICIAN_COUNT"
VAR_MUSICIAN_TABLE = "@MUSICIAN_TABLE"
2014-05-31 19:09:18 +00:00
TMP_NEW = 'tmp_new_musicians'
2014-05-31 20:34:42 +00:00
TMP_PAIRS = 'tmp_receivers_new_musicians'
2014-05-31 19:09:18 +00:00
2014-05-16 07:58:06 +00:00
def self.subject
"New musicians with good Internet connections to you have joined JamKazam!"
end
2014-05-31 19:09:18 +00:00
# inserts eligible sessions to temp table
def _fetch_new_musicians
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_NEW}")
sql =<<SQL
SELECT
2014-05-31 20:34:42 +00:00
users.id AS new_user_id, users.last_jam_locidispid AS last_jam_locidispid
2014-05-31 19:09:18 +00:00
INTO TEMP TABLE #{TMP_NEW}
FROM users
WHERE
musician = 't' AND
last_jam_locidispid IS NOT NULL AND
users.created_at > '#{time_since_last_batch(SINCE_DAYS)}'
SQL
ActiveRecord::Base.connection.execute(sql)
end
2014-05-31 20:34:42 +00:00
def _fetch_eligible_receivers
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_PAIRS}")
# load eligible recipients into tmp table
sql =<<SQL
SELECT
#{TMP_NEW}.new_user_id,
receivers.id AS receiver_id,
2014-05-31 20:34:42 +00:00
scores.score AS latency
INTO TEMP TABLE #{TMP_PAIRS}
FROM scores
INNER JOIN users AS receivers ON receivers.last_jam_locidispid = scores.alocidispid
2014-05-31 20:34:42 +00:00
INNER JOIN #{TMP_NEW} ON #{TMP_NEW}.last_jam_locidispid = scores.blocidispid
WHERE
scores.score < #{Score::MAX_YELLOW_LATENCY} AND
receivers.id IN (
SELECT
rr.id
FROM users rr
FULL OUTER JOIN #{TMP_NEW} ON rr.id = #{TMP_NEW}.new_user_id
WHERE
rr.musician = 't' AND
rr.subscribe_email = 't' AND
rr.last_jam_locidispid IS NOT NULL AND
(#{TMP_NEW}.new_user_id IS NULL OR rr.id IS NULL)
)
2014-05-31 19:09:18 +00:00
SQL
ActiveRecord::Base.connection.execute(sql)
end
def fetch_recipients(per_page=500)
2014-05-31 19:09:18 +00:00
objs = []
# load new musicians into tmp table
self._fetch_new_musicians
# load email receivers into tmp table
self._fetch_eligible_receivers
countsql = "SELECT DISTINCT COUNT(receiver_id) AS num FROM #{TMP_PAIRS} GROUP BY receiver_id"
rr = ActiveRecord::Base.connection.execute(countsql)
num_pair = 0 < rr.count ? rr[0]['num'].to_i : 0
loops = (num_pair / per_page) + (num_pair % per_page) - 1
0.upto(loops) do |nn|
offset = nn * per_page
sql =<<SQL
SELECT DISTINCT receiver_id
FROM #{TMP_PAIRS}
GROUP BY receiver_id
ORDER BY receiver_id ASC
LIMIT #{per_page}
OFFSET #{offset}
SQL
ActiveRecord::Base.connection.execute(sql).each do |result|
receiver = User.find_by_id(result['receiver_id'])
new_musicians = User.select("users.*, #{TMP_PAIRS}.latency")
.joins("INNER JOIN #{TMP_PAIRS} ON #{TMP_PAIRS}.new_user_id = users.id")
.where(["#{TMP_PAIRS}.receiver_id = ?", receiver.id])
.includes(:instruments)
block_given? ? yield(receiver, new_musicians) : objs << [receiver, new_musicians]
2014-05-16 07:58:06 +00:00
end
2014-05-17 17:38:22 +00:00
end
2014-05-31 19:09:18 +00:00
objs
2014-05-17 17:38:22 +00:00
end
def deliver_batch_sets!
self.opt_in_count = 0
2014-05-31 19:09:18 +00:00
self.fetch_recipients do |user, new_musicians|
self.opt_in_count += 1
bset = EmailBatchSet.new_musician_set(self, user, new_musicians)
2014-06-03 03:25:23 +00:00
UserMailer.new_musicians(user, new_musicians).deliver
2014-05-16 07:58:06 +00:00
end
2014-05-31 19:09:18 +00:00
self.sent_count = self.opt_in_count
2014-05-16 07:58:06 +00:00
self.save
2014-05-17 17:38:22 +00:00
self.did_batch_run!
2014-05-16 07:58:06 +00:00
end
2014-05-31 09:36:10 +00:00
def self.send_new_musician_batch
oo = self.create
oo..deliver_batch
oo
end
2014-05-16 07:58:06 +00:00
end
end