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

build(log-viewer-webui): Migrate server codebase to TypeScript and update dependencies. #647

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Replace arrays with sets for faster lookup.
  • Loading branch information
junhaoliao committed Dec 31, 2024
commit 58a434c27e4ed74e568d4d5e09b6cd24a1d28cfe
2 changes: 1 addition & 1 deletion components/log-viewer-webui/server/src/DbManager.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no significant change from DbManager.js except that the exported Fastify plugin callback function is extracted as a const for easier type specification.

Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class DbManager {
}
const status = job[QUERY_JOBS_TABLE_COLUMN_NAMES.STATUS];

if (false === QUERY_JOB_STATUS_WAITING_STATES.includes(status)) {
if (false === QUERY_JOB_STATUS_WAITING_STATES.has(status)) {
if (QUERY_JOB_STATUS.CANCELLED === status) {
throw new Error(`Job ${jobId} was cancelled.`);
} else if (QUERY_JOB_STATUS.SUCCEEDED !== status) {
Expand Down
11 changes: 6 additions & 5 deletions components/log-viewer-webui/server/src/main.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to the original main.js, changes in this file include:

  1. envToLogger was pulled out from the main() scope into global scope.
  2. Reference to process.env.NODE_ENV was removed in favour of parseEnv().NODE_ENV, which requires parseEnv() to also parse NODE_ENV from the environment variables.
  3. Added isKnownNodeEnv() for easier type deduction.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import app from "./app.js";


type NodeEnv = "development" | "production" | "test";
const KNOWN_NODE_ENVS = new Set<NodeEnv>([
"development",
"production",
"test",
]);

const NODE_ENV_DEFAULT: NodeEnv = "development";

Expand All @@ -33,11 +38,7 @@ const ENV_TO_LOGGER
* @return True if the `value` is a known Node.js environment; otherwise, false.
*/
const isKnownNodeEnv = (value: string): value is NodeEnv => {
return [
"development",
"production",
"test",
].includes(value);
return KNOWN_NODE_ENVS.has(value as NodeEnv);
};

interface EnvVars {
Expand Down
2 changes: 1 addition & 1 deletion components/log-viewer-webui/server/src/routes/query.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Added Typebox as a Fastify type provider.
  • Also specified the schemas for the routes.
  • Also removed POST body value checks in favour of TypeBox's schema check.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const routes: FastifyPluginAsync = async (app) => {
},
}, async (req, resp) => {
const {extractJobType, logEventIdx, streamId} = req.body;
if (false === EXTRACT_JOB_TYPES.includes(extractJobType)) {
if (false === EXTRACT_JOB_TYPES.has(extractJobType)) {
resp.code(StatusCodes.BAD_REQUEST);
throw new Error(`Invalid extractJobType="${extractJobType}".`);
}
Expand Down
4 changes: 2 additions & 2 deletions components/log-viewer-webui/server/src/typings/DbManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum QUERY_JOB_TYPE {
/**
* List of valid extract job types.
*/
const EXTRACT_JOB_TYPES = Object.freeze([
const EXTRACT_JOB_TYPES = new Set([
QUERY_JOB_TYPE.EXTRACT_IR,
QUERY_JOB_TYPE.EXTRACT_JSON,
]);
Expand All @@ -36,7 +36,7 @@ enum QUERY_JOB_STATUS {
/**
* List of states that indicate the job is either pending or in progress.
*/
const QUERY_JOB_STATUS_WAITING_STATES = Object.freeze([
const QUERY_JOB_STATUS_WAITING_STATES = new Set([
QUERY_JOB_STATUS.PENDING,
QUERY_JOB_STATUS.RUNNING,
QUERY_JOB_STATUS.CANCELLING,
Expand Down
Loading