VRFS-1664 tweaking query
This commit is contained in:
parent
edbd3b1c6d
commit
aa44302517
|
|
@ -9,7 +9,8 @@ module JamRuby
|
|||
VAR_MUSICIAN_TABLE = "@MUSICIAN_TABLE"
|
||||
|
||||
TMP_NEW = 'tmp_new_musicians'
|
||||
TMP_RECEIVERS = 'tmp_receivers_new_musicians'
|
||||
TMP_CAND = 'tmp_receiver_candidates'
|
||||
TMP_PAIRS = 'tmp_receivers_new_musicians'
|
||||
|
||||
def self.subject
|
||||
"New musicians with good Internet connections to you have joined JamKazam!"
|
||||
|
|
@ -20,7 +21,7 @@ module JamRuby
|
|||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_NEW}")
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
users.*
|
||||
users.id AS new_user_id, users.last_jam_locidispid AS last_jam_locidispid
|
||||
INTO TEMP TABLE #{TMP_NEW}
|
||||
FROM users
|
||||
WHERE
|
||||
|
|
@ -31,26 +32,37 @@ SQL
|
|||
ActiveRecord::Base.connection.execute(sql)
|
||||
end
|
||||
|
||||
def _fetch_receiver_candidates
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_CAND}")
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
users.id AS receiver_candidate_id, users.last_jam_locidispid AS last_jam_locidispid
|
||||
INTO TEMP TABLE #{TMP_CAND}
|
||||
FROM users
|
||||
FULL OUTER JOIN #{TMP_NEW} ON users.id = #{TMP_NEW}.new_user_id
|
||||
WHERE
|
||||
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_RECEIVERS}")
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_PAIRS}")
|
||||
# load eligible recipients into tmp table
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
users.id AS user_id,
|
||||
tmp_new.id AS new_user_id,
|
||||
scores.score AS latency
|
||||
INTO TEMP TABLE #{TMP_RECEIVERS}
|
||||
FROM users
|
||||
LEFT JOIN #{TMP_NEW} AS tmp_new_skip ON tmp_new_skip.id = users.id
|
||||
INNER JOIN #{TMP_NEW} AS tmp_new ON tmp_new.id = users.id
|
||||
INNER JOIN scores ON scores.alocidispid = users.last_jam_locidispid AND
|
||||
scores.blocidispid = tmp_new.last_jam_locidispid
|
||||
WHERE
|
||||
users.musician = 't' AND
|
||||
users.subscribe_email = 't' AND
|
||||
tmp_new_skip.id IS NULL AND
|
||||
#{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
|
||||
scores.score < #{Score::MAX_YELLOW_LATENCY}
|
||||
GROUP BY users.id, tmp_new.id, scores.score
|
||||
SQL
|
||||
ActiveRecord::Base.connection.execute(sql)
|
||||
binding.pry
|
||||
|
|
@ -61,15 +73,18 @@ SQL
|
|||
# load new musicians into tmp table
|
||||
self._fetch_new_musicians
|
||||
|
||||
# load receiver candidates into tmp table
|
||||
self._fetch_receiver_candidates
|
||||
|
||||
# load email receivers into tmp table
|
||||
self._fetch_eligible_receivers
|
||||
|
||||
sql = "SELECT DISTINCT user_id FROM #{TMP_RECEIVERS} GROUP BY user_id"
|
||||
sql = "SELECT DISTINCT user_id FROM #{TMP_PAIRS} GROUP BY user_id"
|
||||
binding.pry
|
||||
ActiveRecord::Base.connection.execute(sql).each do |result|
|
||||
receiver = User.find_by_id(result['user_id'])
|
||||
|
||||
sql = "SELECT new_user_id, latency FROM #{TMP_RECEIVERS} WHERE user_id = '#{user.id}'"
|
||||
sql = "SELECT new_user_id, latency FROM #{TMP_PAIRS} WHERE user_id = '#{user.id}'"
|
||||
new_musicians = ActiveRecord::Base.connection.execute(sql).collect do |rr|
|
||||
new_user = User.where(['id = ?',rr['new_user_id']])
|
||||
.limit(1)
|
||||
|
|
|
|||
Loading…
Reference in New Issue