Skip to content

Commit

Permalink
Change Queues to use top level types
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurickh committed Jan 25, 2020
1 parent 69c93cd commit 6c54cbf
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions src/routes/queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,35 @@ import { RequestHandler, Request } from 'express'
import { Job } from 'bull'
import { Job as JobMq } from 'bullmq'

import { BullBoardQueues, BullBoardQueue } from '../@types'

interface ValidMetrics {
total_system_memory?: string
redis_version?: string
used_memory?: string
mem_fragmentation_ratio?: string
connected_clients?: string
blocked_clients?: string
}
import * as api from '../@types/api'
import * as app from '../@types/app'
// REVIEW: this is enough of an argument to move these constants to the top level, WDYT?
import { Status } from '../ui/components/constants'

type MetricName = keyof app.ValidMetrics

const metrics = [
const metrics: MetricName[] = [
'redis_version',
'used_memory',
'mem_fragmentation_ratio',
'connected_clients',
'blocked_clients',
]

const getStats = async ({ queue }: BullBoardQueue): Promise<ValidMetrics> => {
const getStats = async ({
queue,
}: app.BullBoardQueue): Promise<app.ValidMetrics> => {
const redisClient = await queue.client
const redisInfoRaw = await redisClient.info()
const redisInfo: { [key: string]: any } = parseRedisInfo(redisInfoRaw)

const validMetrics: ValidMetrics = metrics.reduce(
(acc: { [key: string]: any }, metric) => {
if (redisInfo[metric]) {
acc[metric] = redisInfo[metric]
}
const validMetrics = metrics.reduce((acc, metric) => {
if (redisInfo[metric]) {
acc[metric] = redisInfo[metric]
}

return acc
},
{},
)
return acc
}, {} as Record<MetricName, string>)

// eslint-disable-next-line @typescript-eslint/camelcase
validMetrics.total_system_memory =
Expand All @@ -45,7 +40,7 @@ const getStats = async ({ queue }: BullBoardQueue): Promise<ValidMetrics> => {
return validMetrics
}

const formatJob = async (job: Job | JobMq) => {
const formatJob = async (job: Job | JobMq): Promise<app.AppJob> => {
const jobProps = job.toJSON()

return {
Expand All @@ -64,7 +59,7 @@ const formatJob = async (job: Job | JobMq) => {
}
}

const statuses = [
const statuses: Status[] = [
'active',
'completed',
'delayed',
Expand All @@ -74,9 +69,9 @@ const statuses = [
]

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

Expand All @@ -87,16 +82,15 @@ const getDataForQueues = async (
}
}

const queues = await Promise.all(
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]
const jobs: (Job | JobMq)[] = await queue.getJobs(status, 0, 10)

return {
name,
counts,
counts: counts as Record<Status, number>,
jobs: await Promise.all(jobs.map(formatJob)),
}
}),
Expand Down

0 comments on commit 6c54cbf

Please sign in to comment.