From 243e06533b808118c2ec2a48dcad298923e666d4 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Thu, 29 May 2014 07:19:55 +0000 Subject: [PATCH 1/4] VRFS-1665 scheduled session impl --- ruby/lib/jam_ruby.rb | 2 + .../models/email_batch_scheduled_sessions.rb | 112 ++++++++++++++++++ ruby/lib/jam_ruby/models/score.rb | 3 + .../resque/scheduled/daily_session_emailer.rb | 15 +++ ruby/spec/factories.rb | 5 +- .../email_batch_spec_scheduled_session.rb | 90 ++++++++++++++ web/config/scheduler.yml | 5 + 7 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb create mode 100644 ruby/lib/jam_ruby/resque/scheduled/daily_session_emailer.rb create mode 100644 ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb diff --git a/ruby/lib/jam_ruby.rb b/ruby/lib/jam_ruby.rb index 48f217d47..f51815bf8 100755 --- a/ruby/lib/jam_ruby.rb +++ b/ruby/lib/jam_ruby.rb @@ -43,6 +43,7 @@ require "jam_ruby/resque/scheduled/icecast_source_check" require "jam_ruby/resque/scheduled/cleanup_facebook_signup" require "jam_ruby/resque/scheduled/unused_music_notation_cleaner" require "jam_ruby/resque/scheduled/user_progress_emailer" +require "jam_ruby/resque/scheduled/daily_session_emailer" require "jam_ruby/resque/google_analytics_event" require "jam_ruby/mq_router" require "jam_ruby/base_manager" @@ -149,6 +150,7 @@ require "jam_ruby/models/email_batch" require "jam_ruby/models/email_batch_periodic" require "jam_ruby/models/email_batch_new_musician" require "jam_ruby/models/email_batch_progression" +require "jam_ruby/models/email_batch_scheduled_sessions" require "jam_ruby/models/email_batch_set" require "jam_ruby/models/email_error" require "jam_ruby/app/mailers/async_mailer" diff --git a/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb new file mode 100644 index 000000000..e2b6fc937 --- /dev/null +++ b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb @@ -0,0 +1,112 @@ +module JamRuby + class EmailBatchScheduledSessions < EmailBatchPeriodic + + BATCH_SIZE = 500 + SINCE_DAYS = 2 + MIN_HOURS_START = 2 + + TMP_SNAP = 'scheduled_session_snapshot' + TMP_USER = '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 + + def prep_tmp_table + ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_SNAP}") + sql =< '#{time_since_last_batch(SINCE_DAYS)}' AND + scheduled_start >= '#{Time.now() + MIN_HOURS_START.hours}' AND + (rrrs.rsvp_slot_id IS NULL OR rrrs.chosen != 't') +SQL + ActiveRecord::Base.connection.execute(sql) + end + + def fetch_recipients + objs = [] + self.prep_tmp_table + ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_USER}") + sql =< JamRuby::EmailBatchProgression do end + factory :email_batch_scheduled_session, :class => JamRuby::EmailBatchScheduledSessions do + end + factory :notification, :class => JamRuby::Notification do factory :notification_text_message do @@ -474,7 +477,7 @@ FactoryGirl.define do factory :rsvp_slot, class: JamRuby::RsvpSlot do association :instrument, factory: :instrument association :music_session, factory: :music_session - association :rsvp_request_slot, factory: :rsvp_request_slot + # association :rsvp_request_slot, factory: :rsvp_request_slot proficiency_level 'beginner' end diff --git a/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb b/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb new file mode 100644 index 000000000..e2cf73aef --- /dev/null +++ b/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb @@ -0,0 +1,90 @@ +require 'spec_helper' + +describe EmailBatch do + + after(:each) do + Timecop.return + end + + describe 'daily scheduled' do + # before { pending } + + let (:scheduled_batch) { FactoryGirl.create(:email_batch_scheduled_session) } + + let (:drums) { FactoryGirl.create(:instrument, :description => 'drums') } + let (:guitar) { FactoryGirl.create(:instrument, :description => 'guitar') } + let (:bass) { FactoryGirl.create(:instrument, :description => 'bass') } + let (:vocals) { FactoryGirl.create(:instrument, :description => 'vocal') } + + let (:drummer) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + let (:guitarist) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + let (:bassist) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + let (:vocalist) { FactoryGirl.create(:user, + :last_jam_locidispid => 1, + :last_jam_addr => 1) } + + let (:session1) do + FactoryGirl.create(:music_session, + :creator => drummer, + :scheduled_start => Time.now() + 2.days, + :musician_access => true, + :approval_required => false) + end + let (:session2) do + FactoryGirl.create(:music_session, + :creator => drummer, + :scheduled_start => Time.now() + 2.days, + :musician_access => true, + :approval_required => false) + end + + before(:all) do + drummer.musician_instruments << FactoryGirl.build(:musician_instrument, user: drummer, instrument: drums, proficiency_level: 2) + drummer.musician_instruments << FactoryGirl.build(:musician_instrument, user: drummer, instrument: guitar, proficiency_level: 2) + + guitarist.musician_instruments << FactoryGirl.build(:musician_instrument, user: guitarist, instrument: guitar, proficiency_level: 2) + guitarist.musician_instruments << FactoryGirl.build(:musician_instrument, user: guitarist, instrument: bass, proficiency_level: 2) + + bassist.musician_instruments << FactoryGirl.build(:musician_instrument, user: bassist, instrument: bass, proficiency_level: 2) + bassist.musician_instruments << FactoryGirl.build(:musician_instrument, user: bassist, instrument: guitar, proficiency_level: 2) + + vocalist.musician_instruments << FactoryGirl.build(:musician_instrument, user: vocalist, instrument: vocals, proficiency_level: 2) + + FactoryGirl.create(:rsvp_slot, :instrument => drums, :music_session => session1) + FactoryGirl.create(:rsvp_slot, :instrument => guitar, :music_session => session1) + FactoryGirl.create(:rsvp_slot, :instrument => bass, :music_session => session1) + + FactoryGirl.create(:rsvp_slot, :instrument => drums, :music_session => session2) + FactoryGirl.create(:rsvp_slot, :instrument => guitar, :music_session => session2) + FactoryGirl.create(:rsvp_slot, :instrument => bass, :music_session => session2) + # oo = FactoryGirl.create(:rsvp_slot, :instrument => vocals, :music_session => session1) + # oo.rsvp_request_slot.update_attributes(chosen: true) + # oo = FactoryGirl.create(:rsvp_request, :user => vocalist, :rsvp_slot => oo) + # oo.rsvp_request_slot.update_attributes(chosen: true) + + JamRuby::Score.delete_all + JamRuby::Score.createx(1, 'a', 1, 1, 'a', 1, 10) + end + + before(:each) do + # @u1 = FactoryGirl.create(:user, :email => 'jonathan@jamkazam.com', :subscribe_email => true) + # @u2 = FactoryGirl.create(:user, :subscribe_email => true) + # @u3 = FactoryGirl.create(:user, :subscribe_email => false) + # @u4 = FactoryGirl.create(:user, :subscribe_email => true) + end + + it 'sets up data properly' do + expect(drummer.instruments.include?(drums)).to eq(true) + expect(drummer.instruments.include?(guitar)).to eq(true) + obj = scheduled_batch.fetch_recipients + expect(obj.count).to eq(3) + end + + end +end diff --git a/web/config/scheduler.yml b/web/config/scheduler.yml index 3617cd3ed..b5f180ea1 100644 --- a/web/config/scheduler.yml +++ b/web/config/scheduler.yml @@ -33,3 +33,8 @@ UserProgressEmailer: cron: "30 21 * * *" class: "JamRuby::UserProgressEmailer" description: "Sends periodic user progress emails" + +DailySessionEmailer: + cron: "0 6 * * *" + class: "JamRuby::DailySessionEmailer" + description: "Sends daily scheduled session emails" From f4533ebbf112eea03902ba77024fdc6cce0992ce Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Thu, 29 May 2014 07:41:28 +0000 Subject: [PATCH 2/4] VRFS-1665 added non-pass case --- .../jam_ruby/models/email_batch_spec_scheduled_session.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb b/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb index e2cf73aef..194a2ad47 100644 --- a/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb +++ b/ruby/spec/jam_ruby/models/email_batch_spec_scheduled_session.rb @@ -28,6 +28,9 @@ describe EmailBatch do let (:vocalist) { FactoryGirl.create(:user, :last_jam_locidispid => 1, :last_jam_addr => 1) } + let (:loser) { FactoryGirl.create(:user, + :last_jam_locidispid => 2, + :last_jam_addr => 2) } let (:session1) do FactoryGirl.create(:music_session, @@ -56,6 +59,9 @@ describe EmailBatch do vocalist.musician_instruments << FactoryGirl.build(:musician_instrument, user: vocalist, instrument: vocals, proficiency_level: 2) + loser.musician_instruments << FactoryGirl.build(:musician_instrument, user: loser, instrument: vocals, proficiency_level: 2) + loser.musician_instruments << FactoryGirl.build(:musician_instrument, user: loser, instrument: drums, proficiency_level: 2) + FactoryGirl.create(:rsvp_slot, :instrument => drums, :music_session => session1) FactoryGirl.create(:rsvp_slot, :instrument => guitar, :music_session => session1) FactoryGirl.create(:rsvp_slot, :instrument => bass, :music_session => session1) @@ -70,6 +76,7 @@ describe EmailBatch do JamRuby::Score.delete_all JamRuby::Score.createx(1, 'a', 1, 1, 'a', 1, 10) + JamRuby::Score.createx(1, 'a', 1, 2, 'a', 2, Score::MAX_YELLOW_LATENCY + 1) end before(:each) do From f90f55fb11ec15d13c78d327e1b91eb8648ce031 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Thu, 29 May 2014 09:15:46 +0000 Subject: [PATCH 3/4] VRFS-1665 added mail delivery test --- ruby/lib/jam_ruby/app/mailers/user_mailer.rb | 17 ++++++++++++ .../scheduled_session_daily.html.erb | 10 +++++++ .../scheduled_session_daily.text.erb | 23 ++++++++++++++++ .../models/email_batch_scheduled_sessions.rb | 27 ++++++++++++------- ruby/lib/jam_ruby/models/email_batch_set.rb | 11 ++++++++ .../email_batch_spec_scheduled_session.rb | 21 ++++++++++----- 6 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb create mode 100644 ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index 23bdd724c..975e8f9a8 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -391,6 +391,23 @@ end end + def scheduled_session_daily(receiver, sessions) + sendgrid_category "Notification" + sendgrid_unique_args :type => "scheduled_session_daily" + + sendgrid_recipients([receiver.email]) + sendgrid_substitute('@USERID', [receiver.id]) + + @user = receiver + + mail(:to => receiver.email, + :subject => EmailBatchScheduledSessions.subject, + :title => 'New Scheduled Sessions Matched to You') do |format| + format.text + format.html + end + end + def band_session_join(email, msg, session_id) subject = "A band that you follow has joined a session" unique_args = {:type => "band_session_join"} diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb new file mode 100644 index 000000000..4a1fc0718 --- /dev/null +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb @@ -0,0 +1,10 @@ +<% provide(:title, 'Scheduled Session RSVP') %> + +

<%= @body %>

+ +

+ <%= @session_name %>
+ <%= @session_date %> +

+ +

View Session Details

diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb new file mode 100644 index 000000000..c5515de0c --- /dev/null +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb @@ -0,0 +1,23 @@ +<% provide(:title, @title) %> + +

Hello <%= @user.first_name %> -- +

+ +

The following new sessions that that have been posted during the last 24 hours: +

+
    +
  1. Need someone who plays an instrument that you play
  2. +
  3. Were posted by someone to whom you have either a good or medium latency connection
  4. +
+ +

Take a look through these new sessions below, and just click the RSVP button on the far right side of the row for any session in which you'd like to play. This will let the session organizer know you're interested, and you'll be notified if the session organizer accepts your request to play in that session! +

+ +HERE WE PRESENT THE SESSIONS IN A TABLE JUST LIKE THE FIND SESSION PAGE + +

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. +

+ +

Best Regards,

+ +Team JamKazam diff --git a/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb index e2b6fc937..4608ed95b 100644 --- a/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb +++ b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb @@ -80,25 +80,29 @@ WHERE SQL results = ActiveRecord::Base.connection.execute(sql) results.each do |result| - sql = "SELECT session_id, creator_id FROM #{TMP_USER} WHERE user_id = '#{result['user_id']}'" + sql =< false) end - before(:all) do + before(:each) do + MusicianInstrument.delete_all + RsvpSlot.delete_all + JamRuby::Score.delete_all + drummer.musician_instruments << FactoryGirl.build(:musician_instrument, user: drummer, instrument: drums, proficiency_level: 2) drummer.musician_instruments << FactoryGirl.build(:musician_instrument, user: drummer, instrument: guitar, proficiency_level: 2) @@ -74,16 +82,11 @@ describe EmailBatch do # oo = FactoryGirl.create(:rsvp_request, :user => vocalist, :rsvp_slot => oo) # oo.rsvp_request_slot.update_attributes(chosen: true) - JamRuby::Score.delete_all JamRuby::Score.createx(1, 'a', 1, 1, 'a', 1, 10) JamRuby::Score.createx(1, 'a', 1, 2, 'a', 2, Score::MAX_YELLOW_LATENCY + 1) end before(:each) do - # @u1 = FactoryGirl.create(:user, :email => 'jonathan@jamkazam.com', :subscribe_email => true) - # @u2 = FactoryGirl.create(:user, :subscribe_email => true) - # @u3 = FactoryGirl.create(:user, :subscribe_email => false) - # @u4 = FactoryGirl.create(:user, :subscribe_email => true) end it 'sets up data properly' do @@ -93,5 +96,11 @@ describe EmailBatch do expect(obj.count).to eq(3) end + it 'sends email' do + ebatch = scheduled_batch + ebatch.deliver_batch + expect(UserMailer.deliveries.length).to eq(3) + end + end end From ff15da9f4e8ebe04034b51d6bc3901f1cd73266d Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Thu, 29 May 2014 16:32:22 +0000 Subject: [PATCH 4/4] VRFS-1665 mail formatting; bug fixing --- ruby/lib/jam_ruby/app/mailers/user_mailer.rb | 7 +-- .../scheduled_session_daily.html.erb | 42 ++++++++++++--- .../scheduled_session_daily.text.erb | 26 +++++---- .../models/email_batch_scheduled_sessions.rb | 54 +++++++++++-------- ruby/lib/jam_ruby/models/music_session.rb | 2 + 5 files changed, 85 insertions(+), 46 deletions(-) diff --git a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb index 975e8f9a8..9b2406c0d 100644 --- a/ruby/lib/jam_ruby/app/mailers/user_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/user_mailer.rb @@ -391,7 +391,7 @@ end end - def scheduled_session_daily(receiver, sessions) + def scheduled_session_daily(receiver, sessions_and_latency) sendgrid_category "Notification" sendgrid_unique_args :type => "scheduled_session_daily" @@ -399,10 +399,11 @@ sendgrid_substitute('@USERID', [receiver.id]) @user = receiver + @sessions_and_latency = sessions_and_latency + @title = 'New Scheduled Sessions Matched to You' mail(:to => receiver.email, - :subject => EmailBatchScheduledSessions.subject, - :title => 'New Scheduled Sessions Matched to You') do |format| + :subject => EmailBatchScheduledSessions.subject) do |format| format.text format.html end diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb index 4a1fc0718..40788d8de 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.html.erb @@ -1,10 +1,38 @@ -<% provide(:title, 'Scheduled Session RSVP') %> +<% provide(:title, @title) %> -

<%= @body %>

- -

- <%= @session_name %>
- <%= @session_date %> +

Hello <%= @user.first_name %> --

-

View Session Details

+

The following new sessions that that have been posted during the last 24 hours: +

+
    +
  1. Need someone who plays an instrument that you play
  2. +
  3. Were posted by someone to whom you have either a good or medium latency connection
  4. +
+ +

Take a look through these new sessions below, and just click the RSVP button on the far right side of the row for any session in which you'd like to play. This will let the session organizer know you're interested, and you'll be notified if the session organizer accepts your request to play in that session! +

+ + + + + + + + + +<% @sessions_and_latency.each do |sess| %> + + + + + +<% end %> +
GENREDESCRIPTIONLATENCY
<%= sess.genre.description %><%= sess.description %><%= sess.latency_store %>
+ +

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. +

+ +

Best Regards,

+ +Team JamKazam diff --git a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb index c5515de0c..16ce1e7c1 100644 --- a/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb +++ b/ruby/lib/jam_ruby/app/views/jam_ruby/user_mailer/scheduled_session_daily.text.erb @@ -1,23 +1,21 @@ <% provide(:title, @title) %> -

Hello <%= @user.first_name %> -- -

+Hello <%= @user.first_name %> -- -

The following new sessions that that have been posted during the last 24 hours: -

-
    -
  1. Need someone who plays an instrument that you play
  2. -
  3. Were posted by someone to whom you have either a good or medium latency connection
  4. -
+The following new sessions that that have been posted during the last 24 hours: -

Take a look through these new sessions below, and just click the RSVP button on the far right side of the row for any session in which you'd like to play. This will let the session organizer know you're interested, and you'll be notified if the session organizer accepts your request to play in that session! -

+1. Need someone who plays an instrument that you play +2. Were posted by someone to whom you have either a good or medium latency connection -HERE WE PRESENT THE SESSIONS IN A TABLE JUST LIKE THE FIND SESSION PAGE +Take a look through these new sessions below, and just click the RSVP button on the far right side of the row for any session in which you'd like to play. This will let the session organizer know you're interested, and you'll be notified if the session organizer accepts your request to play in that session! -

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. -

+GENRE | DESCRIPTION | LATENCY +<% @sessions_and_latency.each do |sess| %> +<%= sess.genre.description %> | <%= sess.description %> | <%= sess.latency_store %> +<% end %> -

Best Regards,

+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. + +Best Regards, Team JamKazam diff --git a/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb index 4608ed95b..db13ea5b8 100644 --- a/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb +++ b/ruby/lib/jam_ruby/models/email_batch_scheduled_sessions.rb @@ -12,17 +12,18 @@ module JamRuby "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 + # 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 ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{TMP_SNAP}") sql =<