quintodrome/ui/src/utils/formatters.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.6 KiB
JavaScript
Raw Normal View History

export const formatBytes = (bytes, decimals = 2) => {
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]
}
export const formatDuration = (d) => {
2022-10-26 05:09:04 -08:00
d = Math.round(d)
const days = Math.floor(d / 86400)
const hours = Math.floor(d / 3600) % 24
const minutes = Math.floor(d / 60) % 60
2022-10-26 05:09:04 -08:00
const seconds = Math.floor(d % 60)
const f = [hours, minutes, seconds]
.map((v) => v.toString())
.map((v) => (v.length !== 2 ? '0' + v : v))
.filter((v, i) => v !== '00' || i > 0 || days > 0)
.join(':')
return `${days > 0 ? days + ':' : ''}${f}`
}
2023-05-24 10:47:51 -08:00
feat(ui): add scan progress and error reporting to UI (#4094) * feat(scanner): add LastScanError tracking to scanner status - Introduced LastScanErrorKey constant for error tracking. - Updated StatusInfo struct to include LastError field. - Modified scanner logic to store and retrieve last scan error. - Enhanced ScanStatus response to include error information. - Updated UI components to display last scan error when applicable. - Added tests to verify last scan error functionality. Signed-off-by: Deluan <deluan@navidrome.org> * feat(scanner): enhance scan status with type and elapsed time tracking - Added LastScanTypeKey and LastScanStartTimeKey constants for tracking scan type and start time. - Updated StatusInfo struct to include ScanType and ElapsedTime fields. - Implemented getScanInfo method to retrieve scan type, elapsed time, and last error. - Modified scanner logic to store scan type and start time during scans. - Enhanced ScanStatus response and UI components to display scan type and elapsed time. - Added formatShortDuration utility for better elapsed time representation. - Updated activity reducer to handle new scan status fields. Signed-off-by: Deluan <deluan@navidrome.org> * refactor(tests): consolidate controller status tests into a single file - Removed the old controller_status_test.go file. - Merged relevant tests into the new controller_test.go file for better organization and maintainability. - Ensured all existing test cases for controller status are preserved and functional. Signed-off-by: Deluan <deluan@navidrome.org> * Fix formatting issues * refactor(scanner): update getScanInfo method documentation --------- Signed-off-by: Deluan <deluan@navidrome.org>
2025-05-21 05:30:23 -08:00
export const formatShortDuration = (ns) => {
// Convert nanoseconds to seconds
const seconds = ns / 1e9
if (seconds < 1.0) {
return '<1s'
}
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const secs = Math.floor(seconds % 60)
if (hours > 0) {
return `${hours}h${minutes}m`
}
if (minutes > 0) {
return `${minutes}m${secs}s`
}
return `${secs}s`
}
export const formatFullDate = (date, locale) => {
2023-05-24 10:47:51 -08:00
const dashes = date.split('-').length - 1
let options = {
year: 'numeric',
timeZone: 'UTC',
...(dashes > 0 && { month: 'short' }),
...(dashes > 1 && { day: 'numeric' }),
}
if (dashes > 2 || (dashes === 0 && date.length > 4)) {
return ''
}
return new Date(date).toLocaleDateString(locale, options)
2023-05-24 10:47:51 -08:00
}