Rename WifiStats table to AccessPointStats
This commit is contained in:
parent
2c6405edc9
commit
1abb3b4716
|
@ -1,29 +0,0 @@
|
|||
/*
|
||||
Warnings:
|
||||
|
||||
- The `uptime` column on the `AccessPoint` table would be dropped and recreated. This will lead to data loss if there is data in the column.
|
||||
- The `uptime` column on the `WifiDevice` table would be dropped and recreated. This will lead to data loss if there is data in the column.
|
||||
- You are about to drop the column `avgSignal` on the `WifiStats` table. All the data in the column will be lost.
|
||||
- You are about to drop the column `maxSignal` on the `WifiStats` table. All the data in the column will be lost.
|
||||
- You are about to drop the column `minSignal` on the `WifiStats` table. All the data in the column will be lost.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE "AccessPoint" ADD COLUMN "usage" BIGINT,
|
||||
DROP COLUMN "uptime",
|
||||
ADD COLUMN "uptime" INTEGER;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "WifiDevice" DROP COLUMN "uptime",
|
||||
ADD COLUMN "uptime" INTEGER,
|
||||
ALTER COLUMN "usage" SET DATA TYPE BIGINT;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "WifiStats" DROP COLUMN "avgSignal",
|
||||
DROP COLUMN "maxSignal",
|
||||
DROP COLUMN "minSignal",
|
||||
ADD COLUMN "avgSignalStrength" INTEGER,
|
||||
ADD COLUMN "maxSignalStrength" INTEGER,
|
||||
ADD COLUMN "minSignalStrength" INTEGER,
|
||||
ALTER COLUMN "avgUsage" SET DATA TYPE BIGINT,
|
||||
ALTER COLUMN "sumUsage" SET DATA TYPE BIGINT;
|
46
server/prisma/migrations/20220615200713_usage/migration.sql
Normal file
46
server/prisma/migrations/20220615200713_usage/migration.sql
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Warnings:
|
||||
|
||||
- The `uptime` column on the `AccessPoint` table would be dropped and recreated. This will lead to data loss if there is data in the column.
|
||||
- The `uptime` column on the `WifiDevice` table would be dropped and recreated. This will lead to data loss if there is data in the column.
|
||||
- You are about to drop the `WifiStats` table. If the table is not empty, all the data it contains will be lost.
|
||||
|
||||
*/
|
||||
-- DropForeignKey
|
||||
ALTER TABLE "WifiStats" DROP CONSTRAINT "WifiStats_accessPointId_fkey";
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "AccessPoint" ADD COLUMN "usage" BIGINT,
|
||||
DROP COLUMN "uptime",
|
||||
ADD COLUMN "uptime" INTEGER;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "WifiDevice" DROP COLUMN "uptime",
|
||||
ADD COLUMN "uptime" INTEGER,
|
||||
ALTER COLUMN "usage" SET DATA TYPE BIGINT;
|
||||
|
||||
-- DropTable
|
||||
DROP TABLE "WifiStats";
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "AccessPointStats" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"clients" INTEGER,
|
||||
"avgSignalStrength" INTEGER,
|
||||
"minSignalStrength" INTEGER,
|
||||
"maxSignalStrength" INTEGER,
|
||||
"avgSpeed" INTEGER,
|
||||
"minSpeed" INTEGER,
|
||||
"maxSpeed" INTEGER,
|
||||
"avgClientUptime" INTEGER,
|
||||
"maxClientUptime" INTEGER,
|
||||
"avgUsage" BIGINT,
|
||||
"sumUsage" BIGINT,
|
||||
"accessPointId" INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT "AccessPointStats_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "AccessPointStats" ADD CONSTRAINT "AccessPointStats_accessPointId_fkey" FOREIGN KEY ("accessPointId") REFERENCES "AccessPoint"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
|
@ -175,10 +175,10 @@ model AccessPoint {
|
|||
updatedAt DateTime @updatedAt
|
||||
|
||||
wifiDevices WifiDevice[] @relation("wifidevice_to_ap")
|
||||
stats WifiStats[] @relation("wifistats_to_ap")
|
||||
stats AccessPointStats[] @relation("accesspointstats_to_ap")
|
||||
}
|
||||
|
||||
model WifiStats {
|
||||
model AccessPointStats {
|
||||
id Int @id @default(autoincrement())
|
||||
timestamp DateTime @default(now())
|
||||
|
||||
|
@ -199,7 +199,7 @@ model WifiStats {
|
|||
sumUsage BigInt?
|
||||
|
||||
accessPointId Int
|
||||
accessPoint AccessPoint @relation("wifistats_to_ap", fields: [accessPointId], references: [id])
|
||||
accessPoint AccessPoint @relation("accesspointstats_to_ap", fields: [accessPointId], references: [id])
|
||||
}
|
||||
|
||||
model Network {
|
||||
|
|
|
@ -8,7 +8,7 @@ import { updateAccessPoints } from './lib/accessPoints'
|
|||
import {
|
||||
deleteOldStats,
|
||||
generateStatsForAllAccessPoints
|
||||
} from './lib/wifiStats'
|
||||
} from './lib/accessPointStats'
|
||||
|
||||
// WARNING! All crontasks are blocking! Do not await inside it
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ async function generateStatsForAccessPoint(mac) {
|
|||
sumUsage: dbStats._sum.usage || 0
|
||||
}
|
||||
|
||||
await prisma.wifiStats.create({
|
||||
await prisma.accessPointStats.create({
|
||||
data: {
|
||||
...stats,
|
||||
accessPoint: { connect: { mac } }
|
||||
|
@ -78,13 +78,13 @@ export async function generateStatsForAllAccessPoints() {
|
|||
}
|
||||
|
||||
logSuccess({
|
||||
tags: ['wifiStats', 'generateStatsForAllAccessPoints'],
|
||||
tags: ['accessPointStats', 'generateStatsForAllAccessPoints'],
|
||||
message: `Estatísticas geradas para ${accessPoints.length} access points`
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
logError({
|
||||
tags: ['wifiStats', 'generateStatsForAllAccessPoints'],
|
||||
tags: ['accessPointStats', 'generateStatsForAllAccessPoints'],
|
||||
message: 'Erro ao gerar estatísticas para todos os access points',
|
||||
data: e
|
||||
})
|
||||
|
@ -93,17 +93,17 @@ export async function generateStatsForAllAccessPoints() {
|
|||
|
||||
export async function deleteOldStats() {
|
||||
try {
|
||||
const stats = await prisma.wifiStats.deleteMany({
|
||||
const stats = await prisma.accessPointStats.deleteMany({
|
||||
where: { timestamp: { lt: subDays(new Date(), DAYS_TO_KEEP) } }
|
||||
})
|
||||
|
||||
logSuccess({
|
||||
tags: ['wifiStats', 'deleteOldStats'],
|
||||
tags: ['accessPointStats', 'deleteOldStats'],
|
||||
message: `${stats.count} estatísticas com mais de ${DAYS_TO_KEEP} dias deletadas.`
|
||||
})
|
||||
} catch (e) {
|
||||
logError({
|
||||
tags: ['wifiStats', 'deleteOldStats'],
|
||||
tags: ['accessPointStats', 'deleteOldStats'],
|
||||
message: 'Erro ao deletar estatísticas antigas',
|
||||
data: e
|
||||
})
|
|
@ -1,5 +1,7 @@
|
|||
import prisma from '../prisma'
|
||||
import { getSubnetInfo } from '../lib/subnetInfo'
|
||||
import { subDays } from 'date-fns'
|
||||
import { distributedCopy } from '../utils/distributedCopy'
|
||||
|
||||
export const AccessPoint = {
|
||||
updatedAt: (parent, data, context, info) => parent.updatedAt?.toISOString(),
|
||||
|
@ -21,23 +23,28 @@ export const AccessPoint = {
|
|||
usage: (parent, data, context, info) => parent.usage.toString(),
|
||||
|
||||
stats: async (parent, { take, dateIn, dateOut }, context, info) => {
|
||||
const stats = await prisma.wifiStats.findMany({
|
||||
if (!dateOut) {
|
||||
dateOut = new Date()
|
||||
dateIn = subDays(dateOut, 1)
|
||||
}
|
||||
|
||||
const stats = await prisma.accessPointStats.findMany({
|
||||
where: {
|
||||
accessPoint: {
|
||||
id: parent.id,
|
||||
timestamp_gte: dateIn,
|
||||
timestamp_lte: dateOut
|
||||
}
|
||||
id: parent.id
|
||||
},
|
||||
orderBy: { timestamp: 'desc' },
|
||||
take
|
||||
timestamp: { gte: dateIn, lte: dateOut }
|
||||
},
|
||||
orderBy: { timestamp: 'desc' }
|
||||
})
|
||||
|
||||
return stats.reverse()
|
||||
if (take) return distributedCopy(stats, take)
|
||||
|
||||
return stats
|
||||
},
|
||||
|
||||
latestStats: async (parent, data, context, info) =>
|
||||
prisma.wifiStats.findFirst({
|
||||
prisma.accessPointStats.findFirst({
|
||||
where: { accessPoint: { id: parent.id } },
|
||||
orderBy: { timestamp: 'desc' }
|
||||
})
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const WifiStats = {
|
||||
const AccessPointStats = {
|
||||
timestamp: parent => parent.timestamp.toString(),
|
||||
avgUsage: parent => parent.avgUsage.toString(),
|
||||
sumUsage: parent => parent.sumUsage.toString()
|
||||
}
|
||||
|
||||
export { WifiStats }
|
||||
export { AccessPointStats }
|
|
@ -11,7 +11,7 @@ import { Stats } from './Stats'
|
|||
import { User } from './User'
|
||||
import { UserPresence } from './UserPresence'
|
||||
import { WifiDevice } from './WifiDevice'
|
||||
import { WifiStats } from './WifiStats'
|
||||
import { AccessPointStats } from './AccessPointStats'
|
||||
|
||||
const resolvers = {
|
||||
Mutation,
|
||||
|
@ -27,7 +27,7 @@ const resolvers = {
|
|||
User,
|
||||
UserPresence,
|
||||
WifiDevice,
|
||||
WifiStats
|
||||
AccessPointStats
|
||||
}
|
||||
|
||||
export { resolvers }
|
||||
|
|
|
@ -328,12 +328,12 @@ const typeDefs = gql`
|
|||
updatedAt: String
|
||||
wifiDevices: [WifiDevice]
|
||||
|
||||
stats(take: Int = 50, dateIn: String, dateOut: String): [WifiStats!]!
|
||||
latestStats: WifiStats!
|
||||
stats(take: Int = 50, dateIn: String, dateOut: String): [AccessPointStats!]!
|
||||
latestStats: AccessPointStats!
|
||||
}
|
||||
|
||||
"A Wireless Access Point stats"
|
||||
type WifiStats {
|
||||
type AccessPointStats {
|
||||
id: ID!
|
||||
timestamp: String
|
||||
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
<script>
|
||||
import { Line as LineChart } from 'vue-chartjs/legacy'
|
||||
import { time } from '../../plugins/date'
|
||||
|
||||
import { Chart as ChartJS, registerables } from 'chart.js'
|
||||
import 'chartjs-adapter-date-fns'
|
||||
|
|
Loading…
Reference in New Issue
Block a user