Skip to content

Commit

Permalink
fix(socket): high memory usage on notification updates
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jan 30, 2019
1 parent b47da40 commit b647d4c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 67 deletions.
11 changes: 11 additions & 0 deletions src/models/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,21 @@ notificationSchema.statics.findAllForUser = function (oId, callback) {
var q = this.model(COLLECTION)
.find({ owner: oId })
.sort({ created: -1 })
.limit(100)

return q.exec(callback)
}

notificationSchema.statics.getForUserWithLimit = function (oId, callback) {
if (_.isUndefined(oId)) return callback('Invalid ObjectId - NotificationSchema.GetForUserWithLimit()', null)

return this.model(COLLECTION)
.find({ owner: oId })
.sort({ created: -1 })
.limit(5)
.exec(callback)
}

notificationSchema.statics.getCount = function (oId, callback) {
if (_.isUndefined(oId)) {
return callback('Invalid ObjectId - NotificationSchema.GetCount()', null)
Expand Down
2 changes: 2 additions & 0 deletions src/public/js/angularjs/controllers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ define([

$scope.showAllNotificationsWindow = $('#viewAllNotificationsModal')
if ($scope.showAllNotificationsWindow.length > 0) {
socket.ui.emitUpdateAllNotifications()

var modal = UI.modal($scope.showAllNotificationsWindow, {
bgclose: true
})
Expand Down
120 changes: 63 additions & 57 deletions src/public/js/modules/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ define('modules/ui', [
this.onDisconnect()
this.updateUsers()
this.updateNotifications()
this.updateAllNotifications()
// this.updateMailNotifications();
this.updateConversationsNotifications()
this.updateComments()
Expand Down Expand Up @@ -700,6 +701,10 @@ define('modules/ui', [
e.preventDefault()
}

socketUi.emitUpdateAllNotifications = function () {
socket.emit('updateAllNotifications')
}

socketUi.updateComments = function () {
socket.removeAllListeners('updateComments')
socket.on('updateComments', function (data) {
Expand Down Expand Up @@ -1117,11 +1122,9 @@ define('modules/ui', [
var $notifications = $('#notifications-Messages').find('ul')
if ($notifications.length < 1) return

var last5 = _.take(data.items, 5)

$notifications.html('')
// Build Notifications
_.each(last5, function (item) {
_.each(data.items, function (item) {
var html = ''
html +=
'<li>' +
Expand Down Expand Up @@ -1187,60 +1190,6 @@ define('modules/ui', [
})
})

// All Notifications
var $notificationsTable = $('table.notificationsTable')
if ($notifications.length > 0) {
var $tbody = $notificationsTable.find('tbody')
$tbody.html('')
_.each(data.items, function (item) {
if (!item.data && item.data.ticket) return
var html = ''
html +=
'<tr class="notification-row ' +
(item.unread ? 'unread' : '') +
'" data-notificationid="' +
item._id +
'" data-ticket-uid="' +
item.data.ticket.uid +
'" ng-click="notificationClick($event)">'
html += '<td class="type">'
html += '<i class="fa fa-2x fa-check"></i>'
html += '</td>'
html += '<td class="title">'
html += '<p>' + item.title + '</p>'
html += '<div class="body">'
html += item.message
html += '</div>'
html += '</td>'
html += '<td class="date">'
html +=
'<time datetime="' +
helpers.formatDate(item.created, 'YYYY-MM-DDThh:mm') +
'">' +
helpers.formatDate(item.created, 'MMM DD, YYYY') +
'</time>'
html += '</td>'
html += '</tr>'

$tbody.append(html)

var $nRows = $tbody.find('.notification-row')
$.each($nRows, function (k, val) {
var $item = $(val)
$item.off('click')
$item.on('click', function (e) {
e.preventDefault()
e.stopPropagation()
var $id = $(e.currentTarget).attr('data-notificationId')
var $uid = $(e.currentTarget).attr('data-ticket-uid')
socketUi.markNotificationRead($id)
helpers.closeNotificationsWindow()
History.pushState(null, null, '/tickets/' + $uid)
})
})
})
}

var $notificationsCount = $('#btn_notifications').find('span')
var $bottomActions = $('#notifications').find('.bottom-actions')
if ($notificationsCount.length > 0) {
Expand All @@ -1257,6 +1206,63 @@ define('modules/ui', [
})
}

socketUi.updateAllNotifications = function () {
socket.removeAllListeners('updateAllNotifications')
socket.on('updateAllNotifications', function (data) {
// All Notifications
var $notificationsTable = $('table.notificationsTable')
var $tbody = $notificationsTable.find('tbody')
$tbody.html('')
_.each(data.items, function (item) {
if (!item.data && item.data.ticket) return
var html = ''
html +=
'<tr class="notification-row ' +
(item.unread ? 'unread' : '') +
'" data-notificationid="' +
item._id +
'" data-ticket-uid="' +
item.data.ticket.uid +
'" ng-click="notificationClick($event)">'
html += '<td class="type">'
html += '<i class="fa fa-2x fa-check"></i>'
html += '</td>'
html += '<td class="title">'
html += '<p>' + item.title + '</p>'
html += '<div class="body">'
html += item.message
html += '</div>'
html += '</td>'
html += '<td class="date">'
html +=
'<time datetime="' +
helpers.formatDate(item.created, 'YYYY-MM-DDThh:mm') +
'">' +
helpers.formatDate(item.created, 'MMM DD, YYYY') +
'</time>'
html += '</td>'
html += '</tr>'

$tbody.append(html)

var $nRows = $tbody.find('.notification-row')
$.each($nRows, function (k, val) {
var $item = $(val)
$item.off('click')
$item.on('click', function (e) {
e.preventDefault()
e.stopPropagation()
var $id = $(e.currentTarget).attr('data-notificationId')
var $uid = $(e.currentTarget).attr('data-ticket-uid')
socketUi.markNotificationRead($id)
helpers.closeNotificationsWindow()
History.pushState(null, null, '/tickets/' + $uid)
})
})
})
})
}

socketUi.onTicketCreated = function () {
socket.removeAllListeners('ticket:created')
socket.on('ticket:created', function () {
Expand Down
57 changes: 47 additions & 10 deletions src/socketio/notificationSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
* Copyright (c) 2014-2019. All rights reserved.
*/
var _ = require('lodash')
var async = require('async')
var winston = require('winston')
var utils = require('../helpers/utils')

var events = {}

function register (socket) {
events.updateNotifications(socket)
events.updateAllNotifications(socket)
events.markNotificationRead(socket)
events.clearNotifications(socket)
}
Expand All @@ -31,19 +33,48 @@ function updateNotifications () {
_.each(io.sockets.sockets, function (socket) {
var notifications = {}
var notificationSchema = require('../models/notification')
notificationSchema.findAllForUser(socket.request.user._id, function (err, items) {
if (err) {
winston.warn(err)
return true
async.parallel(
[
function (done) {
notificationSchema.getForUserWithLimit(socket.request.user._id, function (err, items) {
if (err) return done(err)

notifications.items = items
return done()
})
},
function (done) {
notificationSchema.getUnreadCount(socket.request.user._id, function (err, count) {
if (err) return done(err)

notifications.count = count
return done()
})
}
],
function (err) {
if (err) {
winston.warn(err)
return true
}

utils.sendToSelf(socket, 'updateNotifications', notifications)
}
)
})
}

// notifications.items = _.take(items, 5);
notifications.items = items
var p = _.filter(items, { unread: true })
notifications.count = _.size(p)
function updateAllNotifications (socket) {
var notifications = {}
var notificationSchema = require('../models/notification')
notificationSchema.findAllForUser(socket.request.user._id, function (err, items) {
if (err) return false

utils.sendToSelf(socket, 'updateNotifications', notifications)
})
notifications.items = items

console.log('Called')

utils.sendToSelf(socket, 'updateAllNotifications', notifications)
})
}

Expand All @@ -53,6 +84,12 @@ events.updateNotifications = function (socket) {
})
}

events.updateAllNotifications = function (socket) {
socket.on('updateAllNotifications', function () {
updateAllNotifications(socket)
})
}

events.markNotificationRead = function (socket) {
socket.on('markNotificationRead', function (_id) {
if (_.isUndefined(_id)) return true
Expand Down

0 comments on commit b647d4c

Please sign in to comment.