/
logstore_new.go
162 lines (138 loc) · 3.96 KB
/
logstore_new.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
package datastoresql
import (
"context"
"encoding/json"
"time"
"github.com/direktiv/direktiv/pkg/refactor/core"
"github.com/direktiv/direktiv/pkg/refactor/datastore"
"gorm.io/gorm"
)
var _ datastore.LogStore = &sqlLogNewStore{}
// A fixed page size prevents request with unreasonable high numbers,
// that would cause the system to freeze.
const pageSize = 200
type sqlLogNewStore struct {
db *gorm.DB
}
type ScanResult struct {
ID int
Time time.Time
Tag string
Data []byte
}
func (s sqlLogNewStore) GetOlder(ctx context.Context, track string, t time.Time) ([]core.LogEntry, error) {
query := `
SELECT id, time, tag, data
FROM fluentbit
WHERE tag = ? AND time < ?
ORDER BY time DESC
LIMIT ?;
`
resultList := make([]ScanResult, 0)
tx := s.db.WithContext(ctx).Raw(query, track, t, pageSize).Scan(&resultList)
if tx.Error != nil {
return nil, tx.Error
}
return convertScanResults(resultList)
}
func (s sqlLogNewStore) GetOlderInstance(ctx context.Context, track string, t time.Time) ([]core.LogEntry, error) {
query := `
SELECT id, time, tag, data
FROM fluentbit
WHERE tag LIKE ? AND time < ?
ORDER BY time DESC
LIMIT ?;
`
resultList := make([]ScanResult, 0)
tx := s.db.WithContext(ctx).Raw(query, track, t, pageSize).Scan(&resultList)
if tx.Error != nil {
return nil, tx.Error
}
return convertScanResults(resultList)
}
func (s sqlLogNewStore) GetNewer(ctx context.Context, track string, t time.Time) ([]core.LogEntry, error) {
query := `
SELECT id, time, tag, data
FROM fluentbit
WHERE tag = ? AND time > ?
ORDER BY time ASC
LIMIT ?;
`
resultList := make([]ScanResult, 0)
tx := s.db.WithContext(ctx).Raw(query, track, t, pageSize).Scan(&resultList)
if tx.Error != nil {
return nil, tx.Error
}
return convertScanResults(resultList)
}
func (s sqlLogNewStore) GetNewerInstance(ctx context.Context, track string, t time.Time) ([]core.LogEntry, error) {
query := `
SELECT id, time, tag, data
FROM fluentbit
WHERE tag LIKE ? AND time > ?
ORDER BY time ASC
LIMIT ?;
`
resultList := make([]ScanResult, 0)
tx := s.db.WithContext(ctx).Raw(query, track, t, pageSize).Scan(&resultList)
if tx.Error != nil {
return nil, tx.Error
}
return convertScanResults(resultList)
}
func (s sqlLogNewStore) GetStartingIDUntilTime(ctx context.Context, track string, lastID int, t time.Time) ([]core.LogEntry, error) {
query := `
SELECT id, time, tag, data
FROM fluentbit
WHERE tag = ? AND id > ? time <= ?
ORDER BY time ASC;
`
resultList := make([]ScanResult, 0)
tx := s.db.WithContext(ctx).Raw(query, track, lastID, t).Scan(&resultList)
if tx.Error != nil {
return nil, tx.Error
}
return convertScanResults(resultList)
}
func (s sqlLogNewStore) GetStartingIDUntilTimeInstance(ctx context.Context, track string, lastID int, t time.Time) ([]core.LogEntry, error) {
query := `
SELECT id, time, tag, data
FROM fluentbit
WHERE tag LIKE ? AND id > ? time <= ?
ORDER BY time ASC;
`
resultList := make([]ScanResult, 0)
tx := s.db.WithContext(ctx).Raw(query, track, lastID, t).Scan(&resultList)
if tx.Error != nil {
return nil, tx.Error
}
return convertScanResults(resultList)
}
func convertScanResults(scanResults []ScanResult) ([]core.LogEntry, error) {
resultList := make([]core.LogEntry, 0)
for _, result := range scanResults {
var dataMap map[string]interface{}
err := json.Unmarshal(result.Data, &dataMap)
if err != nil {
return nil, err
}
resultList = append(resultList, core.LogEntry{
ID: result.ID,
Time: result.Time,
Tag: result.Tag,
Data: dataMap,
})
}
return resultList, nil
}
func (s sqlLogNewStore) DeleteOldLogs(ctx context.Context, t time.Time) error {
query := `
DELETE FROM fluentbit
WHERE time < ?
`
tx := s.db.WithContext(ctx).Exec(query, t)
if tx.Error != nil {
return tx.Error
}
return nil
}