Optimized userPresence query

This commit is contained in:
Douglas Barone 2020-11-07 09:14:23 -04:00
parent 77c5e7117b
commit 680fb910e2
11 changed files with 352 additions and 18 deletions

View File

@ -16,7 +16,7 @@ services:
restart: 'no'
environment:
PGADMIN_DEFAULT_EMAIL: 'admin@pg.com'
PGADMIN_DEFAULT_PASSWORD: 'senhas'
PGADMIN_DEFAULT_PASSWORD: 'admin'
ports:
- '4477:80'

View File

@ -0,0 +1,50 @@
# Migration `20201107123940-init`
This migration has been generated by Douglas Barone at 11/7/2020, 8:39:40 AM.
You can check out the [state of the schema](./schema.prisma) after the migration.
## Database Steps
```sql
```
## Changes
```diff
diff --git schema.prisma schema.prisma
migration 20201106194210-init..20201107123940-init
--- datamodel.dml
+++ datamodel.dml
@@ -3,9 +3,9 @@
}
datasource db {
provider = "postgresql"
- url = "***"
+ url = "***"
}
model ResetToken {
id Int @id @default(autoincrement())
@@ -67,9 +67,9 @@
whenChanged String?
whenCreated String?
createdTokens ResetToken[] @relation("resettoken_to_user")
tokens ResetToken[] @relation("resettoken_to_creator")
- WifiDevice WifiDevice[]
+ WifiDevice WifiDevice[] @relation("wifidevice_to_user")
}
model WifiDevice {
id Int @id @default(autoincrement())
@@ -85,9 +85,9 @@
status Status?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId Int
- user User @relation(fields: [userId], references: [id])
+ user User @relation(fields: [userId], references: [id], name: "wifidevice_to_user")
}
enum Status {
ONLINE
```

View File

@ -0,0 +1,95 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "***"
}
model ResetToken {
id Int @id @default(autoincrement())
token String @unique
expiration DateTime
usedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], name: "resettoken_to_user")
userId Int
creator User @relation(fields: [creatorId], references: [id], name: "resettoken_to_creator")
creatorId Int
}
model User {
id Int @id @default(autoincrement())
lastLogin DateTime?
lastLoginPrior DateTime?
roles Json?
groups Json?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
accountExpires String?
badPasswordTime String?
badPwdCount String?
cn String?
department String?
description String?
displayName String?
distinguishedName String?
dn String?
extensionAttribute1 String?
extensionAttribute10 String?
extensionAttribute2 String?
extensionAttribute6 String?
extensionAttribute7 String?
givenName String?
homeDirectory String?
homeDrive String?
lastLogoff String?
lastLogon String?
lastLogonTimestamp String?
lockoutTime String?
logonCount String?
mail String?
name String?
objectCategory String?
objectGUID String?
objectSid String?
primaryGroupID String?
pwdLastSet String?
sAMAccountName String @unique
sAMAccountType String?
sn String?
thumbnailPhoto String?
title String?
userAccountControl String?
userPrincipalName String?
whenChanged String?
whenCreated String?
createdTokens ResetToken[] @relation("resettoken_to_user")
tokens ResetToken[] @relation("resettoken_to_creator")
WifiDevice WifiDevice[] @relation("wifidevice_to_user")
}
model WifiDevice {
id Int @id @default(autoincrement())
oui String?
mac String? @unique
hostname String?
firstSeen String?
lastSeen String?
essid String?
ip String?
uptime String?
apName String?
status Status?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId Int
user User @relation(fields: [userId], references: [id], name: "wifidevice_to_user")
}
enum Status {
ONLINE
OFFLINE
}

View File

@ -0,0 +1,44 @@
{
"version": "0.3.14-fixed",
"steps": [
{
"tag": "CreateDirective",
"location": {
"path": {
"tag": "Field",
"model": "User",
"field": "WifiDevice"
},
"directive": "relation"
}
},
{
"tag": "CreateArgument",
"location": {
"tag": "Directive",
"path": {
"tag": "Field",
"model": "User",
"field": "WifiDevice"
},
"directive": "relation"
},
"argument": "",
"value": "\"wifidevice_to_user\""
},
{
"tag": "CreateArgument",
"location": {
"tag": "Directive",
"path": {
"tag": "Field",
"model": "WifiDevice",
"field": "user"
},
"directive": "relation"
},
"argument": "name",
"value": "\"wifidevice_to_user\""
}
]
}

View File

@ -0,0 +1,40 @@
# Migration `20201107124332-init`
This migration has been generated by Douglas Barone at 11/7/2020, 8:43:32 AM.
You can check out the [state of the schema](./schema.prisma) after the migration.
## Database Steps
```sql
ALTER TABLE "public"."WifiDevice" ALTER COLUMN "mac" SET NOT NULL
```
## Changes
```diff
diff --git schema.prisma schema.prisma
migration 20201107123940-init..20201107124332-init
--- datamodel.dml
+++ datamodel.dml
@@ -3,9 +3,9 @@
}
datasource db {
provider = "postgresql"
- url = "***"
+ url = "***"
}
model ResetToken {
id Int @id @default(autoincrement())
@@ -73,9 +73,9 @@
model WifiDevice {
id Int @id @default(autoincrement())
oui String?
- mac String? @unique
+ mac String @unique
hostname String?
firstSeen String?
lastSeen String?
essid String?
```

View File

@ -0,0 +1,95 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "***"
}
model ResetToken {
id Int @id @default(autoincrement())
token String @unique
expiration DateTime
usedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], name: "resettoken_to_user")
userId Int
creator User @relation(fields: [creatorId], references: [id], name: "resettoken_to_creator")
creatorId Int
}
model User {
id Int @id @default(autoincrement())
lastLogin DateTime?
lastLoginPrior DateTime?
roles Json?
groups Json?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
accountExpires String?
badPasswordTime String?
badPwdCount String?
cn String?
department String?
description String?
displayName String?
distinguishedName String?
dn String?
extensionAttribute1 String?
extensionAttribute10 String?
extensionAttribute2 String?
extensionAttribute6 String?
extensionAttribute7 String?
givenName String?
homeDirectory String?
homeDrive String?
lastLogoff String?
lastLogon String?
lastLogonTimestamp String?
lockoutTime String?
logonCount String?
mail String?
name String?
objectCategory String?
objectGUID String?
objectSid String?
primaryGroupID String?
pwdLastSet String?
sAMAccountName String @unique
sAMAccountType String?
sn String?
thumbnailPhoto String?
title String?
userAccountControl String?
userPrincipalName String?
whenChanged String?
whenCreated String?
createdTokens ResetToken[] @relation("resettoken_to_user")
tokens ResetToken[] @relation("resettoken_to_creator")
WifiDevice WifiDevice[] @relation("wifidevice_to_user")
}
model WifiDevice {
id Int @id @default(autoincrement())
oui String?
mac String @unique
hostname String?
firstSeen String?
lastSeen String?
essid String?
ip String?
uptime String?
apName String?
status Status?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId Int
user User @relation(fields: [userId], references: [id], name: "wifidevice_to_user")
}
enum Status {
ONLINE
OFFLINE
}

View File

@ -0,0 +1,11 @@
{
"version": "0.3.14-fixed",
"steps": [
{
"tag": "UpdateField",
"model": "WifiDevice",
"field": "mac",
"arity": "Required"
}
]
}

View File

@ -1,3 +1,5 @@
# Prisma Migrate lockfile v1
20201106194210-init
20201106194210-init
20201107123940-init
20201107124332-init

View File

@ -68,13 +68,13 @@ model User {
whenCreated String?
createdTokens ResetToken[] @relation("resettoken_to_user")
tokens ResetToken[] @relation("resettoken_to_creator")
WifiDevice WifiDevice[]
WifiDevice WifiDevice[] @relation("wifidevice_to_user")
}
model WifiDevice {
id Int @id @default(autoincrement())
oui String?
mac String? @unique
mac String @unique
hostname String?
firstSeen String?
lastSeen String?
@ -86,7 +86,7 @@ model WifiDevice {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId Int
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], name: "wifidevice_to_user")
}
enum Status {

View File

@ -88,7 +88,9 @@ const Query = {
where: { NOT: { usedAt: null } }
}),
tokenCountExpired: 0,
tokenCountNotUsed: 0
tokenCountNotUsed: prisma.resetToken.count({
where: { usedAt: null }
})
}
},
@ -111,14 +113,15 @@ const Query = {
}
const usersWithWifiDevices = await prisma.user.findMany({
where: {
AND: {
WifiDevice: { some: { lastSeen: { not: null } } }
}
},
include: { WifiDevice: true }
})
const filteredUsersWithWifiDevices = usersWithWifiDevices.filter(
user => user.WifiDevice.length > 0
)
const userPresences = filteredUsersWithWifiDevices
const userPresences = usersWithWifiDevices
.filter(user =>
user.displayName.toLowerCase().includes(search.toLowerCase())
)

View File

@ -48,13 +48,7 @@ const updateDBWithOnlineDevices = async () => {
for (let device of onlineDevices) {
const newDevice = {
...device,
user: device.user
? {
connect: {
sAMAccountName: device.user
}
}
: null
user: device.user ? { connect: { sAMAccountName: device.user } } : null
}
try {