asynqmon/ui/src/utils.ts

96 lines
2.7 KiB
TypeScript
Raw Normal View History

import { AxiosError } from "axios";
2021-01-27 13:25:42 +08:00
// toErrorStringWithHttpStatus returns a string representaion of axios error with HTTP status.
export function toErrorStringWithHttpStatus(error: AxiosError<string>): string {
const { response } = error;
if (!response) {
return "error: no error response data available";
}
return `${response.status} (${response.statusText}): ${response.data}`;
}
2021-01-27 13:25:42 +08:00
// toErrorString returns a string representaion of axios error.
export function toErrorString(error: AxiosError<string>): string {
const { response } = error;
if (!response) {
return "Unknown error occurred. See the logs for details.";
}
return response.data;
}
2020-11-24 22:54:00 +08:00
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 {
if (d.hour > 24) {
const n = Math.floor(d.hour / 24);
return n + (n === 1 ? " day" : " days");
}
2020-11-24 22:54:00 +08:00
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);
2020-11-24 22:54:00 +08:00
} catch {
return "-";
}
}
export function timeAgo(timestamp: string): string {
try {
2021-01-05 04:05:37 +08:00
return timeAgoUnix(Date.parse(timestamp) / 1000);
} catch (error) {
console.error("Could not parse timestamp: ", timestamp, error);
2020-11-24 22:54:00 +08:00
return "-";
}
}
2021-01-05 04:05:37 +08:00
export function timeAgoUnix(unixtime: number) {
const duration = durationBetween(Date.now(), unixtime * 1000);
return stringifyDuration(duration) + " ago";
}
2020-11-24 22:54:00 +08:00
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}`;
}
2020-12-21 22:51:07 +08:00
export function uuidPrefix(uuid: string): string {
const idx = uuid.indexOf("-");
if (idx === -1) {
return uuid;
}
return uuid.substr(0, idx);
}
2021-01-25 05:37:45 +08:00
export function percentage(numerator: number, denominator: number): string {
const perc = ((numerator / denominator) * 100).toFixed(2);
return `${perc} %`;
}