Skip to content

Commit

Permalink
feat: add lighthouse score in database fields
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Sep 30, 2024
1 parent 9cf46e6 commit 6c2a093
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 11 additions & 7 deletions src/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 4 additions & 0 deletions src/server/prisma/zod/websitelighthousereport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
18 changes: 18 additions & 0 deletions src/server/trpc/routers/website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -613,13 +614,30 @@ 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,
},
data: {
status: WebsiteLighthouseReportStatus.Success,
result: JSON.stringify(result),
performanceScore,
accessibilityScore,
bestPracticesScore,
seoScore,
},
});
})
Expand Down

0 comments on commit 6c2a093

Please sign in to comment.