Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanmani1122 committed Nov 9, 2024
1 parent af4d9d7 commit fd82212
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tools-docker-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
122 changes: 68 additions & 54 deletions app/api/logs/block-height/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
};
43 changes: 26 additions & 17 deletions app/logs/download/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fd82212

Please sign in to comment.