Skip to content

Commit

Permalink
force jsonb objects for data
Browse files Browse the repository at this point in the history
  • Loading branch information
smayya337 committed Oct 8, 2023
1 parent 364a211 commit 98e98bb
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 17 deletions.
14 changes: 6 additions & 8 deletions app/admin/upload/submit/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { isAdmin } from '@/lib/auth/admin';
import { getCurrentUserID } from '@/lib/auth/helpers';
import { getRouteHandlerClient } from '@/lib/global/supabase';
import { addResultFromYAMLFile } from '@/lib/results/async';
// import { addResultFromYAMLFile } from '@/lib/results/async';
import { ResultsAddQueue } from '@/lib/results/queue';
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs';
import { cookies, headers } from 'next/headers';
import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';

export async function POST(request: NextRequest) {
// const supabase = getRouteHandlerClient(cookies);
// if (!(await isAdmin(supabase))) {
// return new Response(null, { status: 403 });
// }
const supabase = getRouteHandlerClient(cookies);
if (!(await isAdmin(supabase))) {
return new NextResponse(null, { status: 403 });
}
const data = await request.formData();
const allFiles = data.getAll('yaml');
const q = ResultsAddQueue.getInstance();
Expand Down
2 changes: 1 addition & 1 deletion lib/events/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ export async function createEventDataInput(event: Event, duosmiumID: string) {
return {
resultDuosmiumId: duosmiumID,
name: event.name,
data: event.rep
data: sql`${event.rep}::jsonb`
};
}
2 changes: 2 additions & 0 deletions lib/global/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export async function keepTryingUntilItWorks(fn, data) {
} catch (e: PostgresError) {
if (e.message === 'deadlock detected') {
return await keepTryingUntilItWorks(fn, data);
} else {
throw e;
}
}
}
2 changes: 1 addition & 1 deletion lib/histograms/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ export async function addHistogram(histogramData: object, tx = db) {
export async function createHistogramDataInput(histogram: Histogram, duosmiumID: string) {
return {
resultDuosmiumId: duosmiumID,
data: histogram.rep
data: sql`${histogram.rep}::jsonb`
};
}
2 changes: 1 addition & 1 deletion lib/penalties/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ export async function createPenaltyDataInput(penalty: Penalty, duosmiumID: strin
return {
resultDuosmiumId: duosmiumID,
teamNumber: penalty.team.number,
data: penalty.rep
data: sql`${penalty.rep}::jsonb`
};
}
2 changes: 1 addition & 1 deletion lib/placings/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ export async function createPlacingDataInput(placing: Placing, duosmiumID: strin
eventName: placing.event.name,
teamNumber: placing.team.number,
resultDuosmiumId: duosmiumID,
data: placing.rep
data: sql`${placing.rep}::jsonb`
};
}
5 changes: 4 additions & 1 deletion lib/results/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ export async function addResultFromYAMLFile(
// @ts-ignore
const obj: object = load(yaml);
const interpreter: Interpreter = getInterpreter(obj);
// if (await resultExists(generateFilename(interpreter))) {
// return;
// }
try {
await keepTryingUntilItWorks(addCompleteResult, interpreter);
// await addCompleteResult(interpreter);
Expand Down Expand Up @@ -237,7 +240,7 @@ export async function regenerateAllMetadata() {
for (const id of ids) {
// @ts-ignore
const input = await createResultDataInput(getInterpreter(await getCompleteResult(id)));
await addResult(input);
await addResult(input, tx);
}
});
return await operation;
Expand Down
6 changes: 5 additions & 1 deletion lib/results/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { QueueObject } from 'async';
import { queue } from 'async';
import { addResultFromYAMLFile } from './async';

const MAX_PROCESSES = 16;
const MAX_PROCESSES = 64;

export class ResultsAddQueue {
private static instance: ResultsAddQueue;
Expand Down Expand Up @@ -31,6 +31,10 @@ export class ResultsAddQueue {
this.q.drain(arg);
}

public error(arg: (err: any, task: any) => void) {
this.q.error(arg);
}

public push(arg: File) {
this.q.push(arg);
console.log(
Expand Down
2 changes: 1 addition & 1 deletion lib/teams/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function createTeamDataInput(team: Team, duosmiumID: string) {
return {
resultDuosmiumId: duosmiumID,
number: team.number,
data: team.rep,
data: sql`${team.rep}::jsonb`,
name: locationName,
city: locationCity,
state: locationState,
Expand Down
2 changes: 1 addition & 1 deletion lib/tournaments/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function addTournament(tournamentData: object, tx = db) {
export async function createTournamentDataInput(tournament: Tournament, duosmiumID: string) {
return {
resultDuosmiumId: duosmiumID,
data: tournament.rep
data: sql`${tournament.rep}::jsonb`
};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tracks/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ export async function createTrackDataInput(track: Track, duosmiumID: string) {
return {
resultDuosmiumId: duosmiumID,
name: track.name.toString(),
data: track.rep
data: sql`${track.rep}::jsonb`
};
}

0 comments on commit 98e98bb

Please sign in to comment.