1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { PropertyValue } from 'models/search-bar';
10 } from 'models/vocabulary';
12 export const formatDate = (isoDate?: string | null, utc: boolean = false) => {
14 const date = new Date(isoDate);
17 text = date.toUTCString();
19 text = date.toLocaleString();
21 return text === 'Invalid Date' ? '(none)' : text;
26 export const formatFileSize = (size?: number | string) => {
27 if (typeof size === 'number') {
32 for (const { base, unit } of FILE_SIZES) {
34 return `${(size / base).toFixed(base === 1 ? 0 : 1)} ${unit}`;
38 if ((typeof size === 'string' && size === '') || size === undefined) {
44 export const formatCWLResourceSize = (size: number) => {
45 return `${(size / CWL_SIZE.base).toFixed(0)} ${CWL_SIZE.unit}`;
48 export const formatTime = (time: number, seconds?: boolean) => {
49 const minutes = Math.floor((time / (1000 * 60)) % 60).toFixed(0);
50 const hours = Math.floor(time / (1000 * 60 * 60)).toFixed(0);
53 const seconds = Math.floor((time / 1000) % 60).toFixed(0);
54 return hours + 'h ' + minutes + 'm ' + seconds + 's';
57 return hours + 'h ' + minutes + 'm';
60 export const getTimeDiff = (endTime: string, startTime: string) => {
61 return new Date(endTime).getTime() - new Date(startTime).getTime();
64 export const formatProgress = (loaded: number, total: number) => {
65 const progress = loaded >= 0 && total > 0 ? (loaded * 100) / total : 0;
66 return `${progress.toFixed(2)}%`;
69 export function formatUploadSpeed(
76 loaded > prevLoaded && currentTime > prevTime
77 ? (loaded - prevLoaded) / (currentTime - prevTime)
80 return `${(speed / 1000).toFixed(2)} MB/s`;
111 export const formatPropertyValue = (
113 vocabulary?: Vocabulary
115 if (vocabulary && pv.keyID && pv.valueID) {
116 return `${getTagKeyLabel(pv.keyID, vocabulary)}: ${getTagValueLabel(
123 return pv.value ? `${pv.key}: ${pv.value}` : pv.key;
128 export const formatCost = (cost: number): string => {
129 const decimalPlaces = 3;
131 const factor = Math.pow(10, decimalPlaces);
132 const rounded = Math.round(cost * factor) / factor;
133 if (cost > 0 && rounded === 0) {
134 // Display min value of 0.001
135 return `$${1 / factor}`;
137 // Otherwise use rounded value to proper decimal places
138 return `$${rounded}`;