Added RECENT status

This commit is contained in:
Douglas Barone 2020-12-17 16:28:09 -04:00
parent ea5e698264
commit 9945b58fb1
6 changed files with 76 additions and 27 deletions

View File

@ -1,3 +1,4 @@
import { subMinutes } from 'date-fns'
import { getOnlineWifiDevices as getOnlineUnifiDevices } from './unifiController'
import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController'
@ -6,6 +7,7 @@ import prisma from '../prisma'
import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub'
const DEBOUNCE_TIME_MS = 10000
const RECENT_THRESHOLD_IN_MINUTES = 5
let working = false
@ -23,13 +25,31 @@ async function getOnlineDevices() {
return onlineDevices
}
// TODO: Add time threshold
async function setAllDevicesAsOffline() {
await prisma.wifiDevice.updateMany({
async function updateDevicesStatus() {
const recent = prisma.wifiDevice.updateMany({
where: {
status: 'OFFLINE',
lastSeen: {
gt: subMinutes(new Date(), RECENT_THRESHOLD_IN_MINUTES)
}
},
data: {
status: 'RECENT'
}
})
const offline = prisma.wifiDevice.updateMany({
where: {
lastSeen: {
lte: subMinutes(new Date(), RECENT_THRESHOLD_IN_MINUTES)
}
},
data: {
status: 'OFFLINE'
}
})
return Promise.all([recent, offline])
}
async function forceUserDisconnect(mac) {
@ -77,10 +97,10 @@ async function updateDevicesInfo() {
try {
const onlineDevices = await getOnlineDevices()
await setAllDevicesAsOffline()
await updateDB(onlineDevices)
await updateDevicesStatus()
pubsub.publish(USER_PRESENCE_UPDATED, {
userPresenceUpdated: onlineDevices.length
})

View File

@ -114,7 +114,7 @@ const Query = {
include: { wifiDevices: { orderBy: [{ lastSeen: 'desc' }] } }
})
search = search.toLowerCase()
search = search.toLowerCase().trim()
const userPresences = usersWithWifiDevices
.filter(

View File

@ -1,7 +1,9 @@
const WifiDevice = {
lastSeen: _ => _.lastSeen?.toISOString(),
firstSeen: _ => _.firstSeen?.toISOString(),
isOnline: _ => _.status == 'ONLINE'
isOnline: _ => _.status == 'ONLINE',
isRecent: _ => _.status == 'RECENT',
isOffline: _ => _.status == 'OFFLINE'
}
export { WifiDevice }

View File

@ -168,6 +168,8 @@ const typeDefs = gql`
apName: String
status: Status
isOnline: Boolean
isRecent: Boolean
isOffline: Boolean
}
type UserPresence {
@ -177,6 +179,7 @@ const typeDefs = gql`
enum Status {
ONLINE
RECENT
OFFLINE
}

View File

@ -17,7 +17,10 @@
<v-card
outlined
class="border-highlight light-shadow"
:class="{ online: userPresence.wifiDevices[0].isOnline }"
:class="{
online: userPresence.wifiDevices[0].isOnline,
recent: userPresence.wifiDevices[0].isRecent
}"
>
<v-list three-line>
<v-list-item>
@ -42,25 +45,41 @@
<v-list-item-title>
{{ userPresence.user.displayName }}
</v-list-item-title>
<v-list-item-subtitle
v-if="!userPresence.wifiDevices[0].isOnline"
>
<span class="font-weight-medium">Off-line</span>
</v-list-item-subtitle>
<v-list-item-subtitle v-else>
<template v-if="userPresence.wifiDevices[0].isOnline">
<v-list-item-subtitle>
<span class="font-weight-medium">On-line</span>
</v-list-item-subtitle>
<v-list-item-subtitle
v-if="userPresence.wifiDevices[0].isOnline"
>
<v-list-item-subtitle>
Próximo ao AP
<span class="font-weight-medium"
>{{ userPresence.wifiDevices[0].apName }}
<span class="font-weight-medium">
{{ userPresence.wifiDevices[0].apName }}
</span>
</v-list-item-subtitle>
<v-list-item-subtitle v-else>
Visto {{ userPresence.wifiDevices[0].lastSeen | from }}
</template>
<template v-if="userPresence.wifiDevices[0].isRecent">
<v-list-item-subtitle>
<span class="font-weight-medium">Visto recentemente</span>
</v-list-item-subtitle>
<v-list-item-subtitle>
Próximo ao AP
<span class="font-weight-medium">
{{ userPresence.wifiDevices[0].apName }}
</span>
</v-list-item-subtitle>
</template>
<template v-if="userPresence.wifiDevices[0].isOffline">
<v-list-item-subtitle>
<span class="font-weight-medium">Off-line</span>
</v-list-item-subtitle>
<v-list-item-subtitle>
<span class="font-weight-medium">
Visto {{ userPresence.wifiDevices[0].lastSeen | from }}
</span>
</v-list-item-subtitle>
</template>
</v-list-item-content>
</v-list-item>
</v-list>
@ -114,4 +133,7 @@ export default {
.online {
border-left-color: #117d4c;
}
.recent {
border-left-color: #ffc107;
}
</style>

View File

@ -153,6 +153,8 @@ export default {
id
lastSeen
isOnline
isRecent
isOffline
controller
}
}