VRFS-1665 rewrote daily email queries and adding pagination; other tweaks
This commit is contained in:
parent
2719b35dee
commit
7ea659c71f
|
|
@ -25,7 +25,7 @@
|
|||
<tr>
|
||||
<td><%= sess.genre.description %></td>
|
||||
<td><%= sess.description %></td>
|
||||
<td style="text-align:center"><%= sess.latency_store %></td>
|
||||
<td style="text-align:center"><%= sess.latency %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ Take a look through these new sessions below, and just click the RSVP button on
|
|||
|
||||
GENRE | DESCRIPTION | LATENCY
|
||||
<% @sessions_and_latency.each do |sess| %>
|
||||
<%= sess.genre.description %> | <%= sess.description %> | <%= sess.latency_store %>
|
||||
<%= sess.genre.description %> | <%= sess.description %> | <%= sess.latency %>
|
||||
<% end %>
|
||||
|
||||
To see ALL the scheduled sessions that you might be interested in joining, view our Find Session page at: http://www.jamkazam.com/client#/findSession.
|
||||
|
|
|
|||
|
|
@ -69,7 +69,9 @@ SQL
|
|||
self._fetch_eligible_receivers
|
||||
|
||||
countsql = "SELECT DISTINCT COUNT(receiver_id) AS num FROM #{TMP_PAIRS} GROUP BY receiver_id"
|
||||
num_pair = ActiveRecord::Base.connection.execute(countsql)[0]['num'].to_i
|
||||
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
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ module JamRuby
|
|||
SINCE_DAYS = 2
|
||||
MIN_HOURS_START = 2
|
||||
|
||||
TMP_SNAP = 'tmp_scheduled_session_snapshot'
|
||||
TMP_USER = 'tmp_scheduled_session_user'
|
||||
TMP_SESS = 'tmp_candidate_sessions'
|
||||
TMP_RECIP = 'tmp_candidate_recipients'
|
||||
TMP_MATCH = 'tmp_matches'
|
||||
|
||||
def self.subject
|
||||
"New sessions have been scheduled that may be a good match for you!"
|
||||
|
|
@ -14,22 +15,23 @@ module JamRuby
|
|||
|
||||
# inserts eligible sessions to temp table
|
||||
def _collect_eligible_sessions
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_SNAP}")
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_SESS}")
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
rs.instrument_id AS instrument_id,
|
||||
msess.id AS session_id,
|
||||
msess.user_id AS creator_id,
|
||||
users.last_jam_locidispid AS creator_score_idx
|
||||
INTO TEMP TABLE #{TMP_SNAP}
|
||||
msess.id AS session_id,
|
||||
msess.user_id AS creator_id,
|
||||
users.last_jam_locidispid AS creator_score_idx,
|
||||
rs.instrument_id
|
||||
INTO TEMP TABLE #{TMP_SESS}
|
||||
FROM music_sessions msess
|
||||
INNER JOIN users ON users.id = msess.user_id
|
||||
INNER JOIN rsvp_slots AS rs ON rs.music_session_id = msess.id
|
||||
LEFT JOIN rsvp_requests_rsvp_slots AS rrrs ON rrrs.rsvp_slot_id = rs.id
|
||||
INNER JOIN users ON users.id = msess.user_id
|
||||
WHERE
|
||||
musician_access = 't' AND
|
||||
approval_required = 'f' AND
|
||||
msess.created_at > '#{time_since_last_batch(SINCE_DAYS)}' AND
|
||||
msess.created_at < '#{self.created_at}' AND
|
||||
scheduled_start >= '#{Time.now() + MIN_HOURS_START.hours}' AND
|
||||
(rrrs.rsvp_slot_id IS NULL OR rrrs.chosen != 't')
|
||||
SQL
|
||||
|
|
@ -37,41 +39,65 @@ SQL
|
|||
end
|
||||
|
||||
def _collect_eligible_recipients
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_USER}")
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_RECIP}")
|
||||
# load eligible recipients into tmp table
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
users.id AS user_id,
|
||||
users.last_jam_locidispid AS user_score_idx,
|
||||
tmp.session_id AS session_id,
|
||||
tmp.creator_id AS creator_id,
|
||||
tmp.creator_score_idx AS creator_score_idx
|
||||
INTO TEMP TABLE #{TMP_USER}
|
||||
users.id AS receiver_id,
|
||||
users.last_jam_locidispid AS receiver_score_idx
|
||||
INTO TEMP TABLE #{TMP_RECIP}
|
||||
FROM users
|
||||
INNER JOIN musicians_instruments AS mi ON mi.user_id = users.id
|
||||
LEFT JOIN #{TMP_SNAP} AS tmp ON tmp.instrument_id = mi.instrument_id
|
||||
JOIN #{TMP_SESS} ON #{TMP_SESS}.instrument_id = mi.instrument_id
|
||||
WHERE
|
||||
users.musician = 't' AND
|
||||
users.subscribe_email = 't' AND
|
||||
tmp.session_id IS NOT NULL
|
||||
GROUP BY users.id, tmp.session_id, tmp.creator_id, tmp.creator_score_idx
|
||||
users.subscribe_email = 't'
|
||||
SQL
|
||||
ActiveRecord::Base.connection.execute(sql)
|
||||
end
|
||||
|
||||
def _collect_scored_recipients
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_MATCH}")
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
DISTINCT #{TMP_RECIP}.receiver_id,
|
||||
#{TMP_SESS}.session_id,
|
||||
scores.score AS latency
|
||||
INTO TEMP TABLE #{TMP_MATCH}
|
||||
FROM scores
|
||||
INNER JOIN #{TMP_SESS} ON #{TMP_SESS}.creator_score_idx = scores.alocidispid
|
||||
INNER JOIN #{TMP_RECIP} ON #{TMP_RECIP}.receiver_score_idx = scores.blocidispid
|
||||
WHERE
|
||||
scores.score < #{Score::MAX_YELLOW_LATENCY}
|
||||
GROUP BY #{TMP_RECIP}.receiver_id, #{TMP_SESS}.session_id, latency
|
||||
SQL
|
||||
ActiveRecord::Base.connection.execute(sql)
|
||||
end
|
||||
|
||||
# select recipients whose score is below minimum threshold
|
||||
def _select_scored_recipients
|
||||
sql =<<SQL
|
||||
SELECT DISTINCT user_id, scores.score AS latency
|
||||
FROM #{TMP_USER}
|
||||
INNER JOIN scores ON scores.alocidispid = #{TMP_USER}.creator_score_idx AND scores.blocidispid = #{TMP_USER}.user_score_idx
|
||||
WHERE
|
||||
scores.score < #{Score::MAX_YELLOW_LATENCY}
|
||||
def _select_scored_recipients(offset=0)
|
||||
if 0 > offset
|
||||
sql =<<SQL
|
||||
SELECT DISTINCT COUNT(receiver_id) AS num
|
||||
FROM #{TMP_MATCH}
|
||||
GROUP BY receiver_id
|
||||
SQL
|
||||
ActiveRecord::Base.connection.execute(sql)
|
||||
rr = ActiveRecord::Base.connection.execute(sql)
|
||||
return 0 < rr.count ? rr[0]['num'].to_i : 0
|
||||
else
|
||||
sql =<<SQL
|
||||
SELECT DISTINCT receiver_id
|
||||
FROM #{TMP_MATCH}
|
||||
GROUP BY receiver_id
|
||||
ORDER BY receiver_id ASC
|
||||
LIMIT #{@per_page}
|
||||
OFFSET #{offset}
|
||||
SQL
|
||||
return ActiveRecord::Base.connection.execute(sql)
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_recipients
|
||||
def fetch_recipients(per_page=500)
|
||||
objs = []
|
||||
|
||||
# load eligible sessions into tmp table
|
||||
|
|
@ -80,20 +106,24 @@ SQL
|
|||
# load eligible mail recipients into tmp table
|
||||
self._collect_eligible_recipients
|
||||
|
||||
# now just get the sessions/latency for each distinct mail recipient
|
||||
_select_scored_recipients.each do |result|
|
||||
user = User.find_by_id(result['user_id'])
|
||||
# load mail recipients with minimum score into tmp table
|
||||
self._collect_scored_recipients
|
||||
|
||||
sql = "SELECT session_id FROM #{TMP_USER} WHERE user_id = '#{user.id}'"
|
||||
sessions = ActiveRecord::Base.connection.execute(sql).collect do |rr|
|
||||
msess = MusicSession.where(['id = ?',rr['session_id']])
|
||||
.limit(1)
|
||||
@per_page = per_page
|
||||
num_recip = _select_scored_recipients(-1)
|
||||
loops = (num_recip / @per_page) + (num_recip % @per_page) - 1
|
||||
|
||||
0.upto(loops) do |nn|
|
||||
offset = nn * @per_page
|
||||
# now just get the sessions/latency for each distinct mail recipient
|
||||
_select_scored_recipients(offset).each do |result|
|
||||
receiver = User.find_by_id(result['receiver_id'])
|
||||
sessions = MusicSession.select("music_sessions.*, #{TMP_MATCH}.latency")
|
||||
.joins("INNER JOIN #{TMP_MATCH} ON #{TMP_MATCH}.session_id = music_sessions.id")
|
||||
.where(["#{TMP_MATCH}.receiver_id = ?", receiver.id])
|
||||
.includes([:genre, :creator])
|
||||
.first
|
||||
msess.latency_store = result['latency']
|
||||
msess
|
||||
block_given? ? yield(receiver, sessions) : objs << [receiver, sessions]
|
||||
end
|
||||
block_given? ? yield(user, sessions) : objs << [user, sessions]
|
||||
end
|
||||
objs
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,9 +10,6 @@ module JamRuby
|
|||
|
||||
attr_accessor :legal_terms, :recurring_mode, :language_description, :scheduled_start_time, :access_description
|
||||
|
||||
# used for temporary data store of latency between creator and some other user
|
||||
attr_accessor :latency_store
|
||||
|
||||
self.table_name = "music_sessions"
|
||||
|
||||
self.primary_key = 'id'
|
||||
|
|
|
|||
|
|
@ -19,9 +19,6 @@ module JamRuby
|
|||
# updating_password corresponds to a lost_password
|
||||
attr_accessor :updating_password, :updating_email, :updated_email, :update_email_confirmation_url, :administratively_created, :current_password, :setting_password, :confirm_current_password, :updating_avatar, :updating_progression_field, :mods_json
|
||||
|
||||
# used for temporary data store of latency between self and some other user
|
||||
attr_accessor :latency_store
|
||||
|
||||
belongs_to :icecast_server_group, class_name: "JamRuby::IcecastServerGroup", inverse_of: :users, foreign_key: 'icecast_server_group_id'
|
||||
|
||||
# authorizations (for facebook, etc -- omniauth)
|
||||
|
|
|
|||
|
|
@ -41,14 +41,16 @@ describe EmailBatchScheduledSessions do
|
|||
:creator => drummer,
|
||||
:scheduled_start => Time.now() + 2.days,
|
||||
:musician_access => true,
|
||||
:approval_required => false)
|
||||
:approval_required => false,
|
||||
:created_at => Time.now - 1.hour)
|
||||
end
|
||||
let (:session2) do
|
||||
FactoryGirl.create(:music_session,
|
||||
:creator => drummer,
|
||||
:scheduled_start => Time.now() + 2.days,
|
||||
:musician_access => true,
|
||||
:approval_required => false)
|
||||
:approval_required => false,
|
||||
:created_at => Time.now - 1.hour)
|
||||
end
|
||||
|
||||
before(:each) do
|
||||
|
|
|
|||
Loading…
Reference in New Issue