From 6c2a0938423385d67309deefa67a3d971bf8d7c8 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Tue, 1 Oct 2024 01:41:10 +0800 Subject: [PATCH] feat: add lighthouse score in database fields --- .../migration.sql | 5 +++++ src/server/prisma/schema.prisma | 18 +++++++++++------- .../prisma/zod/websitelighthousereport.ts | 4 ++++ src/server/trpc/routers/website.ts | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 src/server/prisma/migrations/20240930172940_add_lighthouse_score/migration.sql diff --git a/src/server/prisma/migrations/20240930172940_add_lighthouse_score/migration.sql b/src/server/prisma/migrations/20240930172940_add_lighthouse_score/migration.sql new file mode 100644 index 0000000..d699862 --- /dev/null +++ b/src/server/prisma/migrations/20240930172940_add_lighthouse_score/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "WebsiteLighthouseReport" ADD COLUMN "accessibilityScore" INTEGER NOT NULL DEFAULT 0, +ADD COLUMN "bestPracticesScore" INTEGER NOT NULL DEFAULT 0, +ADD COLUMN "performanceScore" INTEGER NOT NULL DEFAULT 0, +ADD COLUMN "seoScore" INTEGER NOT NULL DEFAULT 0; diff --git a/src/server/prisma/schema.prisma b/src/server/prisma/schema.prisma index 91ba191..773d6b7 100644 --- a/src/server/prisma/schema.prisma +++ b/src/server/prisma/schema.prisma @@ -253,13 +253,17 @@ enum WebsiteLighthouseReportStatus { } model WebsiteLighthouseReport { - id String @id() @default(cuid()) @db.VarChar(30) - websiteId String? @db.VarChar(30) - createdAt DateTime @default(now()) @db.Timestamptz(6) - updatedAt DateTime @updatedAt @db.Timestamptz(6) - url String - result String // json string - status WebsiteLighthouseReportStatus @default(Pending) + id String @id() @default(cuid()) @db.VarChar(30) + websiteId String? @db.VarChar(30) + createdAt DateTime @default(now()) @db.Timestamptz(6) + updatedAt DateTime @updatedAt @db.Timestamptz(6) + url String + result String // json string + status WebsiteLighthouseReportStatus @default(Pending) + performanceScore Int @default(0) + accessibilityScore Int @default(0) + bestPracticesScore Int @default(0) + seoScore Int @default(0) @@index([createdAt]) @@index([websiteId]) diff --git a/src/server/prisma/zod/websitelighthousereport.ts b/src/server/prisma/zod/websitelighthousereport.ts index ed8c2de..7314323 100644 --- a/src/server/prisma/zod/websitelighthousereport.ts +++ b/src/server/prisma/zod/websitelighthousereport.ts @@ -10,4 +10,8 @@ export const WebsiteLighthouseReportModelSchema = z.object({ url: z.string(), result: z.string(), status: z.nativeEnum(WebsiteLighthouseReportStatus), + performanceScore: z.number().int(), + accessibilityScore: z.number().int(), + bestPracticesScore: z.number().int(), + seoScore: z.number().int(), }) diff --git a/src/server/trpc/routers/website.ts b/src/server/trpc/routers/website.ts index 3355a5d..b6029e2 100644 --- a/src/server/trpc/routers/website.ts +++ b/src/server/trpc/routers/website.ts @@ -38,6 +38,7 @@ import { generateLighthouse } from '../../utils/screenshot/lighthouse.js'; import { WebsiteLighthouseReportModelSchema } from '../../prisma/zod/websitelighthousereport.js'; import { buildCursorResponseSchema } from '../../utils/schema.js'; import { logger } from '../../utils/logger.js'; +import { get } from 'lodash-es'; const websiteNameSchema = z.string().max(100); const websiteDomainSchema = z.union([ @@ -613,6 +614,19 @@ export const websiteRouter = router({ generateLighthouse(url) .then(async (result) => { logger.info('Successfully generated lighthouse report'); + + const performanceScore = + Number(get(result, ['categories', 'performance', 'score'], 0)) * + 100; + const accessibilityScore = + Number(get(result, ['categories', 'accessibility', 'score'], 0)) * + 100; + const bestPracticesScore = + Number(get(result, ['categories', 'best-practices', 'score'], 0)) * + 100; + const seoScore = + Number(get(result, ['categories', 'seo', 'score'], 0)) * 100; + await prisma.websiteLighthouseReport.update({ where: { id: websiteInfo.id, @@ -620,6 +634,10 @@ export const websiteRouter = router({ data: { status: WebsiteLighthouseReportStatus.Success, result: JSON.stringify(result), + performanceScore, + accessibilityScore, + bestPracticesScore, + seoScore, }, }); })