-
Notifications
You must be signed in to change notification settings - Fork 14
/
models.go
138 lines (118 loc) · 3.24 KB
/
models.go
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
package domain
import (
"crypto/rand"
"encoding/base64"
"errors"
"io"
"time"
)
const (
Version = "1.4.3"
)
var (
ErrConflict = errors.New("conflict")
ErrNotFound = errors.New("not found")
)
type (
Duration time.Duration
CollectionState int
JobState int
JobStatusCode int
JobHistoryStatus int
CollectionItem struct {
ID string `json:"id"`
Name string `json:"name"`
State CollectionState `json:"state"`
}
Collection struct {
CollectionItem
Updated time.Time `json:"updated"`
}
VariableItem struct {
ID string `json:"id"`
Name string `json:"name"`
CollectionID string `json:"collectionId"`
Updated time.Time `json:"updated"`
}
Variable struct {
VariableItem
Value string `json:"value"`
}
JobItem struct {
ID string `json:"id"`
Name string `json:"name"`
CollectionID string `json:"collectionId"`
State JobState `json:"state"`
Schedule string `json:"schedule"`
Status *JobStatusCode `json:"status,omitempty"`
ErrorRate *float32 `json:"errorRate,omitempty"`
}
JobDefinition struct {
JobItem
Updated time.Time `json:"updated"`
Action *Action `json:"action"`
}
Action struct {
Type string `json:"type"`
Request *HttpRequest `json:"request"`
RetryPolicy *RetryPolicy `json:"retryPolicy,omitempty"`
}
HttpRequest struct {
Method string `json:"method,omitempty"`
URI string `json:"uri"`
Headers []*NameValuePair `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
}
NameValuePair struct {
Name string `json:"name"`
Value string `json:"value"`
}
RetryPolicy struct {
RetryCount int `json:"retryCount"`
RetryInterval Duration `json:"retryInterval"`
Deadline Duration `json:"deadline"`
}
JobStatus struct {
Updated time.Time `json:"updated"`
Running bool `json:"running"`
RunCount int `json:"runCount"`
ErrorCount int `json:"errorCount"`
LastRun *time.Time `json:"lastRun,omitempty"`
NextRun *time.Time `json:"nextRun,omitempty"`
}
JobHistory struct {
JobID string `json:"-"`
Action string `json:"action"`
Started time.Time `json:"started"`
Finished time.Time `json:"finished"`
Status JobHistoryStatus `json:"status"`
RetryCount int `json:"retryCount,omitempty"`
Message *string `json:"message,omitempty"`
}
UpdateEvent struct {
ObjectType string
Operation string
ObjectID string
}
)
var (
DefaultRetryPolicy = &RetryPolicy{
RetryCount: 3,
RetryInterval: 5 * Duration(time.Second),
Deadline: 20 * Duration(time.Second),
}
Connected = &UpdateEvent{ObjectType: "connection", Operation: "connected"}
Disconnected = &UpdateEvent{ObjectType: "connection", Operation: "disconnected"}
Reconnected = &UpdateEvent{ObjectType: "connection", Operation: "reconnected"}
)
func NewID() string {
id := make([]byte, 8)
_, err := io.ReadAtLeast(rand.Reader, id, 8)
if err != nil {
return ""
}
if id[0] >= 248 { // Exclude '-' and '_'
id[0] -= 248
}
return base64.RawURLEncoding.EncodeToString(id)
}