diff --git a/index.js b/index.js index 66897a5..078f5e0 100644 --- a/index.js +++ b/index.js @@ -107,22 +107,22 @@ async function logServerState(serverData) { await serverState.save() logger.info('Saved server state to DB', 'query') - logEchoAttendance() - logFoxtrotAttendance() + logSquadAttendance(`echo`) + logSquadAttendance(`foxtrot`) } -async function logEchoAttendance() { +async function logSquadAttendance(squad) { - logger.info('Trying to log echo attendance to DB', 'webserver/echo') + logger.info(`Attempting to log ${squad} attendance`, `webserver/${squad}`) const dbState = await ServerState.find() - logger.trace('dbState: ' + dbState, 'webserver/echo') + logger.trace('dbState: ' + dbState, `webserver/${squad}`) const serverLog = dbState[dbState.length - 1] - logger.trace('serverLog: ' + JSON.stringify(serverLog), 'webserver/echo') - const echotSquad = process.env.ECHO.toString().split(',') - logger.trace('echoSquad: ' + echotSquad, 'webserver/echo') + logger.trace('serverLog: ' + JSON.stringify(serverLog), `webserver/${squad}`) + const squadMembers = (process.env.squad.toUpperCase()).toString().split(',') + logger.trace('squadMembers' + squadMembers, `webserver/${squad}`) const strippedPlayers = serverLog.players.toString().replaceAll(/\s*\[.*?]/g, '') - logger.trace('strippedPlayers: ' + strippedPlayers, 'webserver/echo') + logger.trace('strippedPlayers: ' + strippedPlayers, `webserver/${squad}`) var temp = { squad: String, @@ -138,51 +138,7 @@ async function logEchoAttendance() { temp.missionName = serverLog.missionName temp.date = serverLog.date.split('T')[0] temp.playerCount = serverLog.playerCount - echotSquad.forEach(member => { - if (strippedPlayers.toString().includes(member)) { - tempPlayers.push(member) - } - }) - temp.players = tempPlayers - temp.squadCount = tempPlayers.length - temp.attendance = ((temp.squadCount / echotSquad.length) * 100) - temp.squad = 'echo' - logger.trace('temp: ' + temp, 'webserver/echo') - - const attendanceModel = new AttendanceModel(temp) - logger.info('Logging echo attendance to DB', 'webserver/echo') - await attendanceModel.save() - logger.info('Loged echo attendance to DB', 'webserver/echo') - -} - -async function logFoxtrotAttendance() { - - logger.info('Trying to log foxtrot attendance to DB', 'webserver/foxtrot') - const dbState = await ServerState.find() - logger.trace('dbState: ' + dbState, 'webserver/foxtrot') - const serverLog = dbState[dbState.length - 1] - logger.trace('serverLog: ' + JSON.stringify(serverLog), 'webserver/foxtrot') - const foxtrotSquad = process.env.FOXTROT.toString().split(',') - logger.trace('foxtrotSquad' + foxtrotSquad, 'webserver/foxtrot') - const strippedPlayers = serverLog.players.toString().replaceAll(/\s*\[.*?]/g, '') - logger.trace('strippedPlayers: ' + strippedPlayers, 'webserver/foxtrot') - - var temp = { - squad: String, - missionName: String, - date: String, - playerCount: Number, - squadCount: Number, - attendance: Number, - players: Array - } - - var tempPlayers = [] - temp.missionName = serverLog.missionName - temp.date = serverLog.date.split('T')[0] - temp.playerCount = serverLog.playerCount - foxtrotSquad.forEach(member => { + squadMembers.forEach(member => { if (strippedPlayers.toString().includes(member)) { tempPlayers.push(member) } @@ -190,72 +146,19 @@ async function logFoxtrotAttendance() { temp.players = tempPlayers temp.squadCount = tempPlayers.length temp.attendance = ((temp.squadCount / foxtrotSquad.length) * 100) - temp.squad = 'foxtrot' - logger.trace('temp: ' + temp, 'webserver/foxtrot') + temp.squad = squad + logger.trace('temp: ' + temp, `webserver/${squad}`) const attendanceModel = new AttendanceModel(temp) - logger.info('Logging foxtrot attendance to DB', 'webserver/foxtrot') + logger.info(`Logging ${squad} attendance to DB`, `webserver/${squad}`) await attendanceModel.save() - logger.info('Loged foxtrot attendance to DB', 'webserver/foxtrot') + logger.info(`Loged ${squad} attendance to DB`, `webserver/${squad}`) } -app.get('/', async (req, res) => { - - const dbState = await ServerState.find() - logger.trace('dbState: ' + dbState, 'webserver') - - var serverLogs = [] - - dbState.forEach( serverLog => { - - var temp = { - missionName: String, - date: String, - playerCount: Number, - players: Array - } - - temp.missionName = serverLog.missionName - temp.date = serverLog.date.split('T')[0] - temp.playerCount = serverLog.playerCount - temp.players = serverLog.players.toString().replaceAll(/\s*\[.*?]/g, '') - serverLogs.unshift(temp) - }) - - var chartData = [] - - dbState.forEach(element => { - - var temp = { - playerCount: Number, - date: String - } - - temp.date = element.date.split('T')[0] - temp.playerCount = element.playerCount - - chartData.unshift(temp) - - }) - - logger.trace('serverLogs: ' + JSON.stringify(serverLogs), 'webserver') - logger.trace('chartData: ' + JSON.stringify(chartData), 'webserver') - res.render('index', { - serverLogs: serverLogs, - chartData: JSON.stringify(chartData) - }) - -}) - -app.get('/echo', async (req, res) => { - - const dbState = await AttendanceModel.find({squad: 'echo'}) - logger.trace('dbState: ' + dbState, 'webserver/echo') - const echotSquad = process.env.ECHO.toString().split(',') - logger.trace('echoSquad: ' + echotSquad, 'webserver/echo') - - var serverLogs = [] +function getSquadServerLogs(dbState, squad) { + + var toReturn = [] dbState.slice().reverse().forEach( serverLog => { @@ -274,19 +177,23 @@ app.get('/echo', async (req, res) => { temp.missionName = serverLog.missionName temp.date = serverLog.date.split('T')[0] temp.playerCount = serverLog.playerCount - echotSquad.forEach(member => { + squad.forEach(member => { if (strippedPlayers.toString().includes(member)) { tempPlayers.push(member) } }) temp.players = tempPlayers temp.squadCount = tempPlayers.length - temp.attendance = ((temp.squadCount / echotSquad.length) * 100).toFixed(2) - serverLogs.unshift(temp) + temp.attendance = ((temp.squadCount / squad.length) * 100).toFixed(2) + toReturn.unshift(temp) }) - logger.trace('serverLogs: ' + JSON.stringify(serverLogs), 'webserver/echo') - var chartData = [] + return toReturn +} + +function getSquadChartData(serverLogs, squadMembers) { + + var toReturn = [] serverLogs.forEach(element => { @@ -296,130 +203,133 @@ app.get('/echo', async (req, res) => { } temp.date = element.date.split('T')[0] - temp.attendancePercentage = ((element.squadCount / process.env.ECHO.toString().split(',').length) * 100) + temp.attendancePercentage = ((element.squadCount / squadMembers.toString().split(',').length) * 100) - chartData.unshift(temp) + toReturn.unshift(temp) }) - logger.trace('chartData: ' + JSON.stringify(chartData), 'webserver/echo') - var individualAttendance = [] + return toReturn +} - echotSquad.forEach(player => { +function getSquadIndividualAttendance(dbState, squad, squadName) { + + var toReturn = [] + + squad.forEach(player => { var temp = { playerName: String, attendance: Number } temp.playerName = player - logger.trace('Calculating attendance for player ' + player, 'webserver/echo') + logger.trace('Calculating attendance for player ' + player, `webserver/${squadName}`) var attended = 0 dbState.forEach(element => { - logger.trace('Checking attendance for ' + player + ' in operation ' + element.missionName, 'webserver/echo') + logger.trace('Checking attendance for ' + player + ' in operation ' + element.missionName, `webserver/${squadName}`) if(element.players.toString().replaceAll(/\s*\[.*?]/g, '').includes(player)){ - logger.trace('Bumping attendance counter for player ' + player, 'webserver/echo') + logger.trace('Bumping attendance counter for player ' + player, `webserver/${squadName}`) attended++ } }) - logger.trace('Attended OPs for player ' + player + ' calculated to ' + attended, 'webserver/echo') + logger.trace('Attended OPs for player ' + player + ' calculated to ' + attended, `webserver/${squadName}`) var att = ((attended / dbState.length) * 100).toFixed(2) - logger.trace('Attendedance for player ' + player + ' calculated to ' + att + '%', 'webserver/echo') + logger.trace('Attendedance for player ' + player + ' calculated to ' + att + '%', `webserver/${squadName}`) temp.attendance = att - individualAttendance.unshift(temp) + toReturn.unshift(temp) }) - logger.trace('individualAttendance: ' + JSON.stringify(individualAttendance), 'webserver/echo') - res.render('echo', { - serverLogs: serverLogs, - individualAttendance: individualAttendance, - chartData: JSON.stringify(chartData) - }) - -}) + return toReturn +} -app.get('/foxtrot', async (req, res) => { +app.get('/', async (req, res) => { - const dbState = await AttendanceModel.find({squad: 'foxtrot'}) - logger.trace('dbState: ' + dbState, 'webserver/foxtrot') - const foxtrotSquad = process.env.FOXTROT.toString().split(',') - logger.trace('foxtrotSquad: ' + foxtrotSquad, 'webserver/foxtrot') + const dbState = await ServerState.find() + logger.trace('dbState: ' + dbState, 'webserver') var serverLogs = [] - dbState.slice().reverse().forEach( serverLog => { - - var strippedPlayers = serverLog.players.toString().replaceAll(/\s*\[.*?]/g, '') + dbState.forEach( serverLog => { var temp = { missionName: String, date: String, playerCount: Number, - squadCount: Number, - attendance: Number, players: Array } - var tempPlayers = [] temp.missionName = serverLog.missionName temp.date = serverLog.date.split('T')[0] temp.playerCount = serverLog.playerCount - foxtrotSquad.forEach(member => { - if (strippedPlayers.toString().includes(member)) { - tempPlayers.push(member) - } - }) - temp.players = tempPlayers - temp.squadCount = tempPlayers.length - temp.attendance = ((temp.squadCount / foxtrotSquad.length) * 100).toFixed(2) + temp.players = serverLog.players.toString().replaceAll(/\s*\[.*?]/g, '') serverLogs.unshift(temp) }) - logger.trace('serverLogs: ' + JSON.stringify(serverLogs), 'webserver/foxtrot') var chartData = [] - serverLogs.forEach(element => { + dbState.forEach(element => { var temp = { - attendancePercentage: Number, + playerCount: Number, date: String } temp.date = element.date.split('T')[0] - temp.attendancePercentage = ((element.squadCount / process.env.FOXTROT.toString().split(',').length) * 100) + temp.playerCount = element.playerCount chartData.unshift(temp) }) - logger.trace('chartData' + JSON.stringify(chartData), 'webserver/foxtrot') - var individualAttendance = [] + logger.trace('serverLogs: ' + JSON.stringify(serverLogs), 'webserver') + logger.trace('chartData: ' + JSON.stringify(chartData), 'webserver') + res.render('index', { + serverLogs: serverLogs, + chartData: JSON.stringify(chartData) + }) + +}) - foxtrotSquad.forEach(player => { - var temp = { - playerName: String, - attendance: Number - } - temp.playerName = player - - logger.trace('Calculating attendance for player ' + player, 'webserver/foxtrot') - var attended = 0 +app.get('/echo', async (req, res) => { - dbState.forEach(element => { - logger.trace('Checking attendance for ' + player + ' in operation ' + element.missionName, 'webserver/foxtrot') - if(element.players.toString().replaceAll(/\s*\[.*?]/g, '').includes(player)){ - logger.trace('Bumping attendance counter for player ' + player, 'webserver/foxtrot') - attended++ - } - }) + const dbState = await AttendanceModel.find({squad: 'echo'}) + logger.trace('dbState: ' + dbState, 'webserver/echo') + const echotSquad = process.env.ECHO.toString().split(',') + logger.trace('echoSquad: ' + echotSquad, 'webserver/echo') - logger.trace('Attended OPs for player ' + player + ' calculated to ' + attended, 'webserver/foxtrot') - var att = ((attended / dbState.length) * 100).toFixed(2) - logger.trace('Attendedance for player ' + player + ' calculated to ' + att + '%', 'webserver/foxtrot') - temp.attendance = att - individualAttendance.unshift(temp) + var serverLogs = getSquadServerLogs(dbState, echotSquad) + logger.trace('serverLogs: ' + JSON.stringify(serverLogs), 'webserver/echo') + + var chartData = getSquadChartData(serverLogs, echotSquad) + logger.trace('chartData: ' + JSON.stringify(chartData), 'webserver/echo') + + var individualAttendance = getSquadIndividualAttendance(dbState, echotSquad, 'echo') + logger.trace('individualAttendance: ' + JSON.stringify(individualAttendance), 'webserver/echo') + + res.render('echo', { + serverLogs: serverLogs, + individualAttendance: individualAttendance, + chartData: JSON.stringify(chartData) }) + +}) + +app.get('/foxtrot', async (req, res) => { + + const dbState = await AttendanceModel.find({squad: 'foxtrot'}) + logger.trace('dbState: ' + dbState, 'webserver/foxtrot') + const foxtrotSquad = process.env.FOXTROT.toString().split(',') + logger.trace('foxtrotSquad: ' + foxtrotSquad, 'webserver/foxtrot') + + var serverLogs = getSquadServerLogs(dbState, foxtrotSquad) + logger.trace('serverLogs: ' + JSON.stringify(serverLogs), 'webserver/foxtrot') + + var chartData = getSquadChartData(serverLogs, foxtrotSquad) + logger.trace('chartData' + JSON.stringify(chartData), 'webserver/foxtrot') + + var individualAttendance = getSquadIndividualAttendance(dbState, foxtrotSquad, 'foxtrot') logger.trace('individualAttendance: ' + JSON.stringify(individualAttendance), 'webserver/foxtrot') res.render('foxtrot', {