From 5ad6db90ef7d4601ca3668e9f7a091e162ec4cf6 Mon Sep 17 00:00:00 2001 From: Seth Call Date: Mon, 16 Jun 2014 18:13:39 -0500 Subject: [PATCH] * VRFS-1684 - approved RSVP'ed users now have scores as well --- db/manifest | 1 + db/up/update_ams_index_2.sql | 69 +++++++++++++++++++ .../api_music_sessions_controller_spec.rb | 9 ++- web/spec/factories.rb | 1 + 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 db/up/update_ams_index_2.sql diff --git a/db/manifest b/db/manifest index 2600c5981..182876293 100755 --- a/db/manifest +++ b/db/manifest @@ -174,3 +174,4 @@ fix_users_location_fields.sql audio_latency.sql ams_index.sql update_ams_index.sql +update_ams_index_2.sql diff --git a/db/up/update_ams_index_2.sql b/db/up/update_ams_index_2.sql new file mode 100644 index 000000000..c37541b50 --- /dev/null +++ b/db/up/update_ams_index_2.sql @@ -0,0 +1,69 @@ +CREATE OR REPLACE FUNCTION ams_index (my_user_id VARCHAR, my_locidispid BIGINT, my_audio_latency INTEGER) RETURNS VOID STRICT VOLATILE AS $$ + BEGIN + -- output table to hold tagged music sessions with latency + CREATE TEMPORARY TABLE ams_music_session_tmp (music_session_id VARCHAR(64) NOT NULL, tag INTEGER, latency INTEGER) ON COMMIT DROP; + + -- populate ams_music_session_tmp as all music sessions + INSERT INTO ams_music_session_tmp SELECT DISTINCT id, NULL::INTEGER AS tag, NULL::INTEGER AS latency + FROM active_music_sessions; + + -- TODO worry about active music session where my_user_id is the creator? + -- eh, maybe, but if the music session is active and you're the creator wouldn't you already be in it? + -- so maybe you're on another computer, so why care? plus seth is talking about auto rsvp'ing the session + -- for you, so maybe not a problem. + + -- tag accepted rsvp as 1 + UPDATE ams_music_session_tmp q SET tag = 1 FROM rsvp_slots s, rsvp_requests_rsvp_slots rrs, rsvp_requests r WHERE + q.music_session_id = s.music_session_id AND + s.id = rrs.rsvp_slot_id AND + rrs.rsvp_request_id = r.id AND + r.user_id = my_user_id AND + rrs.chosen = TRUE AND + q.tag is NULL; + + -- tag invitation as 2 + UPDATE ams_music_session_tmp q SET tag = 2 FROM invitations i WHERE + q.music_session_id = i.music_session_id AND + i.receiver_id = my_user_id AND + q.tag IS NULL; + + -- musician access as 3 + UPDATE ams_music_session_tmp q SET tag = 3 FROM music_sessions m WHERE + q.music_session_id = m.id AND + m.musician_access = TRUE AND + q.tag IS NULL; + + -- delete anything not tagged + DELETE FROM ams_music_session_tmp WHERE tag IS NULL; + + -- output table to hold users involved in the ams_music_session_tmp sessions and their latency + CREATE TEMPORARY TABLE ams_users_tmp (music_session_id VARCHAR(64), user_id VARCHAR(64) NOT NULL, latency INTEGER) ON COMMIT DROP; + + -- populate ams_users_tmp with users that have a connection for sessions in the ams_music_session_tmp table, accompanied with full latency and music session + INSERT INTO ams_users_tmp SELECT c.music_session_id, c.user_id, (s.score+my_audio_latency+c.last_jam_audio_latency)/2 AS latency + FROM ams_music_session_tmp q + INNER JOIN connections c ON c.music_session_id = q.music_session_id + LEFT OUTER JOIN current_scores s ON s.alocidispid = c.locidispid + WHERE s.blocidispid = my_locidispid; + + -- populate ams_users_tmp with users that have an approved RSVP for sessions inthe ams_music_session_tmp table, accompanied with full latency and music session + -- specify NULL for music_session_id, because we don't want RSVP users to affect the AVG computed for each session later + INSERT INTO ams_users_tmp SELECT NULL, users.id, (s.score+my_audio_latency+users.last_jam_audio_latency)/2 AS latency + FROM ams_music_session_tmp q + INNER JOIN rsvp_slots ON rsvp_slots.music_session_id = q.music_session_id + INNER JOIN rsvp_requests_rsvp_slots ON rsvp_requests_rsvp_slots.rsvp_slot_id = rsvp_slots.id + INNER JOIN rsvp_requests ON rsvp_requests.id = rsvp_requests_rsvp_slots.rsvp_request_id + INNER JOIN users ON rsvp_requests.user_id = users.id + LEFT OUTER JOIN current_scores s ON s.alocidispid = users.last_jam_locidispid + WHERE + s.blocidispid = my_locidispid AND + rsvp_requests_rsvp_slots.chosen = TRUE AND + users.id NOT IN (SELECT user_id FROM ams_users_tmp); + + -- calculate the average latency + UPDATE ams_music_session_tmp q SET latency = (select AVG(u.latency) FROM ams_users_tmp u WHERE + q.music_session_id = u.music_session_id); + + RETURN; + END; +$$ LANGUAGE plpgsql; diff --git a/web/spec/controllers/api_music_sessions_controller_spec.rb b/web/spec/controllers/api_music_sessions_controller_spec.rb index 31a948bd8..d5ecd1823 100644 --- a/web/spec/controllers/api_music_sessions_controller_spec.rb +++ b/web/spec/controllers/api_music_sessions_controller_spec.rb @@ -62,7 +62,7 @@ describe ApiMusicSessionsController do get :ams_index, {client_id: conn.client_id} json = JSON.parse(response.body, :symbolize_names => true) json.length.should == 1 - json[0][:active_music_session][:participants][0][:user][:latency].should == 17.5 + json[0][:active_music_session][:participants][0][:user][:latency].should == 17.5.ceil end it "someone else with a score with a self, and another with an approved RSVP" do @@ -70,6 +70,11 @@ describe ApiMusicSessionsController do ams = FactoryGirl.create(:active_music_session, creator: other) other_conn.join_the_session(ams.music_session, true, tracks, other, 10) other_conn.errors.any?.should be_false + + # we need to make sure that third_user, (the RSVP user) has a locidispid matching the searching user + third_user.last_jam_audio_latency = 10 # RSVP's are an 'offline' search, meaning they use user.last_jam_audio_latency instead of connection.last_jam_audio_latency + third_user.last_jam_locidispid = conn.locidispid + third_user.save! Score.createx(conn.locidispid, conn.client_id, conn.addr, other_conn.locidispid, other_conn.client_id, other_conn.addr, 20, nil) rsvp_slot = FactoryGirl.create(:rsvp_slot, music_session: ams.music_session, instrument: Instrument.find('piano')) @@ -81,7 +86,7 @@ describe ApiMusicSessionsController do json.length.should == 1 json[0][:active_music_session][:participants][0][:user][:latency].should_not be_nil json[0][:approved_rsvps][0][:id].should == third_user.id - json[0][:approved_rsvps][0][:latency].should == 17.5 + json[0][:approved_rsvps][0][:latency].should == 17.5.ceil # ( 20 (network score) + 10 (third_user.last_jam_audio_latency)+ 5 (user.last_jam_audio_latency) ) / 2 end end end diff --git a/web/spec/factories.rb b/web/spec/factories.rb index 0c8cd23f8..51369b296 100644 --- a/web/spec/factories.rb +++ b/web/spec/factories.rb @@ -136,6 +136,7 @@ FactoryGirl.define do addr {JamIsp.ip_to_num(ip_address)} locidispid 0 client_type 'client' + last_jam_audio_latency { user.last_jam_audio_latency} end factory :friendship, :class => JamRuby::Friendship do