Skip to content

Commit

Permalink
Fix prettier config && reformat the code
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed May 27, 2021
1 parent 59ad669 commit ab134ca
Show file tree
Hide file tree
Showing 54 changed files with 594 additions and 833 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = {
'no-only-tests/no-only-tests': 'error',

// UI
'prettier/prettier': 'error',
'react/prop-types': 'off',
'react/display-name': 'off',
},
Expand All @@ -49,4 +48,4 @@ module.exports = {
},
},
],
}
};
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

5 changes: 5 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
trailingComma: 'es5',
singleQuote: true,
printWidth: 120,
};
7 changes: 1 addition & 6 deletions src/@types/app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { BaseAdapter } from '../queueAdapters/base';
import { Status } from '../ui/components/constants';

export type JobCleanStatus =
| 'completed'
| 'wait'
| 'active'
| 'delayed'
| 'failed';
export type JobCleanStatus = 'completed' | 'wait' | 'active' | 'delayed' | 'failed';

export type JobStatus = Status;

Expand Down
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export function createBullBoard(
}

function removeQueue(queueOrName: string | BaseAdapter) {
const name =
typeof queueOrName === 'string' ? queueOrName : queueOrName.getName();
const name = typeof queueOrName === 'string' ? queueOrName : queueOrName.getName();

bullBoardQueues.delete(name);
}
Expand All @@ -48,9 +47,7 @@ export function createBullBoard(
}

function replaceQueues(newBullQueues: ReadonlyArray<BaseAdapter>): void {
const queuesToPersist: string[] = newBullQueues.map((queue) =>
queue.getName()
);
const queuesToPersist: string[] = newBullQueues.map((queue) => queue.getName());

bullBoardQueues.forEach((_queue, name) => {
if (queuesToPersist.indexOf(name) === -1) {
Expand Down
28 changes: 5 additions & 23 deletions src/queueAdapters/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
JobCleanStatus,
JobCounts,
JobStatus,
QueueAdapterOptions,
QueueJob,
} from '../@types/app';
import { JobCleanStatus, JobCounts, JobStatus, QueueAdapterOptions, QueueJob } from '../@types/app';

export abstract class BaseAdapter {
public readonly readOnlyMode: boolean;
Expand All @@ -14,34 +8,22 @@ export abstract class BaseAdapter {
this.readOnlyMode = options.readOnlyMode === true;
}

public setFormatter(
field: 'data' | 'returnValue',
formatter: (data: any) => any
): void {
public setFormatter(field: 'data' | 'returnValue', formatter: (data: any) => any): void {
this.formatters[field] = formatter;
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public format(field: 'data' | 'returnValue', data: any): any {
return typeof this.formatters[field] === 'function'
? this.formatters[field](data)
: data;
return typeof this.formatters[field] === 'function' ? this.formatters[field](data) : data;
}

public abstract clean(
queueStatus: JobCleanStatus,
graceTimeMs: number
): Promise<void>;
public abstract clean(queueStatus: JobCleanStatus, graceTimeMs: number): Promise<void>;

public abstract getJob(id: string): Promise<QueueJob | undefined | null>;

public abstract getJobCounts(...jobStatuses: JobStatus[]): Promise<JobCounts>;

public abstract getJobs(
jobStatuses: JobStatus[],
start?: number,
end?: number
): Promise<QueueJob[]>;
public abstract getJobs(jobStatuses: JobStatus[], start?: number, end?: number): Promise<QueueJob[]>;

public abstract getJobLogs(id: string): Promise<string[]>;

Expand Down
13 changes: 2 additions & 11 deletions src/queueAdapters/bull.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { Job, Queue } from 'bull';
import {
JobCleanStatus,
JobCounts,
JobStatus,
QueueAdapterOptions,
} from '../@types/app';
import { JobCleanStatus, JobCounts, JobStatus, QueueAdapterOptions } from '../@types/app';
import { BaseAdapter } from './base';

export class BullAdapter extends BaseAdapter {
Expand All @@ -28,11 +23,7 @@ export class BullAdapter extends BaseAdapter {
return this.queue.getJob(id);
}

public getJobs(
jobStatuses: JobStatus[],
start?: number,
end?: number
): Promise<Job[]> {
public getJobs(jobStatuses: JobStatus[], start?: number, end?: number): Promise<Job[]> {
return this.queue.getJobs(jobStatuses as any, start, end);
}

Expand Down
22 changes: 4 additions & 18 deletions src/queueAdapters/bullMQ.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { Job, Queue } from 'bullmq';
import {
JobCleanStatus,
JobCounts,
JobStatus,
QueueAdapterOptions,
} from '../@types/app';
import { JobCleanStatus, JobCounts, JobStatus, QueueAdapterOptions } from '../@types/app';
import { BaseAdapter } from './base';

export class BullMQAdapter extends BaseAdapter {
private readonly LIMIT = 1000;

constructor(
private queue: Queue,
options: Partial<QueueAdapterOptions> = {}
) {
constructor(private queue: Queue, options: Partial<QueueAdapterOptions> = {}) {
super(options);
}

Expand All @@ -34,18 +26,12 @@ export class BullMQAdapter extends BaseAdapter {
return this.queue.getJob(id);
}

public getJobs(
jobStatuses: JobStatus[],
start?: number,
end?: number
): Promise<Job[]> {
public getJobs(jobStatuses: JobStatus[], start?: number, end?: number): Promise<Job[]> {
return this.queue.getJobs(jobStatuses, start, end);
}

public getJobCounts(...jobStatuses: JobStatus[]): Promise<JobCounts> {
return (this.queue.getJobCounts(
...jobStatuses
) as unknown) as Promise<JobCounts>;
return (this.queue.getJobCounts(...jobStatuses) as unknown) as Promise<JobCounts>;
}

public getJobLogs(id: string): Promise<string[]> {
Expand Down
24 changes: 4 additions & 20 deletions src/routes/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,10 @@ import { wrapAsync } from './middlewares/wrapAsync';
export const apiRouter = Router()
.get('/queues', wrapAsync(queuesHandler))
.put('/queues/:queueName/retry', queueProvider(), wrapAsync(retryAll))
.put(
'/queues/:queueName/:jobId/retry',
[queueProvider(), jobProvider()],
wrapAsync(retryJob)
)
.put(
'/queues/:queueName/:jobId/clean',
[queueProvider(), jobProvider()],
wrapAsync(cleanJob)
)
.put(
'/queues/:queueName/:jobId/promote',
[queueProvider(), jobProvider()],
wrapAsync(promoteJob)
)
.put(
'/queues/:queueName/clean/:queueStatus',
queueProvider(),
wrapAsync(cleanAll)
)
.put('/queues/:queueName/:jobId/retry', [queueProvider(), jobProvider()], wrapAsync(retryJob))
.put('/queues/:queueName/:jobId/clean', [queueProvider(), jobProvider()], wrapAsync(cleanJob))
.put('/queues/:queueName/:jobId/promote', [queueProvider(), jobProvider()], wrapAsync(promoteJob))
.put('/queues/:queueName/clean/:queueStatus', queueProvider(), wrapAsync(cleanAll))
.get(
'/queues/:queueName/:jobId/logs',
[queueProvider({ skipReadOnlyModeCheck: true }), jobProvider()],
Expand Down
5 changes: 1 addition & 4 deletions src/routes/handlers/cleanAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ type RequestParams = {
queueStatus: JobCleanStatus;
};

export const cleanAll: RequestHandler<RequestParams> = async (
req: Request,
res: Response
) => {
export const cleanAll: RequestHandler<RequestParams> = async (req: Request, res: Response) => {
const { queueStatus } = req.params;
const { queue } = res.locals;

Expand Down
5 changes: 1 addition & 4 deletions src/routes/handlers/cleanJob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Request, RequestHandler, Response } from 'express-serve-static-core';
import { QueueJob } from '../../@types/app';

export const cleanJob: RequestHandler = async (
_req: Request,
res: Response
) => {
export const cleanJob: RequestHandler = async (_req: Request, res: Response) => {
const { job } = res.locals as { job: QueueJob };

await job.remove();
Expand Down
5 changes: 1 addition & 4 deletions src/routes/handlers/promoteJob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Request, RequestHandler, Response } from 'express-serve-static-core';
import { QueueJob } from '../../@types/app';

export const promoteJob: RequestHandler = async (
_req: Request,
res: Response
) => {
export const promoteJob: RequestHandler = async (_req: Request, res: Response) => {
const { job } = res.locals as { job: QueueJob };

await job.promote();
Expand Down
25 changes: 5 additions & 20 deletions src/routes/handlers/queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const getStats = async (queue: BaseAdapter): Promise<app.ValidMetrics> => {
return acc;
}, {} as Record<MetricName, string>);

validMetrics.total_system_memory =
redisInfo.total_system_memory || redisInfo.maxmemory;
validMetrics.total_system_memory = redisInfo.total_system_memory || redisInfo.maxmemory;

return validMetrics;
};
Expand All @@ -55,19 +54,9 @@ const formatJob = (job: QueueJob, queue: BaseAdapter): app.AppJob => {
};
};

const statuses: JobStatus[] = [
'active',
'completed',
'delayed',
'failed',
'paused',
'waiting',
];
const statuses: JobStatus[] = ['active', 'completed', 'delayed', 'failed', 'paused', 'waiting'];

const getDataForQueues = async (
bullBoardQueues: app.BullBoardQueues,
req: Request
): Promise<api.GetQueues> => {
const getDataForQueues = async (bullBoardQueues: app.BullBoardQueues, req: Request): Promise<api.GetQueues> => {
const query = req.query || {};
const pairs = [...bullBoardQueues.entries()];

Expand All @@ -81,8 +70,7 @@ const getDataForQueues = async (
const queues: app.AppQueue[] = await Promise.all(
pairs.map(async ([name, queue]) => {
const counts = await queue.getJobCounts(...statuses);
const status =
query[name] === 'latest' ? statuses : (query[name] as JobStatus[]);
const status = query[name] === 'latest' ? statuses : (query[name] as JobStatus[]);
const jobs = await queue.getJobs(status, 0, 10);

return {
Expand All @@ -102,10 +90,7 @@ const getDataForQueues = async (
};
};

export const queuesHandler: RequestHandler = async (
req: Request,
res: Response
) => {
export const queuesHandler: RequestHandler = async (req: Request, res: Response) => {
const { bullBoardQueues } = req.app.locals as {
bullBoardQueues: BullBoardQueues;
};
Expand Down
5 changes: 1 addition & 4 deletions src/routes/handlers/retryAll.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Request, RequestHandler, Response } from 'express-serve-static-core';
import { BaseAdapter } from '../../queueAdapters/base';

export const retryAll: RequestHandler = async (
_req: Request,
res: Response
) => {
export const retryAll: RequestHandler = async (_req: Request, res: Response) => {
const { queue } = res.locals as { queue: BaseAdapter };

const jobs = await queue.getJobs(['failed']);
Expand Down
5 changes: 1 addition & 4 deletions src/routes/handlers/retryJob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Request, RequestHandler, Response } from 'express-serve-static-core';
import { QueueJob } from '../../@types/app';

export const retryJob: RequestHandler = async (
_req: Request,
res: Response
) => {
export const retryJob: RequestHandler = async (_req: Request, res: Response) => {
const { job } = res.locals as { job: QueueJob };

await job.retry();
Expand Down
3 changes: 1 addition & 2 deletions src/routes/middlewares/wrapAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ import { ParamsDictionary, RequestHandler } from 'express-serve-static-core';

export const wrapAsync = <Params extends ParamsDictionary>(
fn: RequestHandler<Params>
): RequestHandler<Params> => async (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
): RequestHandler<Params> => async (req, res, next) => Promise.resolve(fn(req, res, next)).catch(next);
33 changes: 7 additions & 26 deletions src/ui/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export const App = ({ api }: { api: Api }) => {

return (
<>
<Header>
{state.data?.stats && <RedisStats stats={state.data?.stats} />}
</Header>
<Header>{state.data?.stats && <RedisStats stats={state.data?.stats} />}</Header>
<main>
<div>
{state.loading ? (
Expand All @@ -28,39 +26,22 @@ export const App = ({ api }: { api: Api }) => {
path="/queue/:name"
render={({ match: { params } }) => {
const currentQueueName = decodeURIComponent(params.name);
const queue = state.data?.queues.find(
(q) => q.name === currentQueueName
);
const queue = state.data?.queues.find((q) => q.name === currentQueueName);

return (
<QueuePage
queue={queue}
actions={actions}
selectedStatus={selectedStatuses}
/>
);
return <QueuePage queue={queue} actions={actions} selectedStatus={selectedStatuses} />;
}}
/>

<Route path="/" exact>
{!!state.data &&
Array.isArray(state.data?.queues) &&
state.data.queues.length > 0 && (
<Redirect
to={`/queue/${encodeURIComponent(
state.data?.queues[0].name
)}`}
/>
)}
{!!state.data && Array.isArray(state.data?.queues) && state.data.queues.length > 0 && (
<Redirect to={`/queue/${encodeURIComponent(state.data?.queues[0].name)}`} />
)}
</Route>
</Switch>
)}
</div>
</main>
<Menu
queues={state.data?.queues.map((q) => q.name)}
selectedStatuses={selectedStatuses}
/>
<Menu queues={state.data?.queues.map((q) => q.name)} selectedStatuses={selectedStatuses} />
<ToastContainer />
</>
);
Expand Down
Loading

0 comments on commit ab134ca

Please sign in to comment.