diff --git a/.github/workflows/tools-docker-image.yaml b/.github/workflows/tools-docker-image.yaml index ee8d9ba..ff663a8 100644 --- a/.github/workflows/tools-docker-image.yaml +++ b/.github/workflows/tools-docker-image.yaml @@ -13,7 +13,7 @@ jobs: - name: Checkout uses: actions/checkout@v4 # with: - # ref: ${{ github.event.inputs.CLUSTER_NAME }} + # ref: ${{ github.event.inputs.BRANCH_NAME }} - name: Generate Timestamp run: | diff --git a/app/api/logs/block-height/route.ts b/app/api/logs/block-height/route.ts index 5761045..58441c3 100644 --- a/app/api/logs/block-height/route.ts +++ b/app/api/logs/block-height/route.ts @@ -257,84 +257,98 @@ const streamFile = ( }; export const GET = async (request: NextRequest) => { - const body = Object.fromEntries( - new URL(request.url).searchParams.entries(), - ) as unknown as LogsRequestBody; + try { + const body = Object.fromEntries( + new URL(request.url).searchParams.entries(), + ) as unknown as LogsRequestBody; - if (!(body.clusterName && body.endTime && body.namespace && body.startTime)) - return NextResponse.json('Invalid Form', { status: 400 }); + if (!(body.clusterName && body.endTime && body.namespace && body.startTime)) + return NextResponse.json({ message: 'Invalid Form' }, { status: 400 }); - const accessToken = await getAccessToken(); + const accessToken = await getAccessToken(); - const fileName = await fetchLogsBetween(accessToken, body); + const fileName = await fetchLogsBetween(accessToken, body); - const data = streamFile(fileName); + const data = streamFile(fileName); - after(() => - unlink( - fileName, - (err) => err && console.error(`Error removing file ${fileName}: `, err), - ), - ); + after(() => + unlink( + fileName, + (err) => err && console.error(`Error removing file ${fileName}: `, err), + ), + ); - return new NextResponse(data, { - headers: new Headers({ - 'Content-Disposition': `attachment; filename=${basename(fileName)}`, - 'Content-Length': String(statSync(fileName).size), - 'Content-Type': 'text/plain', - }), - status: 200, - }); + return new NextResponse(data, { + headers: new Headers({ + 'Content-Disposition': `attachment; filename=${basename(fileName)}`, + 'Content-Length': String(statSync(fileName).size), + 'Content-Type': 'text/plain', + }), + status: 200, + }); + } catch (err) { + return NextResponse.json({ message: String(err) }, { status: 500 }); + } }; export const POST = async (request: NextRequest) => { - const body: TimestampsRequestBody = await request.json(); - - if ( - !( - body.clusterName && - body.namespace && - body.startBlockHeight && - (!body.endBlockHeight || body.endBlockHeight >= body.startBlockHeight) + try { + const body: TimestampsRequestBody = await request.json(); + + if ( + !( + body.clusterName && + body.namespace && + body.startBlockHeight && + (!body.endBlockHeight || body.endBlockHeight >= body.startBlockHeight) + ) ) - ) - return NextResponse.json('Invalid Form', { status: 400 }); + return NextResponse.json({ message: 'Invalid Form' }, { status: 400 }); - const accessToken = await getAccessToken(); + const accessToken = await getAccessToken(); - const beginBlockFilter = ` + const beginBlockFilter = ` jsonPayload.blockHeight=${body.startBlockHeight} jsonPayload.type="${START_BLOCK_EVENT_TYPE}" resource.labels.cluster_name="${body.clusterName}" resource.labels.namespace_name="${body.namespace}" `; - const commitBlockFinishFilter = ` + const commitBlockFinishFilter = ` jsonPayload.blockHeight=${body.endBlockHeight} jsonPayload.type="${COMMIT_BLOCK_FINISH_EVENT_TYPE}" resource.labels.cluster_name="${body.clusterName}" resource.labels.namespace_name="${body.namespace}" `; - const foundBeginBlock = await searchForLogEntry({ - accessToken, - filter: beginBlockFilter, - }); - - if (!foundBeginBlock) - return NextResponse.json('Start time search exhausted', { status: 404 }); + const foundBeginBlock = await searchForLogEntry({ + accessToken, + filter: beginBlockFilter, + }); - const foundCommitBlockFinish = await searchForLogEntry({ - accessToken, - filter: commitBlockFinishFilter, - searchForward: true, - startTime: new Date(foundBeginBlock.timestamp), - }); + if (!foundBeginBlock) + return NextResponse.json( + { message: 'Start time search exhausted' }, + { status: 404 }, + ); - if (!foundCommitBlockFinish) - return NextResponse.json('End time search exhausted', { status: 404 }); + const foundCommitBlockFinish = await searchForLogEntry({ + accessToken, + filter: commitBlockFinishFilter, + searchForward: true, + startTime: new Date(foundBeginBlock.timestamp), + }); + + if (!foundCommitBlockFinish) + return NextResponse.json( + { message: 'End time search exhausted' }, + { status: 404 }, + ); - return NextResponse.json({ - endTime: foundCommitBlockFinish.timestamp, - startTime: foundBeginBlock.timestamp, - }); + return NextResponse.json({ + endTime: foundCommitBlockFinish.timestamp, + startTime: foundBeginBlock.timestamp, + }); + } catch (err) { + return NextResponse.json({ message: String(err) }, { status: 500 }); + } }; diff --git a/app/logs/download/page.tsx b/app/logs/download/page.tsx index 422db77..9a9167e 100644 --- a/app/logs/download/page.tsx +++ b/app/logs/download/page.tsx @@ -48,26 +48,35 @@ const Page = () => { const data = (await timestampsResponse.json()) as { endTime: string; + message?: string; startTime: string; }; - const downloadUrl = new URL( - '/api/logs/block-height', - window.location.origin, - ); - - downloadUrl.searchParams.set('clusterName', clusterName); - downloadUrl.searchParams.set('endTime', data.endTime); - downloadUrl.searchParams.set('namespace', namespace); - downloadUrl.searchParams.set('startTime', data.startTime); - - window.open(downloadUrl.toString(), '_blank', 'noopener'); - - setState((prevState) => ({ - ...prevState, - inProgress: false, - message: '', - })); + if (!timestampsResponse.ok) + setState((prevState) => ({ + ...prevState, + inProgress: false, + message: String(data.message), + })); + else { + const downloadUrl = new URL( + '/api/logs/block-height', + window.location.origin, + ); + + downloadUrl.searchParams.set('clusterName', clusterName); + downloadUrl.searchParams.set('endTime', data.endTime); + downloadUrl.searchParams.set('namespace', namespace); + downloadUrl.searchParams.set('startTime', data.startTime); + + window.open(downloadUrl.toString(), '_blank', 'noopener'); + + setState((prevState) => ({ + ...prevState, + inProgress: false, + message: '', + })); + } } catch (err) { setState((prevState) => ({ ...prevState,