-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_entries.go
167 lines (138 loc) · 3.87 KB
/
log_entries.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
package models
import (
"bufio"
"database/sql"
"encoding/json"
"os"
"strings"
"time"
"turm/app"
)
/*LogEntries contains all relevant log entries. */
type LogEntries []LogEntry
/*LogEntry represents a line of the error log. */
type LogEntry struct {
ID int `db:"id, primarykey, autoincrement"`
TimeOfCreation time.Time `db:"time_of_creation"`
JSON string `db:"json"`
Solved bool `db:"solved"`
TimeOfCreationStr string `db:"time_of_creation_str"`
}
/*Select all log entries. */
func (entries *LogEntries) Select() (err error) {
err = app.Db.Select(entries, stmtSelectLogEntries, app.TimeZone)
if err != nil {
log.Error("failed to select log entries", "error", err.Error())
}
return
}
/*Insert opens the log file and inserts all new log entries. */
func (entries *LogEntries) Insert() (err error) {
//open file in read-only mode
file, err := os.Open(app.PathErrorLog)
if err != nil {
log.Error("failed to open error log file", "filepath", app.PathErrorLog,
"error", err.Error())
return
}
//new file scanner
scanner := bufio.NewScanner(file)
//config the scanner to read each line
scanner.Split(bufio.ScanLines)
//store each line in the jsons slice
var jsons []string
for scanner.Scan() {
jsons = append(jsons, scanner.Text())
}
//close the file
file.Close()
tx, err := app.Db.Beginx()
if err != nil {
log.Error("failed to begin tx", "error", err.Error())
return
}
//get the last extraction time
var lastLogEntryTime sql.NullTime
err = tx.Get(&lastLogEntryTime, stmtGetLastExtractionTime)
if err != nil {
log.Error("failed to get last extraction time", "error", err.Error())
tx.Rollback()
return
}
lastLogEntryTime.Time = lastLogEntryTime.Time.Add(time.Microsecond)
//insert all log entries occuring after the last extraction time
for _, line := range jsons {
//unmarshal line
var jsonLine map[string]interface{}
err = json.Unmarshal([]byte(line), &jsonLine)
if err != nil {
log.Error("failed to unmarshal line", "line", line, "error", err.Error())
tx.Rollback()
return
}
if jsonLine["t"] == nil {
log.Error("failed to get time of creation of log entry", "line", line)
tx.Rollback()
return
}
if jsonLine["caller"] != nil {
caller := jsonLine["caller"].(string)
if strings.Contains(caller, "revel_logger.go:39") || strings.Contains(caller,
"compress.go:151") || strings.Contains(caller, "results.go:428") {
continue
}
}
//format: 2021-03-09T09:10:48.033279498+01:00
timeOfCreation, err := time.Parse("2006-01-02T15:04:05.999999999-07:00", jsonLine["t"].(string))
if err != nil {
log.Error("failed to parse time of creation", "t", jsonLine["t"].(string),
"error", err.Error())
tx.Rollback()
return err
}
if lastLogEntryTime.Time.Before(timeOfCreation) &&
!lastLogEntryTime.Time.Equal(timeOfCreation) {
_, err = tx.Exec(stmtInsertLogEntry, timeOfCreation, line)
if err != nil {
log.Error("failed to insert log entry", "timeOfCreation", timeOfCreation,
"line", line, "error", err.Error())
tx.Rollback()
return err
}
}
}
tx.Commit()
return
}
/*Solve a log entry. */
func (entry *LogEntry) Solve() (err error) {
_, err = app.Db.Exec(stmtSolveLogEntry, entry.ID)
if err != nil {
log.Error("failed to solve log entry", "entryID", entry.ID,
"error", err.Error())
}
return
}
const (
stmtSelectLogEntries = `
SELECT id, time_of_creation, json,
TO_CHAR (time_of_creation AT TIME ZONE $1, 'YYYY-MM-DD HH24:MI:SS') AS time_of_creation_str
FROM log_entries
WHERE NOT solved
ORDER BY time_of_creation DESC
`
stmtSolveLogEntry = `
UPDATE log_entries
SET solved = true
WHERE id = $1
`
stmtGetLastExtractionTime = `
SELECT MAX(time_of_creation) AS last_log_entry_time
FROM log_entries
`
stmtInsertLogEntry = `
INSERT INTO log_entries
(time_of_creation, json)
VALUES ($1, $2)
`
)