-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ESV-Sweetplum/metric-logging
Create metric logging
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- CreateTable | ||
CREATE TABLE "Metric" ( | ||
"id" SERIAL NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"userCount" INTEGER NOT NULL DEFAULT 0, | ||
"mapCount" INTEGER NOT NULL DEFAULT 0, | ||
"ratingCount" INTEGER NOT NULL DEFAULT 0, | ||
"scoreCount" INTEGER NOT NULL DEFAULT 0, | ||
"categoryMapCount" INTEGER[] DEFAULT ARRAY[0, 0, 0, 0, 0]::INTEGER[], | ||
|
||
CONSTRAINT "Metric_pkey" PRIMARY KEY ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { NextRequest } from "next/server"; | ||
import prisma from "../../../../prisma/initialize"; | ||
import { Category, Prisma } from "@prisma/client"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const authHeader = request.headers.get("authorization"); | ||
|
||
console.log(authHeader); | ||
|
||
if (authHeader !== `Bearer ${process.env.CRON_AUTH}`) | ||
return Response.json({ | ||
status: 401, | ||
message: "Unauthorized", | ||
}); | ||
|
||
const userCount = await prisma.user.count(); | ||
const mapCount = await prisma.map.count(); | ||
const ratingCount = await prisma.rating.count(); | ||
|
||
const categories: Category[] = [ | ||
"Reading", | ||
"Hybrid", | ||
"Memory", | ||
"Reverse", | ||
"Splitscroll", | ||
]; | ||
|
||
const categoryMapCount = []; | ||
|
||
for (let i = 0; i < categories.length; i++) { | ||
const count = await prisma.map.count({ | ||
where: { category: categories[i] }, | ||
}); | ||
categoryMapCount.push(count); | ||
} | ||
|
||
await prisma.metric.create({ | ||
data: { | ||
userCount, | ||
mapCount, | ||
ratingCount, | ||
scoreCount: 0, | ||
categoryMapCount, | ||
}, | ||
}); | ||
} |