storage.go 20.6 KB
Newer Older
Stefan Dähling's avatar
Stefan Dähling committed
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
/*
Copyright 2020 Institute for Automation of Complex Power Systems,
E.ON Energy Research Center, RWTH Aachen University

This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.

Apache License, Version 2.0:

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package logger

import (
	"errors"
Qianwen's avatar
Qianwen committed
49
	"math"
Stefan Dähling's avatar
Stefan Dähling committed
50
	"sort"
Qianwen's avatar
Qianwen committed
51
52
	"strconv"
	"strings"
Stefan Dähling's avatar
Stefan Dähling committed
53
54
	"sync"
	"time"
55
56

	"git.rwth-aachen.de/acs/public/cloud/mas/clonemap/pkg/schemas"
Stefan Dähling's avatar
Stefan Dähling committed
57
58
59
60
61
)

// storage is the interface for logging and state storage (either local or in db)
type storage interface {
	// addAgentLogMessage adds an entry to specified logging entry
Stefan Dähling's avatar
Stefan Dähling committed
62
	addAgentLogMessage(log schemas.LogMessage) (err error)
Stefan Dähling's avatar
Stefan Dähling committed
63

Qianwen's avatar
Qianwen committed
64
65
66
67
68
69
	// addAgentLogSeries add the log series
	addAgentLogSeries(series schemas.LogSeries)

	// addAgentBehStats add the behavior stats
	addAgentBehStats(logBeh schemas.BehStats)

Stefan Dähling's avatar
Stefan Dähling committed
70
	// getLatestAgentLogMessages return the latest num log messages
Stefan Dähling's avatar
Stefan Dähling committed
71
	getLatestAgentLogMessages(masID int, agentID int, topic string,
Stefan Dähling's avatar
Stefan Dähling committed
72
		num int) (logs []schemas.LogMessage, err error)
Stefan Dähling's avatar
Stefan Dähling committed
73
74

	// getAgentLogMessagesInRange return the log messages in the specified time range
Stefan Dähling's avatar
Stefan Dähling committed
75
	getAgentLogMessagesInRange(masID int, agentID int, topic string, start time.Time,
Stefan Dähling's avatar
Stefan Dähling committed
76
		end time.Time) (logs []schemas.LogMessage, err error)
Stefan Dähling's avatar
Stefan Dähling committed
77

Qianwen's avatar
Qianwen committed
78
79
	// getAgentLogSeriesNames gets the name of the log series
	getAgentLogSeriesNames(masID int, agentID int) (names []string, err error)
Qianwen's avatar
Qianwen committed
80
81

	// getAgentLogSeries get the log series
Qianwen's avatar
Qianwen committed
82
83
	getAgentLogSeries(masID int, agentID int, name string, start time.Time, end time.Time) (series []schemas.LogSeries, err error)

Qianwen's avatar
fix    
Qianwen committed
84
85
	// getMsgHeatmap get the msg communication frequency within a range
	getMsgHeatmap(masID int, start time.Time, end time.Time) (heatmap map[[2]int]int, err error)
Qianwen's avatar
Qianwen committed
86

Qianwen's avatar
Qianwen committed
87
88
	// getStats get the data of a certain behtype
	getStats(masID int, agentID int, behType string, start time.Time, end time.Time) (statsInfo schemas.StatsInfo, err error)
Qianwen's avatar
Qianwen committed
89

Stefan Dähling's avatar
Stefan Dähling committed
90
91
92
93
94
95
96
97
98
99
	// deleteAgentLogMessages deletes all log messages og an agent
	deleteAgentLogMessages(masID int, agentID int) (err error)

	// updateCommunication updates communication data
	updateCommunication(masID int, agentID int, commData []schemas.Communication) (err error)

	// getCommunication returns communication data
	getCommunication(masID int, agentID int) (commData []schemas.Communication, err error)

	// updateAgentState updates the agent status
Stefan Dähling's avatar
Stefan Dähling committed
100
	updateAgentState(masID int, agentID int, state schemas.State) (err error)
Stefan Dähling's avatar
Stefan Dähling committed
101
102

	// getAgentState return the latest agent status
Stefan Dähling's avatar
Stefan Dähling committed
103
	getAgentState(masID int, agentID int) (state schemas.State, err error)
Stefan Dähling's avatar
Stefan Dähling committed
104
105
106
107
108
109

	// deleteAgentState deletes the status of an agent
	deleteAgentState(masID int, agentID int) (err error)
}

// LogMessage contains the content of a single log message
Stefan Dähling's avatar
Stefan Dähling committed
110
111
112
113
114
// type LogMessage struct {
// 	Timestamp time.Time
// 	Message   string
// 	Data      string
// }
Stefan Dähling's avatar
Stefan Dähling committed
115
116
117
118
119
120
121
122
123
124
125
126

// IDs of agents and mas correspond to index in slices!
type localStorage struct {
	mas   []masStorage
	mutex *sync.Mutex
}

type masStorage struct {
	agents []agentStorage
}

type agentStorage struct {
Qianwen's avatar
Qianwen committed
127
128
129
130
131
	errLogs   []schemas.LogMessage
	dbgLogs   []schemas.LogMessage
	msgLogs   []schemas.LogMessage
	statLogs  []schemas.LogMessage
	appLogs   []schemas.LogMessage
Qianwen's avatar
Qianwen committed
132
	behLogs   []schemas.LogMessage
Qianwen's avatar
Qianwen committed
133
	behsStats map[string][]schemas.BehStats
Qianwen's avatar
Qianwen committed
134
	logSeries map[string][]schemas.LogSeries
Qianwen's avatar
Qianwen committed
135
136
	state     schemas.State
	commData  []schemas.Communication
Stefan Dähling's avatar
Stefan Dähling committed
137
138
139
}

// addAgentLogMessage adds an entry to specified logging entry
Stefan Dähling's avatar
Stefan Dähling committed
140
func (stor *localStorage) addAgentLogMessage(log schemas.LogMessage) (err error) {
Stefan Dähling's avatar
Stefan Dähling committed
141
142
	stor.mutex.Lock()
	numMAS := len(stor.mas)
Stefan Dähling's avatar
Stefan Dähling committed
143
144
	if numMAS <= log.MASID {
		for i := 0; i < log.MASID-numMAS+1; i++ {
Stefan Dähling's avatar
Stefan Dähling committed
145
146
147
			stor.mas = append(stor.mas, masStorage{})
		}
	}
Stefan Dähling's avatar
Stefan Dähling committed
148
149
150
151
	numAgents := len(stor.mas[log.MASID].agents)
	if numAgents <= log.AgentID {
		for i := 0; i < log.AgentID-numAgents+1; i++ {
			stor.mas[log.MASID].agents = append(stor.mas[log.MASID].agents, agentStorage{})
Stefan Dähling's avatar
Stefan Dähling committed
152
153
		}
	}
Stefan Dähling's avatar
Stefan Dähling committed
154
	switch log.Topic {
Stefan Dähling's avatar
Stefan Dähling committed
155
	case "error":
Stefan Dähling's avatar
Stefan Dähling committed
156
		stor.mas[log.MASID].agents[log.AgentID].errLogs = append(stor.mas[log.MASID].agents[log.AgentID].errLogs,
Stefan Dähling's avatar
Stefan Dähling committed
157
158
			log)
	case "debug":
Stefan Dähling's avatar
Stefan Dähling committed
159
		stor.mas[log.MASID].agents[log.AgentID].dbgLogs = append(stor.mas[log.MASID].agents[log.AgentID].dbgLogs,
Stefan Dähling's avatar
Stefan Dähling committed
160
161
			log)
	case "msg":
Stefan Dähling's avatar
Stefan Dähling committed
162
		stor.mas[log.MASID].agents[log.AgentID].msgLogs = append(stor.mas[log.MASID].agents[log.AgentID].msgLogs,
Stefan Dähling's avatar
Stefan Dähling committed
163
164
			log)
	case "status":
Stefan Dähling's avatar
Stefan Dähling committed
165
		stor.mas[log.MASID].agents[log.AgentID].statLogs = append(stor.mas[log.MASID].agents[log.AgentID].statLogs,
Stefan Dähling's avatar
Stefan Dähling committed
166
167
			log)
	case "app":
Stefan Dähling's avatar
Stefan Dähling committed
168
		stor.mas[log.MASID].agents[log.AgentID].appLogs = append(stor.mas[log.MASID].agents[log.AgentID].appLogs,
Stefan Dähling's avatar
Stefan Dähling committed
169
			log)
Qianwen's avatar
Qianwen committed
170
171
172
	case "beh":
		stor.mas[log.MASID].agents[log.AgentID].behLogs = append(stor.mas[log.MASID].agents[log.AgentID].behLogs,
			log)
Stefan Dähling's avatar
Stefan Dähling committed
173
	default:
174
		err = errors.New("wrong topic")
Stefan Dähling's avatar
Stefan Dähling committed
175
176
177
178
179
180
	}
	stor.mutex.Unlock()
	return
}

// getLatestAgentLogMessages return the latest num log messages
Stefan Dähling's avatar
Stefan Dähling committed
181
func (stor *localStorage) getLatestAgentLogMessages(masID int, agentID int, topic string,
Stefan Dähling's avatar
Stefan Dähling committed
182
	num int) (logs []schemas.LogMessage, err error) {
Stefan Dähling's avatar
Stefan Dähling committed
183
184
185
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
Stefan Dähling's avatar
Stefan Dähling committed
186
			switch topic {
Stefan Dähling's avatar
Stefan Dähling committed
187
188
189
190
191
			case "error":
				length := len(stor.mas[masID].agents[agentID].errLogs)
				if length < num {
					num = length
				}
192
				logs = make([]schemas.LogMessage, num)
Stefan Dähling's avatar
Stefan Dähling committed
193
194
195
196
197
198
				copy(logs, stor.mas[masID].agents[agentID].errLogs[length-num:length])
			case "debug":
				length := len(stor.mas[masID].agents[agentID].dbgLogs)
				if length < num {
					num = length
				}
199
				logs = make([]schemas.LogMessage, num)
Stefan Dähling's avatar
Stefan Dähling committed
200
201
202
203
204
205
				copy(logs, stor.mas[masID].agents[agentID].dbgLogs[length-num:length])
			case "msg":
				length := len(stor.mas[masID].agents[agentID].msgLogs)
				if length < num {
					num = length
				}
206
				logs = make([]schemas.LogMessage, num)
Stefan Dähling's avatar
Stefan Dähling committed
207
208
209
210
211
212
				copy(logs, stor.mas[masID].agents[agentID].msgLogs[length-num:length])
			case "status":
				length := len(stor.mas[masID].agents[agentID].statLogs)
				if length < num {
					num = length
				}
213
				logs = make([]schemas.LogMessage, num)
Stefan Dähling's avatar
Stefan Dähling committed
214
215
216
217
218
219
				copy(logs, stor.mas[masID].agents[agentID].statLogs[length-num:length])
			case "app":
				length := len(stor.mas[masID].agents[agentID].appLogs)
				if length < num {
					num = length
				}
220
				logs = make([]schemas.LogMessage, num)
Stefan Dähling's avatar
Stefan Dähling committed
221
				copy(logs, stor.mas[masID].agents[agentID].appLogs[length-num:length])
Qianwen's avatar
Qianwen committed
222
223
224
225
226
227
228
			case "beh":
				length := len(stor.mas[masID].agents[agentID].behLogs)
				if length < num {
					num = length
				}
				logs = make([]schemas.LogMessage, num)
				copy(logs, stor.mas[masID].agents[agentID].behLogs[length-num:length])
Stefan Dähling's avatar
Stefan Dähling committed
229
			default:
230
				err = errors.New("wrong topic")
Stefan Dähling's avatar
Stefan Dähling committed
231
232
233
234
235
236
237
238
			}
		}
	}
	stor.mutex.Unlock()
	return
}

// getAgentLogMessagesInRange return the log messages in the specified time range
Stefan Dähling's avatar
Stefan Dähling committed
239
func (stor *localStorage) getAgentLogMessagesInRange(masID int, agentID int, topic string,
Stefan Dähling's avatar
Stefan Dähling committed
240
	start time.Time, end time.Time) (logs []schemas.LogMessage, err error) {
Stefan Dähling's avatar
Stefan Dähling committed
241
242
243
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
Stefan Dähling's avatar
Stefan Dähling committed
244
			switch topic {
Stefan Dähling's avatar
Stefan Dähling committed
245
246
247
248
249
			case "error":
				length := len(stor.mas[masID].agents[agentID].errLogs)
				if length > 0 {
					startIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
250
							return stor.mas[masID].agents[agentID].errLogs[i].Timestamp.After(start)
Stefan Dähling's avatar
Stefan Dähling committed
251
252
253
						})
					endIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
254
							return stor.mas[masID].agents[agentID].errLogs[i].Timestamp.After(end)
Stefan Dähling's avatar
Stefan Dähling committed
255
						})
Stefan Dähling's avatar
Stefan Dähling committed
256
					if endIndex-startIndex >= 0 {
257
						logs = make([]schemas.LogMessage, endIndex-startIndex)
Stefan Dähling's avatar
Stefan Dähling committed
258
259
260
261
262
263
264
265
						copy(logs, stor.mas[masID].agents[agentID].errLogs[startIndex:endIndex])
					}
				}
			case "debug":
				length := len(stor.mas[masID].agents[agentID].dbgLogs)
				if length > 0 {
					startIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
266
							return stor.mas[masID].agents[agentID].dbgLogs[i].Timestamp.After(start)
Stefan Dähling's avatar
Stefan Dähling committed
267
268
269
						})
					endIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
270
							return stor.mas[masID].agents[agentID].dbgLogs[i].Timestamp.After(end)
Stefan Dähling's avatar
Stefan Dähling committed
271
						})
Stefan Dähling's avatar
Stefan Dähling committed
272
					if endIndex-startIndex >= 0 {
273
						logs = make([]schemas.LogMessage, endIndex-startIndex)
Stefan Dähling's avatar
Stefan Dähling committed
274
275
276
277
278
279
280
281
						copy(logs, stor.mas[masID].agents[agentID].dbgLogs[startIndex:endIndex])
					}
				}
			case "msg":
				length := len(stor.mas[masID].agents[agentID].msgLogs)
				if length > 0 {
					startIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
282
							return stor.mas[masID].agents[agentID].msgLogs[i].Timestamp.After(start)
Stefan Dähling's avatar
Stefan Dähling committed
283
284
285
						})
					endIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
286
							return stor.mas[masID].agents[agentID].msgLogs[i].Timestamp.After(end)
Stefan Dähling's avatar
Stefan Dähling committed
287
						})
Stefan Dähling's avatar
Stefan Dähling committed
288
					if endIndex-startIndex >= 0 {
289
						logs = make([]schemas.LogMessage, endIndex-startIndex)
Stefan Dähling's avatar
Stefan Dähling committed
290
291
292
293
294
295
296
297
						copy(logs, stor.mas[masID].agents[agentID].msgLogs[startIndex:endIndex])
					}
				}
			case "status":
				length := len(stor.mas[masID].agents[agentID].statLogs)
				if length > 0 {
					startIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
298
299
							return stor.mas[masID].agents[agentID].statLogs[i].Timestamp.
								After(start)
Stefan Dähling's avatar
Stefan Dähling committed
300
301
302
						})
					endIndex := sort.Search(length,
						func(i int) bool {
Stefan Dähling's avatar
Stefan Dähling committed
303
							return stor.mas[masID].agents[agentID].statLogs[i].Timestamp.After(end)
Stefan Dähling's avatar
Stefan Dähling committed
304
						})
Stefan Dähling's avatar
Stefan Dähling committed
305
					if endIndex-startIndex >= 0 {
306
						logs = make([]schemas.LogMessage, endIndex-startIndex)
Stefan Dähling's avatar
Stefan Dähling committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
						copy(logs, stor.mas[masID].agents[agentID].statLogs[startIndex:endIndex])
					}
				}
			case "app":
				length := len(stor.mas[masID].agents[agentID].appLogs)
				if length > 0 {
					startIndex := sort.Search(length,
						func(i int) bool {
							return stor.mas[masID].agents[agentID].appLogs[i].Timestamp.After(start)
						})
					endIndex := sort.Search(length,
						func(i int) bool {
							return stor.mas[masID].agents[agentID].appLogs[i].Timestamp.After(end)
						})
Stefan Dähling's avatar
Stefan Dähling committed
321
					if endIndex-startIndex >= 0 {
322
						logs = make([]schemas.LogMessage, endIndex-startIndex)
Stefan Dähling's avatar
Stefan Dähling committed
323
324
325
						copy(logs, stor.mas[masID].agents[agentID].appLogs[startIndex:endIndex])
					}
				}
Qianwen's avatar
Qianwen committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
			case "beh":
				length := len(stor.mas[masID].agents[agentID].behLogs)
				if length > 0 {
					startIndex := sort.Search(length,
						func(i int) bool {
							return stor.mas[masID].agents[agentID].behLogs[i].Timestamp.After(start)
						})
					endIndex := sort.Search(length,
						func(i int) bool {
							return stor.mas[masID].agents[agentID].behLogs[i].Timestamp.After(end)
						})
					if endIndex-startIndex >= 0 {
						logs = make([]schemas.LogMessage, endIndex-startIndex)
						copy(logs, stor.mas[masID].agents[agentID].behLogs[startIndex:endIndex])
					}
				}
Stefan Dähling's avatar
Stefan Dähling committed
342
			default:
343
				err = errors.New("wrong topic")
Stefan Dähling's avatar
Stefan Dähling committed
344
345
346
347
348
349
350
			}
		}
	}
	stor.mutex.Unlock()
	return
}

Qianwen's avatar
Qianwen committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// addAgentLogSeries add the log series
func (stor *localStorage) addAgentLogSeries(series schemas.LogSeries) {
	stor.mutex.Lock()
	numMAS := len(stor.mas)
	if numMAS <= series.MASID {
		for i := 0; i < series.MASID-numMAS+1; i++ {
			stor.mas = append(stor.mas, masStorage{})
		}
	}
	numAgents := len(stor.mas[series.MASID].agents)
	if numAgents <= series.AgentID {
		for i := 0; i < series.AgentID-numAgents+1; i++ {
			stor.mas[series.MASID].agents = append(stor.mas[series.MASID].agents, agentStorage{})
		}
	}

Qianwen's avatar
Qianwen committed
367
368
369
370
	if stor.mas[series.MASID].agents[series.AgentID].logSeries == nil {
		stor.mas[series.MASID].agents[series.AgentID].logSeries = make(map[string][]schemas.LogSeries)
	}
	stor.mas[series.MASID].agents[series.AgentID].logSeries[series.Name] = append(stor.mas[series.MASID].agents[series.AgentID].logSeries[series.Name], series)
Qianwen's avatar
Qianwen committed
371
372
373
374
	stor.mutex.Unlock()
	return
}

Qianwen's avatar
Qianwen committed
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// addAgentBehStats add the behavior stats
func (stor *localStorage) addAgentBehStats(behStats schemas.BehStats) {
	stor.mutex.Lock()
	numMAS := len(stor.mas)
	if numMAS <= behStats.MASID {
		for i := 0; i < behStats.MASID-numMAS+1; i++ {
			stor.mas = append(stor.mas, masStorage{})
		}
	}
	numAgents := len(stor.mas[behStats.MASID].agents)
	if numAgents <= behStats.AgentID {
		for i := 0; i < behStats.AgentID-numAgents+1; i++ {
			stor.mas[behStats.MASID].agents = append(stor.mas[behStats.MASID].agents, agentStorage{})
		}
	}

	if stor.mas[behStats.MASID].agents[behStats.AgentID].behsStats == nil {
		stor.mas[behStats.MASID].agents[behStats.AgentID].behsStats = make(map[string][]schemas.BehStats)
	}
	stor.mas[behStats.MASID].agents[behStats.AgentID].behsStats[behStats.BehType] = append(stor.mas[behStats.MASID].agents[behStats.AgentID].behsStats[behStats.BehType], behStats)
	stor.mutex.Unlock()
	return
}

// getAgentLogSeriesNames gets the name of the log series
func (stor *localStorage) getAgentLogSeriesNames(masID int, agentID int) (names []string, err error) {
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
			if stor.mas[masID].agents[agentID].logSeries != nil {
				names = make([]string, 0, len(stor.mas[masID].agents[agentID].logSeries))
				for k := range stor.mas[masID].agents[agentID].logSeries {
					names = append(names, k)
				}
			}
		}
	}
	stor.mutex.Unlock()
	return
}

Qianwen's avatar
Qianwen committed
416
// getAgentLogSeries return the log series
Qianwen's avatar
Qianwen committed
417
func (stor *localStorage) getAgentLogSeries(masID int, agentID int, name string, start time.Time, end time.Time) (series []schemas.LogSeries, err error) {
Qianwen's avatar
Qianwen committed
418
419
420
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
Qianwen's avatar
Qianwen committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
			length := len(stor.mas[masID].agents[agentID].logSeries[name])
			if length > 0 {
				startIndex := sort.Search(length,
					func(i int) bool {
						return stor.mas[masID].agents[agentID].logSeries[name][i].Timestamp.After(start)
					})
				endIndex := sort.Search(length,
					func(i int) bool {
						return stor.mas[masID].agents[agentID].logSeries[name][i].Timestamp.After(end)
					})
				if endIndex-startIndex >= 0 {
					series = make([]schemas.LogSeries, endIndex-startIndex)
					copy(series, stor.mas[masID].agents[agentID].logSeries[name][startIndex:endIndex])
				}
			}
		}
	}
	stor.mutex.Unlock()
	return
}

Qianwen's avatar
fix    
Qianwen committed
442
443
444
// getMsgHeatmap return the msg communication frequency within a range
func (stor *localStorage) getMsgHeatmap(masID int, start time.Time, end time.Time) (heatmap map[[2]int]int, err error) {
	heatmap = make(map[[2]int]int)
Qianwen's avatar
Qianwen committed
445
	stor.mutex.Lock()
446
447
448
449
	if masID >= len(stor.mas) {
		stor.mutex.Unlock()
		return
	}
Qianwen's avatar
fix    
Qianwen committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
	for agentID := 0; agentID < len(stor.mas[masID].agents); agentID++ {
		length := len(stor.mas[masID].agents[agentID].msgLogs)
		if length > 0 {
			startIndex := sort.Search(length,
				func(i int) bool {
					return stor.mas[masID].agents[agentID].msgLogs[i].Timestamp.After(start)
				})
			endIndex := sort.Search(length,
				func(i int) bool {
					return stor.mas[masID].agents[agentID].msgLogs[i].Timestamp.After(end)
				})
			if endIndex-startIndex >= 0 {
				for _, log := range stor.mas[masID].agents[agentID].msgLogs[startIndex:endIndex] {
					if log.Message == "ACL send" {
						sender := log.AgentID
						recvnfo := strings.Split(log.AdditionalData, ";")[1]
						receiver, err := strconv.Atoi(strings.Split(recvnfo, ": ")[1])
						if err == nil {
							idx := [2]int{sender, receiver}
							heatmap[idx] += 1
						}
					}
				}
			}
		}
Qianwen's avatar
Qianwen committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	}
	stor.mutex.Unlock()
	return
}

func getDuration(behsStats []schemas.BehStats) (duration []int) {
	for _, behStats := range behsStats {
		duration = append(duration, behStats.Duration)
	}
	return
}

func getMax(duration []int) (maxVal int) {
	maxVal = 0
	for _, v := range duration {
		if v > maxVal {
			maxVal = v
		}
	}
	return
}

func getMin(duration []int) (minVal int) {
	minVal = math.MaxInt32
	for _, v := range duration {
		if v < minVal {
			minVal = v
		}
	}
	return
}

func getAverage(duration []int) (averageVal float32) {
	sum := 0
	for _, v := range duration {
		sum += v
	}
	return float32(sum) / float32(len(duration))
}

Qianwen's avatar
Qianwen committed
515
516
// getStats get the data of a certain behavior type
func (stor *localStorage) getStats(masID int, agentID int, behType string, start time.Time, end time.Time) (statsInfo schemas.StatsInfo, err error) {
Qianwen's avatar
Qianwen committed
517
518
519
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
Qianwen's avatar
Qianwen committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
			res, ok := stor.mas[masID].agents[agentID].behsStats[behType]
			if ok {
				length := len(res)
				startIndex := sort.Search(length,
					func(i int) bool {
						return res[i].Start.After(start)
					})
				endIndex := sort.Search(length,
					func(i int) bool {
						return res[i].Start.After(end)
					})
				if endIndex-startIndex >= 0 {
					logs := make([]schemas.BehStats, endIndex-startIndex)
					copy(logs, stor.mas[masID].agents[agentID].behsStats[behType][startIndex:endIndex])
Qianwen's avatar
Qianwen committed
534
535
536
537
538
539
					statsInfo.List = logs
					statsInfo.Max = getMax(getDuration(logs))
					statsInfo.Min = getMin(getDuration(logs))
					statsInfo.Count = len(logs)
					statsInfo.Average = getAverage((getDuration(logs)))

Qianwen's avatar
Qianwen committed
540
541
				}
			}
Qianwen's avatar
Qianwen committed
542
543
544
545
546
547
		}
	}
	stor.mutex.Unlock()
	return
}

Stefan Dähling's avatar
Stefan Dähling committed
548
549
550
551
552
553
554
555
556
557
// deleteAgentLogMessages deletes all log messages og an agent
func (stor *localStorage) deleteAgentLogMessages(masID int, agentID int) (err error) {
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
			stor.mas[masID].agents[agentID].errLogs = nil
			stor.mas[masID].agents[agentID].dbgLogs = nil
			stor.mas[masID].agents[agentID].msgLogs = nil
			stor.mas[masID].agents[agentID].statLogs = nil
			stor.mas[masID].agents[agentID].appLogs = nil
Qianwen's avatar
Qianwen committed
558
			stor.mas[masID].agents[agentID].behLogs = nil
Stefan Dähling's avatar
Stefan Dähling committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
		}
	}
	stor.mutex.Unlock()
	return
}

// updateCommunication updates communication data
func (stor *localStorage) updateCommunication(masID int, agentID int,
	commData []schemas.Communication) (err error) {
	stor.mutex.Lock()
	numMAS := len(stor.mas)
	if numMAS <= masID {
		for i := 0; i < masID-numMAS+1; i++ {
			stor.mas = append(stor.mas, masStorage{})
		}
	}
	numAgents := len(stor.mas[masID].agents)
	if numAgents <= agentID {
		for i := 0; i < agentID-numAgents+1; i++ {
			stor.mas[i].agents = append(stor.mas[i].agents, agentStorage{})
		}
	}
	stor.mas[masID].agents[agentID].commData = commData
	stor.mutex.Unlock()
	return
}

// getCommunication returns communication data
func (stor *localStorage) getCommunication(masID int,
	agentID int) (commData []schemas.Communication, err error) {
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
			commData = stor.mas[masID].agents[agentID].commData
		}
	}
	stor.mutex.Unlock()
	return
}

// updateAgentState updates the agent status
Stefan Dähling's avatar
Stefan Dähling committed
600
func (stor *localStorage) updateAgentState(masID int, agentID int, state schemas.State) (err error) {
Stefan Dähling's avatar
Stefan Dähling committed
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
	stor.mutex.Lock()
	numMAS := len(stor.mas)
	if numMAS <= masID {
		for i := 0; i < masID-numMAS+1; i++ {
			stor.mas = append(stor.mas, masStorage{})
		}
	}
	numAgents := len(stor.mas[masID].agents)
	if numAgents <= agentID {
		for i := 0; i < agentID-numAgents+1; i++ {
			stor.mas[i].agents = append(stor.mas[i].agents, agentStorage{})
		}
	}
	stor.mas[masID].agents[agentID].state = state
	stor.mutex.Unlock()
	return
}

// getAgentState return the latest agent status
Stefan Dähling's avatar
Stefan Dähling committed
620
func (stor *localStorage) getAgentState(masID int, agentID int) (state schemas.State, err error) {
Stefan Dähling's avatar
Stefan Dähling committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
			state = stor.mas[masID].agents[agentID].state
		}
	}
	stor.mutex.Unlock()
	return
}

// deleteAgentState deletes the status of an agent
func (stor *localStorage) deleteAgentState(masID int, agentID int) (err error) {
	stor.mutex.Lock()
	if masID < len(stor.mas) {
		if agentID < len(stor.mas[masID].agents) {
Stefan Dähling's avatar
Stefan Dähling committed
636
			stor.mas[masID].agents[agentID].state = schemas.State{}
Stefan Dähling's avatar
Stefan Dähling committed
637
638
639
640
641
642
643
644
645
646
647
648
		}
	}
	stor.mutex.Unlock()
	return
}

// newLocalStorage returns Storage interface with localStorage type
func newLocalStorage() storage {
	var temp localStorage
	temp.mutex = &sync.Mutex{}
	return &temp
}