-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
67 lines (56 loc) · 1.48 KB
/
message.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
package main
import (
"errors"
"strconv"
"strings"
"time"
)
// A Message represents emitted by the call monitor.
type Message struct {
Timestamp time.Time `json:"timestamp"`
ConnectionID int `json:"connectionId"`
Type string `json:"type"`
Event Event `json:"event"`
}
// A Parser parses string messages
type Parser struct {
location *time.Location
}
// NewParser creates and returns a new Parser
func NewParser(config *tomlConfig) (Parser, error) {
location, err := time.LoadLocation(config.Callmonitor.Timezone)
if err != nil {
return Parser{}, err
}
return Parser{location}, nil
}
// Parse takes a string line from the call monitor and returns a Message object
func (p Parser) Parse(input string) (Message, error) {
fields := strings.Split(input, ";")
fields = fields[:len(fields)-1]
if len(fields) < 3 {
return Message{}, errors.New("Invalid number of fields in received string")
}
dateStr := fields[0]
eventType := fields[1]
connectionStr := fields[2]
// TODO: Replace UTC with configurable timezone
timestamp, err := time.ParseInLocation("02.01.06 15:04:05", dateStr, p.location)
if err != nil {
return Message{}, err
}
connectionID, err := strconv.Atoi(connectionStr)
if err != nil {
return Message{}, err
}
event, err := parseEvent(eventType, fields[3:])
if err != nil {
return Message{}, err
}
return Message{
Timestamp: timestamp,
ConnectionID: connectionID,
Type: eventType,
Event: event,
}, nil
}