1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { PropertyValue } from "models/search-bar";
6 import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "models/vocabulary";
8 export const formatDate = (isoDate?: string | null, utc: boolean = false) => {
10 const date = new Date(isoDate);
13 text = date.toUTCString();
16 text = date.toLocaleString();
18 return text === 'Invalid Date' ? "(none)" : text;
23 export const formatFileSize = (size?: number | string) => {
24 if (typeof size === "number") {
25 if (size === 0) { return "0 B"; }
27 for (const { base, unit } of FILE_SIZES) {
29 return `${(size / base).toFixed()} ${unit}`;
33 if ((typeof size === "string" && size === '') || size === undefined) {
39 export const formatTime = (time: number, seconds?: boolean) => {
40 const minutes = Math.floor(time / (1000 * 60) % 60).toFixed(0);
41 const hours = Math.floor(time / (1000 * 60 * 60)).toFixed(0);
44 const seconds = Math.floor(time / (1000) % 60).toFixed(0);
45 return hours + "h " + minutes + "m " + seconds + "s";
48 return hours + "h " + minutes + "m";
51 export const getTimeDiff = (endTime: string, startTime: string) => {
52 return new Date(endTime).getTime() - new Date(startTime).getTime();
55 export const formatProgress = (loaded: number, total: number) => {
56 const progress = loaded >= 0 && total > 0 ? loaded * 100 / total : 0;
57 return `${progress.toFixed(2)}%`;
60 export function formatUploadSpeed(prevLoaded: number, loaded: number, prevTime: number, currentTime: number) {
61 const speed = loaded > prevLoaded && currentTime > prevTime
62 ? (loaded - prevLoaded) / (currentTime - prevTime)
65 return `${(speed / 1000).toFixed(2)} MB/s`;
91 export const formatPropertyValue = (pv: PropertyValue, vocabulary?: Vocabulary) => {
92 if (vocabulary && pv.keyID && pv.valueID) {
93 return `${getTagKeyLabel(pv.keyID, vocabulary)}: ${getTagValueLabel(pv.keyID, pv.valueID!, vocabulary)}`;
97 ? `${pv.key}: ${pv.value}`
103 export const formatContainerCost = (cost: number): string => {
104 const decimalPlaces = 3;
106 const factor = Math.pow(10, decimalPlaces);
107 const rounded = Math.round(cost*factor)/factor;
108 if (cost > 0 && rounded === 0) {
109 // Display min value of 0.001
110 return `$${1/factor}`;
112 // Otherwise use rounded value to proper decimal places
113 return `$${rounded}`;