Skip to content

Commit

Permalink
update timestamp calculation on dsnviewr
Browse files Browse the repository at this point in the history
  • Loading branch information
Xm0onh committed Jan 15, 2025
1 parent 786de85 commit eb4d1a4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
1 change: 1 addition & 0 deletions agent-memory-viewer/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@emotion/styled": "^11.11.0",
"@types/cors": "^2.8.17",
"axios": "^1.6.7",
"dayjs": "^1.11.13",
"framer-motion": "^11.14.4",
"lodash": "^4.17.21",
"react": "19.0.0",
Expand Down
4 changes: 2 additions & 2 deletions agent-memory-viewer/frontend/src/components/DSNViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { colors } from '../styles/theme/colors';
import { useSearchParams } from 'react-router-dom';
import SearchBar from './SearchBar';
import StatusFilter from './StatusFilter';
import { getRelativeTime } from '../utils/timeUtils';
import { utcToLocalRelativeTime } from '../utils/timeUtils';

function DSNViewer() {
const [searchParams, setSearchParams] = useSearchParams();
Expand Down Expand Up @@ -112,7 +112,7 @@ function DSNViewer() {
_hover={{ color: 'gray.600' }}
cursor="help"
>
{getRelativeTime(item.timestamp)}
{utcToLocalRelativeTime(item.timestamp)}
</Text>
</Tooltip>
</HStack>
Expand Down
82 changes: 50 additions & 32 deletions agent-memory-viewer/frontend/src/utils/timeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
export function getRelativeTime(timestamp: string): string {
const date = new Date(timestamp);
const now = new Date();
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);

if (diffInSeconds < 60) {
return `${diffInSeconds}s ago`;
}

const diffInMinutes = Math.floor(diffInSeconds / 60);
if (diffInMinutes < 60) {
return `${diffInMinutes}m ago`;
}

const diffInHours = Math.floor(diffInMinutes / 60);
if (diffInHours < 24) {
return `${diffInHours}h ago`;
}

const diffInDays = Math.floor(diffInHours / 24);
if (diffInDays < 30) {
return `${diffInDays}d ago`;
}

const diffInMonths = Math.floor(diffInDays / 30);
if (diffInMonths < 12) {
return `${diffInMonths}mo ago`;
}

const diffInYears = Math.floor(diffInDays / 365);
return `${diffInYears}y ago`;
}
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import utc from 'dayjs/plugin/utc'

dayjs.extend(relativeTime)
dayjs.extend(utc)

export const formatSeconds = (seconds: number | bigint): string => {
if (typeof seconds === 'number' && seconds < 0) {
throw new Error('Seconds cannot be negative')
} else if (typeof seconds === 'bigint' && seconds < 0n) {
throw new Error('Seconds cannot be negative')
}

const timeUnits = [
{ unit: 'd', value: 86400n },
{ unit: 'h', value: 3600n },
{ unit: 'm', value: 60n },
{ unit: 's', value: 1n },
]

let secondsBigInt = BigInt(seconds)

return (
timeUnits
.reduce((result, { unit, value }) => {
if (secondsBigInt >= value) {
const time = secondsBigInt / value
secondsBigInt %= value
result += `${time}${unit} `
}
return result
}, '')
.trim() || '0s'
)
}

export const utcToLocalRelativeTime = (timestamp: string): string => {
const now = dayjs()
const time = dayjs.utc(timestamp).local()
const diffInSeconds = now.diff(time, 'second')

if (diffInSeconds < 60) return `${diffInSeconds} seconds ago`
return time.fromNow(true) + ' ago'
}

export const utcToLocalTime = (timestamp: string): string =>
dayjs.utc(timestamp).local().format('DD MMM YYYY | HH:mm:ss(Z)')

export const currentYear = (): number => dayjs().year()
5 changes: 5 additions & 0 deletions agent-memory-viewer/frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,11 @@ csstype@^3.0.2, csstype@^3.1.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.3.tgz#d80ff294d114fb0e6ac500fbf85b60137d7eff81"
integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==

dayjs@^1.11.13:
version "1.11.13"
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.13.tgz#92430b0139055c3ebb60150aa13e860a4b5a366c"
integrity sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==

debug@^4.1.0, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
version "4.4.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.0.tgz#2b3f2aea2ffeb776477460267377dc8710faba8a"
Expand Down

0 comments on commit eb4d1a4

Please sign in to comment.