118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package dashboard
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"sort"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type registryCycleView struct {
|
||
|
|
ipDetailView
|
||
|
|
CycleID int
|
||
|
|
RunID int64
|
||
|
|
Date time.Time
|
||
|
|
Passed int
|
||
|
|
Badge Badge
|
||
|
|
}
|
||
|
|
|
||
|
|
func buildRegistryCycles(history registryHistoryResponse) []registryCycleView {
|
||
|
|
cycles := map[int]*registryCycleView{}
|
||
|
|
cycleFor := func(id int) *registryCycleView {
|
||
|
|
if cycles[id] == nil {
|
||
|
|
cycles[id] = ®istryCycleView{CycleID: id}
|
||
|
|
}
|
||
|
|
return cycles[id]
|
||
|
|
}
|
||
|
|
for _, check := range history.Checks {
|
||
|
|
c := cycleFor(check.CycleID)
|
||
|
|
c.Detail.Checks = append(c.Detail.Checks, check)
|
||
|
|
c.Detail.IP.AttemptNumber = check.AttemptNumber
|
||
|
|
if check.ValidatorID != "" {
|
||
|
|
c.Detail.IP.OwnerValidatorID = &check.ValidatorID
|
||
|
|
}
|
||
|
|
if check.Success {
|
||
|
|
c.Passed++
|
||
|
|
}
|
||
|
|
if check.CheckedAt.After(c.Date) {
|
||
|
|
c.Date = check.CheckedAt
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, e := range buildEventViews(history.Events) {
|
||
|
|
c := cycleFor(e.CycleID)
|
||
|
|
c.Events = append(c.Events, e)
|
||
|
|
}
|
||
|
|
if ip := history.CurrentIP; ip != nil && (ip.State != "queued" || cycles[ip.CycleID] != nil) {
|
||
|
|
c := cycleFor(ip.CycleID)
|
||
|
|
c.Detail.IP = *ip
|
||
|
|
c.HasQueueData = true
|
||
|
|
}
|
||
|
|
for _, meta := range history.Cycles {
|
||
|
|
if c := cycles[meta.CycleID]; c != nil {
|
||
|
|
c.RunID = meta.RunID
|
||
|
|
c.Detail.SelfCheckFailedOn = meta.SelfCheckFailedOn
|
||
|
|
if !c.HasQueueData {
|
||
|
|
c.Detail.IP.OverallResult = meta.Verdict
|
||
|
|
c.Detail.IP.AggregatedAt = meta.AggregatedAt
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
out := make([]registryCycleView, 0, len(cycles))
|
||
|
|
for _, c := range cycles {
|
||
|
|
if !c.HasQueueData {
|
||
|
|
c.restoreEventMetadata()
|
||
|
|
}
|
||
|
|
ip := c.Detail.IP
|
||
|
|
switch {
|
||
|
|
case c.HasQueueData:
|
||
|
|
c.Badge = ipBadge(ip.State, ip.OverallResult, ip.FIPAssociatedAt, 0)
|
||
|
|
case ip.State == "occupied":
|
||
|
|
c.Badge = ipBadge(ip.State, "", nil, 0)
|
||
|
|
case ip.OverallResult != "":
|
||
|
|
c.Badge = ipBadge("done", ip.OverallResult, nil, 0)
|
||
|
|
}
|
||
|
|
switch {
|
||
|
|
case ip.AggregatedAt != nil:
|
||
|
|
c.Date = *ip.AggregatedAt
|
||
|
|
case c.Date.IsZero() && len(c.Events) > 0:
|
||
|
|
c.Date = c.Events[0].OccurredAt
|
||
|
|
case c.Date.IsZero() && ip.AssignedAt != nil:
|
||
|
|
c.Date = *ip.AssignedAt
|
||
|
|
case c.Date.IsZero():
|
||
|
|
c.Date = ip.CreatedAt
|
||
|
|
}
|
||
|
|
out = append(out, *c)
|
||
|
|
}
|
||
|
|
sort.Slice(out, func(i, j int) bool { return out[i].CycleID > out[j].CycleID })
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *registryCycleView) restoreEventMetadata() {
|
||
|
|
ip := &c.Detail.IP
|
||
|
|
for _, e := range c.Events {
|
||
|
|
switch e.EventType {
|
||
|
|
case "aggregated":
|
||
|
|
var payload struct{ Result string }
|
||
|
|
if ip.OverallResult == "" && json.Unmarshal([]byte(e.Payload), &payload) == nil {
|
||
|
|
switch payload.Result {
|
||
|
|
case "pass", "partial", "fail":
|
||
|
|
ip.OverallResult = payload.Result
|
||
|
|
ip.AggregatedAt = &e.OccurredAt
|
||
|
|
}
|
||
|
|
}
|
||
|
|
case "force_cancel":
|
||
|
|
if ip.OverallResult == "" {
|
||
|
|
ip.OverallResult = "cancelled"
|
||
|
|
ip.AggregatedAt = &e.OccurredAt
|
||
|
|
}
|
||
|
|
case "fip_occupied":
|
||
|
|
ip.State = "occupied"
|
||
|
|
case "fip_associated":
|
||
|
|
var payload struct {
|
||
|
|
ValidatorID string `json:"validator_id"`
|
||
|
|
}
|
||
|
|
if ip.OwnerValidatorID == nil && json.Unmarshal([]byte(e.Payload), &payload) == nil && payload.ValidatorID != "" {
|
||
|
|
ip.OwnerValidatorID = &payload.ValidatorID
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|