Prepare DB for new feature

This commit is contained in:
Douglas Barone 2022-06-02 17:15:22 +00:00
parent bc4fab24c1
commit 7333981b2c
5 changed files with 150 additions and 47 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ pti.code-workspace
temporary-captain-to-deploy.tar temporary-captain-to-deploy.tar
deploy.sh deploy.sh
output.json output.json
client_sample.json

View File

@ -0,0 +1,59 @@
/*
Warnings:
- You are about to drop the `Statistic` table. If the table is not empty, all the data it contains will be lost.
*/
-- AlterTable
ALTER TABLE "AccessPoint" ADD COLUMN "inventoryTag" TEXT,
ADD COLUMN "sshPassword" TEXT,
ADD COLUMN "sshUser" TEXT;
-- AlterTable
ALTER TABLE "WifiDevice" ADD COLUMN "frequency" TEXT,
ADD COLUMN "identity" TEXT,
ADD COLUMN "ownerId" INTEGER,
ADD COLUMN "protocol" TEXT,
ADD COLUMN "signalStrength" INTEGER,
ADD COLUMN "speed" INTEGER,
ADD COLUMN "usage" INTEGER;
-- DropTable
DROP TABLE "Statistic";
-- CreateTable
CREATE TABLE "WifiStats" (
"id" SERIAL NOT NULL,
"timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"clients" INTEGER NOT NULL,
"accessPointId" INTEGER NOT NULL,
CONSTRAINT "WifiStats_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Network" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"shortName" TEXT NOT NULL,
"cidr" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Network_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Network_name_key" ON "Network"("name");
-- CreateIndex
CREATE UNIQUE INDEX "Network_shortName_key" ON "Network"("shortName");
-- CreateIndex
CREATE UNIQUE INDEX "Network_cidr_key" ON "Network"("cidr");
-- AddForeignKey
ALTER TABLE "WifiDevice" ADD CONSTRAINT "WifiDevice_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "WifiStats" ADD CONSTRAINT "WifiStats_accessPointId_fkey" FOREIGN KEY ("accessPointId") REFERENCES "AccessPoint"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -14,20 +14,22 @@ model ResetToken {
usedAt DateTime? usedAt DateTime?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
userId Int
creatorId Int userId Int
creator User @relation("resettoken_to_creator", fields: [creatorId], references: [id]) user User @relation("resettoken_to_user", fields: [userId], references: [id])
user User @relation("resettoken_to_user", fields: [userId], references: [id])
creatorId Int
creator User @relation("resettoken_to_creator", fields: [creatorId], references: [id])
} }
model User { model User {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
lastLogin DateTime? lastLogin DateTime?
lastLoginPrior DateTime? lastLoginPrior DateTime?
roles Json? roles Json?
groups Json? groups Json?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
accountExpires String? accountExpires String?
badPasswordTime String? badPasswordTime String?
badPwdCount String? badPwdCount String?
@ -57,7 +59,7 @@ model User {
objectSid String? objectSid String?
primaryGroupID String? primaryGroupID String?
pwdLastSet DateTime? pwdLastSet DateTime?
sAMAccountName String @unique sAMAccountName String @unique
sAMAccountType String? sAMAccountType String?
sn String? sn String?
thumbnailPhoto String? thumbnailPhoto String?
@ -66,31 +68,48 @@ model User {
userPrincipalName String? userPrincipalName String?
whenChanged String? whenChanged String?
whenCreated String? whenCreated String?
createdTokens ResetToken[] @relation("resettoken_to_creator")
tokens ResetToken[] @relation("resettoken_to_user") createdTokens ResetToken[] @relation("resettoken_to_creator")
wifiDevices WifiDevice[] @relation("wifidevice_to_user") tokens ResetToken[] @relation("resettoken_to_user")
PAHost PAHost[] @relation("pahost_to_user") wifiDevices WifiDevice[] @relation("wifidevice_to_user")
ownedWifiDevices WifiDevice[] @relation("wifidevice_to_owner")
PAHost PAHost[] @relation("pahost_to_user")
} }
model WifiDevice { model WifiDevice {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
oui String? oui String?
mac String @unique mac String @unique
hostname String? hostname String?
firstSeen DateTime? @default(now()) firstSeen DateTime? @default(now())
lastSeen DateTime? lastSeen DateTime?
essid String? ip String?
ip String?
uptime String? essid String?
apName String? uptime String?
apName String?
signalStrength Int?
frequency String?
protocol String?
speed Int?
usage Int?
status Status?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
controller String @default("unknown")
identity String? // The user informed by the controller
accessPointId Int? accessPointId Int?
accessPoint AccessPoint? @relation("wifidevice_to_ap", fields: [accessPointId], references: [id]) accessPoint AccessPoint? @relation("wifidevice_to_ap", fields: [accessPointId], references: [id])
status Status?
createdAt DateTime @default(now()) userId Int? // The connected User
updatedAt DateTime @updatedAt user User? @relation("wifidevice_to_user", fields: [userId], references: [id])
userId Int?
controller String @default("unknown") ownerId Int? // The owner of the device
user User? @relation("wifidevice_to_user", fields: [userId], references: [id]) owner User? @relation("wifidevice_to_owner", fields: [ownerId], references: [id])
} }
enum Status { enum Status {
@ -116,16 +135,6 @@ enum LogLevel {
ERROR ERROR
} }
model Statistic {
id Int @id @default(autoincrement())
timestamp DateTime @default(now())
onlineUsers Int
offlineUsers Int
totalUsers Int
totalWifiDevices Int
onlineWifiDevices Int
}
model PAHost { model PAHost {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
user String user String
@ -141,20 +150,44 @@ model PAHost {
} }
model AccessPoint { model AccessPoint {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
mac String @unique mac String @unique
hostname String @unique hostname String @unique
name String? name String?
local String? local String?
notes String? notes String?
inventoryTag String?
uptime String? uptime String?
controller String? controller String?
model String? model String?
ip String? ip String?
clients Int? clients Int?
sshUser String?
sshPassword String?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
wifiDevices WifiDevice[] @relation("wifidevice_to_ap") wifiDevices WifiDevice[] @relation("wifidevice_to_ap")
WifiStats WifiStats[] @relation("wifistats_to_ap")
}
model WifiStats {
id Int @id @default(autoincrement())
timestamp DateTime @default(now())
clients Int
accessPointId Int
accessPoint AccessPoint? @relation("wifistats_to_ap", fields: [accessPointId], references: [id])
}
model Network {
id Int @id @default(autoincrement())
name String @unique
shortName String @unique
cidr String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
} }

View File

@ -105,7 +105,12 @@ export async function getOnlineWifiDevices() {
uptime: client.UT.toString(), uptime: client.UT.toString(),
apName: client.AP, apName: client.AP,
status: client.ST == 'Online' ? 'ONLINE' : 'OFFLINE', status: client.ST == 'Online' ? 'ONLINE' : 'OFFLINE',
controller: 'Cisco' controller: 'Cisco',
signalStrength: client.SS,
frequency: client.FB,
protocol: client.PT,
speed: client.SD,
usage: client.bytes_total
})) }))
return restructuredOnlineDevices return restructuredOnlineDevices

View File

@ -189,7 +189,12 @@ export async function getOnlineWifiDevices() {
uptime: client.uptime.toString(), uptime: client.uptime.toString(),
apName: accessPoints[0].find(ap => ap.mac === client.ap_mac).name, apName: accessPoints[0].find(ap => ap.mac === client.ap_mac).name,
status: 'ONLINE', status: 'ONLINE',
controller: 'UniFi' controller: 'UniFi',
signalStrength: client.signal,
frequency: null,
protocol: null,
speed: client.tx_rate,
usage: +client.tx_bytes + +client.rx_bytes
})) }))
return restructuredOnlineDevices return restructuredOnlineDevices