Skip to content
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

Health check integration #82

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions apps/playground-web/components/Playground/TerminalUI.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import { Dice1, Dice3, Dice5, Info } from 'lucide-react';
import Shell from '../Shell/Shell';
import { formatTime } from '@/shared/utils/commonUtils';
import { useState } from 'react';
import { useEffect, useState } from 'react';

import Tooltip from '../Overlays/Tooltip';
export function TerminalUI({ initialCommandsLeft = 1000 }) {
const [commandsLeft, setCommandsLeft] = useState(initialCommandsLeft);
const [cleanupTimeLeft, setCleanupTimeLeft] = useState(15 * 60);
import { fetchHealthCheck } from '@/lib/api';

export function TerminalUI() {

const [commandsLeft, setCommandsLeft] = useState<number | null>(null);
const [cleanupTimeLeft, setCleanupTimeLeft] = useState<number | null>(null);

useEffect(() => {
const initializeHealthCheck = async () => {
try {
const { commandsLeft, cleanupTimeLeft } = await fetchHealthCheck();
setCommandsLeft(commandsLeft);
setCleanupTimeLeft(cleanupTimeLeft);
} catch (error) {
console.error('Error initializing health check:', error);
setCommandsLeft(1000); // Fallback value
setCleanupTimeLeft(15 * 60); // Fallback value
}
};

initializeHealthCheck();
}, []);

const handleCommandExecuted = (commands: number, cleanup: number) => {
setCommandsLeft(commands);
if (cleanup !== -1) {
Expand Down Expand Up @@ -78,3 +99,4 @@ function TerminalCounter({
</div>
);
}

39 changes: 31 additions & 8 deletions apps/playground-web/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,41 @@ export const executeShellCommandOnServer = async (

try {
const response = await WebService.post(cmdExecURL, cmdOptions);
// Type guard to check if the body is MonoResponse
if ('data' in response.body) {
const body = response?.body as MonoResponse | undefined;
if (body?.data || body?.error) {
return { headers: response.headers, body };
}
throw new Error('Unexpected response structure');
} else {
throw new Error('Unexpected response structure');
}
} catch (error) {
// Propagate the error from the backend exactly as it is
console.error('Error executing command:', error);
throw new Error(`${error}`);
}
};


// Check if the response contains data or if it's an error response
if (response?.body?.data) {
return response;
} else if (response?.body?.error) {
return response;
// Fetch health check data from the server
export const fetchHealthCheck = async (): Promise<{ commandsLeft: number; cleanupTimeLeft: number }> => {
const healthCheckURL = `/health`;

try {
const response = await WebService.get(healthCheckURL);

if (response?.headers) {
const commandsLeft = parseInt(response?.headers['x-ratelimit-remaining'] || '1000', 10);
const cleanupTimeLeft = parseInt(response?.headers['x-next-cleanup-time'] || `${15 * 60}`, 10);

return { commandsLeft, cleanupTimeLeft };
} else {
throw new Error('Unexpected response structure');
throw new Error('Unexpected response structure: missing headers');
}
} catch (error) {
// Propagate the error from the backend exactly as it is
console.error('Error executing command:', error);
console.error('Error fetching health check data:', error);
throw new Error(`${error}`);
}
};
3 changes: 3 additions & 0 deletions apps/playground-web/lib/healthResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface HealthResponse {
message: string | null;
}
13 changes: 0 additions & 13 deletions apps/playground-web/lib/monoSchema.ts

This file was deleted.

20 changes: 20 additions & 0 deletions apps/playground-web/lib/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { z } from 'zod';

// Schema for normal API responses
const BodySchema = z.object({
data: z.string(),
});

// Define the schema for an error response
const ErrorSchema = z.object({
error: z.string(),
});

// Schema for /health endpoint responses
export const HealthSchema = z.object({
message: z.string(),
});

// Combined schema for normal responses
export const MonoSchema = z.union([BodySchema, ErrorSchema, HealthSchema]);

12 changes: 10 additions & 2 deletions apps/playground-web/services/webServices.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { MonoSchema } from '@/lib/monoSchema';
import { MonoSchema, } from '@/lib/schemas';

import { z } from 'zod';

Check warning on line 3 in apps/playground-web/services/webServices.ts

View workflow job for this annotation

GitHub Actions / Build and Test

'z' is defined but never used
import { MonoResponse } from '@/lib/monoResponse';
import { HealthResponse } from '@/lib//healthResponse';

let PLAYGROUND_MONO_URL = process.env.NEXT_PUBLIC_PLAYGROUND_MONO_URL;

Expand Down Expand Up @@ -30,7 +32,7 @@
headers: HeadersType,
): Promise<{
headers: { [key: string]: string };
body: MonoResponse;
body: MonoResponse | HealthResponse;
}> => {
const options: RequestOptions = {
method,
Expand Down Expand Up @@ -69,6 +71,11 @@
headers: headers,
body: { data: parsedBody?.data?.data, error: null },
};
} else if('message' in parsedBody.data) {
return {
headers: headers,
body: { message: parsedBody?.data?.message },
};
} else {
return {
headers: headers,
Expand Down Expand Up @@ -98,4 +105,5 @@
post: (url: string, data: any, headers: HeadersType = {}) => {
return WebService.request(url, 'POST', data, headers);
},

};
6 changes: 3 additions & 3 deletions tooling/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"react-internal.js"
],
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@vercel/style-guide": "^5.2.0",
"eslint-config-turbo": "^2.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-turbo": "^2.0.0",
"eslint-plugin-only-warn": "^1.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"typescript": "^5.3.3"
}
}
Loading