mirror of
https://github.com/ershisan99/www.git
synced 2025-12-17 05:19:23 +00:00
add refresh history by date endpoint
This commit is contained in:
50
scripts/refresh-history-by-date.ts
Normal file
50
scripts/refresh-history-by-date.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { syncHistoryByDateRange } from '@/server/api/routers/history'
|
||||
|
||||
async function refreshHistoryByDate(startDate?: string, endDate?: string) {
|
||||
try {
|
||||
console.log('Refreshing history by date range...')
|
||||
if (startDate) {
|
||||
console.log(`Start date: ${startDate}`)
|
||||
}
|
||||
if (endDate) {
|
||||
console.log(`End date: ${endDate}`)
|
||||
}
|
||||
|
||||
await syncHistoryByDateRange(startDate, endDate)
|
||||
console.log('History refresh completed successfully')
|
||||
} catch (err) {
|
||||
console.error('History refresh failed:', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Parse command line arguments
|
||||
function parseArgs() {
|
||||
const args = process.argv.slice(2)
|
||||
let startDate: string | undefined
|
||||
let endDate: string | undefined
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--start-date' && i + 1 < args.length) {
|
||||
startDate = args[i + 1]
|
||||
i++
|
||||
} else if (args[i] === '--end-date' && i + 1 < args.length) {
|
||||
endDate = args[i + 1]
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return { startDate, endDate }
|
||||
}
|
||||
|
||||
// Run if called directly
|
||||
if (require.main === module) {
|
||||
const { startDate, endDate } = parseArgs()
|
||||
|
||||
refreshHistoryByDate(startDate, endDate)
|
||||
.then(() => process.exit(0))
|
||||
.catch((err) => {
|
||||
console.error('Refresh failed:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
41
src/app/api/refresh-history-by-date/route.ts
Normal file
41
src/app/api/refresh-history-by-date/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { env } from '@/env'
|
||||
import { syncHistoryByDateRange } from '@/server/api/routers/history'
|
||||
import { headers } from 'next/headers'
|
||||
|
||||
const SECURE_TOKEN = env.CRON_SECRET
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const headersList = await headers()
|
||||
const authToken = headersList.get('authorization')?.replace('Bearer ', '')
|
||||
|
||||
if (authToken !== SECURE_TOKEN) {
|
||||
return new Response('unauthorized', { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse request body to get date range parameters
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const startDate = body.start_date
|
||||
const endDate = body.end_date
|
||||
|
||||
try {
|
||||
console.log('refreshing history by date range...')
|
||||
if (startDate) {
|
||||
console.log(`Start date: ${startDate}`)
|
||||
}
|
||||
if (endDate) {
|
||||
console.log(`End date: ${endDate}`)
|
||||
}
|
||||
|
||||
await syncHistoryByDateRange(startDate, endDate)
|
||||
} catch (err) {
|
||||
console.error('history refresh by date range failed:', err)
|
||||
return new Response('internal error', { status: 500 })
|
||||
}
|
||||
|
||||
return Response.json({ success: true })
|
||||
} catch (err) {
|
||||
console.error('refresh failed:', err)
|
||||
return new Response('internal error', { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,16 @@ export const history_router = createTRPCRouter({
|
||||
sync: publicProcedure.mutation(async () => {
|
||||
return syncHistory()
|
||||
}),
|
||||
syncByDateRange: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
start_date: z.string().optional(),
|
||||
end_date: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
return syncHistoryByDateRange(input.start_date, input.end_date)
|
||||
}),
|
||||
})
|
||||
|
||||
export async function syncHistory() {
|
||||
@@ -80,6 +90,36 @@ export async function syncHistory() {
|
||||
return data
|
||||
}
|
||||
|
||||
export async function syncHistoryByDateRange(
|
||||
start_date?: string,
|
||||
end_date?: string
|
||||
) {
|
||||
const searchParams: Record<string, string> = {}
|
||||
|
||||
if (start_date) {
|
||||
searchParams.start_date = start_date
|
||||
}
|
||||
|
||||
if (end_date) {
|
||||
searchParams.end_date = end_date
|
||||
}
|
||||
|
||||
const data = await ky
|
||||
.get('https://api.neatqueue.com/api/history/1226193436521267223', {
|
||||
searchParams,
|
||||
timeout: 60000,
|
||||
})
|
||||
.json<any>()
|
||||
console.log(data)
|
||||
const chunkedData = chunk(data.data, 100)
|
||||
for (const chunk of chunkedData) {
|
||||
await insertGameHistory(chunk).catch((e) => {
|
||||
console.error(e)
|
||||
})
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
function processGameEntry(gameId: number, game_num: number, entry: any) {
|
||||
const parsedEntry = typeof entry === 'string' ? JSON.parse(entry) : entry
|
||||
if (parsedEntry.game === '1v1-attrition') {
|
||||
|
||||
Reference in New Issue
Block a user