template_helpers.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package app
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. // getStatusColor returns Bootstrap color class for status
  7. func getStatusColor(status string) string {
  8. switch status {
  9. case "active", "healthy":
  10. return "success"
  11. case "warning":
  12. return "warning"
  13. case "critical", "unreachable":
  14. return "danger"
  15. default:
  16. return "secondary"
  17. }
  18. }
  19. // formatBytes converts bytes to human readable format
  20. func formatBytes(bytes int64) string {
  21. if bytes == 0 {
  22. return "0 B"
  23. }
  24. units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
  25. var i int
  26. value := float64(bytes)
  27. for value >= 1024 && i < len(units)-1 {
  28. value /= 1024
  29. i++
  30. }
  31. if i == 0 {
  32. return fmt.Sprintf("%.0f %s", value, units[i])
  33. }
  34. return fmt.Sprintf("%.1f %s", value, units[i])
  35. }
  36. // formatNumber formats large numbers with commas
  37. func formatNumber(num int64) string {
  38. if num == 0 {
  39. return "0"
  40. }
  41. str := strconv.FormatInt(num, 10)
  42. result := ""
  43. for i, char := range str {
  44. if i > 0 && (len(str)-i)%3 == 0 {
  45. result += ","
  46. }
  47. result += string(char)
  48. }
  49. return result
  50. }
  51. // calculatePercent calculates percentage for progress bars
  52. func calculatePercent(current, max int) int {
  53. if max == 0 {
  54. return 0
  55. }
  56. return (current * 100) / max
  57. }