This commit is contained in:
2025-05-23 15:11:03 +04:00
parent c86165f88c
commit 92b9c356b8
83 changed files with 450 additions and 810 deletions

View File

@@ -8,7 +8,7 @@ import (
"syscall"
"time"
"github.com/freeflowuniverse/heroagent/pkg/system/stats"
"git.ourworld.tf/herocode/heroagent/pkg/system/stats"
)
func main() {
@@ -18,11 +18,11 @@ func main() {
// Create a new stats manager with Redis connection
// Create a custom configuration
config := &stats.Config{
RedisAddr: "localhost:6379",
RedisPassword: "",
RedisDB: 0,
Debug: false,
QueueSize: 100,
RedisAddr: "localhost:6379",
RedisPassword: "",
RedisDB: 0,
Debug: false,
QueueSize: 100,
DefaultTimeout: 5 * time.Second,
ExpirationTimes: map[string]time.Duration{
"system": 60 * time.Second, // System info expires after 60 seconds
@@ -32,7 +32,7 @@ func main() {
"hardware": 120 * time.Second, // Hardware stats expire after 2 minutes
},
}
manager, err := stats.NewStatsManager(config)
if err != nil {
fmt.Printf("Error creating stats manager: %v\n", err)
@@ -61,12 +61,12 @@ func main() {
fmt.Printf(" Cores: %d\n", sysInfo.CPU.Cores)
fmt.Printf(" Model: %s\n", sysInfo.CPU.ModelName)
fmt.Printf(" Usage: %.1f%%\n", sysInfo.CPU.UsagePercent)
fmt.Println("\nMemory Information:")
fmt.Printf(" Total: %.1f GB\n", sysInfo.Memory.Total)
fmt.Printf(" Used: %.1f GB (%.1f%%)\n", sysInfo.Memory.Used, sysInfo.Memory.UsedPercent)
fmt.Printf(" Free: %.1f GB\n", sysInfo.Memory.Free)
fmt.Println("\nNetwork Information:")
fmt.Printf(" Upload Speed: %s\n", sysInfo.Network.UploadSpeed)
fmt.Printf(" Download Speed: %s\n", sysInfo.Network.DownloadSpeed)
@@ -81,7 +81,7 @@ func main() {
} else {
fmt.Printf("Found %d disks:\n", len(diskStats.Disks))
for _, disk := range diskStats.Disks {
fmt.Printf(" %s: %.1f GB total, %.1f GB free (%.1f%% used)\n",
fmt.Printf(" %s: %.1f GB total, %.1f GB free (%.1f%% used)\n",
disk.Path, disk.Total, disk.Free, disk.UsedPercent)
}
}
@@ -93,12 +93,12 @@ func main() {
if err != nil {
fmt.Printf("Error getting process stats: %v\n", err)
} else {
fmt.Printf("Total processes: %d (showing top %d)\n",
fmt.Printf("Total processes: %d (showing top %d)\n",
processStats.Total, len(processStats.Processes))
fmt.Println("\nTop Processes by CPU Usage:")
for i, proc := range processStats.Processes {
fmt.Printf(" %d. PID %d: %s (CPU: %.1f%%, Memory: %.1f MB)\n",
fmt.Printf(" %d. PID %d: %s (CPU: %.1f%%, Memory: %.1f MB)\n",
i+1, proc.PID, proc.Name, proc.CPUPercent, proc.MemoryMB)
}
}
@@ -107,10 +107,10 @@ func main() {
fmt.Println("\n4. CACHING DEMONSTRATION")
fmt.Println("----------------------")
fmt.Println("Getting network speed multiple times (should use cache):")
for i := 0; i < 3; i++ {
netSpeed := manager.GetNetworkSpeedResult()
fmt.Printf(" Request %d: Upload: %s, Download: %s\n",
fmt.Printf(" Request %d: Upload: %s, Download: %s\n",
i+1, netSpeed.UploadSpeed, netSpeed.DownloadSpeed)
time.Sleep(500 * time.Millisecond)
}
@@ -127,17 +127,17 @@ func main() {
fmt.Println("--------------------------")
fmt.Println("Enabling debug mode (direct fetching without cache)...")
manager.Debug = true
fmt.Println("Getting system info in debug mode:")
debugSysInfo, err := manager.GetSystemInfo()
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf(" CPU Usage: %.1f%%\n", debugSysInfo.CPU.UsagePercent)
fmt.Printf(" Memory Used: %.1f GB (%.1f%%)\n",
fmt.Printf(" Memory Used: %.1f GB (%.1f%%)\n",
debugSysInfo.Memory.Used, debugSysInfo.Memory.UsedPercent)
}
// Reset debug mode
manager.Debug = false
@@ -148,17 +148,17 @@ func main() {
for statsType, duration := range manager.Expiration {
fmt.Printf(" %s: %v\n", statsType, duration)
}
fmt.Println("\nChanging system stats expiration to 10 seconds...")
manager.Expiration["system"] = 10 * time.Second
fmt.Println("Updated expiration times:")
for statsType, duration := range manager.Expiration {
fmt.Printf(" %s: %v\n", statsType, duration)
}
fmt.Println("\nDemo complete. Press Ctrl+C to exit.")
// Keep the program running
select {}
}