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,10 +14,12 @@ model ResetToken {
usedAt DateTime? usedAt DateTime?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
userId Int userId Int
user User @relation("resettoken_to_user", fields: [userId], references: [id])
creatorId Int creatorId Int
creator User @relation("resettoken_to_creator", fields: [creatorId], references: [id]) creator User @relation("resettoken_to_creator", fields: [creatorId], references: [id])
user User @relation("resettoken_to_user", fields: [userId], references: [id])
} }
model User { model User {
@ -66,9 +68,12 @@ model User {
userPrincipalName String? userPrincipalName String?
whenChanged String? whenChanged String?
whenCreated String? whenCreated String?
createdTokens ResetToken[] @relation("resettoken_to_creator") createdTokens ResetToken[] @relation("resettoken_to_creator")
tokens ResetToken[] @relation("resettoken_to_user") tokens ResetToken[] @relation("resettoken_to_user")
wifiDevices WifiDevice[] @relation("wifidevice_to_user") wifiDevices WifiDevice[] @relation("wifidevice_to_user")
ownedWifiDevices WifiDevice[] @relation("wifidevice_to_owner")
PAHost PAHost[] @relation("pahost_to_user") PAHost PAHost[] @relation("pahost_to_user")
} }
@ -79,18 +84,32 @@ model WifiDevice {
hostname String? hostname String?
firstSeen DateTime? @default(now()) firstSeen DateTime? @default(now())
lastSeen DateTime? lastSeen DateTime?
essid String?
ip String? ip String?
essid String?
uptime String? uptime String?
apName String? apName String?
accessPointId Int? signalStrength Int?
accessPoint AccessPoint? @relation("wifidevice_to_ap", fields: [accessPointId], references: [id]) frequency String?
protocol String?
speed Int?
usage Int?
status Status? status Status?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
userId Int?
controller String @default("unknown") controller String @default("unknown")
identity String? // The user informed by the controller
accessPointId Int?
accessPoint AccessPoint? @relation("wifidevice_to_ap", fields: [accessPointId], references: [id])
userId Int? // The connected User
user User? @relation("wifidevice_to_user", fields: [userId], references: [id]) user User? @relation("wifidevice_to_user", fields: [userId], references: [id])
ownerId Int? // The owner of the device
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
@ -147,14 +156,38 @@ model AccessPoint {
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