added stale agent function

sig-changes^2
Joshua Collins 4 years ago
parent 8f0cb0b7b6
commit c418e6dba2
  1. 4
      app/app.js
  2. 29
      app/store/agents.js
  3. 5
      package-lock.json
  4. 3
      package.json

@ -107,8 +107,8 @@ app.get('/api/tasks/:taskId', (req, res) => {
// Beacon
app.post('/beacon', (req, res) => {
const [ip, os, profile] = req.body.split(/\|{2}/)
const agentId = agentStore.addAgent(os, ip, profile)
const [ip, os, profile, interval] = req.body.split(/\|{2}/)
const agentId = agentStore.addAgent(os, ip, profile, interval)
res.status(200).send(`${agentId}`)
})

@ -1,13 +1,16 @@
const moment = require('moment')
const Agents = () => {
const obj = {}
obj.agents = []
obj.addAgent = (os, ip, profile) => {
obj.addAgent = (os, ip, profile, interval) => {
const agent = {}
agent.id = obj.agents.length + 1
agent.os = os
agent.ip = ip
agent.profile = profile
agent.interval = parseInterval(interval)
agent.last_beacon_date = new Date(Date.now()).toLocaleString()
agent.status = true
obj.agents.push(agent)
@ -16,9 +19,16 @@ const Agents = () => {
obj.getAgentById = (agentId) => {
const agent = obj.agents.find(a => a.id === agentId)
return agent
? agent
: null
if (!agent) {
return null
} else {
const lbd = moment(agent.last_beacon_date)
const diff = (lbd.diff(moment(), 'second') * -1)
if (diff > agent.interval) {
agent.status = false
}
return agent
}
}
obj.updateAgentBeaconTime = (agentId) => {
@ -51,7 +61,16 @@ const Agents = () => {
}
}
obj.getAllAgents = () => obj.agents
obj.getAllAgents = () => {
obj.agents.forEach(agent => {
const lbd = moment(agent.last_beacon_date)
const diff = (lbd.diff(moment(), 'second') * -1)
if (diff > agent.interval) {
agent.status = false
}
})
return obj.agents
}
return obj
}

5
package-lock.json generated

@ -4250,6 +4250,11 @@
}
}
},
"moment": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz",
"integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ=="
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",

@ -12,7 +12,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
"express": "^4.17.1",
"moment": "^2.27.0"
},
"devDependencies": {
"jest": "^26.4.0",

Loading…
Cancel
Save