-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimportlogs.go
More file actions
155 lines (119 loc) · 3.95 KB
/
importlogs.go
File metadata and controls
155 lines (119 loc) · 3.95 KB
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
package database
import (
"context"
_ "embed"
"strings"
"time"
"github.com/jackc/pgx/v4/pgxpool"
)
type ImportLogsTable struct {
*pgxpool.Pool
}
type ImportRun struct {
RunId int `json:"run_id"`
RunType string `json:"run_type"`
Date time.Time `json:"date"`
Logs []ImportLogs `json:"logs"`
}
type ImportLogs struct {
GuildId uint64 `json:"guild_id"`
LogType string `json:"log_type"`
RunType string `json:"run_type"`
RunId int `json:"run_id"`
RunLogId int `json:"run_log_id"`
EntityType *string `json:"entity_type"`
Message *string `json:"message"`
Date time.Time `json:"date"`
}
var (
//go:embed sql/import_logs/schema.sql
importLogsSchema string
//go:embed sql/import_logs/set.sql
importLogsSet string
//go:embed sql/import_logs/set_run.sql
importLogsSetRun string
)
func newImportLogs(db *pgxpool.Pool) *ImportLogsTable {
return &ImportLogsTable{
db,
}
}
func (s ImportLogsTable) Schema() string {
return importLogsSchema
}
func (s *ImportLogsTable) GetFinishedRuns(ctx context.Context, guildId uint64) ([]ImportRun, error) {
query := `SELECT run_id, run_type, date FROM import_logs WHERE "guild_id" = $1 AND log_type = 'RUN_COMPLETE';`
var runs []ImportRun
rows, err := s.Query(ctx, query, guildId)
if err != nil {
return nil, err
}
for rows.Next() {
var mappingEntry ImportRun
if err := rows.Scan(&mappingEntry.RunId, &mappingEntry.RunType, &mappingEntry.Date); err != nil {
return nil, err
}
runs = append(runs, mappingEntry)
}
return runs, nil
}
func (s *ImportLogsTable) GetRuns(ctx context.Context, guildId uint64) ([]ImportRun, error) {
query := `SELECT run_id, run_type, date FROM import_logs WHERE "guild_id" = $1 AND log_type = 'RUN_START';`
var runs []ImportRun
rows, err := s.Query(ctx, query, guildId)
if err != nil {
return nil, err
}
for rows.Next() {
var mappingEntry ImportRun
if err := rows.Scan(&mappingEntry.RunId, &mappingEntry.RunType, &mappingEntry.Date); err != nil {
return nil, err
}
runs = append(runs, mappingEntry)
}
for i := range runs {
logs, err := s.GetRunLogs(ctx, guildId, runs[i].RunId)
if err != nil {
return nil, err
}
runs[i].Logs = logs
}
return runs, nil
}
func (s *ImportLogsTable) GetRunLogs(ctx context.Context, guildId uint64, runId int) ([]ImportLogs, error) {
query := `SELECT guild_id, log_type, run_id, run_log_id, run_type, entity_type, message, date FROM import_logs WHERE guild_id = $1 AND run_id = $2 ORDER BY run_log_id ASC;`
var logs []ImportLogs
rows, err := s.Query(ctx, query, guildId, runId)
if err != nil {
return nil, err
}
for rows.Next() {
var logEntry ImportLogs
if err := rows.Scan(&logEntry.GuildId, &logEntry.LogType, &logEntry.RunId, &logEntry.RunLogId, &logEntry.RunType, &logEntry.EntityType, &logEntry.Message, &logEntry.Date); err != nil {
return nil, err
}
logs = append(logs, logEntry)
}
return logs, nil
}
func (s *ImportLogsTable) CreateRun(ctx context.Context, guildId uint64, runType string) (int, error) {
runCount := 1
currentRuns, _ := s.GetRuns(ctx, guildId)
runCount += len(currentRuns)
_, err := s.Exec(ctx, importLogsSetRun, guildId, "RUN_START", runCount, runType)
return runCount, err
}
func (s *ImportLogsTable) AddLog(ctx context.Context, guildId uint64, runId int, runType string, logType string, entityType string, message string) error {
return s.addLog(ctx, guildId, runId, runType, logType, entityType, message, 1)
}
func (s *ImportLogsTable) addLog(ctx context.Context, guildId uint64, runId int, runType string, logType string, entityType string, message string, try int) error {
_, err := s.Exec(ctx, importLogsSet, guildId, logType, runId, runType, entityType, message)
if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
// Try again
if try > 5 {
return err
}
return s.addLog(ctx, guildId, runId, runType, logType, entityType, message, try+1)
}
return err
}