/
model.go
363 lines (313 loc) · 7.08 KB
/
model.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"strings"
"sync"
"time"
)
type (
// CommitStore is commits storing json store
CommitStore struct {
Version int64
Commits []*Commit
Hearbeats *map[string]string
Logs *map[string]string
Info *map[string]string
Lock *sync.Mutex `json:"-"` //Write sync mutex
}
LogUpdateRequest struct {
Level string `json:"level" validate:"required"`
Data string `json:"data" validate:"required"`
}
InfoKey string
)
var (
currentStore *CommitStore
currentStorePath string
lastUpdateVersion int64
currentStoreDebugString string
)
const (
CallCount InfoKey = InfoKey("call_count")
)
// OpenStore open commit store. When there is no file in path, create new store on the path.
func OpenStore(path string) (isNew bool, openError error) {
if currentStore != nil {
openError = errors.New("commit store is already opened")
return
}
before := time.Now()
storeSize := 0
if _, err := os.Stat(path); err == nil {
data, err := ioutil.ReadFile(path)
if err != nil {
openError = err
return
}
storeSize = len(data)
if err := json.Unmarshal(data, ¤tStore); err != nil {
openError = err
return
}
currentStorePath = path
isNew = false
} else if os.IsNotExist(err) {
currentStore = &CommitStore{}
data, err := json.Marshal(currentStore)
if err != nil {
openError = err
return
}
if err := ioutil.WriteFile(path, data, os.ModePerm); err != nil {
openError = err
return
}
currentStorePath = path
isNew = true
} else {
openError = err
return
}
if currentStore.Hearbeats == nil {
currentStore.Hearbeats = &map[string]string{}
}
if currentStore.Logs == nil {
currentStore.Logs = &map[string]string{}
}
if currentStore.Info == nil {
currentStore.Info = &map[string]string{}
}
currentStore.Lock = &sync.Mutex{}
after := time.Since(before)
currentStoreDebugString = fmt.Sprintf("크기는 %d 바이트, 여는데 %d ms 걸렸습니다.", storeSize, after.Milliseconds())
return
}
// AppendCommitToStore append given commits in commit store
func AppendCommitToStore(commits ...*Commit) error {
if currentStore == nil {
return errors.New("there is no opened store")
}
currentStore.Lock.Lock()
currentStore.Commits = append(currentStore.Commits, commits...)
currentStore.Version++
currentStore.Lock.Unlock()
return nil
}
func AppendLogToStore(ip string, level string, data string) error {
if currentStore == nil {
return errors.New("there is no opened store")
}
currentStore.Lock.Lock()
logs := (*currentStore.Logs)
log, exist := logs[ip]
line := fmt.Sprintf("[%s] %s %s", time.Now().Format("06/01/02 15:04:05"), level, data)
if !exist {
log = line
} else {
log = strings.Join([]string{log, line}, "\n")
}
logs[ip] = log
currentStore.Logs = &logs
currentStore.Version++
currentStore.Lock.Unlock()
return nil
}
func SetInfoToStore(key InfoKey, data string) error {
if currentStore == nil {
return errors.New("there is no opened store")
}
currentStore.Lock.Lock()
info := (*currentStore.Info)
info[string(key)] = data
currentStore.Info = &info
currentStore.Version++
currentStore.Lock.Unlock()
return nil
}
func UpdateHeartbeatToStore(ip string) error {
if currentStore == nil {
return errors.New("there is no opened store")
}
currentStore.Lock.Lock()
beatTime := (*currentStore.Hearbeats)
beatTime[ip] = time.Now().Format(time.RFC3339)
currentStore.Hearbeats = &beatTime
currentStore.Version++
currentStore.Lock.Unlock()
return nil
}
func GetStoreVersion() int64 {
return currentStore.Version
}
func GetStoreDebugString() string {
return currentStoreDebugString
}
func GetInfo(key InfoKey) string {
if v, ok := (*currentStore.Info)[string(key)]; ok {
return v
} else {
return ""
}
}
func GetLogString() map[string]string {
logs := *currentStore.Logs
constraintLogs := map[string]string{}
lengthLimit := 500
for k, v := range logs {
if len(v) > lengthLimit {
constraintLogs[k] = v[len(v)-lengthLimit:]
} else {
constraintLogs[k] = v
}
}
return constraintLogs
}
func GetLatestHeartbeat() *struct {
string
int64
} {
list := make(map[string]int64, 8)
hbs := *currentStore.Hearbeats
for ip, t := range hbs {
pt, err := time.Parse(time.RFC3339, t)
if err == nil {
list[ip] = int64(time.Since(pt).Seconds())
}
}
if len(list) == 0 {
return nil
}
mink := ""
minv := int64(math.MaxInt64)
for ip, t := range list {
if t <= minv {
mink = ip
minv = t
}
}
return &struct {
string
int64
}{mink, minv}
}
func GetHeartbeatString() string {
hbs := *currentStore.Hearbeats
str := ""
for ip, t := range hbs {
pt, err := time.Parse(time.RFC3339, t)
if err == nil {
t = formatSecond(int64(time.Since(pt).Seconds()))
}
str += fmt.Sprintf("[%s] : %s\n", ip, t)
}
if str == "" {
str = "내용 없음"
}
return str
}
// SaveStore save commit store to file system
func SaveStore() error {
if currentStore == nil {
return errors.New("there is no opened store")
}
if currentStore.Version <= lastUpdateVersion {
return nil
}
f, err := os.OpenFile(currentStorePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
err = f.Truncate(0)
if err != nil {
return err
}
data, err := json.Marshal(currentStore)
if err != nil {
return err
}
_, err = f.Write(data)
if err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}
// CloseStore close and free commit store
func CloseStore() error {
if err := SaveStore(); err != nil {
return err
}
currentStore = nil
lastUpdateVersion = 0
return nil
}
// SelectLatestCommit returns latest commit
func SelectLatestCommit() (*Commit, error) {
tcommit, err := SelectLatestStatus(true)
if err != nil {
return nil, err
}
fcommit, err := SelectLatestStatus(false)
if err != nil {
return nil, err
}
if tcommit.CommitTime.After(fcommit.CommitTime) {
return tcommit, nil
} else {
return fcommit, nil
}
}
// SelectLatestStatus returns latest commits matched with given condition
func SelectLatestStatus(status bool) (*Commit, error) {
if len(currentStore.Commits) < 2 {
return nil, errors.New("there is no commit to select in store")
}
var (
latestCommit *Commit = currentStore.Commits[0]
)
for _, c := range currentStore.Commits {
if c.IsOpen == status {
if c.CommitTime.After(latestCommit.CommitTime) {
latestCommit = c
}
}
}
return latestCommit, nil
}
// GetAllCommits return slice of whole commits in store
func GetAllCommits() *[]*Commit {
return ¤tStore.Commits
}
func FixUniqueness() error {
if currentStore == nil {
return errors.New("there is no opened store")
}
existHash := []string{}
uniqueCommits := []*Commit{}
cs := GetAllCommits()
for _, c := range *cs {
if !contains(existHash, c.Hash) {
existHash = append(existHash, c.Hash)
uniqueCommits = append(uniqueCommits, c)
}
}
currentStore.Lock.Lock()
currentStore.Commits = uniqueCommits
currentStore.Lock.Unlock()
return nil
}
func contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}