chore: add ansible deploy simple logic, upgrade admin auth logic and docs
ci-agent / build (push) Failing after 1m55s
ci-agent / build (push) Failing after 1m55s
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/backend/internal/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetMockLogs returns 100 mock log entries for frontend development
|
||||
// @Summary Get mock logs
|
||||
// @Description Returns 100 mock log entries for frontend development (no ClickHouse required)
|
||||
// @Tags logs
|
||||
// @Produce json
|
||||
// @Param level query string false "Filter by level"
|
||||
// @Param service query string false "Filter by service"
|
||||
// @Param agent query string false "Filter by agent"
|
||||
// @Param limit query int false "Limit results" default(100)
|
||||
// @Param offset query int false "Offset results" default(0)
|
||||
// @Success 200 {array} storage.LogEntry
|
||||
// @Router /logs/mock [get]
|
||||
func (lh *LogHandlers) GetMockLogs(c *gin.Context) {
|
||||
levelFilter := c.Query("level")
|
||||
serviceFilter := c.Query("service")
|
||||
agentFilter := c.Query("agent")
|
||||
|
||||
limit := 100
|
||||
offset := 0
|
||||
|
||||
if l := c.Query("limit"); l != "" {
|
||||
if parsed, err := strconv.Atoi(l); err == nil && parsed > 0 {
|
||||
limit = parsed
|
||||
}
|
||||
}
|
||||
if o := c.Query("offset"); o != "" {
|
||||
if parsed, err := strconv.Atoi(o); err == nil && parsed >= 0 {
|
||||
offset = parsed
|
||||
}
|
||||
}
|
||||
|
||||
logs := generateMockLogs(100)
|
||||
|
||||
// Apply filters
|
||||
var filtered []storage.LogEntry
|
||||
for _, log := range logs {
|
||||
if levelFilter != "" && log.Level != levelFilter {
|
||||
continue
|
||||
}
|
||||
if serviceFilter != "" && log.Service != serviceFilter {
|
||||
continue
|
||||
}
|
||||
if agentFilter != "" && log.Agent != agentFilter {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, log)
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
end := offset + limit
|
||||
if end > len(filtered) {
|
||||
end = len(filtered)
|
||||
}
|
||||
if offset > len(filtered) {
|
||||
filtered = []storage.LogEntry{}
|
||||
} else {
|
||||
filtered = filtered[offset:end]
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, filtered)
|
||||
}
|
||||
|
||||
func generateMockLogs(count int) []storage.LogEntry {
|
||||
services := []string{
|
||||
"auth-service",
|
||||
"user-service",
|
||||
"agent-service",
|
||||
"gateway",
|
||||
"scheduler",
|
||||
"notification-service",
|
||||
"metrics-collector",
|
||||
"deployment-service",
|
||||
}
|
||||
|
||||
agents := []string{
|
||||
"agent-prod-01",
|
||||
"agent-prod-02",
|
||||
"agent-staging-01",
|
||||
"agent-dev-01",
|
||||
"agent-dev-02",
|
||||
"agent-monitoring-01",
|
||||
"agent-backup-01",
|
||||
"agent-ci-runner-01",
|
||||
}
|
||||
|
||||
levels := []string{"INFO", "WARNING", "ERROR", "FATAL", "DEBUG"}
|
||||
levelWeights := []int{50, 20, 15, 5, 10} // weighted distribution
|
||||
|
||||
messages := map[string][]string{
|
||||
"INFO": {
|
||||
"Service started successfully",
|
||||
"Health check passed",
|
||||
"Configuration loaded",
|
||||
"Connection established to database",
|
||||
"Cache refreshed successfully",
|
||||
"Request processed in 45ms",
|
||||
"User login successful",
|
||||
"Agent registered successfully",
|
||||
"Deployment completed for 3 servers",
|
||||
"Metrics exported to storage",
|
||||
"Backup completed successfully",
|
||||
"SSL certificate valid for 89 days",
|
||||
"Task scheduled: cleanup-temp-files",
|
||||
"Webhook delivered successfully",
|
||||
"Session created for user admin",
|
||||
},
|
||||
"WARNING": {
|
||||
"High memory usage detected: 85%",
|
||||
"Slow query detected: 2.3s",
|
||||
"Rate limit approaching for client 192.168.1.50",
|
||||
"Certificate expires in 7 days",
|
||||
"Retry attempt 2/3 for request",
|
||||
"Disk usage above threshold: 78%",
|
||||
"Connection pool nearly exhausted: 45/50",
|
||||
"Deprecated API endpoint called: /api/v1/legacy",
|
||||
"Response time exceeded SLA: 1.2s > 1s",
|
||||
"Agent heartbeat delayed by 5s",
|
||||
},
|
||||
"ERROR": {
|
||||
"Failed to connect to database: timeout after 30s",
|
||||
"Authentication failed for user test_user",
|
||||
"Agent deployment failed: SSH connection refused",
|
||||
"Failed to send notification: SMTP server unavailable",
|
||||
"Request failed with status 500",
|
||||
"File not found: /etc/hellreign/config.yml",
|
||||
"Invalid token provided",
|
||||
"Permission denied for user viewer",
|
||||
"Failed to parse configuration: invalid YAML",
|
||||
"Agent unreachable: connection timeout",
|
||||
},
|
||||
"FATAL": {
|
||||
"Out of memory: cannot allocate 512MB",
|
||||
"Database connection lost, all retries exhausted",
|
||||
"Critical: SSL certificate expired",
|
||||
"Unrecoverable error: data corruption detected",
|
||||
"Service crashed: segmentation fault",
|
||||
},
|
||||
"DEBUG": {
|
||||
"Processing request payload: 2.3KB",
|
||||
"Cache hit ratio: 78%",
|
||||
"Executing query: SELECT * FROM logs WHERE...",
|
||||
"HTTP request headers: {Content-Type: application/json}",
|
||||
"Agent status check: 8 agents online",
|
||||
"Memory allocation: 256MB used of 1024MB",
|
||||
"Thread pool size: 12 active, 4 idle",
|
||||
"GC pause: 15ms",
|
||||
},
|
||||
}
|
||||
|
||||
r := rand.New(rand.NewSource(42)) // fixed seed for reproducibility
|
||||
|
||||
var logs []storage.LogEntry
|
||||
now := time.Now()
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
level := weightedRandom(r, levels, levelWeights)
|
||||
service := services[r.Intn(len(services))]
|
||||
agent := agents[r.Intn(len(agents))]
|
||||
msgs := messages[level]
|
||||
message := msgs[r.Intn(len(msgs))]
|
||||
|
||||
// Spread logs over the last 24 hours
|
||||
timestamp := now.Add(-time.Duration(count-i) * time.Minute * 15)
|
||||
|
||||
logs = append(logs, storage.LogEntry{
|
||||
Timestamp: timestamp,
|
||||
Level: level,
|
||||
Service: service,
|
||||
Agent: agent,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
return logs
|
||||
}
|
||||
|
||||
func weightedRandom(r *rand.Rand, items []string, weights []int) string {
|
||||
total := 0
|
||||
for _, w := range weights {
|
||||
total += w
|
||||
}
|
||||
n := r.Intn(total)
|
||||
for i, w := range weights {
|
||||
n -= w
|
||||
if n < 0 {
|
||||
return items[i]
|
||||
}
|
||||
}
|
||||
return items[len(items)-1]
|
||||
}
|
||||
Reference in New Issue
Block a user