VRFS-1664 new musicians email
This commit is contained in:
parent
689e0a8609
commit
edbd3b1c6d
|
|
@ -102,8 +102,8 @@
|
|||
end
|
||||
end
|
||||
|
||||
def new_musicians(user, new_nearby, host='www.jamkazam.com')
|
||||
@user, @new_nearby, @host = user, new_nearby, host
|
||||
def new_musicians(user, new_musicians, host='www.jamkazam.com')
|
||||
@user, @new_musicians, @host = user, new_musicians, host
|
||||
|
||||
sendgrid_recipients([user.email])
|
||||
sendgrid_substitute('@USERID', [user.id])
|
||||
|
|
|
|||
|
|
@ -8,38 +8,89 @@ module JamRuby
|
|||
VAR_MUSICIAN_COUNT = "@MUSICIAN_COUNT"
|
||||
VAR_MUSICIAN_TABLE = "@MUSICIAN_TABLE"
|
||||
|
||||
TMP_NEW = 'tmp_new_musicians'
|
||||
TMP_RECEIVERS = 'tmp_receivers_new_musicians'
|
||||
|
||||
def self.subject
|
||||
"New musicians with good Internet connections to you have joined JamKazam!"
|
||||
end
|
||||
|
||||
def fetch_recipients(since=nil)
|
||||
since ||= Time.now - SINCE_DAYS.days
|
||||
User.geocoded_users
|
||||
.email_opt_in
|
||||
.where(['created_at < ?', since])
|
||||
.find_in_batches(batch_size: EmailBatchNewMusician::BATCH_SIZE) do |users|
|
||||
new_musicians = users.inject({}) do |hh, uu|
|
||||
if 0 < (nearby = uu.nearest_musicians).count
|
||||
hh[uu] = nearby
|
||||
end
|
||||
hh
|
||||
# inserts eligible sessions to temp table
|
||||
def _fetch_new_musicians
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_NEW}")
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
users.*
|
||||
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
|
||||
|
||||
def _fetch_eligible_receivers
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_RECEIVERS}")
|
||||
# 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
|
||||
scores.score < #{Score::MAX_YELLOW_LATENCY}
|
||||
GROUP BY users.id, tmp_new.id, scores.score
|
||||
SQL
|
||||
ActiveRecord::Base.connection.execute(sql)
|
||||
binding.pry
|
||||
end
|
||||
|
||||
def fetch_recipients
|
||||
objs = []
|
||||
# load new musicians into tmp table
|
||||
self._fetch_new_musicians
|
||||
|
||||
# load email receivers into tmp table
|
||||
self._fetch_eligible_receivers
|
||||
|
||||
sql = "SELECT DISTINCT user_id FROM #{TMP_RECEIVERS} 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}'"
|
||||
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
|
||||
end
|
||||
yield(new_musicians) if block_given?
|
||||
block_given? ? yield(receiver, new_musicians) : objs << [receiver, new_musicians]
|
||||
end
|
||||
objs
|
||||
end
|
||||
|
||||
def deliver_batch_sets!
|
||||
self.opt_in_count = 0
|
||||
sent = 0
|
||||
self.fetch_recipients(self.time_since_last_batch(SINCE_DAYS)) do |new_musicians|
|
||||
self.opt_in_count += new_musicians.count
|
||||
self.email_batch_sets << (bset = EmailBatchSet.load_set(self, new_musicians.keys.map(&:id)))
|
||||
new_musicians.each do |uu, nearby|
|
||||
UserMailer.new_musicians(uu, nearby).deliver
|
||||
sent += 1
|
||||
end
|
||||
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
|
||||
end
|
||||
self.sent_count = sent
|
||||
self.sent_count = self.opt_in_count
|
||||
self.save
|
||||
self.did_batch_run!
|
||||
end
|
||||
|
|
|
|||
|
|
@ -5,26 +5,15 @@ module JamRuby
|
|||
SINCE_DAYS = 2
|
||||
MIN_HOURS_START = 2
|
||||
|
||||
TMP_SNAP = 'scheduled_session_snapshot'
|
||||
TMP_USER = 'scheduled_session_user'
|
||||
TMP_SNAP = 'tmp_scheduled_session_snapshot'
|
||||
TMP_USER = 'tmp_scheduled_session_user'
|
||||
|
||||
def self.subject
|
||||
"New sessions have been scheduled that may be a good match for you!"
|
||||
end
|
||||
|
||||
# def fetch_sessions
|
||||
# objs = []
|
||||
# MusicSession.open_sessions
|
||||
# .where(['created_at > ?', time_since_last_batch(SINCE_DAYS)])
|
||||
# .where(['scheduled_start >= ?', Time.now() + MIN_HOURS_START.hours])
|
||||
# .find_each do |ss|
|
||||
# block_given? ? yield(ss) : objs << ss
|
||||
# end
|
||||
# objs
|
||||
# end
|
||||
|
||||
# inserts eligible sessions to temp table
|
||||
def prep_tmp_table
|
||||
def _collect_eligible_sessions
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_SNAP}")
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
|
|
@ -47,12 +36,8 @@ SQL
|
|||
ActiveRecord::Base.connection.execute(sql)
|
||||
end
|
||||
|
||||
def fetch_recipients
|
||||
objs = []
|
||||
# load eligible sessions into tmp table
|
||||
self.prep_tmp_table
|
||||
def _collect_eligible_recipients
|
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_USER}")
|
||||
|
||||
# load eligible recipients into tmp table
|
||||
sql =<<SQL
|
||||
SELECT
|
||||
|
|
@ -72,8 +57,10 @@ WHERE
|
|||
GROUP BY users.id, tmp.session_id, tmp.creator_id, tmp.creator_score_idx
|
||||
SQL
|
||||
ActiveRecord::Base.connection.execute(sql)
|
||||
end
|
||||
|
||||
# select recipients whose score is below minimum threshold
|
||||
# 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}
|
||||
|
|
@ -81,17 +68,23 @@ INNER JOIN scores ON scores.alocidispid = #{TMP_USER}.creator_score_idx AND scor
|
|||
WHERE
|
||||
scores.score < #{Score::MAX_YELLOW_LATENCY}
|
||||
SQL
|
||||
results = ActiveRecord::Base.connection.execute(sql)
|
||||
ActiveRecord::Base.connection.execute(sql)
|
||||
end
|
||||
|
||||
def fetch_recipients
|
||||
objs = []
|
||||
|
||||
# load eligible sessions into tmp table
|
||||
self._collect_eligible_sessions
|
||||
|
||||
# load eligible mail recipients into tmp table
|
||||
self._collect_eligible_recipients
|
||||
|
||||
# now just get the sessions/latency for each distinct mail recipient
|
||||
results.each do |result|
|
||||
sql =<<SQL
|
||||
SELECT session_id
|
||||
FROM #{TMP_USER}
|
||||
WHERE
|
||||
user_id = '#{result['user_id']}'
|
||||
SQL
|
||||
_select_scored_recipients.each do |result|
|
||||
user = User.find_by_id(result['user_id'])
|
||||
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -38,6 +38,17 @@ module JamRuby
|
|||
bset
|
||||
end
|
||||
|
||||
def self.new_musician_set(batch, receiver, new_musicians)
|
||||
bset = self.new
|
||||
bset.email_batch = batch
|
||||
bset.user = receiver
|
||||
bset.user_ids = new_musicians.map(&:id).join(',')
|
||||
bset.started_at = Time.now
|
||||
bset.batch_count = 1
|
||||
bset.save!
|
||||
bset
|
||||
end
|
||||
|
||||
def subject
|
||||
unless sub_type.blank?
|
||||
return EmailBatchProgression.subject(self.sub_type.to_sym)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ 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"
|
||||
|
|
|
|||
|
|
@ -19,6 +19,9 @@ 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)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe EmailBatch do
|
||||
describe EmailBatchScheduledSessions do
|
||||
|
||||
after(:each) do
|
||||
Timecop.return
|
||||
|
|
|
|||
Loading…
Reference in New Issue