diff --git a/server/prisma/migrations/20220614120906_usage_fields/migration.sql b/server/prisma/migrations/20220614120906_usage_fields/migration.sql deleted file mode 100644 index 8696fac..0000000 --- a/server/prisma/migrations/20220614120906_usage_fields/migration.sql +++ /dev/null @@ -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; diff --git a/server/prisma/migrations/20220615200713_usage/migration.sql b/server/prisma/migrations/20220615200713_usage/migration.sql new file mode 100644 index 0000000..35a3fed --- /dev/null +++ b/server/prisma/migrations/20220615200713_usage/migration.sql @@ -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; diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index dea2530..b22089a 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -174,11 +174,11 @@ model AccessPoint { createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - wifiDevices WifiDevice[] @relation("wifidevice_to_ap") - stats WifiStats[] @relation("wifistats_to_ap") + wifiDevices WifiDevice[] @relation("wifidevice_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 { diff --git a/server/src/cronTasks.js b/server/src/cronTasks.js index 1b76671..4e4e503 100644 --- a/server/src/cronTasks.js +++ b/server/src/cronTasks.js @@ -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 diff --git a/server/src/lib/wifiStats.js b/server/src/lib/accessPointStats.js similarity index 87% rename from server/src/lib/wifiStats.js rename to server/src/lib/accessPointStats.js index 6b6c13b..c7a2c99 100644 --- a/server/src/lib/wifiStats.js +++ b/server/src/lib/accessPointStats.js @@ -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 }) diff --git a/server/src/resolvers/AccessPoint.js b/server/src/resolvers/AccessPoint.js index 9a1a2af..a440038 100644 --- a/server/src/resolvers/AccessPoint.js +++ b/server/src/resolvers/AccessPoint.js @@ -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 + }, + timestamp: { gte: dateIn, lte: dateOut } }, - orderBy: { timestamp: 'desc' }, - take + 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' } }) diff --git a/server/src/resolvers/WifiStats.js b/server/src/resolvers/AccessPointStats.js similarity index 73% rename from server/src/resolvers/WifiStats.js rename to server/src/resolvers/AccessPointStats.js index ba137b1..8ee54a9 100644 --- a/server/src/resolvers/WifiStats.js +++ b/server/src/resolvers/AccessPointStats.js @@ -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 } diff --git a/server/src/resolvers/index.js b/server/src/resolvers/index.js index ed5e1b6..f984543 100755 --- a/server/src/resolvers/index.js +++ b/server/src/resolvers/index.js @@ -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 } diff --git a/server/src/typeDefs.js b/server/src/typeDefs.js index 648217b..ccb5590 100644 --- a/server/src/typeDefs.js +++ b/server/src/typeDefs.js @@ -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 diff --git a/web/src/components/Charts/AccessPointSignalStrengthChart.vue b/web/src/components/Charts/AccessPointSignalStrengthChart.vue index c337e2f..58d7d62 100644 --- a/web/src/components/Charts/AccessPointSignalStrengthChart.vue +++ b/web/src/components/Charts/AccessPointSignalStrengthChart.vue @@ -16,7 +16,6 @@