Skip to content

Commit

Permalink
fix: redis uptime is not correct & not localized, closes #758
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed May 30, 2024
1 parent 41c7af7 commit bf7e356
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
20 changes: 1 addition & 19 deletions packages/api/src/handlers/redisStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,6 @@ import { parse as parseRedisInfo } from 'redis-info';
import { BullBoardRequest, ControllerHandlerReturnType, RedisStats } from '../../typings/app';
import { BaseAdapter } from '../queueAdapters/base';

function formatUptime(uptime: number) {
const date = new Date(uptime * 1000);
const days = date.getUTCDate() - 1,
hours = date.getUTCHours(),
minutes = date.getUTCMinutes(),
seconds = date.getUTCSeconds();

// Initialize an array for the uptime.
const segments = [];

// Format the uptime string.
if (days > 0) segments.push(days + ' day' + (days == 1 ? '' : 's'));
if (hours > 0) segments.push(hours + ' hour' + (hours == 1 ? '' : 's'));
if (minutes > 0) segments.push(minutes + ' minute' + (minutes == 1 ? '' : 's'));
if (seconds > 0 && days === 0) segments.push(seconds + ' second' + (seconds == 1 ? '' : 's'));
return segments.join(', ');
}

async function getStats(queue: BaseAdapter): Promise<RedisStats> {
const redisInfoRaw = await queue.getRedisInfo();
const redisInfo = parseRedisInfo(redisInfoRaw);
Expand All @@ -29,7 +11,7 @@ async function getStats(queue: BaseAdapter): Promise<RedisStats> {
mode: redisInfo.redis_mode,
port: +redisInfo.tcp_port,
os: redisInfo.os,
uptime: formatUptime(+redisInfo.uptime_in_seconds),
uptime: +redisInfo.uptime_in_seconds,
memory: {
total: +redisInfo.total_system_memory || +redisInfo.maxmemory,
used: +redisInfo.used_memory,
Expand Down
4 changes: 2 additions & 2 deletions packages/api/typings/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface RedisStats {
mode: RedisInfo['redis_mode'];
port: number;
os: string;
uptime: string;
uptime: number;
memory: {
total: number;
used: number;
Expand Down Expand Up @@ -241,4 +241,4 @@ export type DateFormats = {
* @see https://date-fns.org/v3.6.0/docs/format
*/
full?: string;
}
};
10 changes: 9 additions & 1 deletion packages/ui/src/components/RedisStatsModal/RedisStatsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { RedisStats } from '@bull-board/api/typings/app';
import { formatDistance } from 'date-fns';
import formatBytes from 'pretty-bytes';
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useApi } from '../../hooks/useApi';
import { useInterval } from '../../hooks/useInterval';
import { dateFnsLocale } from '../../services/i18n';
import { Modal } from '../Modal/Modal';
import s from './RedisStatsModal.module.css';

Expand Down Expand Up @@ -62,7 +64,13 @@ export const RedisStatsModal = ({ open, onClose }: RedisStatsModalProps) => {
{ title: t('REDIS.VERSION'), value: stats.version },
{ title: t('REDIS.MODE'), value: stats.mode },
{ title: t('REDIS.OS'), value: stats.os },
{ title: t('REDIS.UP_TIME'), value: stats.uptime },
{
title: t('REDIS.UP_TIME'),
value: formatDistance(0, stats.uptime * 1000, {
includeSeconds: true,
locale: dateFnsLocale,
}),
},
];

return (
Expand Down

0 comments on commit bf7e356

Please sign in to comment.