-
Notifications
You must be signed in to change notification settings - Fork 5
added script and updated json #74
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
Open
GilMM27
wants to merge
3
commits into
main
Choose a base branch
from
add-larc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,168 @@ | ||
| import { PrismaClient } from "@prisma/client"; | ||
| import fs from "fs/promises"; | ||
| import path from "path"; | ||
| const prisma = new PrismaClient(); | ||
|
|
||
| const EDITION_NAME = "LARC 2025"; | ||
| const OUTPUT_FILE = path.resolve("public/PastEditions.json"); | ||
|
|
||
| type TeamScore = { | ||
| nombreEquipo: string; | ||
| puntajePistaA: number; | ||
| puntajePistaB: number; | ||
| puntajePistaC: number; | ||
| puntajeFinal: number; | ||
| }; | ||
|
|
||
| function isTeamScoreArray(val: unknown): val is TeamScore[] { | ||
| return ( | ||
| Array.isArray(val) && | ||
| val.every( | ||
| (v) => | ||
| v && | ||
| typeof v === "object" && | ||
| "nombreEquipo" in v && | ||
| "puntajePistaA" in v && | ||
| "puntajePistaB" in v && | ||
| "puntajePistaC" in v && | ||
| "puntajeFinal" in v, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| function isRecordTeamScoreArray( | ||
| val: unknown, | ||
| ): val is Record<string, TeamScore[]> { | ||
| if (!val || typeof val !== "object") return false; | ||
| return Object.values(val).every(isTeamScoreArray); | ||
biweep863 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Helper to aggregate latest entries per (teamId, roundId), returning per-team round points | ||
| function accumulateLatest< | ||
| T extends { | ||
| teamId: string; | ||
| roundId: string; | ||
| createdAt: Date; | ||
| points: number; | ||
| team: { name: string }; | ||
| }, | ||
| >(records: T[]) { | ||
| const latest: Record<string, Record<string, T>> = {}; | ||
| for (const r of records) { | ||
| const teamMap = latest[r.teamId] ?? (latest[r.teamId] = {}); | ||
| const existing = teamMap[r.roundId]; | ||
| if (!existing || existing.createdAt < r.createdAt) teamMap[r.roundId] = r; | ||
| } | ||
| const perTeam: Record<string, { name: string; rounds: number[] }> = {}; | ||
| for (const teamId of Object.keys(latest)) { | ||
| const rounds = Object.values(latest[teamId]!).map((r) => r.points); | ||
| perTeam[teamId] = { | ||
| name: Object.values(latest[teamId]!)[0]!.team.name, | ||
| rounds, | ||
| }; | ||
| } | ||
| return perTeam; | ||
| } | ||
|
|
||
| function sumTopTwo(values: number[]): number { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good detail |
||
| if (!values || values.length === 0) return 0; | ||
| const sorted = [...values].sort((a, b) => b - a); | ||
| const first = sorted[0] ?? 0; | ||
| const second = sorted[1] ?? 0; | ||
| return first + second; | ||
| } | ||
|
|
||
| export async function exportUsersToCSV() { | ||
| try { | ||
| // Fetch challenge records with minimal fields. | ||
| const [aRecords, bRecords, cRecords] = await Promise.all([ | ||
| prisma.challengeA.findMany({ | ||
| select: { | ||
| teamId: true, | ||
| roundId: true, | ||
| createdAt: true, | ||
| points: true, | ||
| team: { select: { name: true } }, | ||
| }, | ||
| }), | ||
| prisma.challengeB.findMany({ | ||
| select: { | ||
| teamId: true, | ||
| roundId: true, | ||
| createdAt: true, | ||
| points: true, | ||
| team: { select: { name: true } }, | ||
| }, | ||
| }), | ||
| prisma.challengeC.findMany({ | ||
| select: { | ||
| teamId: true, | ||
| roundId: true, | ||
| createdAt: true, | ||
| points: true, | ||
| team: { select: { name: true } }, | ||
| }, | ||
| }), | ||
| ]); | ||
|
|
||
| const aPerTeam = accumulateLatest(aRecords); | ||
| const bPerTeam = accumulateLatest(bRecords); | ||
| const cPerTeam = accumulateLatest(cRecords); | ||
|
|
||
| // Collect unique teamIds from all challenges | ||
| const teamIds = new Set<string>([ | ||
| ...Object.keys(aPerTeam), | ||
| ...Object.keys(bPerTeam), | ||
| ...Object.keys(cPerTeam), | ||
| ]); | ||
|
|
||
| // const yearKey = new Date().getFullYear().toString(); | ||
| const scores: TeamScore[] = []; | ||
| for (const teamId of teamIds) { | ||
| const name = | ||
| aPerTeam[teamId]?.name ?? | ||
| bPerTeam[teamId]?.name ?? | ||
| cPerTeam[teamId]?.name ?? | ||
| "Unknown"; | ||
| const puntajePistaA = sumTopTwo(aPerTeam[teamId]?.rounds ?? []); | ||
| const puntajePistaB = sumTopTwo(bPerTeam[teamId]?.rounds ?? []); | ||
| const puntajePistaC = sumTopTwo(cPerTeam[teamId]?.rounds ?? []); | ||
| const puntajeFinal = puntajePistaA + puntajePistaB + puntajePistaC; | ||
| scores.push({ | ||
| nombreEquipo: name, | ||
| puntajePistaA, | ||
| puntajePistaB, | ||
| puntajePistaC, | ||
| puntajeFinal, | ||
| }); | ||
| } | ||
|
|
||
| // Sort descending by final score | ||
| scores.sort((x, y) => y.puntajeFinal - x.puntajeFinal); | ||
|
|
||
| // Load existing JSON file (if exists) and merge | ||
| let existing: Record<string, TeamScore[]> = {}; | ||
| try { | ||
| const raw = await fs.readFile(OUTPUT_FILE, "utf8"); | ||
| const parsed: unknown = JSON.parse(raw); | ||
| if (isRecordTeamScoreArray(parsed)) existing = parsed; | ||
| } catch (e) { | ||
| // ignore; keep empty existing | ||
| } | ||
|
|
||
| existing[EDITION_NAME] = scores; // Replace / create current year entry | ||
|
|
||
| await fs.writeFile(OUTPUT_FILE, JSON.stringify(existing, null, 2), "utf8"); | ||
| return scores; | ||
| } catch (error) { | ||
| console.error("Failed to export edition scores:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| // Invoke immediately when this module is run (script usage) | ||
| try { | ||
| await exportUsersToCSV(); | ||
| } finally { | ||
| await prisma.$disconnect(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.