Skip to content

Commit

Permalink
perf(core): updates
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed May 10, 2019
1 parent 1a3fe15 commit 8607c6a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 66 deletions.
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ function launchServer (db) {
})

return next()
},
function (next) {
var taskRunner = require('./src/taskrunner')
return taskRunner.init(next)
}
],
function (err) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trudesk",
"version": "1.0.11",
"version": "1.0.12-beta",
"private": true,
"engines": {
"node": ">=9.10.0"
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ middleware.redirectToLogin = function (req, res, next) {
req.session.redirectUrl = req.url
}

return res.redirect(401, '/')
return res.redirect('/')
}

if (req.user.deleted) {
req.logout()
req.session.l2auth = null
req.session.destroy()
return res.redirect(401, '/')
return res.redirect('/')
}

if (req.user.hasL2Auth) {
if (req.session.l2auth !== 'totp') {
return res.redirect(401, '/')
return res.redirect('/')
}
}

Expand Down
48 changes: 0 additions & 48 deletions src/models/task.js

This file was deleted.

116 changes: 102 additions & 14 deletions src/taskrunner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,109 @@
*/

var _ = require('lodash')
var async = require('async')
var winston = require('winston')
var taskSchema = require('../models/task')

/**
* @namespace
*/
;(function () {
// Start up Task Runners
winston.debug('Starting Runners...')

taskSchema.getTasks(function (err, items) {
if (err) {
return winston.warn('Task Runner Error: ' + err.message)
var request = require('request')
var ticketSchema = require('../models/ticket')
var userSchema = require('../models/user')
var groupSchema = require('../models/group')
var conversationSchema = require('../models/chat/conversation')
var settingSchema = require('../models/setting')

var taskRunner = {}

taskRunner.init = function (callback) {
taskRunner.sendStats(function (err) {
if (!err) setInterval(taskRunner.sendStats, 86400000) // 24 hours
})

return callback()
}

taskRunner.sendStats = function (callback) {
settingSchema.getSettingsByName(['gen:installid', 'gen:version'], function (err, settings) {
if (err) return callback(err)
if (!settings || settings.length < 1) return callback()

var versionSetting = _.find(settings, function (x) {
return x.name === 'gen:version'
})
var installIdSetting = _.find(settings, function (x) {
return x.name === 'gen:installid'
})

if (!installIdSetting) return callback()

versionSetting = _.isUndefined(versionSetting) ? { value: '--' } : versionSetting

var result = {
ticketCount: 0,
agentCount: 0,
customerGroupCount: 0,
conversationCount: 0
}

winston.debug('Number of Tasks: ' + _.size(items))
async.parallel(
[
function (done) {
ticketSchema.countDocuments({ deleted: false }, function (err, count) {
if (err) return done(err)

result.ticketCount = count
return done()
})
},
function (done) {
userSchema.getAgents({}, function (err, agents) {
if (err) return done(err)

if (!agents) return done()
result.agentCount = agents.length

return done()
})
},
function (done) {
groupSchema.countDocuments({}, function (err, count) {
if (err) return done(err)

result.customerGroupCount = count

return done()
})
},
function (done) {
conversationSchema.countDocuments({}, function (err, count) {
if (err) return done(err)

result.conversationCount = count

return done()
})
}
],
function (err) {
if (err) return callback()
request(
'https://stats.trudesk.app/api/v1/installation',
{
method: 'POST',
json: true,
body: {
statsKey: 'trudesk',
id: installIdSetting.value,
version: versionSetting.value,
ticketCount: result.ticketCount,
agentCount: result.agentCount,
customerGroupCount: result.customerGroupCount,
conversationCount: result.conversationCount
}
},
callback
)
}
)
})
})()
}

module.exports = taskRunner

0 comments on commit 8607c6a

Please sign in to comment.