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

Logs in details panel #210

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/@types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface QueueAdapter {
start?: number,
end?: number,
): Promise<(Job | JobMq)[]>

getLogs(id: string): Promise<string[]>
getJobCounts(...jobStatuses: JobStatus[]): Promise<JobCounts>

clean(queueStatus: JobCleanStatus, graceTimeMs: number): Promise<any>
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import express from 'express'
import { ParamsDictionary, RequestHandler } from 'express-serve-static-core'
import path from 'path'
import { BullBoardQueues, QueueAdapter } from './@types/app'
import { ParamsDictionary, RequestHandler } from 'express-serve-static-core'

import { cleanAll } from './routes/cleanAll'
import { cleanJob } from './routes/cleanJob'
import { entryPoint } from './routes/index'
import express from 'express'
import { getLogs } from './routes/getLogs'
import path from 'path'
import { promoteJob } from './routes/promoteJob'

import { queuesHandler } from './routes/queues'
import { retryAll } from './routes/retryAll'
import { retryJob } from './routes/retryJob'
Expand All @@ -31,6 +32,7 @@ router.use('/static', express.static(path.resolve(__dirname, '../static')))

router.get(['/', '/queue/:queueName'], entryPoint)
router.get('/api/queues', wrapAsync(queuesHandler))
router.get('/api/queues/:queueName/:id/logs', wrapAsync(getLogs))
router.put('/api/queues/:queueName/retry', wrapAsync(retryAll))
router.put('/api/queues/:queueName/:id/retry', wrapAsync(retryJob))
router.put('/api/queues/:queueName/:id/clean', wrapAsync(cleanJob))
Expand Down
4 changes: 4 additions & 0 deletions src/queueAdapters/bull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class BullAdapter implements QueueAdapter {
return this.queue.getJob(id)
}

public getLogs(id: string): Promise<string[]> {
return this.queue.getJobLogs(id).then(({logs}) => logs);
Copy link
Owner

Choose a reason for hiding this comment

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

You have a lint issue here. pls fix

}

public getJobs(
jobStatuses: JobStatus[],
start?: number,
Expand Down
4 changes: 4 additions & 0 deletions src/queueAdapters/bullMQ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class BullMQAdapter implements QueueAdapter {
return this.queue.getJob(id)
}

public getLogs(id: string): Promise<string[]> {
return this.queue.getJobLogs(id).then(({logs}) => logs);
Copy link
Owner

Choose a reason for hiding this comment

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

You have a lint issue here. pls fix

}

public getJobs(
jobStatuses: JobStatus[],
start?: number,
Expand Down
27 changes: 27 additions & 0 deletions src/routes/getLogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Request, RequestHandler, Response } from 'express-serve-static-core'

import { BullBoardQueues } from '../@types/app'

export const getLogs: RequestHandler = async (req: Request, res: Response) => {
try {
const { queueName, id } = req.params
const { bullBoardQueues } = req.app.locals as {
bullBoardQueues: BullBoardQueues
}
const { queue } = bullBoardQueues[queueName]
if (!queue) {
return res.status(404).send({ error: 'queue not found' })
}

const logs = await queue.getLogs(id)

return res.send(logs)
} catch (e) {
const body = {
error: 'queue error',
details: e.stack,
}

return res.status(500).send(body)
}
}
11 changes: 6 additions & 5 deletions src/ui/components/JobCard/Details/Details.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import React from 'react'
import { AppJob } from '../../../../@types/app'
import { useDetailsTabs } from '../../../hooks/useDetailsTabs'
import { Status } from '../../constants'
import { Button } from '../Button/Button'
import React from 'react'
import { Status } from '../../constants'
import s from './Details.module.css'
import { useDetailsTabs } from '../../../hooks/useDetailsTabs'

interface DetailsProps {
job: AppJob
status: Status
queueName: string
}

export const Details = ({ status, job }: DetailsProps) => {
const { tabs, getTabContent } = useDetailsTabs(status)
export const Details = ({ status, job, queueName }: DetailsProps) => {
const { tabs, getTabContent } = useDetailsTabs(status, queueName)

if (tabs.length === 0) {
return null
Expand Down
11 changes: 6 additions & 5 deletions src/ui/components/JobCard/JobCard.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import React from 'react'
import { AppJob } from '../../../@types/app'
import { Status } from '../constants'
import { Details } from './Details/Details'
import { JobActions } from './JobActions/JobActions'
import s from './JobCard.module.css'
import { Progress } from './Progress/Progress'
import React from 'react'
import { Status } from '../constants'
import { Timeline } from './Timeline/Timeline'
import s from './JobCard.module.css'

interface JobCardProps {
job: AppJob
status: Status
queueName: string
actions: {
promoteJob: () => Promise<void>
retryJob: () => Promise<void>
cleanJob: () => Promise<void>
}
}

export const JobCard = ({ job, status, actions }: JobCardProps) => (
export const JobCard = ({ job, queueName, status, actions }: JobCardProps) => (
<div className={s.card}>
<div className={s.sideInfo}>
<span title={`#${job.id}`}>#{job.id}</span>
Expand All @@ -32,7 +33,7 @@ export const JobCard = ({ job, status, actions }: JobCardProps) => (
<JobActions status={status} actions={actions} />
</div>
<div className={s.content}>
<Details status={status} job={job} />
<Details status={status} job={job} queueName={queueName} />
{typeof job.progress === 'number' && (
<Progress
percentage={job.progress}
Expand Down
31 changes: 31 additions & 0 deletions src/ui/components/Logs/Logs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { useEffect, useState } from 'react'

const basePath = document.head.querySelector('base')?.getAttribute('href') || ''

export const Logs = ({
queueName,
jobId,
}: {
queueName: string
jobId: string | number | undefined
}) => {
const [state, setState] = useState([])

useEffect(() => {
Copy link
Owner

Choose a reason for hiding this comment

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

Components should not know how to make AJAX requests, it breaks the entire encapsulation.
All "actions" are under actions object (which get passed already to JobCard).

This way you don't need to pass the queue name every where.

Pls refactor this 🙏🏼

fetch(
`${basePath}/api/queues/${encodeURIComponent(queueName)}/${jobId}/logs`
)
.then((res) => res.json())
.then((logs) => setState(logs))
}, [jobId])

return (
<ul>
{state.map((line, i) => (
<li key={i}>
<pre>{line}</pre>
</li>
))}
</ul>
)
}
5 changes: 3 additions & 2 deletions src/ui/components/QueuePage/QueuePage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react'
import { AppQueue } from '../../../@types/app'
import { Store } from '../../hooks/useStore'
import { JobCard } from '../JobCard/JobCard'
import { QueueActions } from '../QueueActions/QueueActions'
import React from 'react'
import { StatusMenu } from '../StatusMenu/StatusMenu'
import { Store } from '../../hooks/useStore'
import s from './QueuePage.module.css'

export const QueuePage = ({
Expand Down Expand Up @@ -36,6 +36,7 @@ export const QueuePage = ({
{queue.jobs.map((job) => (
<JobCard
key={job.id}
queueName={queue.name}
job={job}
status={selectedStatus[queue.name]}
actions={{
Expand Down
12 changes: 9 additions & 3 deletions src/ui/hooks/useDetailsTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useEffect, useState } from 'react'
import { STATUSES, Status } from '../components/constants'

import { AppJob } from '../../@types/app'
import { Status, STATUSES } from '../components/constants'
import { Highlight } from '../components/Highlight/Highlight'
import { Logs } from '../components/Logs/Logs'

const regularItems = ['Data', 'Options']
const regularItems = ['Data', 'Options', 'Logs']

export function useDetailsTabs(currentStatus: Status) {
export function useDetailsTabs(currentStatus: Status, queueName: string) {
const [tabs, updateTabs] = useState<string[]>([])
const [selectedTabIdx, setSelectedTabIdx] = useState(0)
const selectedTab = tabs[selectedTabIdx]
Expand All @@ -24,6 +26,7 @@ export function useDetailsTabs(currentStatus: Status) {
})),
selectedTab,
getTabContent: ({
id,
data,
returnValue,
opts,
Expand All @@ -43,6 +46,9 @@ export function useDetailsTabs(currentStatus: Status) {
{JSON.stringify(opts, null, 2)}
</Highlight>
)
case 'Logs': {
return <Logs jobId={id} queueName={queueName} />
}
case 'Error':
return (
<>
Expand Down