forked from apache/kvrocks-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
70 lines (61 loc) · 2.43 KB
/
Copy pathutils.ts
File metadata and controls
70 lines (61 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* Truncates text to a specific length and adds an ellipsis
*/
export const truncateText = (text: string, maxLength: number): string => {
if (!text || text.length <= maxLength) return text;
return `${text.substring(0, maxLength)}...`;
};
/**
* Format a timestamp to a human-readable date
*/
export const formatTimestamp = (timestamp: number): string => {
return new Date(timestamp * 1000).toLocaleString();
};
/**
* Format bytes into a human-readable format
*/
export const formatBytes = (bytes: number, decimals: number = 2): string => {
if (bytes === 0) return "0 Bytes";
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
};
/**
* Calculate uptime from creation timestamp
*/
export const calculateUptime = (timestamp: number): string => {
const now = Math.floor(Date.now() / 1000);
const uptimeSeconds = now - timestamp;
if (uptimeSeconds < 60) return `${uptimeSeconds} seconds`;
if (uptimeSeconds < 3600) return `${Math.floor(uptimeSeconds / 60)} minutes`;
if (uptimeSeconds < 86400) return `${Math.floor(uptimeSeconds / 3600)} hours`;
return `${Math.floor(uptimeSeconds / 86400)} days`;
};
/**
* Format slot ranges for better display
*/
export const formatSlotRanges = (ranges: string[]): string => {
if (!ranges || ranges.length === 0) return "None";
if (ranges.length <= 2) return ranges.join(", ");
return `${ranges[0]}, ${ranges[1]}, ... (+${ranges.length - 2} more)`;
};