17564: Change file size unit base from 1000 to 1024
[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 | string) => {
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     if ((typeof size === "string" && size === '') || size === undefined) {
34         return '';
35     }
36     return "0 B";
37 };
38
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);
42
43     if (seconds) {
44         const seconds = Math.floor(time / (1000) % 60).toFixed(0);
45         return hours + "h " + minutes + "m " + seconds + "s";
46     }
47
48     return hours + "h " + minutes + "m";
49 };
50
51 export const getTimeDiff = (endTime: string, startTime: string) => {
52     return new Date(endTime).getTime() - new Date(startTime).getTime();
53 };
54
55 export const formatProgress = (loaded: number, total: number) => {
56     const progress = loaded >= 0 && total > 0 ? loaded * 100 / total : 0;
57     return `${progress.toFixed(2)}%`;
58 };
59
60 export function formatUploadSpeed(prevLoaded: number, loaded: number, prevTime: number, currentTime: number) {
61     const speed = loaded > prevLoaded && currentTime > prevTime
62         ? (loaded - prevLoaded) / (currentTime - prevTime)
63         : 0;
64     return `${(speed / 1000).toFixed(2)} KB/s`;
65 }
66
67 const FILE_SIZES = [
68     {
69         base: 1099511627776,
70         unit: "TB"
71     },
72     {
73         base: 1073741824,
74         unit: "GB"
75     },
76     {
77         base: 1048576,
78         unit: "MB"
79     },
80     {
81         base: 1024,
82         unit: "KB"
83     },
84     {
85         base: 1,
86         unit: "B"
87     }
88 ];
89
90 export const formatPropertyValue = (pv: PropertyValue, vocabulary?: Vocabulary) => {
91     if (vocabulary && pv.keyID && pv.valueID) {
92         return `${getTagKeyLabel(pv.keyID, vocabulary)}: ${getTagValueLabel(pv.keyID, pv.valueID!, vocabulary)}`;
93     }
94     if (pv.key) {
95         return pv.value
96             ? `${pv.key}: ${pv.value}`
97             : pv.key;
98     }
99     return "";
100 };