mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-06 03:42:22 +00:00
refactor: Migrate billing and config commands to uiContext with error handling and simplified output
- Update HandleBilling to accept CLIConfig, return error, and use uiContext for all output - Update HandleConfig to accept CLIConfig, return error, and use uiContext for all output - Replace manual colorizer/icon initialization with uiContext helper methods - Simplify error handling with uiContext.errorln, warn, success, info, muted methods - Replace direct table creation with uiContext.newTable()
This commit is contained in:
@@ -2,87 +2,181 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/nutcas3/telecom-platform/apps/cli/internal/types"
|
||||
)
|
||||
|
||||
func HandleMonitoring(args []string) {
|
||||
// HandleMonitoring is the entry point for monitoring commands.
|
||||
func HandleMonitoring(args []string, config *types.CLIConfig) error {
|
||||
u := newUIContext(config)
|
||||
if len(args) == 0 {
|
||||
showMonitoringHelp()
|
||||
return
|
||||
showMonitoringHelp(u)
|
||||
return nil
|
||||
}
|
||||
|
||||
u.connectivityBanner()
|
||||
|
||||
command := args[0]
|
||||
switch command {
|
||||
case "metrics":
|
||||
showMetrics()
|
||||
return showMetrics(u)
|
||||
case "alerts":
|
||||
showAlerts()
|
||||
return showAlerts(u)
|
||||
case "health":
|
||||
showHealth()
|
||||
return showHealth(u)
|
||||
case "logs":
|
||||
showLogs(args[1:])
|
||||
return showLogs(u, args[1:])
|
||||
default:
|
||||
fmt.Printf("Unknown monitoring command: %s\n", command)
|
||||
showMonitoringHelp()
|
||||
u.errorln("Unknown monitoring command: " + command)
|
||||
showMonitoringHelp(u)
|
||||
return fmt.Errorf("unknown command: %s", command)
|
||||
}
|
||||
}
|
||||
|
||||
func showMonitoringHelp() {
|
||||
fmt.Println("Monitoring Management")
|
||||
fmt.Println("Usage: telecom-cli monitoring <command> [options]")
|
||||
fmt.Println("\nAvailable commands:")
|
||||
fmt.Println(" metrics - Show system metrics")
|
||||
fmt.Println(" alerts - Show active alerts")
|
||||
fmt.Println(" health - Show system health")
|
||||
fmt.Println(" logs <service> - Show service logs")
|
||||
func showMonitoringHelp(u *uiContext) {
|
||||
u.header("Monitoring Management")
|
||||
u.muted("Usage: telecom-cli monitoring <command> [options]")
|
||||
fmt.Println()
|
||||
|
||||
t := u.newTable()
|
||||
t.AddColumn("Command", 18, "left")
|
||||
t.AddColumn("Description", 40, "left")
|
||||
t.AddRow("metrics", "Show system metrics")
|
||||
t.AddRow("alerts", "Show active alerts")
|
||||
t.AddRow("health", "Show system health")
|
||||
t.AddRow("logs <service>", "Show service logs")
|
||||
fmt.Println(t.Render())
|
||||
}
|
||||
|
||||
func showMetrics() {
|
||||
fmt.Println("System Metrics:")
|
||||
fmt.Println("Metric Value Status")
|
||||
fmt.Println("----------------------------------------------------")
|
||||
fmt.Println("API Response Time 125ms Good")
|
||||
fmt.Println("Database Connections 15/100 Good")
|
||||
fmt.Println("CPU Usage 45% Good")
|
||||
fmt.Println("Memory Usage 2.3GB/8GB Good")
|
||||
fmt.Println("Disk Usage 45% Good")
|
||||
fmt.Println("Network Throughput 1.2GB/s Good")
|
||||
fmt.Println("Active Subscribers 1,234 Good")
|
||||
fmt.Println("Request Rate 450/s Good")
|
||||
func showMetrics(u *uiContext) error {
|
||||
u.header("System Metrics")
|
||||
|
||||
stats, err := u.client.GetSystemStats()
|
||||
t := u.newTable()
|
||||
t.AddColumn("Metric", 24, "left")
|
||||
t.AddColumn("Value", 16, "right")
|
||||
t.AddColumn("Status", 10, "left")
|
||||
|
||||
if err != nil {
|
||||
u.warn("Using placeholder data: " + err.Error())
|
||||
rows := [][]string{
|
||||
{"API Response Time", "125ms", "Good"},
|
||||
{"Database Connections", "15/100", "Good"},
|
||||
{"CPU Usage", "45%", "Good"},
|
||||
{"Memory Usage", "2.3GB/8GB", "Good"},
|
||||
{"Active Subscribers", "1234", "Good"},
|
||||
{"Request Rate", "450/s", "Good"},
|
||||
}
|
||||
for _, r := range rows {
|
||||
t.AddStyledRow(statusStyle("HEALTHY").Style, r[0], r[1], r[2])
|
||||
}
|
||||
fmt.Println(t.Render())
|
||||
return nil
|
||||
}
|
||||
t.AddRow("Active Sessions", fmt.Sprintf("%d", stats.ActiveSessions))
|
||||
t.AddRow("Total Accounts", fmt.Sprintf("%d", stats.TotalAccounts))
|
||||
t.AddRow("Blocked Users", fmt.Sprintf("%d", stats.BlockedUsers))
|
||||
t.AddRow("Low Balance Alerts", fmt.Sprintf("%d", stats.LowBalanceAlerts))
|
||||
t.AddRow("CPU Usage", fmt.Sprintf("%.1f%%", stats.CPUUsage))
|
||||
t.AddRow("Memory Usage", fmt.Sprintf("%.1f%%", stats.MemoryUsage))
|
||||
t.AddRow("Uptime", stats.Uptime)
|
||||
fmt.Println(t.Render())
|
||||
return nil
|
||||
}
|
||||
|
||||
func showAlerts() {
|
||||
fmt.Println("Active Alerts:")
|
||||
fmt.Println("Severity Service Message Time")
|
||||
fmt.Println("--------------------------------------------------------------------")
|
||||
fmt.Println("High API Server High memory usage detected 14:30")
|
||||
fmt.Println("Medium Database Slow query detected 14:25")
|
||||
fmt.Println("Low Gateway Packet loss increased 14:20")
|
||||
func showAlerts(u *uiContext) error {
|
||||
u.header("Active Alerts")
|
||||
|
||||
alerts, err := u.client.ListAlerts()
|
||||
t := u.newTable()
|
||||
t.AddColumn("Severity", 10, "left")
|
||||
t.AddColumn("Service", 16, "left")
|
||||
t.AddColumn("Message", 40, "left")
|
||||
t.AddColumn("Time", 10, "left")
|
||||
|
||||
if err != nil {
|
||||
u.warn("Using placeholder data: " + err.Error())
|
||||
rows := [][]string{
|
||||
{"High", "API Server", "High memory usage detected", "14:30"},
|
||||
{"Medium", "Database", "Slow query detected", "14:25"},
|
||||
{"Low", "Gateway", "Packet loss increased", "14:20"},
|
||||
}
|
||||
for _, r := range rows {
|
||||
style := statusStyle(strings.ToUpper(r[0]))
|
||||
if r[0] == "High" {
|
||||
style = statusStyle("ERROR")
|
||||
} else if r[0] == "Medium" {
|
||||
style = statusStyle("WARNING")
|
||||
}
|
||||
t.AddStyledRow(style.Style, r[0], r[1], r[2], r[3])
|
||||
}
|
||||
fmt.Println(t.Render())
|
||||
return nil
|
||||
}
|
||||
for _, a := range alerts {
|
||||
style := statusStyle(strings.ToUpper(a.Severity))
|
||||
t.AddStyledRow(style.Style, a.Severity, a.Service, a.Message, a.Time.Format("15:04"))
|
||||
}
|
||||
fmt.Println(t.Render())
|
||||
return nil
|
||||
}
|
||||
|
||||
func showHealth() {
|
||||
fmt.Println("System Health Status:")
|
||||
fmt.Println("Service Status Uptime Last Check")
|
||||
fmt.Println("----------------------------------------------------")
|
||||
fmt.Println("API Server Healthy 99.9% 14:45:30")
|
||||
fmt.Println("Charging Engine Healthy 99.8% 14:45:30")
|
||||
fmt.Println("Packet Gateway Healthy 99.7% 14:45:30")
|
||||
fmt.Println("Web Dashboard Healthy 99.9% 14:45:30")
|
||||
fmt.Println("Database Healthy 99.5% 14:45:30")
|
||||
fmt.Println("Redis Healthy 99.9% 14:45:30")
|
||||
func showHealth(u *uiContext) error {
|
||||
u.header("System Health Status")
|
||||
|
||||
health, err := u.client.GetHealth()
|
||||
t := u.newTable()
|
||||
t.AddColumn("Service", 18, "left")
|
||||
t.AddColumn("Status", 10, "left")
|
||||
t.AddColumn("Uptime", 10, "right")
|
||||
t.AddColumn("Last Check", 12, "right")
|
||||
|
||||
if err != nil {
|
||||
u.warn("Using placeholder data: " + err.Error())
|
||||
rows := [][]string{
|
||||
{"API Server", "Healthy", "99.9%", "14:45:30"},
|
||||
{"Charging Engine", "Healthy", "99.8%", "14:45:30"},
|
||||
{"Packet Gateway", "Healthy", "99.7%", "14:45:30"},
|
||||
{"Web Dashboard", "Healthy", "99.9%", "14:45:30"},
|
||||
{"Database", "Healthy", "99.5%", "14:45:30"},
|
||||
{"Redis", "Healthy", "99.9%", "14:45:30"},
|
||||
}
|
||||
for _, r := range rows {
|
||||
t.AddStyledRow(statusStyle(strings.ToUpper(r[1])).Style, r[0], r[1], r[2], r[3])
|
||||
}
|
||||
fmt.Println(t.Render())
|
||||
return nil
|
||||
}
|
||||
for _, h := range health {
|
||||
t.AddStyledRow(statusStyle(strings.ToUpper(h.Status)).Style,
|
||||
h.Service, h.Status, h.Uptime, h.LastCheck.Format("15:04:05"))
|
||||
}
|
||||
fmt.Println(t.Render())
|
||||
return nil
|
||||
}
|
||||
|
||||
func showLogs(args []string) {
|
||||
func showLogs(u *uiContext, args []string) error {
|
||||
service := "all"
|
||||
if len(args) > 0 {
|
||||
service = args[0]
|
||||
}
|
||||
|
||||
fmt.Printf("Recent logs for %s:\n", service)
|
||||
fmt.Println("2024-01-15 16:45:30 [INFO] Service started successfully")
|
||||
fmt.Println("2024-01-15 16:45:35 [INFO] Database connection established")
|
||||
fmt.Println("2024-01-15 16:45:40 [INFO] API server listening on port 8000")
|
||||
fmt.Println("2024-01-15 16:45:45 [INFO] Health check passed")
|
||||
fmt.Println("2024-01-15 16:45:50 [WARN] High memory usage detected")
|
||||
fmt.Println("2024-01-15 16:45:55 [ERROR] Database connection timeout")
|
||||
fmt.Println("2024-01-15 16:46:00 [INFO] Database connection restored")
|
||||
u.header("Recent logs for " + service)
|
||||
u.muted("(Log streaming API not yet connected - showing placeholder)")
|
||||
logs := []struct {
|
||||
line string
|
||||
level string
|
||||
}{
|
||||
{"2024-01-15 16:45:30 [INFO] Service started successfully", "OK"},
|
||||
{"2024-01-15 16:45:35 [INFO] Database connection established", "OK"},
|
||||
{"2024-01-15 16:45:40 [INFO] API server listening on port 8000", "OK"},
|
||||
{"2024-01-15 16:45:45 [INFO] Health check passed", "OK"},
|
||||
{"2024-01-15 16:45:50 [WARN] High memory usage detected", "WARNING"},
|
||||
{"2024-01-15 16:45:55 [ERROR] Database connection timeout", "ERROR"},
|
||||
{"2024-01-15 16:46:00 [INFO] Database connection restored", "OK"},
|
||||
}
|
||||
for _, l := range logs {
|
||||
fmt.Println(u.colorizer.Colorize(l.line, statusStyle(l.level)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user