mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-10-07 12:42:00 +08:00
Use uuid prefix in task table
This commit is contained in:
61
ui/src/utils.ts
Normal file
61
ui/src/utils.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
interface Duration {
|
||||
hour: number;
|
||||
minute: number;
|
||||
second: number;
|
||||
totalSeconds: number;
|
||||
}
|
||||
|
||||
// start and end are in milliseconds.
|
||||
function durationBetween(start: number, end: number): Duration {
|
||||
const durationInMillisec = start - end;
|
||||
const totalSeconds = Math.floor(durationInMillisec / 1000);
|
||||
const hour = Math.floor(totalSeconds / 3600);
|
||||
const minute = Math.floor((totalSeconds - 3600 * hour) / 60);
|
||||
const second = totalSeconds - 3600 * hour - 60 * minute;
|
||||
return { hour, minute, second, totalSeconds };
|
||||
}
|
||||
|
||||
function stringifyDuration(d: Duration): string {
|
||||
return (
|
||||
(d.hour !== 0 ? `${d.hour}h` : "") +
|
||||
(d.minute !== 0 ? `${d.minute}m` : "") +
|
||||
`${d.second}s`
|
||||
);
|
||||
}
|
||||
|
||||
export function durationBefore(timestamp: string): string {
|
||||
try {
|
||||
const duration = durationBetween(Date.parse(timestamp), Date.now());
|
||||
if (duration.totalSeconds < 1) {
|
||||
return "now";
|
||||
}
|
||||
return "in " + stringifyDuration(duration);
|
||||
} catch {
|
||||
return "-";
|
||||
}
|
||||
}
|
||||
|
||||
export function timeAgo(timestamp: string): string {
|
||||
try {
|
||||
const duration = durationBetween(Date.now(), Date.parse(timestamp));
|
||||
return stringifyDuration(duration) + " ago";
|
||||
} catch {
|
||||
return "-";
|
||||
}
|
||||
}
|
||||
|
||||
export function getCurrentUTCDate(): string {
|
||||
const today = new Date();
|
||||
const dd = today.getUTCDate().toString().padStart(2, "0");
|
||||
const mm = (today.getMonth() + 1).toString().padStart(2, "0");
|
||||
const yyyy = today.getFullYear();
|
||||
return `${yyyy}-${mm}-${dd}`;
|
||||
}
|
||||
|
||||
export function uuidPrefix(uuid: string): string {
|
||||
const idx = uuid.indexOf("-");
|
||||
if (idx === -1) {
|
||||
return uuid;
|
||||
}
|
||||
return uuid.substr(0, idx);
|
||||
}
|
Reference in New Issue
Block a user