* VRFS-2530 - denormalize online for performance reasons

This commit is contained in:
Seth Call 2014-12-07 16:04:10 -06:00
parent 34f0aff328
commit 53ce955838
6 changed files with 105 additions and 2 deletions

View File

@ -231,3 +231,4 @@ shopping_carts.sql
recurly.sql
add_track_resource_id.sql
user_genres.sql
user_online.sql

25
db/up/user_online.sql Normal file
View File

@ -0,0 +1,25 @@
-- this was patched on the server without a proper migration, so the migration script here takes care to make sure it can run 2x
ALTER TABLE users DROP COLUMN IF EXISTS online;
DROP TRIGGER IF EXISTS connection_up_down ON connections;
ALTER TABLE users ADD COLUMN online BOOL DEFAULT FALSE NOT NULL;
CREATE OR REPLACE FUNCTION manage_user_online() RETURNS TRIGGER
STRICT VOLATILE AS $$
BEGIN
IF (TG_OP = 'DELETE') THEN
UPDATE users set ONLINE = (SELECT COUNT(conn.id) FROM connections conn WHERE conn.user_id = OLD.user_id) > 0 WHERE users.id = OLD.user_id;
RETURN NULL;
ELSIF (TG_OP = 'INSERT') THEN
UPDATE users set ONLINE = (SELECT COUNT(conn.id) FROM connections conn WHERE conn.user_id = NEW.user_id) > 0 WHERE users.id = NEW.user_id;
RETURN NULL;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER connection_up_down AFTER INSERT OR DELETE
ON connections FOR EACH ROW EXECUTE PROCEDURE manage_user_online(user_id);
-- ONE time bootstrap the new online field
UPDATE users set online = (SELECT COUNT(conn.id) FROM connections conn WHERE conn.user_id = users.id) > 0;

View File

@ -267,7 +267,7 @@ module JamRuby
notification_msg = format_msg(notification.description, {:user => user})
if friend.online
if friend.online?
msg = @@message_factory.friend_request(
friend.id,
friend_request_id,

View File

@ -253,7 +253,7 @@ module JamRuby
end
def online
@online ||= !self.connections.nil? && self.connections.size > 0
online?
end
def name

View File

@ -103,4 +103,79 @@ describe JamRuby::Connection do
end
end
describe "triggers" do
describe "manage_user_online" do
it "offline for new user" do
user.online.should be_false
end
it "online/offline based on connection up/down" do
conn.touch # make a connection
user.reload
user.online.should be_true
conn.delete
user.reload
user.online.should be_false
end
it "verify that online up/down is only for affected user (no spillover)" do
user2 = FactoryGirl.create(:user)
user2.online.should be_false
conn.touch
user.reload
user2.reload
user.online.should be_true
user2.online.should be_false # make sure there is no spillover to unrelated users
conn2 = FactoryGirl.create(:connection,
:user => user2,
:music_session => music_session,
:ip_address => "1.1.1.1",
:client_id => "2")
user.reload
user2.reload
user.online.should be_true
user2.online.should be_true
# whack one of the connections
conn.delete
user.reload
user2.reload
user.online.should be_false
user2.online.should be_true
conn2.delete
user.reload
user2.reload
user.online.should be_false
user2.online.should be_false
end
it "n connections" do
conn.touch
conn2 = FactoryGirl.create(:connection,
:user => user,
:music_session => music_session,
:ip_address => "1.1.1.1",
:client_id => "2")
user.reload
user.online.should be_true
conn.delete
user.reload
user.online.should be_true
conn2.delete
user.reload
user.online.should be_false
end
end
end
end

View File

@ -779,6 +779,7 @@ describe Notification do
it "success when online" do
receiver_connection = FactoryGirl.create(:connection, user: @receiver)
@receiver.reload
message = "Just a test message!"
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, @sender, @receiver)
@ -796,6 +797,7 @@ describe Notification do
it "success when online with long message" do
receiver_connection = FactoryGirl.create(:connection, user: @receiver)
@receiver.reload
message = "0" * 203 # 200 is clip size
calls = count_publish_to_user_calls
notification = Notification.send_text_message(message, @sender, @receiver)