Added onlineUsers to Stats

This commit is contained in:
Douglas Barone 2020-12-04 10:38:08 -04:00
parent 7f763020c7
commit 2e454ea88f
5 changed files with 64 additions and 18 deletions

View File

@ -2,8 +2,6 @@ import { User } from '../classes/User'
import prisma from '../prisma'
import { updateDevicesInfo } from '../utils/wifiUtils'
const parseSAMAccountName = sAMAccountName =>
sAMAccountName ? sAMAccountName.replace('.', ' ') : ''
@ -75,20 +73,7 @@ const Query = {
},
async stats() {
return {
tokenCountTotal: prisma.resetToken.count(),
tokenCountUsed: prisma.resetToken.count({
where: { NOT: { usedAt: null } }
}),
tokenCountExpired: prisma.resetToken.count({
where: {
AND: [{ expiration: { lt: new Date() } }, { usedAt: null }]
}
}),
tokenCountNotUsed: prisma.resetToken.count({
where: { usedAt: null, expiration: { gt: new Date() } }
})
}
return {}
},
async wifiDevices(_, { identifiedOnly, nonIdentifiedOnly }) {
@ -149,7 +134,7 @@ const Query = {
a.wifiDevices[0].lastSeen > b.wifiDevices[0].lastSeen ? -1 : 1
)
return sortedUserPresences.slice(0, 100)
return sortedUserPresences.slice(0, 1000)
}
}

View File

@ -0,0 +1,41 @@
import prisma from '../prisma'
const Stats = {
tokenCountTotal: async () => prisma.resetToken.count(),
tokenCountUsed: async () =>
prisma.resetToken.count({
where: { NOT: { usedAt: null } }
}),
tokenCountExpired: async () =>
prisma.resetToken.count({
where: {
AND: [{ expiration: { lt: new Date() } }, { usedAt: null }]
}
}),
tokenCountNotUsed: async () =>
prisma.resetToken.count({
where: { usedAt: null, expiration: { gt: new Date() } }
}),
onlineUsers: async () =>
prisma.user.count({
where: { wifiDevices: { some: { status: 'ONLINE' } } }
}),
offlineUsers: async () =>
prisma.user.count({
where: {
wifiDevices: {
every: { status: 'OFFLINE' },
some: { lastSeen: { not: null } }
}
}
}),
totalUsers: async () => prisma.user.count()
}
export { Stats }

View File

@ -6,6 +6,7 @@ import { User } from './User'
import { Group } from './Group'
import { ResetToken } from './ResetToken'
import { WifiDevice } from './WifiDevice'
import { Stats } from './Stats'
const resolvers = {
Query,
@ -14,7 +15,8 @@ const resolvers = {
User,
Group,
ResetToken,
WifiDevice
WifiDevice,
Stats
}
export { resolvers }

View File

@ -144,6 +144,9 @@ const typeDefs = gql`
tokenCountUsed: Int!
tokenCountExpired: Int!
tokenCountNotUsed: Int!
onlineUsers: Int!
offlineUsers: Int!
totalUsers: Int!
}
type WifiDevice {

View File

@ -13,6 +13,10 @@
/>
</v-toolbar>
<div v-if="stats" class="text-center mb-2">
{{ stats.onlineUsers }} usuários online.
</div>
<UserPresenceStatusList
:user-presences="pagedUserPresences"
:loading="$apollo.queries.userPresence.loading && !userPresence"
@ -86,6 +90,16 @@ export default {
},
debounce: 250
},
stats: {
query: gql`
query {
stats {
onlineUsers
offlineUsers
}
}
`
},
$subscribe: {
userPresenceUpdated: {
query: gql`
@ -95,6 +109,7 @@ export default {
`,
result() {
this.$apollo.queries.userPresence.refresh()
this.$apollo.queries.stats.refresh()
}
}
}