-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
173 lines (158 loc) · 5.3 KB
/
Copy pathmodel.go
File metadata and controls
173 lines (158 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"crypto/rand"
"encoding/hex"
"time"
)
const stateVersion = 1
type Settings struct {
IntervalSeconds int `json:"intervalSeconds" yaml:"interval-seconds"`
Concurrency int `json:"concurrency" yaml:"concurrency"`
TimeoutMS int `json:"timeoutMs" yaml:"timeout-ms"`
ThresholdMS int `json:"thresholdMs" yaml:"threshold-ms"`
ProbeURL string `json:"probeUrl" yaml:"probe-url"`
Attempts int `json:"attempts" yaml:"attempts"`
AutoCoreUpdate bool `json:"autoCoreUpdate" yaml:"auto-core-update"`
CoreCheckHours int `json:"coreCheckHours" yaml:"core-check-hours"`
}
func defaultSettings() Settings {
return Settings{
IntervalSeconds: 300,
Concurrency: 10,
TimeoutMS: 5000,
ThresholdMS: 400,
ProbeURL: "https://www.gstatic.com/generate_204",
Attempts: 1,
AutoCoreUpdate: true,
CoreCheckHours: 24,
}
}
type SettingsOverride struct {
IntervalSeconds *int `json:"intervalSeconds,omitempty"`
Concurrency *int `json:"concurrency,omitempty"`
TimeoutMS *int `json:"timeoutMs,omitempty"`
ThresholdMS *int `json:"thresholdMs,omitempty"`
ProbeURL *string `json:"probeUrl,omitempty"`
Attempts *int `json:"attempts,omitempty"`
}
func (o SettingsOverride) Apply(base Settings) Settings {
if o.IntervalSeconds != nil {
base.IntervalSeconds = *o.IntervalSeconds
}
if o.Concurrency != nil {
base.Concurrency = *o.Concurrency
}
if o.TimeoutMS != nil {
base.TimeoutMS = *o.TimeoutMS
}
if o.ThresholdMS != nil {
base.ThresholdMS = *o.ThresholdMS
}
if o.ProbeURL != nil {
base.ProbeURL = *o.ProbeURL
}
if o.Attempts != nil {
base.Attempts = *o.Attempts
}
return base
}
type Subscription struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Headers map[string]string `json:"headers,omitempty"`
Enabled bool `json:"enabled"`
Overrides SettingsOverride `json:"overrides,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
NextRunAt *time.Time `json:"nextRunAt,omitempty"`
LastSuccessAt *time.Time `json:"lastSuccessAt,omitempty"`
LastStatus string `json:"lastStatus"`
LastError string `json:"lastError,omitempty"`
OutputHeaders map[string]string `json:"outputHeaders,omitempty"`
Published bool `json:"published"`
Stats JobStats `json:"stats"`
}
type JobStats struct {
Total int `json:"total"`
Tested int `json:"tested"`
Kept int `json:"kept"`
Discarded int `json:"discarded"`
MedianDelay int `json:"medianDelay"`
}
type NodeResult struct {
Index int `json:"index"`
Name string `json:"name"`
Type string `json:"type,omitempty"`
Delay int `json:"delay,omitempty"`
Kept bool `json:"kept"`
Reason string `json:"reason,omitempty"`
Error string `json:"error,omitempty"`
OriginalName string `json:"originalName,omitempty"`
}
type LogEntry struct {
Time time.Time `json:"time"`
Level string `json:"level"`
Message string `json:"message"`
}
type Job struct {
ID string `json:"id"`
SubscriptionID string `json:"subscriptionId"`
Status string `json:"status"`
Trigger string `json:"trigger"`
CreatedAt time.Time `json:"createdAt"`
StartedAt *time.Time `json:"startedAt,omitempty"`
FinishedAt *time.Time `json:"finishedAt,omitempty"`
Stage string `json:"stage"`
Error string `json:"error,omitempty"`
Stats JobStats `json:"stats"`
Results []NodeResult `json:"results,omitempty"`
Logs []LogEntry `json:"logs,omitempty"`
}
type CoreInfo struct {
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
BinaryPath string `json:"binaryPath,omitempty"`
Status string `json:"status"`
LastCheckedAt *time.Time `json:"lastCheckedAt,omitempty"`
LastUpdatedAt *time.Time `json:"lastUpdatedAt,omitempty"`
LastError string `json:"lastError,omitempty"`
}
type State struct {
Version int `json:"version"`
Settings Settings `json:"settings"`
Subscriptions []Subscription `json:"subscriptions"`
Jobs []Job `json:"jobs"`
Core CoreInfo `json:"core"`
}
func newID(bytes int) string {
b := make([]byte, bytes)
if _, err := rand.Read(b); err != nil {
return time.Now().UTC().Format("150405.000000")
}
return hex.EncodeToString(b)
}
func validateSettings(s Settings) Settings {
if s.IntervalSeconds < 10 {
s.IntervalSeconds = 300
}
if s.Concurrency < 1 || s.Concurrency > 100 {
s.Concurrency = 10
}
if s.TimeoutMS < 100 || s.TimeoutMS > 60000 {
s.TimeoutMS = 5000
}
if s.ThresholdMS < 1 || s.ThresholdMS > s.TimeoutMS {
s.ThresholdMS = min(400, s.TimeoutMS)
}
if s.ProbeURL != "https://www.gstatic.com/generate_204" && s.ProbeURL != "https://www.g.cn/generate_204" {
s.ProbeURL = "https://www.gstatic.com/generate_204"
}
if s.Attempts != 1 && s.Attempts != 3 {
s.Attempts = 1
}
if s.CoreCheckHours < 1 || s.CoreCheckHours > 168 {
s.CoreCheckHours = 24
}
return s
}