Skip to content

Commit

Permalink
perf(elasticsearch): fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Apr 19, 2019
1 parent dccb695 commit eddc577
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 132 deletions.
35 changes: 18 additions & 17 deletions src/client/containers/Departments/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Button from 'components/Button'
import PageContent from 'components/PageContent'

import UIKit from 'uikit'
import helpers from 'lib/helpers'

class DepartmentsContainer extends React.Component {
componentDidMount () {
Expand Down Expand Up @@ -60,10 +61,6 @@ class DepartmentsContainer extends React.Component {
}

render () {
const mappedDepartments = this.props.departments.map(department => {
return department.name
})

return (
<div>
<PageTitle
Expand Down Expand Up @@ -157,19 +154,23 @@ class DepartmentsContainer extends React.Component {
</td>
<td>
<ButtonGroup>
<Button
text={'Edit'}
small={true}
waves={true}
onClick={() => this.onEditDepartmentClick(department)}
/>
<Button
text={'Delete'}
style={'danger'}
small={true}
waves={true}
onClick={() => this.onDeleteDepartmentClick(department.get('_id'))}
/>
{helpers.canUser('departments:update', true) && (
<Button
text={'Edit'}
small={true}
waves={true}
onClick={() => this.onEditDepartmentClick(department)}
/>
)}
{helpers.canUser('departments:delete', true) && (
<Button
text={'Delete'}
style={'danger'}
small={true}
waves={true}
onClick={() => this.onDeleteDepartmentClick(department.get('_id'))}
/>
)}
</ButtonGroup>
</td>
</tr>
Expand Down
21 changes: 13 additions & 8 deletions src/client/containers/Groups/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import TableCell from 'components/Table/TableCell'
import ButtonGroup from 'components/ButtonGroup'

import UIKit from 'uikit'
import helpers from 'lib/helpers'

class GroupsContainer extends React.Component {
componentDidMount () {
Expand Down Expand Up @@ -106,14 +107,18 @@ class GroupsContainer extends React.Component {
</TableCell>
<TableCell style={{ textAlign: 'right', paddingRight: 15 }}>
<ButtonGroup>
<Button text={'Edit'} small={true} waves={true} onClick={() => this.onEditGroupClick(group.toJS())} />
<Button
text={'Delete'}
style={'danger'}
small={true}
waves={true}
onClick={() => this.onDeleteGroupClick(group.get('_id'))}
/>
{helpers.canUser('groups:update', true) && (
<Button text={'Edit'} small={true} waves={true} onClick={() => this.onEditGroupClick(group.toJS())} />
)}
{helpers.canUser('groups:delete', true) && (
<Button
text={'Delete'}
style={'danger'}
small={true}
waves={true}
onClick={() => this.onDeleteGroupClick(group.get('_id'))}
/>
)}
</ButtonGroup>
</TableCell>
</TableRow>
Expand Down
15 changes: 9 additions & 6 deletions src/client/containers/Settings/Elasticsearch/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ class ElasticsearchSettingsContainer extends React.Component {
getStatus () {
const self = this
self.esStatus = 'Please Wait...'
self.inSyncText = 'Please Wait...'
// self.esStatus = 'Please Wait...'
// self.inSyncText = 'Please Wait...'
// if (!this.state.configured) {
// this.esStatus = 'Not Configured'
// this.indexCount = 0
Expand All @@ -144,7 +144,10 @@ class ElasticsearchSettingsContainer extends React.Component {
.get('/api/v2/es/status')
.then(res => {
const data = res.data
self.esStatus = data.status.esStatus
if (data.status.isRebuilding) {
self.esStatus = 'Rebuilding...'
self.esStatusClass = ''
} else self.esStatus = data.status.esStatus
if (self.esStatus.toLowerCase() === 'connected') self.esStatusClass = 'text-success'
else if (self.esStatus.toLowerCase() === 'error') self.esStatusClass = 'text-danger'

Expand All @@ -158,7 +161,7 @@ class ElasticsearchSettingsContainer extends React.Component {
}

if (data.status.isRebuilding) {
setTimeout(self.getStatus, 5000)
setTimeout(self.getStatus, 3000)
self.disableRebuild = true
} else self.disableRebuild = false
})
Expand Down Expand Up @@ -207,7 +210,7 @@ class ElasticsearchSettingsContainer extends React.Component {
return (
<div className={this.props.active ? '' : 'hide'}>
<SettingItem
title={'Elasticsearch'}
title={'Elasticsearch - Beta'}
subtitle={'Enable the Elasticsearch engine'}
component={
<EnableSwitch
Expand Down Expand Up @@ -236,7 +239,7 @@ class ElasticsearchSettingsContainer extends React.Component {
/>
<SettingItem
title={'Elasticsearch Server Configuration'}
tooltip={'Changing server settings will require a rebuild of the index'}
tooltip={'Changing server settings will require a rebuild of the index and server restart.'}
subtitle={'The connection settings to the Elasticsearch server.'}
>
<form onSubmit={e => this.onFormSubmit(e)}>
Expand Down
20 changes: 12 additions & 8 deletions src/client/containers/Teams/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,18 @@ class TeamsContainer extends React.Component {
</TableCell>
<TableCell style={{ textAlign: 'right', paddingRight: 15 }}>
<ButtonGroup>
<Button text={'Edit'} small={true} waves={true} onClick={() => this.onEditTeamClick(team.toJS())} />
<Button
text={'Delete'}
style={'danger'}
small={true}
waves={true}
onClick={() => this.onDeleteTeamClick(team.get('_id'))}
/>
{helpers.canUser('teams:update', true) && (
<Button text={'Edit'} small={true} waves={true} onClick={() => this.onEditTeamClick(team.toJS())} />
)}
{helpers.canUser('teams:delete', true) && (
<Button
text={'Delete'}
style={'danger'}
small={true}
waves={true}
onClick={() => this.onDeleteTeamClick(team.get('_id'))}
/>
)}
</ButtonGroup>
</TableCell>
</TableRow>
Expand Down
5 changes: 4 additions & 1 deletion src/controllers/api/v2/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ apiElasticSearch.status = function (req, res) {

async.parallel(
[
function (done) {
return es.checkConnection(done)
},
function (done) {
es.getIndexCount(function (err, data) {
if (err) return done(err)
Expand All @@ -51,7 +54,7 @@ apiElasticSearch.status = function (req, res) {
if (err) return res.status(500).json({ success: false, error: err })

response.esStatus = global.esStatus
response.isRebuilding = global.esRebuilding || false
response.isRebuilding = global.esRebuilding === true
response.inSync = response.dbCount === response.indexCount

res.json({ success: true, status: response })
Expand Down
63 changes: 30 additions & 33 deletions src/controllers/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ installController.install = function (req, res) {
)
}
],
next
function (err) {
return next(err)
}
)
},
function (next) {
Expand Down Expand Up @@ -298,33 +300,18 @@ installController.install = function (req, res) {
)
},
function (roleResults, next) {
GroupSchema.getGroupByName('Administrators', function (err, group) {
if (err) {
winston.error('Database Error: ' + err.message)
return next('Database Error:' + err.message)
}

if (!_.isNull(group) && !_.isUndefined(group) && !_.isEmpty(group)) {
return next(null, group)
}

// Create Admin Group
var adminGroup = new GroupSchema({
name: 'Administrators',
var TeamSchema = require('../models/team')
TeamSchema.create(
{
name: 'Support (Default)',
members: []
})

adminGroup.save(function (err) {
if (err) {
winston.error('Database Error:' + err.message)
return next('Database Error:' + err.message)
}

return next(null, adminGroup, roleResults)
})
})
},
function (err, team) {
return next(err, team, roleResults)
}
)
},
function (adminGroup, roleResults, next) {
function (defaultTeam, roleResults, next) {
UserSchema.getUserByUsername(user.username, function (err, admin) {
if (err) {
winston.error('Database Error: ' + err.message)
Expand Down Expand Up @@ -356,7 +343,7 @@ installController.install = function (req, res) {
return next('Database Error: ' + err.message)
}

adminGroup.addMember(savedUser._id, function (err, success) {
defaultTeam.addMember(savedUser._id, function (err, success) {
if (err) {
winston.error('Database Error: ' + err.message)
return next('Database Error: ' + err.message)
Expand All @@ -366,18 +353,32 @@ installController.install = function (req, res) {
return next('Unable to add user to Administrator group!')
}

adminGroup.save(function (err) {
defaultTeam.save(function (err) {
if (err) {
winston.error('Database Error: ' + err.message)
return next('Database Error: ' + err.message)
}

return next(null)
return next(null, defaultTeam)
})
})
})
})
},
function (defaultTeam, next) {
var DepartmentSchema = require('../models/department')
DepartmentSchema.create(
{
name: 'Support - All Groups (Default)',
teams: [defaultTeam._id],
allGroups: true,
groups: []
},
function (err) {
return next(err)
}
)
},
function (next) {
if (!process.env.TRUDESK_DOCKER) return next()
var S = require('../models/setting')
Expand Down Expand Up @@ -411,10 +412,6 @@ installController.install = function (req, res) {
database: database,
shard: port === '---'
},
elasticsearch: {
host: eHost,
port: ePort
},
tokens: {
secret: chance.hash() + chance.md5(),
expires: 900 // 15min
Expand Down
35 changes: 25 additions & 10 deletions src/elasticsearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ function checkConnection (callback) {
)
}

ES.testConnection = function (callback) {
if (process.env.ELATICSEARCH_URI) ES.host = process.env.ELATICSEARCH_URI
else ES.host = nconf.get('elasticsearch:host') + ':' + nconf.get('elasticsearch:port')

ES.esclient = new elasticsearch.Client({
host: ES.host
})

checkConnection(callback)
}
// ES.testConnection = function (callback) {
// if (process.env.ELATICSEARCH_URI) ES.host = process.env.ELATICSEARCH_URI
// else ES.host = nconf.get('elasticsearch:host') + ':' + nconf.get('elasticsearch:port')
//
// ES.esclient = new elasticsearch.Client({
// host: ES.host
// })
//
// checkConnection(callback)
// }

ES.setupHooks = function () {
var ticketSchema = require('../models/ticket')
Expand Down Expand Up @@ -208,6 +208,7 @@ ES.rebuildIndex = function () {

esFork.once('message', function (data) {
global.esStatus = data.success ? 'Connected' : 'Error'
global.esRebuilding = false
})

esFork.on('exit', function () {
Expand Down Expand Up @@ -275,4 +276,18 @@ ES.init = function (callback) {
})
}

ES.checkConnection = function (callback) {
// global.esStatus = 'Please Wait...'
return checkConnection(function (err) {
if (err) {
global.esStatus = 'Error'
winston.warn(err)
return callback()
}

global.esStatus = 'Connected'
return callback()
})
}

module.exports = ES
11 changes: 5 additions & 6 deletions src/elasticsearch/rebuildIndexChild.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function sendAndEmptyQueue (bulk, callback) {
}
}
)
}
} else if (typeof callback === 'function') return callback()

return []
}
Expand Down Expand Up @@ -370,14 +370,13 @@ function rebuild (callback) {
setupClient()
rebuild(function (err) {
if (err) {
process.send({ success: false, error: err })
process.kill(0)
return process.send({ success: false, error: err })
}

process.send({ success: true })
// Kill it in 10sec to offset refresh timers
setTimeout(function () {
process.kill(0)
}, 10000)
process.send({ success: true })
return process.kill(0)
}, 6000)
})
})()
Loading

0 comments on commit eddc577

Please sign in to comment.