github.com/wfusion/gofusion@v1.1.14/common/infra/asynq/asynqmon/ui/src/utils.ts (about) 1 import { AxiosError } from "axios"; 2 3 // toErrorStringWithHttpStatus returns a string representaion of axios error with HTTP status. 4 export function toErrorStringWithHttpStatus(error: AxiosError<string>): string { 5 const { response } = error; 6 if (!response) { 7 return "error: no error response data available"; 8 } 9 return `${response.status} (${response.statusText}): ${response.data}`; 10 } 11 12 // toErrorString returns a string representaion of axios error. 13 export function toErrorString(error: AxiosError<string>): string { 14 const { response } = error; 15 if (!response) { 16 return "Unknown error occurred. See the logs for details."; 17 } 18 return response.data; 19 } 20 21 interface Duration { 22 hour: number; 23 minute: number; 24 second: number; 25 totalSeconds: number; 26 } 27 28 // Returns a duration from the number of seconds provided. 29 export function durationFromSeconds(totalSeconds: number): Duration { 30 const hour = Math.floor(totalSeconds / 3600); 31 const minute = Math.floor((totalSeconds - 3600 * hour) / 60); 32 const second = totalSeconds - 3600 * hour - 60 * minute; 33 return { hour, minute, second, totalSeconds }; 34 } 35 36 // start and end are in milliseconds. 37 function durationBetween(start: number, end: number): Duration { 38 const durationInMillisec = start - end; 39 const totalSeconds = Math.floor(durationInMillisec / 1000); 40 return durationFromSeconds(totalSeconds); 41 } 42 43 export function stringifyDuration(d: Duration): string { 44 if (d.hour > 24) { 45 const n = Math.floor(d.hour / 24); 46 return n + (n === 1 ? " day" : " days"); 47 } 48 return ( 49 (d.hour !== 0 ? `${d.hour}h` : "") + 50 (d.minute !== 0 ? `${d.minute}m` : "") + 51 `${d.second}s` 52 ); 53 } 54 55 export function durationBefore(timestamp: string): string { 56 try { 57 const duration = durationBetween(Date.parse(timestamp), Date.now()); 58 if (duration.totalSeconds < 1) { 59 return "now"; 60 } 61 return "in " + stringifyDuration(duration); 62 } catch { 63 return "-"; 64 } 65 } 66 67 const zeroTimestamp = "0001-01-01T00:00:00Z"; 68 export function timeAgo(timestamp: string): string { 69 if (timestamp === zeroTimestamp) { 70 return "-"; 71 } 72 try { 73 return timeAgoUnix(Date.parse(timestamp) / 1000); 74 } catch (error) { 75 console.error("Could not parse timestamp: ", timestamp, error); 76 return "-"; 77 } 78 } 79 80 export function timeAgoUnix(unixtime: number): string { 81 if (unixtime === 0) { 82 return ""; 83 } 84 const duration = durationBetween(Date.now(), unixtime * 1000); 85 return stringifyDuration(duration) + " ago"; 86 } 87 88 export function getCurrentUTCDate(): string { 89 const today = new Date(); 90 const dd = today.getUTCDate().toString().padStart(2, "0"); 91 const mm = (today.getMonth() + 1).toString().padStart(2, "0"); 92 const yyyy = today.getFullYear(); 93 return `${yyyy}-${mm}-${dd}`; 94 } 95 96 export function uuidPrefix(uuid: string): string { 97 const idx = uuid.indexOf("-"); 98 if (idx === -1) { 99 return uuid; 100 } 101 return uuid.substr(0, idx); 102 } 103 104 export function percentage(numerator: number, denominator: number): string { 105 if (denominator === 0) return "0.00%"; 106 const perc = ((numerator / denominator) * 100).toFixed(2); 107 return `${perc} %`; 108 } 109 110 export function isJsonPayload(p: string) { 111 try { 112 JSON.parse(p); 113 } catch (error) { 114 return false; 115 } 116 return true; 117 } 118 119 export function prettifyPayload(p: string) { 120 if (isJsonPayload(p)) { 121 return JSON.stringify(JSON.parse(p), null, 2); 122 } 123 return p; 124 } 125 126 // Returns the number of seconds elapsed since January 1, 1970 00:00:00 UTC. 127 export function currentUnixtime(): number { 128 return Math.floor(Date.now() / 1000); 129 } 130 131 const durationRegex = /([0-9]*(\.[0-9]*)?)[s|m|h]/; 132 // Parses the given string and returns the number of seconds if the input is valid. 133 // Otherwise, it throws an error 134 // Supported time units are "s", "m", "h" 135 export function parseDuration(s: string): number { 136 if (!durationRegex.test(s)) { 137 throw new Error("invalid duration"); 138 } 139 const val = parseFloat(s.slice(0, -1)); 140 switch (s.slice(-1)) { 141 case "s": 142 return val; 143 case "m": 144 return val * 60; 145 case "h": 146 return val * 60 * 60; 147 default: 148 throw new Error("invalid duration unit"); 149 } 150 }