Commit 0e4ba663 authored by Andy Trevorah's avatar Andy Trevorah Committed by Andrew Newdigate

Merge pull request #2407 from troupe/feature/stop-table-scans-for-recent-public-chats

stop-table-scans-for-recent-public-chats
parents 8a2749ad 071c9911
......@@ -20,6 +20,7 @@ function createMessage(fixtureName, f) {
meta: f.meta,
sent: f.sent,
editedAt: f.editedAt,
pub: f.pub || false,
readBy: f.readBy,
});
}
......
......@@ -36,6 +36,11 @@ var CURRENT_META_DATA_VERSION = markdownMajorVersion;
/* @const */
var MAX_CHAT_EDIT_AGE_SECONDS = 600;
/**
* Milliseconds considered 'recent'
*/
var RECENT_WINDOW_MILLISECONDS = 60 * 60 * 1000; // 1 hour
var ObjectID = require('mongodb').ObjectID;
......@@ -217,15 +222,27 @@ exports.newChatMessageToTroupe = function(troupe, user, data) {
// Returns some recent public chats
exports.getRecentPublicChats = function() {
var twentyFourHoursAgo = new Date(Date.now() - 86400000);
var minRecentTime = Date.now() - RECENT_WINDOW_MILLISECONDS;
var minId = mongoUtils.createIdForTimestamp(minRecentTime);
return ChatMessage
.where({ pub: true })
.where({ sent: { $gt: twentyFourHoursAgo} })
.sort({ _id: -1 })
.limit(100)
.exec();
var aggregation = [{
$match: {
_id: { $gt: minId },
pub: true
}
}, {
$sample: {
size: 100
}
}, {
$sort: {
_id: -1
}
}];
return ChatMessage.aggregate(aggregation)
.read(mongoReadPrefs.secondaryPreferred)
.exec();
};
/**
......
'use strict';
process.env.DISABLE_API_LISTEN = '1';
var Promise = require('bluebird');
var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
var assert = require('assert');
describe('room-api', function() {
var app, request;
before(function() {
request = require("supertest-as-promised")(Promise);
app = require('../../server/api');
});
var fixture = fixtureLoader.setup({
user1: {
accessToken: 'web-internal'
},
user2: {
accessToken: 'web-internal'
},
troupe1: {
security: 'PUBLIC',
users: ['user1']
},
message1: {
user: 'user1',
troupe: 'troupe1',
text: 'HELLO',
sent: new Date(),
pub: 1
},
});
fixtureLoader.disableMongoTableScans();
it('GET /private/sample-chats', function() {
return request(app)
.get('/private/sample-chats')
.set('x-access-token', fixture.user1.accessToken)
.expect(200)
.then(function(result) {
var sampleChats = result.body;
assert(sampleChats.length >= 1);
var sampleChat = sampleChats[0];
assert(sampleChat.avatarUrl);
assert(sampleChat.username);
assert(sampleChat.displayName);
assert(sampleChat.room);
});
});
})
......@@ -23,7 +23,8 @@ describe('chatService', function() {
user: 'user1',
troupe: 'troupe1',
text: 'old_message',
sent: new Date("01/01/2014")
sent: new Date("01/01/2014"),
pub: true
},
message2: {
user: 'user1',
......@@ -168,4 +169,14 @@ describe('chatService', function() {
});
it('getRecentPublicChats #slow', function() {
fixtureLoader.disableMongoTableScans();
return chatService.getRecentPublicChats()
.then(function(chats) {
assert(chats.length >= 1);
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment