Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with most active rooms #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions backend/Storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,43 @@ var DB_NAME = 'uwchat',
DB_ADDESS = '127.0.0.1',
DB_PORT = 27017;

/**
* Inserts document into mongodb
* @param {String} coll_name name of the collection in the database
* @param {Object} data an object literal to insert into the database
*/
Storage.save = function(coll_name, data) {
var client = new Db(DB_NAME, new Server(DB_ADDESS, DB_PORT));
var client = new Db(DB_NAME, new Server(DB_ADDESS, DB_PORT));

client.open(function(p_client) {
client.collection(coll_name, function(err, collection) {
collection.insert(data, function(err, docs) {
sys.puts('log message ' + JSON.stringify(data) + '.');
client.close();
});
client.open(function(p_client) {
client.collection(coll_name, function(err, collection) {
collection.insert(data, function(err, docs) {
sys.puts('log message ' + JSON.stringify(data) + '.');
client.close();
});
});
});
}

/**
* Gets the most active rooms on UWChat. Does a SQL-like GROUP BY
* query to get the number of messages in the last half hour in each
* existing chat room.
* @param {String} coll_name name of the collection in the database
* @param {Function} callback callback to send document back to
*/
Storage.getActiveRooms = function(coll_name, callback){
var client = new Db(DB_NAME, new Server(DB_ADDESS, DB_PORT));

var SINCE = [300000, 600000, 1800000]; //5, 10, 30 minutes

client.open(function(p_client) {
client.collection(coll_name, function(err, collection) {
collection.group(['room'], {type: 'msg', timestamp: {$gt : new Date().getTime() - SINCE[2]}}, {sum:0}, function(doc, prev){ prev.sum += 1}, function(err, docs) {
sys.puts('log message ' + JSON.stringify(docs) + '.');
if(typeof callback == "function") callback(docs);
client.close();
});
});
});
});
}
12 changes: 11 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var fu = require("./fu"),
ch = require("./Channel"),
sys = require("sys"),
url = require("url"),
qs = require("querystring");
qs = require("querystring"),
storage = require('./Storage.js');

var SESSION_TIMEOUT = 60 * 1000;

Expand Down Expand Up @@ -442,3 +443,12 @@ fu.get("/send", function (req, res) {
res.simpleJSON(200, { rss: mem.rss });
});

fu.get("/activerooms", function (req, res) {
sys.puts('serv: activerooms');

storage.getActiveRooms('messages', function(data){
res.simpleJSON(200, data);
});

});

70 changes: 69 additions & 1 deletion frontend/css/chat_style.css
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ a:hover { text-decoration: underline; color: #aaa; }
text-align: center;
width: 600px;
margin: 0 auto;
margin-top: 200px;
margin-top: 50px;
z-index: 3;
}

Expand Down Expand Up @@ -173,3 +173,71 @@ a:hover { text-decoration: underline; color: #aaa; }
font-size:0.8em;
text-align:center;
}

/*
BUTTONS
*/
.button{
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
background: url("/images/button-grey.gif") repeat-x scroll left top #D8D8D8;
border-color: #BBBBBB #BBBBBB #999999;
border-style: solid;
border-width: 1px;
color: #333333;
cursor: pointer;
display: inline;
font-size: 0.6em;
font-weight: bold;
padding: 3px 6px;
text-align: center;
text-shadow: 0 1px #F0F0F0;
}

.join_button{
background-color: #C3DD82;
background-image: url("/images/button-green.gif");
border-bottom: 1px solid #648517;
border-color: #8CB332 #8CB332 #648517;
color: #406A24;
text-shadow: 0 1px #D4ED95;
}

.join_button:hover{
border-color: #BBBBBB #BBBBBB #999999;
color: #406A24;
}


#activerooms_splash{
text-align: center;
width: 250px;
margin: 0 auto;
margin-top: 75px;
z-index: 3;
}

#activerooms_splash > div{
clear: both;
}

#activerooms_splash h2{
font-size: 0.8em;
font-weight: normal;
text-decoration: underline;
margin-bottom: 10px;
}

#activerooms_splash .no_active_rooms{
color: grey;
font-size: 0.7em;
}

#activerooms_splash .room_name{
float: left;
font-size: 0.8em;
}

#activerooms_splash .join_button_div {
float: right;
}
Binary file added frontend/images/button-green.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/images/button-grey.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
</div>
<div id="uwc_divider"></div>
</div>


<div id = "activerooms_splash"><h2>Active Rooms</h2></div>

<div id="uwc_chat">
<h2>Room Title</h2>
<div id="log">
Expand Down
45 changes: 45 additions & 0 deletions frontend/js/chat_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,43 @@ function updateUptime () {
}
}

function updateActiveRooms (data) {
var topRoomsTable = $("#activerooms_splash"),
room,
content = '';

if (data.length == 0){
//No active rooms
topRoomsTable.append("<div class = 'no_active_rooms'>None</div>");
return;
}
//mongodb-native doesn't have a sort function after a
// group query. So we have to sort client side.
data.sort(function(a,b){ return b.sum - a.sum});

// What about using Mustache templates instead?
data.forEach(function(obj){
room = obj.room.length > 21 ? obj.room.substr(0, 20) + "..." : obj.room;

content += '<div>'
+ '<div class = "room_name">' + room + '</div>'
+ '<div class = "join_button_div">'
+ '<a href = "#" class = "button join_button" data-action = "'+obj.room+'">Join</a>'
+ '</div>'
+ '</div>';
});

topRoomsTable.append(content);

$("#activerooms_splash .join_button").click(function(e){
//Should probably abstract this out later.
var roomName = $(this).attr("data-action");
$("#nickInput").attr("value", roomName);
$("#connectButton").click();
});

}

var transmission_errors = 0;
var first_poll = true;

Expand Down Expand Up @@ -420,10 +457,17 @@ function showChat (nick) {

$("#loading").hide();
$("#example").hide();
$("#activerooms_splash").hide();

scrollDown();
}

function showActiveRooms () {
jQuery.get("/activerooms", {}, function (data) {
updateActiveRooms(eval("(" + data + ")"));
});
}

//we want to show a count of unread messages when the window does not have focus
function updateTitle(){
if (CONFIG.unread) {
Expand Down Expand Up @@ -590,6 +634,7 @@ $(document).ready(function() {
$("#log table").remove();

showConnect();
showActiveRooms();
});

//if we can, notify the server that we're going away.
Expand Down