17426: Add dialog box with form to example plugin.
[arvados-workbench2.git] / src / common / formatters.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { PropertyValue } from "~/models/search-bar";
6 import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "~/models/vocabulary";
7
8 export const formatDate = (isoDate?: string | null, utc: boolean = false) => {
9     if (isoDate) {
10         const date = new Date(isoDate);
11         let text: string;
12         if (utc) {
13             text = date.toUTCString();
14         }
15         else {
16             text = date.toLocaleString();
17         }
18         return text === 'Invalid Date' ? "(none)" : text;
19     }
20     return "(none)";
21 };
22
23 export const formatFileSize = (size?: number) => {
24     if (typeof size === "number") {
25         if (size === 0) { return "0 B"; }
26
27         for (const { base, unit } of FILE_SIZES) {
28             if (size >= base) {
29                 return `${(size / base).toFixed()} ${unit}`;
30             }
31         }
32     }
33     return "0 B";
34 };
35
36 export const formatTime = (time: number, seconds?: boolean) => {
37     const minutes = Math.floor(time / (1000 * 60) % 60).toFixed(0);
38     const hours = Math.floor(time / (1000 * 60 * 60)).toFixed(0);
39
40     if (seconds) {
41         const seconds = Math.floor(time / (1000) % 60).toFixed(0);
42         return hours + "h " + minutes + "m " + seconds + "s";
43     }
44
45     return hours + "h " + minutes + "m";
46 };
47
48 export const getTimeDiff = (endTime: string, startTime: string) => {
49     return new Date(endTime).getTime() - new Date(startTime).getTime();
50 };
51
52 export const formatProgress = (loaded: number, total: number) => {
53     const progress = loaded >= 0 && total > 0 ? loaded * 100 / total : 0;
54     return `${progress.toFixed(2)}%`;
55 };
56
57 export function formatUploadSpeed(prevLoaded: number, loaded: number, prevTime: number, currentTime: number) {
58     const speed = loaded > prevLoaded && currentTime > prevTime
59         ? (loaded - prevLoaded) / (currentTime - prevTime)
60         : 0;
61     return `${(speed / 1000).toFixed(2)} KB/s`;
62 }
63
64 const FILE_SIZES = [
65     {
66         base: 1000000000000,
67         unit: "TB"
68     },
69     {
70         base: 1000000000,
71         unit: "GB"
72     },
73     {
74         base: 1000000,
75         unit: "MB"
76     },
77     {
78         base: 1000,
79         unit: "KB"
80     },
81     {
82         base: 1,
83         unit: "B"
84     }
85 ];
86
87 export const formatPropertyValue = (pv: PropertyValue, vocabulary?: Vocabulary) => {
88     if (vocabulary && pv.keyID && pv.valueID) {
89         return `${getTagKeyLabel(pv.keyID, vocabulary)}: ${getTagValueLabel(pv.keyID, pv.valueID!, vocabulary)}`;
90     }
91     if (pv.key) {
92         return pv.value
93             ? `${pv.key}: ${pv.value}`
94             : pv.key;
95     }
96     return "";
97 };