diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-08-08 13:48:29 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-08-08 13:48:36 -0400 |
commit | f375e7a85a9c74a6b77438139bee4750c2aa00d4 (patch) | |
tree | b0f3210ccf7bf98dd3f6cea510d163de2ef6adc0 | |
parent | cc7906c7648ca74878d694ccba889b24b0671ab3 (diff) |
Random affected services
Incidents are not every 10 seconds
-rw-r--r-- | internal/api/handlers.go | 12 | ||||
-rw-r--r-- | internal/api/utilities.go | 24 |
2 files changed, 33 insertions, 3 deletions
diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 4c4b2ec..bdde394 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -174,14 +174,13 @@ func InitializeAPI(logs *[LogLength]string, n *int, toLogParser chan string) { status.metrics.ResponseTime = status.overallStatus.ResponseTime case <-tick1s.C: - fmt.Println("1 s tick") case <-tick10s.C: for i := range status.incidents { runChance(0.05, func() { status.incidents[i].Status = StatusOperational }) } - runChance(1.0, func() { + runChance(0.08, func() { severity := SeverityMinor runChance(0.3, func() { severity = SeverityMajor @@ -191,6 +190,13 @@ func InitializeAPI(logs *[LogLength]string, n *int, toLogParser chan string) { runChance(0.5, func() { serviceStatus = StatusDegraded }) + + affectedServicesCount := rand.Intn(2) + 1 + affectedServices := randomSelect(status.services, affectedServicesCount) + affectedServiceNames := []string{} + for _, s := range affectedServices { + affectedServiceNames = append(affectedServiceNames, s.Id) + } newIncident := incident{ Id: fmt.Sprintf("%d", incidentId), Title: allIncidentTitles[i], @@ -198,7 +204,7 @@ func InitializeAPI(logs *[LogLength]string, n *int, toLogParser chan string) { Status: serviceStatus, Severity: severity, StartTime: time.Now().UTC(), - AffectedServices: []string{}, + AffectedServices: affectedServiceNames, } status.incidents = append(status.incidents, newIncident) incidentId++ diff --git a/internal/api/utilities.go b/internal/api/utilities.go index 508e341..4a816fc 100644 --- a/internal/api/utilities.go +++ b/internal/api/utilities.go @@ -72,3 +72,27 @@ func addRotLog(logs *[LogLength]string, last *int, parser chan string, value str } } } + +func randomSelect[T any](src []T, n int) []T { + if n <= 0 || len(src) == 0 { + return nil + } + if n >= len(src) { + // Return a copy of the whole slice + cpy := make([]T, len(src)) + copy(cpy, src) + return cpy + } + + // Make a copy so we don't modify the original slice + tmp := make([]T, len(src)) + copy(tmp, src) + + // Shuffle the copy in‑place + rand.Shuffle(len(tmp), func(i, j int) { + tmp[i], tmp[j] = tmp[j], tmp[i] + }) + + // Return the first n elements of the shuffled slice + return tmp[:n] +} |