forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.go
400 lines (360 loc) · 10 KB
/
sample.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statping
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"fmt"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
"math/rand"
"time"
)
// InsertSampleData will create the example/dummy services for a brand new Statping installation
func InsertSampleData() error {
utils.Log(1, "Inserting Sample Data...")
s1 := ReturnService(&types.Service{
Name: "Google",
Domain: "https://google.com",
ExpectedStatus: 200,
Interval: 10,
Type: "http",
Method: "GET",
Timeout: 10,
Order: 1,
})
s2 := ReturnService(&types.Service{
Name: "Statping Github",
Domain: "https://github.com/hunterlong/statping",
ExpectedStatus: 200,
Interval: 30,
Type: "http",
Method: "GET",
Timeout: 20,
Order: 2,
})
s3 := ReturnService(&types.Service{
Name: "JSON Users Test",
Domain: "https://jsonplaceholder.typicode.com/users",
ExpectedStatus: 200,
Interval: 60,
Type: "http",
Method: "GET",
Timeout: 30,
Order: 3,
})
s4 := ReturnService(&types.Service{
Name: "JSON API Tester",
Domain: "https://jsonplaceholder.typicode.com/posts",
ExpectedStatus: 201,
Expected: types.NewNullString(`(title)": "((\\"|[statup])*)"`),
Interval: 30,
Type: "http",
Method: "POST",
PostData: types.NewNullString(`{ "title": "statup", "body": "bar", "userId": 19999 }`),
Timeout: 30,
Order: 4,
})
s5 := ReturnService(&types.Service{
Name: "Google DNS",
Domain: "8.8.8.8",
Interval: 20,
Type: "tcp",
Port: 53,
Timeout: 120,
Order: 5,
})
s1.Create(false)
s2.Create(false)
s3.Create(false)
s4.Create(false)
s5.Create(false)
insertMessages()
utils.Log(1, "Sample data has finished importing")
return nil
}
// insertSampleCheckins will create 2 checkins with 60 successful hits per Checkin
func insertSampleCheckins() error {
s1 := SelectService(1)
checkin1 := ReturnCheckin(&types.Checkin{
ServiceId: s1.Id,
Interval: 300,
GracePeriod: 300,
})
checkin1.Update()
s2 := SelectService(1)
checkin2 := ReturnCheckin(&types.Checkin{
ServiceId: s2.Id,
Interval: 900,
GracePeriod: 300,
})
checkin2.Update()
checkTime := time.Now().Add(-24 * time.Hour)
for i := 0; i <= 60; i++ {
checkHit := ReturnCheckinHit(&types.CheckinHit{
Checkin: checkin1.Id,
From: "192.168.0.1",
CreatedAt: checkTime.UTC(),
})
checkHit.Create()
checkTime = checkTime.Add(10 * time.Minute)
}
return nil
}
// InsertSampleHits will create a couple new hits for the sample services
func InsertSampleHits() error {
since := time.Now().Add((-24 * 7) * time.Hour).UTC()
for i := int64(1); i <= 5; i++ {
service := SelectService(i)
utils.Log(1, fmt.Sprintf("Adding %v sample hit records to service %v", 360, service.Name))
createdAt := since
alpha := float64(1.05)
for hi := int64(1); hi <= 168; hi++ {
alpha += 0.01
rand.Seed(time.Now().UnixNano())
latency := rand.Float64() * alpha
createdAt = createdAt.Add(1 * time.Hour)
hit := &types.Hit{
Service: service.Id,
CreatedAt: createdAt,
Latency: latency,
}
service.CreateHit(hit)
}
}
return nil
}
// insertSampleCore will create a new Core for the seed
func insertSampleCore() error {
core := &types.Core{
Name: "Statping Sample Data",
Description: "This data is only used to testing",
ApiKey: "sample",
ApiSecret: "samplesecret",
Domain: "http://localhost:8080",
Version: "test",
CreatedAt: time.Now(),
UseCdn: types.NewNullBool(false),
}
query := coreDB().Create(core)
return query.Error
}
// insertSampleUsers will create 2 admin users for a seed database
func insertSampleUsers() error {
u2 := ReturnUser(&types.User{
Username: "testadmin",
Password: "password123",
Email: "info@betatude.com",
Admin: types.NewNullBool(true),
})
u3 := ReturnUser(&types.User{
Username: "testadmin2",
Password: "password123",
Email: "info@adminhere.com",
Admin: types.NewNullBool(true),
})
_, err := u2.Create()
_, err = u3.Create()
return err
}
func insertMessages() error {
m1 := ReturnMessage(&types.Message{
Title: "Routine Downtime",
Description: "This is an example a upcoming message for a service!",
ServiceId: 1,
StartOn: time.Now().Add(15 * time.Minute),
EndOn: time.Now().Add(2 * time.Hour),
})
if _, err := m1.Create(); err != nil {
return err
}
m2 := ReturnMessage(&types.Message{
Title: "Server Reboot",
Description: "This is another example a upcoming message for a service!",
ServiceId: 3,
StartOn: time.Now().Add(15 * time.Minute),
EndOn: time.Now().Add(2 * time.Hour),
})
if _, err := m2.Create(); err != nil {
return err
}
return nil
}
// InsertLargeSampleData will create the example/dummy services for testing the Statping server
func InsertLargeSampleData() error {
if err := insertSampleCore(); err != nil {
return err
}
if err := InsertSampleData(); err != nil {
return err
}
if err := insertSampleUsers(); err != nil {
return err
}
if err := insertSampleCheckins(); err != nil {
return err
}
if err := insertMessages(); err != nil {
return err
}
s6 := ReturnService(&types.Service{
Name: "JSON Lint",
Domain: "https://jsonlint.com",
ExpectedStatus: 200,
Interval: 15,
Type: "http",
Method: "GET",
Timeout: 10,
Order: 6,
})
s7 := ReturnService(&types.Service{
Name: "Demo Page",
Domain: "https://demo.statping.com",
ExpectedStatus: 200,
Interval: 30,
Type: "http",
Method: "GET",
Timeout: 15,
Order: 7,
})
s8 := ReturnService(&types.Service{
Name: "Golang",
Domain: "https://golang.org",
ExpectedStatus: 200,
Interval: 15,
Type: "http",
Method: "GET",
Timeout: 10,
Order: 8,
})
s9 := ReturnService(&types.Service{
Name: "Santa Monica",
Domain: "https://www.santamonica.com",
ExpectedStatus: 200,
Interval: 15,
Type: "http",
Method: "GET",
Timeout: 10,
Order: 9,
})
s10 := ReturnService(&types.Service{
Name: "Oeschs Die Dritten",
Domain: "https://www.oeschs-die-dritten.ch/en/",
ExpectedStatus: 200,
Interval: 15,
Type: "http",
Method: "GET",
Timeout: 10,
Order: 10,
})
s11 := ReturnService(&types.Service{
Name: "XS Project - Bochka, Bass, Kolbaser",
Domain: "https://www.youtube.com/watch?v=VLW1ieY4Izw",
ExpectedStatus: 200,
Interval: 60,
Type: "http",
Method: "GET",
Timeout: 20,
Order: 11,
})
s12 := ReturnService(&types.Service{
Name: "Github",
Domain: "https://github.com/hunterlong",
ExpectedStatus: 200,
Interval: 60,
Type: "http",
Method: "GET",
Timeout: 20,
Order: 12,
})
s13 := ReturnService(&types.Service{
Name: "Failing URL",
Domain: "http://thisdomainisfakeanditsgoingtofail.com",
ExpectedStatus: 200,
Interval: 45,
Type: "http",
Method: "GET",
Timeout: 10,
Order: 13,
})
s14 := ReturnService(&types.Service{
Name: "Oesch's die Dritten - Die Jodelsprache",
Domain: "https://www.youtube.com/watch?v=k3GTxRt4iao",
ExpectedStatus: 200,
Interval: 60,
Type: "http",
Method: "GET",
Timeout: 12,
Order: 14,
})
s15 := ReturnService(&types.Service{
Name: "Gorm",
Domain: "http://gorm.io/",
ExpectedStatus: 200,
Interval: 30,
Type: "http",
Method: "GET",
Timeout: 12,
Order: 15,
})
s6.Create(false)
s7.Create(false)
s8.Create(false)
s9.Create(false)
s10.Create(false)
s11.Create(false)
s12.Create(false)
s13.Create(false)
s14.Create(false)
s15.Create(false)
var dayAgo = time.Now().Add(-24 * time.Hour).Add(-10 * time.Minute)
insertHitRecords(dayAgo, 1450)
insertFailureRecords(dayAgo, 730)
return nil
}
// insertFailureRecords will create failures for 15 services from seed
func insertFailureRecords(since time.Time, amount int64) {
for i := int64(14); i <= 15; i++ {
service := SelectService(i)
utils.Log(1, fmt.Sprintf("Adding %v Failure records to service %v", amount, service.Name))
createdAt := since
for fi := int64(1); fi <= amount; fi++ {
createdAt = createdAt.Add(2 * time.Minute)
failure := &Failure{&types.Failure{
Service: service.Id,
Issue: "testing right here",
CreatedAt: createdAt,
}}
service.CreateFailure(failure)
}
}
}
// insertHitRecords will create successful Hit records for 15 services
func insertHitRecords(since time.Time, amount int64) {
for i := int64(1); i <= 15; i++ {
service := SelectService(i)
utils.Log(1, fmt.Sprintf("Adding %v hit records to service %v", amount, service.Name))
createdAt := since
for hi := int64(1); hi <= amount; hi++ {
rand.Seed(time.Now().UnixNano())
latency := rand.Float64()
createdAt = createdAt.Add(1 * time.Minute)
hit := &types.Hit{
Service: service.Id,
CreatedAt: createdAt,
Latency: latency,
}
service.CreateHit(hit)
}
}
}