|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/tidwall/btree" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + // we don't use a `-` as RouterKey must be alphanumeric |
| 13 | + ModuleName = "downtimedetector" |
| 14 | + StoreKey = ModuleName |
| 15 | + RouterKey = ModuleName |
| 16 | + |
| 17 | + QuerierRoute = ModuleName |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + DowntimeToDuration = btree.NewMap[Downtime, time.Duration](16) |
| 22 | + DefaultLastDowntime = time.Unix(0, 0) |
| 23 | +) |
| 24 | + |
| 25 | +// init initializes the DowntimeToDuration map with mappings |
| 26 | +// from the Duration enum values to their corresponding |
| 27 | +// time.Duration values. |
| 28 | +func init() { |
| 29 | + DowntimeToDuration.Set(Downtime_DURATION_30S, 30*time.Second) |
| 30 | + DowntimeToDuration.Set(Downtime_DURATION_1M, time.Minute) |
| 31 | + DowntimeToDuration.Set(Downtime_DURATION_2M, 2*time.Minute) |
| 32 | + DowntimeToDuration.Set(Downtime_DURATION_3M, 3*time.Minute) |
| 33 | + DowntimeToDuration.Set(Downtime_DURATION_4M, 4*time.Minute) |
| 34 | + DowntimeToDuration.Set(Downtime_DURATION_5M, 5*time.Minute) |
| 35 | + DowntimeToDuration.Set(Downtime_DURATION_10M, 10*time.Minute) |
| 36 | + DowntimeToDuration.Set(Downtime_DURATION_20M, 20*time.Minute) |
| 37 | + DowntimeToDuration.Set(Downtime_DURATION_30M, 30*time.Minute) |
| 38 | + DowntimeToDuration.Set(Downtime_DURATION_40M, 40*time.Minute) |
| 39 | + DowntimeToDuration.Set(Downtime_DURATION_50M, 50*time.Minute) |
| 40 | + DowntimeToDuration.Set(Downtime_DURATION_1H, time.Hour) |
| 41 | + DowntimeToDuration.Set(Downtime_DURATION_1_5H, time.Hour+30*time.Minute) |
| 42 | + DowntimeToDuration.Set(Downtime_DURATION_2H, 2*time.Hour) |
| 43 | + DowntimeToDuration.Set(Downtime_DURATION_2_5H, 2*time.Hour+30*time.Minute) |
| 44 | + DowntimeToDuration.Set(Downtime_DURATION_3H, 3*time.Hour) |
| 45 | + DowntimeToDuration.Set(Downtime_DURATION_4H, 4*time.Hour) |
| 46 | + DowntimeToDuration.Set(Downtime_DURATION_5H, 5*time.Hour) |
| 47 | + DowntimeToDuration.Set(Downtime_DURATION_6H, 6*time.Hour) |
| 48 | + DowntimeToDuration.Set(Downtime_DURATION_9H, 9*time.Hour) |
| 49 | + DowntimeToDuration.Set(Downtime_DURATION_12H, 12*time.Hour) |
| 50 | + DowntimeToDuration.Set(Downtime_DURATION_18H, 18*time.Hour) |
| 51 | + DowntimeToDuration.Set(Downtime_DURATION_24H, 24*time.Hour) |
| 52 | + DowntimeToDuration.Set(Downtime_DURATION_36H, 36*time.Hour) |
| 53 | + DowntimeToDuration.Set(Downtime_DURATION_48H, 48*time.Hour) |
| 54 | +} |
| 55 | + |
| 56 | +func DowntimeStrings() []string { |
| 57 | + arr := []string{} |
| 58 | + DowntimeToDuration.Ascend(Downtime(0), func(_ Downtime, v time.Duration) bool { |
| 59 | + s := strings.Replace(v.String(), "m0s", "m", 1) |
| 60 | + s = strings.Replace(s, "h0m", "h", 1) |
| 61 | + arr = append(arr, s) |
| 62 | + return true |
| 63 | + }) |
| 64 | + return arr |
| 65 | +} |
| 66 | + |
| 67 | +func DowntimeByDuration(duration time.Duration) (Downtime, error) { |
| 68 | + var result Downtime |
| 69 | + found := false |
| 70 | + DowntimeToDuration.Ascend(Downtime(0), func(k Downtime, v time.Duration) bool { |
| 71 | + if v == duration { |
| 72 | + result = k |
| 73 | + found = true |
| 74 | + return false |
| 75 | + } |
| 76 | + return true |
| 77 | + }) |
| 78 | + if found { |
| 79 | + return result, nil |
| 80 | + } |
| 81 | + downtimeOptions := strings.Join(DowntimeStrings(), ", ") |
| 82 | + return result, fmt.Errorf("downtime of %s does not exist. Chain-provided downtimes [%s]", duration.String(), downtimeOptions) |
| 83 | +} |
0 commit comments