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_CAND = 'tmp_receiver_candidates'
|
|
|
|
|
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_receiver_candidates
|
|
|
|
|
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_CAND}")
|
2014-05-31 19:09:18 +00:00
|
|
|
sql =<<SQL
|
|
|
|
|
SELECT
|
2014-05-31 20:34:42 +00:00
|
|
|
users.id AS receiver_candidate_id, users.last_jam_locidispid AS last_jam_locidispid
|
|
|
|
|
INTO TEMP TABLE #{TMP_CAND}
|
2014-05-31 19:09:18 +00:00
|
|
|
FROM users
|
2014-05-31 20:34:42 +00:00
|
|
|
FULL OUTER JOIN #{TMP_NEW} ON users.id = #{TMP_NEW}.new_user_id
|
2014-05-31 19:09:18 +00:00
|
|
|
WHERE
|
2014-05-31 20:34:42 +00:00
|
|
|
users.musician = 't' AND
|
|
|
|
|
users.subscribe_email = 't' AND
|
|
|
|
|
users.last_jam_locidispid IS NOT NULL AND
|
|
|
|
|
(#{TMP_NEW}.new_user_id IS NULL OR users.id IS NULL)
|
|
|
|
|
SQL
|
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
#{TMP_CAND}.receiver_candidate_id AS receiver_id,
|
|
|
|
|
scores.score AS latency
|
|
|
|
|
INTO TEMP TABLE #{TMP_PAIRS}
|
|
|
|
|
FROM scores
|
|
|
|
|
INNER JOIN #{TMP_CAND} ON #{TMP_CAND}.last_jam_locidispid = scores.alocidispid
|
|
|
|
|
INNER JOIN #{TMP_NEW} ON #{TMP_NEW}.last_jam_locidispid = scores.blocidispid
|
|
|
|
|
WHERE
|
2014-05-31 19:09:18 +00:00
|
|
|
scores.score < #{Score::MAX_YELLOW_LATENCY}
|
|
|
|
|
SQL
|
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def fetch_recipients
|
|
|
|
|
objs = []
|
|
|
|
|
# load new musicians into tmp table
|
|
|
|
|
self._fetch_new_musicians
|
|
|
|
|
|
2014-05-31 20:34:42 +00:00
|
|
|
# load receiver candidates into tmp table
|
|
|
|
|
self._fetch_receiver_candidates
|
|
|
|
|
|
2014-05-31 19:09:18 +00:00
|
|
|
# load email receivers into tmp table
|
|
|
|
|
self._fetch_eligible_receivers
|
|
|
|
|
|
2014-05-31 20:41:48 +00:00
|
|
|
sql = "SELECT DISTINCT receiver_id FROM #{TMP_PAIRS} GROUP BY receiver_id"
|
2014-05-31 19:09:18 +00:00
|
|
|
ActiveRecord::Base.connection.execute(sql).each do |result|
|
2014-05-31 20:41:48 +00:00
|
|
|
receiver = User.find_by_id(result['receiver_id'])
|
2014-05-31 19:09:18 +00:00
|
|
|
|
2014-05-31 20:41:48 +00:00
|
|
|
sql = "SELECT new_user_id, latency FROM #{TMP_PAIRS} WHERE receiver_id = '#{receiver.id}'"
|
2014-05-31 19:09:18 +00:00
|
|
|
new_musicians = ActiveRecord::Base.connection.execute(sql).collect do |rr|
|
|
|
|
|
new_user = User.where(['id = ?',rr['new_user_id']])
|
|
|
|
|
.limit(1)
|
|
|
|
|
.includes([:instruments])
|
|
|
|
|
.first
|
|
|
|
|
new_user.latency_store = result['latency']
|
|
|
|
|
new_user
|
2014-05-16 07:58:06 +00:00
|
|
|
end
|
2014-05-31 19:09:18 +00:00
|
|
|
block_given? ? yield(receiver, new_musicians) : objs << [receiver, new_musicians]
|
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)
|
|
|
|
|
UserMailer.new_musicians(uu, 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
|