mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 20:52:33 +02:00

* feat: idle sleep for proxmox LXCs * refactor: replace deprecated docker api types * chore(api): remove debug task list endpoint * refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon * refactor: introduce Pool interface, move agent_pool to agent module * refactor: simplify api code * feat: introduce debug api * refactor: remove net.URL and net.CIDR types, improved unmarshal handling * chore: update Makefile for debug build tag, update README * chore: add gperr.Unwrap method * feat: relative time and duration formatting * chore: add ROOT_DIR environment variable, refactor * migration: move homepage override and icon cache to $BASE_DIR/data, add migration code * fix: nil dereference on marshalling service health * fix: wait for route deletion * chore: enhance tasks debuggability * feat: stdout access logger and MultiWriter * fix(agent): remove agent properly on verify error * fix(metrics): disk exclusion logic and added corresponding tests * chore: update schema and prettify, fix package.json and Makefile * fix: I/O buffer not being shrunk before putting back to pool * feat: enhanced error handling module * chore: deps upgrade * feat: better value formatting and handling --------- Co-authored-by: yusing <yusing@6uo.me>
209 lines
4.8 KiB
Go
209 lines
4.8 KiB
Go
package strutils
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/yusing/go-proxy/internal/utils/strutils/ansi"
|
|
)
|
|
|
|
func AppendDuration(d time.Duration, buf []byte) []byte {
|
|
if d < 0 {
|
|
buf = append(buf, '-')
|
|
d = -d
|
|
}
|
|
|
|
if d == 0 {
|
|
return append(buf, []byte("0 Seconds")...)
|
|
}
|
|
|
|
switch {
|
|
case d < time.Millisecond:
|
|
buf = strconv.AppendInt(buf, int64(d.Nanoseconds()), 10)
|
|
buf = append(buf, []byte(" ns")...)
|
|
return buf
|
|
case d < time.Second:
|
|
buf = strconv.AppendInt(buf, int64(d.Milliseconds()), 10)
|
|
buf = append(buf, []byte(" ms")...)
|
|
return buf
|
|
}
|
|
|
|
// Get total seconds from duration
|
|
totalSeconds := int64(d.Seconds())
|
|
|
|
// Calculate days, hours, minutes, and seconds
|
|
days := totalSeconds / (24 * 3600)
|
|
hours := (totalSeconds % (24 * 3600)) / 3600
|
|
minutes := (totalSeconds % 3600) / 60
|
|
seconds := totalSeconds % 60
|
|
|
|
if days > 0 {
|
|
buf = strconv.AppendInt(buf, days, 10)
|
|
buf = fmt.Appendf(buf, "day%s, ", Pluralize(days))
|
|
}
|
|
if hours > 0 {
|
|
buf = strconv.AppendInt(buf, hours, 10)
|
|
buf = fmt.Appendf(buf, "hour%s, ", Pluralize(hours))
|
|
}
|
|
if minutes > 0 {
|
|
buf = strconv.AppendInt(buf, minutes, 10)
|
|
buf = fmt.Appendf(buf, "minute%s, ", Pluralize(minutes))
|
|
}
|
|
if seconds > 0 && totalSeconds < 3600 {
|
|
buf = strconv.AppendInt(buf, seconds, 10)
|
|
buf = fmt.Appendf(buf, "second%s, ", Pluralize(seconds))
|
|
}
|
|
return buf[:len(buf)-2]
|
|
}
|
|
|
|
func FormatDuration(d time.Duration) string {
|
|
return string(AppendDuration(d, nil))
|
|
}
|
|
|
|
func FormatLastSeen(t time.Time) string {
|
|
if t.IsZero() {
|
|
return "never"
|
|
}
|
|
return FormatTime(t)
|
|
}
|
|
|
|
func appendRound(f float64, buf []byte) []byte {
|
|
return strconv.AppendInt(buf, int64(math.Round(f)), 10)
|
|
}
|
|
|
|
func appendFloat(f float64, buf []byte) []byte {
|
|
f = math.Round(f*100) / 100
|
|
if f == 0 {
|
|
return buf
|
|
}
|
|
return strconv.AppendFloat(buf, f, 'f', -1, 64)
|
|
}
|
|
|
|
func AppendTime(t time.Time, buf []byte) []byte {
|
|
if t.IsZero() {
|
|
return append(buf, []byte("never")...)
|
|
}
|
|
return AppendTimeWithReference(t, time.Now(), buf)
|
|
}
|
|
|
|
func FormatTime(t time.Time) string {
|
|
return string(AppendTime(t, nil))
|
|
}
|
|
|
|
func FormatUnixTime(t int64) string {
|
|
return FormatTime(time.Unix(t, 0))
|
|
}
|
|
|
|
func FormatTimeWithReference(t, ref time.Time) string {
|
|
return string(AppendTimeWithReference(t, ref, nil))
|
|
}
|
|
|
|
func AppendTimeWithReference(t, ref time.Time, buf []byte) []byte {
|
|
if t.IsZero() {
|
|
return append(buf, []byte("never")...)
|
|
}
|
|
diff := t.Sub(ref)
|
|
absDiff := diff.Abs()
|
|
switch {
|
|
case absDiff < time.Second:
|
|
return append(buf, []byte("now")...)
|
|
case absDiff < 3*time.Second:
|
|
if diff < 0 {
|
|
return append(buf, []byte("just now")...)
|
|
}
|
|
fallthrough
|
|
case absDiff < 60*time.Second:
|
|
if diff < 0 {
|
|
buf = appendRound(absDiff.Seconds(), buf)
|
|
buf = append(buf, []byte(" seconds ago")...)
|
|
} else {
|
|
buf = append(buf, []byte("in ")...)
|
|
buf = appendRound(absDiff.Seconds(), buf)
|
|
buf = append(buf, []byte(" seconds")...)
|
|
}
|
|
return buf
|
|
case absDiff < 60*time.Minute:
|
|
if diff < 0 {
|
|
buf = appendRound(absDiff.Minutes(), buf)
|
|
buf = append(buf, []byte(" minutes ago")...)
|
|
} else {
|
|
buf = append(buf, []byte("in ")...)
|
|
buf = appendRound(absDiff.Minutes(), buf)
|
|
buf = append(buf, []byte(" minutes")...)
|
|
}
|
|
return buf
|
|
case absDiff < 24*time.Hour:
|
|
if diff < 0 {
|
|
buf = appendRound(absDiff.Hours(), buf)
|
|
buf = append(buf, []byte(" hours ago")...)
|
|
} else {
|
|
buf = append(buf, []byte("in ")...)
|
|
buf = appendRound(absDiff.Hours(), buf)
|
|
buf = append(buf, []byte(" hours")...)
|
|
}
|
|
return buf
|
|
case t.Year() == ref.Year():
|
|
return t.AppendFormat(buf, "01-02 15:04:05")
|
|
default:
|
|
return t.AppendFormat(buf, "2006-01-02 15:04:05")
|
|
}
|
|
}
|
|
|
|
func FormatByteSize(size int64) string {
|
|
return string(AppendByteSize(size, nil))
|
|
}
|
|
|
|
func AppendByteSize[T ~int64 | ~uint64 | ~float64](size T, buf []byte) []byte {
|
|
const (
|
|
_ = (1 << (10 * iota))
|
|
kb
|
|
mb
|
|
gb
|
|
tb
|
|
pb
|
|
)
|
|
switch {
|
|
case size < kb:
|
|
switch any(size).(type) {
|
|
case int64:
|
|
buf = strconv.AppendInt(buf, int64(size), 10)
|
|
case uint64:
|
|
buf = strconv.AppendUint(buf, uint64(size), 10)
|
|
case float64:
|
|
buf = appendFloat(float64(size), buf)
|
|
}
|
|
buf = append(buf, []byte(" B")...)
|
|
case size < mb:
|
|
buf = appendFloat(float64(size)/kb, buf)
|
|
buf = append(buf, []byte(" KiB")...)
|
|
case size < gb:
|
|
buf = appendFloat(float64(size)/mb, buf)
|
|
buf = append(buf, []byte(" MiB")...)
|
|
case size < tb:
|
|
buf = appendFloat(float64(size)/gb, buf)
|
|
buf = append(buf, []byte(" GiB")...)
|
|
case size < pb:
|
|
buf = appendFloat(float64(size/gb)/kb, buf)
|
|
buf = append(buf, []byte(" TiB")...)
|
|
default:
|
|
buf = appendFloat(float64(size/tb)/kb, buf)
|
|
buf = append(buf, []byte(" PiB")...)
|
|
}
|
|
return buf
|
|
}
|
|
|
|
func DoYouMean(s string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
return "Did you mean " + ansi.HighlightGreen + s + ansi.Reset + "?"
|
|
}
|
|
|
|
func Pluralize(n int64) string {
|
|
if n > 1 {
|
|
return "s"
|
|
}
|
|
return ""
|
|
}
|