-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/stats api endpoint #320
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
58770c6
feat: api stats
rtaieb 698b1c1
feat: api stats should be GET and not POST
rtaieb 0e8d6e0
fix: fix build
rtaieb 07245f1
fix: try to fix build
rtaieb 1e132ad
feat: use old prisma raw query way as new one does not work on CI CD.
rtaieb 2bcbc7f
feat: change north star value
mehdilouraoui 90e08bf
Merge branch 'main' into feat/stats-api-endpoint
rtaieb b388476
chore: mutualize code
rtaieb e26d27a
Revert "chore: mutualize code"
mehdilouraoui a3890af
back to raw query
mehdilouraoui b60cafe
feat: use raw query
mehdilouraoui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,71 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import z from "zod"; | ||
import { add } from "date-fns/add"; | ||
import { startOfYear } from "date-fns/startOfYear"; | ||
import { startOfMonth } from "date-fns/startOfMonth"; | ||
import { startOfWeek } from "date-fns/startOfWeek"; | ||
import { startOfDay } from "date-fns/startOfDay"; | ||
import { getNorthStarStats } from "@/src/lib/prisma/prisma-analytics-queries"; | ||
import { fr } from "date-fns/locale/fr"; | ||
|
||
const StatsRouteSchema = z.object({ | ||
since: z | ||
.number() | ||
.positive() | ||
.max(5000, { message: "Veuillez rentrer une valeur inférieure à 5000 pour le paramètre since" }), | ||
periodicity: z.enum(["year", "month", "week", "day"]), | ||
}); | ||
|
||
interface StatOutputRecord { | ||
value: number; | ||
date: Date; | ||
} | ||
|
||
type StatOutput = { | ||
description?: string; | ||
stats: StatOutputRecord[]; | ||
}; | ||
|
||
export async function GET(request: NextRequest) { | ||
const parsedRequest = StatsRouteSchema.safeParse({ | ||
since: +(request.nextUrl.searchParams.get("since") ?? 0), | ||
periodicity: request.nextUrl.searchParams.get("periodicity"), | ||
}); | ||
if (!parsedRequest.success) { | ||
const { errors } = parsedRequest.error; | ||
return NextResponse.json({ error: { message: "Invalid request", errors } }, { status: 400 }); | ||
} else { | ||
const { since: nbIntervals, periodicity } = parsedRequest.data; | ||
|
||
const ranges = { | ||
year: startOfYear, | ||
month: startOfMonth, | ||
week: (date: Date) => startOfWeek(date, { weekStartsOn: 1, locale: fr }), | ||
day: startOfDay, | ||
}; | ||
|
||
let dateBeginOfLastPeriod = new Date(); | ||
dateBeginOfLastPeriod = ranges[periodicity](new Date()); | ||
dateBeginOfLastPeriod = add(dateBeginOfLastPeriod, { | ||
minutes: -dateBeginOfLastPeriod.getTimezoneOffset(), | ||
}); | ||
|
||
const dateBeginOfFirstPeriod = add(dateBeginOfLastPeriod, { | ||
...(periodicity === "year" && { years: 1 - nbIntervals }), | ||
...(periodicity === "month" && { months: 1 - nbIntervals }), | ||
...(periodicity === "week" && { weeks: 1 - nbIntervals }), | ||
...(periodicity === "day" && { days: 1 - nbIntervals }), | ||
}); | ||
|
||
const results = await getNorthStarStats({ dateFrom: dateBeginOfFirstPeriod, range: periodicity }); | ||
|
||
const sanitizeResults: StatOutput = { | ||
stats: results.map((result) => ({ | ||
value: Number(result.score), | ||
date: result.periode!, | ||
})), | ||
}; | ||
|
||
return NextResponse.json(sanitizeResults); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 On pourrait même extraire cette logique dans les helpers de date et avoir un truc du type :