Skip to content

Commit dbc7ec4

Browse files
authored
Merge pull request #50 from CS3219-AY2425S1/chore/import-sort
Add import sorting, default users
2 parents 5ad131a + aa3f752 commit dbc7ec4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+329
-174
lines changed

.eslintrc.json

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
"ecmaVersion": "latest",
1212
"sourceType": "module"
1313
},
14-
"plugins": ["import", "unused-imports", "react", "@typescript-eslint", "tailwindcss"],
14+
"plugins": ["import", "unused-imports", "react", "@typescript-eslint", "tailwindcss", "simple-import-sort"],
1515
"rules": {
16-
// "@next/next/no-img-element": "off",
1716
"@typescript-eslint/no-explicit-any": "warn",
1817
"@typescript-eslint/explicit-module-boundary-types": "off",
1918
"@typescript-eslint/explicit-function-return-type": 0,
@@ -29,11 +28,21 @@
2928
"@typescript-eslint/no-empty-interface": "off",
3029
"@typescript-eslint/no-unused-vars": 0,
3130
"@typescript-eslint/no-use-before-define": 0,
31+
3232
// Basic
3333
"array-callback-return": "warn",
3434
"no-console": "warn",
35+
"no-multiple-empty-lines": ["error", { "max":1 }],
3536
"no-prototype-builtins": 0,
36-
// "no-expected-multiline": "warn", // can't find rule definition
37+
// "no-expected-multiline": "warn", // can"t find rule definition
38+
39+
"padding-line-between-statements": [
40+
"warn",
41+
{ "blankLine": "always", "prev": "*", "next": "block" },
42+
{ "blankLine": "always", "prev": "block", "next": "*" },
43+
{ "blankLine": "always", "prev": "*", "next": "block-like" },
44+
{ "blankLine": "always", "prev": "block-like", "next": "*" }
45+
],
3746

3847
// React
3948
"react/display-name": 0,
@@ -47,6 +56,8 @@
4756
}
4857
],
4958
"react-hooks/rules-of-hooks": "off",
59+
"simple-import-sort/imports": "error",
60+
"simple-import-sort/exports": "error",
5061
"tailwindcss/enforces-negative-arbitrary-values": "off",
5162
"unused-imports/no-unused-imports": "error",
5263
"unused-imports/no-unused-vars": [
@@ -66,6 +77,22 @@
6677
"rules": {
6778
"react/prop-types": "off"
6879
}
80+
},
81+
{
82+
"files": ["*.js", "*.ts", "*.tsx"],
83+
"rules": {
84+
"simple-import-sort/imports": [
85+
"error",
86+
{
87+
"groups": [
88+
["^(?:os|path|http|fs|crypto|util|events|stream|url|zlib|querystring|tls|dgram|net|dns|child_process|cluster|readline|vm|assert|buffer|process|timers)(\/.*)?$"],
89+
["^(?!(@\/|\\.\\.\/|\\.\/))"],
90+
["^@\/"],
91+
["^(?:\\.\/|\\.\\.\/|\\.)"]
92+
]
93+
}
94+
]
95+
}
6996
}
7097
]
7198
}

backend/matching/src/controllers/match-request.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import type { Request, Response } from 'express';
22
import { StatusCodes } from 'http-status-codes';
33

44
import { client, logQueueStatus } from '@/lib/db';
5-
import type { IRedisClient, IRequestMatchPayload } from '@/types';
6-
import { createNotifSocket, queueingService } from '@/services';
75
import { logger } from '@/lib/utils';
6+
import { createNotifSocket, queueingService } from '@/services';
7+
import type { IRedisClient, IRequestMatchPayload } from '@/types';
88

99
let redisClient: IRedisClient;
10+
1011
export const matchRequestController = async (req: Request, res: Response) => {
1112
const payload: Partial<IRequestMatchPayload> = req.body;
1213
const { userId, difficulty, topic } = payload;
14+
1315
if (!userId || (!difficulty && !topic)) {
1416
return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed Request');
1517
}

backend/matching/src/lib/db/log-queue-status.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const logQueueStatus = async (
1818
if (!IS_MILESTONE_D4) {
1919
return;
2020
}
21+
2122
const queueStatusLog = await getQueueStatusLog(redisClient);
2223
logger.info(message.replace('<PLACEHOLDER>', queueStatusLog));
2324
};

backend/matching/src/lib/db/seed.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SchemaFieldTypes } from 'redis';
2+
23
import { client } from './client';
34
import {
45
MATCH_PREFIX,
@@ -18,18 +19,23 @@ const logger = {
1819

1920
const main = async () => {
2021
const redisClient = await client.connect();
22+
2123
if (!redisClient) {
2224
return;
2325
}
26+
2427
logger.info('Connected');
2528
const isSeeded = await redisClient.hGetAll(SEED_KEY);
29+
2630
if (Object.keys(isSeeded).length > 0) {
2731
const { timeStamp, value } = isSeeded;
32+
2833
if (value === 'true') {
2934
logger.info('Seeded at: ' + new Date(timeStamp).toLocaleString());
3035
return;
3136
}
3237
}
38+
3339
// Set Search Index
3440
await redisClient.ft.create(
3541
POOL_INDEX,

backend/matching/src/routes/match.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { matchRequestController } from '@/controllers/match-request';
21
import { Router } from 'express';
32

3+
import { matchRequestController } from '@/controllers/match-request';
4+
45
const route = Router();
56

67
route.post('/request', matchRequestController);

backend/matching/src/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import http from 'http';
2+
13
import cors from 'cors';
24
import express, { json } from 'express';
3-
import http from 'http';
45
import { StatusCodes } from 'http-status-codes';
56
import pino from 'pino-http';
67

78
import matchRouter from '@/routes/match';
9+
810
import { UI_HOST } from './config';
911
import { createWs } from './ws';
1012

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './create-notif-socket';
2-
export * from './queue';
32
export * from './get-match-items';
3+
export * from './queue';

backend/matching/src/workers/cleaner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { client, logQueueStatus } from '@/lib/db';
22
import { STREAM_CLEANER, STREAM_GROUP, STREAM_NAME } from '@/lib/db/constants';
33
import { decodePoolTicket, getPoolKey } from '@/lib/utils';
44
import { MATCH_SVC_EVENT } from '@/ws';
5+
56
import { connectClient, sendNotif } from './common';
67

78
const logger = {
@@ -17,6 +18,7 @@ const cancel = () => {
1718
stopSignal = true;
1819
clearTimeout(timeout);
1920
};
21+
2022
const shutdown = () => {
2123
cancel();
2224
client.disconnect().then(() => {
@@ -44,11 +46,13 @@ async function clean() {
4446
});
4547
return;
4648
}
49+
4750
// ACK, Delete
4851
for (const message of response.messages) {
4952
if (!message) {
5053
continue;
5154
}
55+
5256
logger.info(`Expiring ${JSON.stringify(message)}`);
5357
const { userId, socketPort: socketRoom } = decodePoolTicket(message);
5458
const POOL_KEY = getPoolKey(userId);
@@ -70,6 +74,7 @@ async function clean() {
7074
}
7175

7276
logger.info('Process Healthy');
77+
7378
(function loop() {
7479
if (stopSignal) {
7580
return;

backend/matching/src/workers/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const sendNotif = (roomIds: Array<string>, event: IMatchEvent, message?:
1717

1818
export const connectClient = async (importedClient: typeof client) => {
1919
let redisClient: typeof client;
20+
2021
try {
2122
redisClient =
2223
importedClient.isOpen || importedClient.isReady
@@ -29,5 +30,6 @@ export const connectClient = async (importedClient: typeof client) => {
2930
);
3031
process.exit(1);
3132
}
33+
3234
return redisClient;
3335
};

backend/matching/src/workers/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import path from 'path';
55
import type { Server } from 'socket.io';
66

77
import { logger } from '@/lib/utils';
8-
import { MATCH_SVC_EVENT, type IChildProcessMessage } from '@/ws';
8+
import { type IChildProcessMessage, MATCH_SVC_EVENT } from '@/ws';
99

1010
let nWorkers = 0; // For tracking graceful exit of main process
11+
1112
export const initWorker = (name: string, io: Server) => {
1213
const lCaseName = name.toLowerCase();
1314
const worker = fork(path.join(__dirname, `${lCaseName}.js`));
@@ -18,21 +19,25 @@ export const initWorker = (name: string, io: Server) => {
1819
logger.info({ pid: worker.pid }, `[${upperCaseName}]: ${message}`);
1920
return;
2021
}
22+
2123
const messagePayload = message.valueOf();
2224
logger.info(
2325
{ pid: worker.pid },
2426
`[${upperCaseName}]: WS Payload: ${JSON.stringify(messagePayload)}`
2527
);
2628
const { rooms, event, message: payload } = messagePayload as IChildProcessMessage;
29+
2730
if (event === MATCH_SVC_EVENT.DISCONNECT) {
2831
io.sockets.in(rooms).disconnectSockets();
2932
return;
3033
}
34+
3135
io.sockets.in(rooms).emit(event, payload);
3236
});
3337
worker.on('exit', (code) => {
3438
logger.error({ pid: worker.pid }, `${upperCaseName} exited with code ${code}.`);
3539
nWorkers -= 1;
40+
3641
if (nWorkers === 0) {
3742
logger.info('Main Process exiting.');
3843
process.exit(0);

0 commit comments

Comments
 (0)