-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
84 lines (67 loc) · 1.62 KB
/
memory.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
package storage
import (
"time"
)
type MemoryStorage struct {
Watchers []WatcherRecord
Responses []ResponseRecord
}
func NewMemorystore() (*MemoryStorage, error) {
store := &MemoryStorage{
Watchers: make([]WatcherRecord, 0),
}
store.InsertTestData()
return store, nil
}
func (s *MemoryStorage) InsertTestData() {
s.Watchers = append(s.Watchers, WatcherRecord{
ID: 1,
URL: "https://www.google.com",
Watcher: "http",
})
s.Watchers = append(s.Watchers, WatcherRecord{
ID: 2,
URL: "https://www.matukal.de",
Watcher: "http",
})
s.Watchers = append(s.Watchers, WatcherRecord{
ID: 3,
URL: "https://www.styria.com",
Watcher: "http",
})
}
func (s *MemoryStorage) GetWatchers() ([]WatcherRecord, error) {
return s.Watchers, nil
}
func (s *MemoryStorage) Close() {
}
func (s *MemoryStorage) InsertWatcher(url string, watcher string) (WatcherRecord, error) {
record := WatcherRecord{
ID: len(s.Watchers) + 1,
URL: url,
Watcher: watcher,
}
s.Watchers = append(s.Watchers, record)
return record, nil
}
func (s *MemoryStorage) GetLastResponses(watcher int, n int) ([]ResponseRecord, error) {
res := make([]ResponseRecord, 0)
for i := range s.Responses {
if s.Responses[i].Watcher == watcher {
res = append(res, s.Responses[i])
}
}
if len(res) <= n {
return res, nil
}
return res[len(res)-n:], nil
}
func (s *MemoryStorage) InsertResponse(watcher int, online bool, responsetime time.Duration) error {
record := ResponseRecord{
Watcher: watcher,
Online: online,
ReponseTime: responsetime,
}
s.Responses = append(s.Responses, record)
return nil
}