-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwatch-scripts.go
More file actions
351 lines (298 loc) · 11.3 KB
/
watch-scripts.go
File metadata and controls
351 lines (298 loc) · 11.3 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
Copyright 2024 Blnk Finance Authors.
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.
*/
package watch
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"time"
_ "github.com/marcboeker/go-duckdb"
"github.com/rs/zerolog/log"
)
func updateInstructionDSL(db *sql.DB, id int64, dslJSON string) error {
stmt, err := instructionDB.Prepare(`UPDATE instructions SET dsl_json = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?;`)
if err != nil {
log.Error().Err(err).Int64("id", id).Msg("Error preparing update DSL statement")
return err
}
defer stmt.Close()
_, err = stmt.Exec(dslJSON, id)
if err != nil {
log.Error().Err(err).Int64("id", id).Msg("Error executing update DSL statement")
return err
}
log.Debug().Int64("id", id).Msg("Successfully updated DSL JSON for instruction")
return nil
}
func GetInstructionByID(id int64) (Instruction, error) {
if instructionDB == nil {
return Instruction{}, errors.New("instruction database not initialized")
}
return getInstructionByIDInternal(instructionDB, id)
}
func getInstructionByIDInternal(db *sql.DB, id int64) (Instruction, error) {
row := db.QueryRow(`
SELECT id, name, text, description, CAST(dsl_json AS VARCHAR), created_at, updated_at
FROM instructions
WHERE id = ?;
`, id)
var i Instruction
err := row.Scan(&i.ID, &i.Name, &i.Text, &i.Description, &i.DSLJSON, &i.CreatedAt, &i.UpdatedAt)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Warn().Int64("id", id).Msg("Instruction not found by ID")
return Instruction{}, fmt.Errorf("instruction with ID %d not found", id)
}
log.Error().Err(err).Int64("id", id).Msg("Error scanning instruction row")
return Instruction{}, err
}
return i, nil
}
func GetInstructionByName(name string) (Instruction, error) {
if instructionDB == nil {
return Instruction{}, errors.New("instruction database not initialized")
}
row := instructionDB.QueryRow(`
SELECT id, name, text, description, CAST(dsl_json AS VARCHAR), created_at, updated_at
FROM instructions
WHERE name = ?;
`, name)
var i Instruction
err := row.Scan(&i.ID, &i.Name, &i.Text, &i.Description, &i.DSLJSON, &i.CreatedAt, &i.UpdatedAt)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Warn().Str("name", name).Msg("Instruction not found by name")
return Instruction{}, fmt.Errorf("instruction with name '%s' not found", name)
}
log.Error().Err(err).Str("name", name).Msg("Error scanning instruction row by name")
return Instruction{}, err
}
return i, nil
}
func GetAllInstructions() ([]Instruction, error) {
if instructionDB == nil {
return nil, errors.New("instruction database not initialized")
}
rows, err := instructionDB.Query(`
SELECT id, name, text, description, CAST(dsl_json AS VARCHAR), created_at, updated_at
FROM instructions
ORDER BY name;
`)
if err != nil {
log.Error().Err(err).Msg("Error querying all instructions")
return nil, err
}
defer rows.Close()
var instructions []Instruction
for rows.Next() {
var i Instruction
var desc sql.NullString
err = rows.Scan(&i.ID, &i.Name, &i.Text, &desc, &i.DSLJSON, &i.CreatedAt, &i.UpdatedAt)
if err != nil {
log.Error().Err(err).Msg("Error scanning instruction row during GetAllInstructions")
return nil, err
}
if desc.Valid {
i.Description = desc.String
}
instructions = append(instructions, i)
}
if err = rows.Err(); err != nil {
log.Error().Err(err).Msg("Error iterating instruction rows")
return nil, err
}
log.Debug().Int("count", len(instructions)).Msg("Successfully retrieved all instructions")
return instructions, nil
}
func DeleteInstruction(id int64) error {
if instructionDB == nil {
return errors.New("instruction database not initialized")
}
stmt, err := instructionDB.Prepare(`DELETE FROM instructions WHERE id = ?;`)
if err != nil {
log.Error().Err(err).Msg("Error preparing delete instruction statement")
return err
}
defer stmt.Close()
result, err := stmt.Exec(id)
if err != nil {
log.Error().Err(err).Int64("id", id).Msg("Error executing delete instruction statement")
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
log.Error().Err(err).Int64("id", id).Msg("Error getting rows affected after delete")
}
if rowsAffected == 0 {
log.Warn().Int64("id", id).Msg("Delete instruction called but no rows were affected (ID might not exist)")
return fmt.Errorf("instruction with ID %d not found for deletion", id)
}
log.Info().Int64("id", id).Msg("Successfully deleted instruction")
return nil
}
func getActiveRules() ([]Rule, error) {
if instructionDB == nil {
return nil, errors.New("instruction database not initialized")
}
rows, err := instructionDB.Query(`
SELECT id, name, CAST(dsl_json AS VARCHAR)
FROM instructions
WHERE dsl_json IS NOT NULL AND dsl_json != '""'::JSON;
`)
if err != nil {
log.Error().Err(err).Msg("Error querying instructions for active DSL rules")
return nil, err
}
defer rows.Close()
var rules []Rule
for rows.Next() {
var id int64
var name string
var dslJSON string
err = rows.Scan(&id, &name, &dslJSON)
if err != nil {
log.Error().Err(err).Msg("Error scanning instruction row during getActiveRules")
return nil, err
}
var rule Rule
err = json.Unmarshal([]byte(dslJSON), &rule)
if err != nil {
log.Warn().Err(err).Int64("instruction_id", id).Str("instruction_name", name).Msg("Failed to unmarshal DSL JSON for instruction, skipping rule")
continue
}
rule.Name = name
rules = append(rules, rule)
}
if err = rows.Err(); err != nil {
log.Error().Err(err).Msg("Error iterating instruction rows for active rules")
return nil, err
}
// log.Debug().Int("count", len(rules)).Msg("Successfully retrieved and unmarshalled active DSL rules from instructions")
return rules, nil
}
func GetTopInstructionsWithDSLExamples(limit int) ([]Instruction, error) {
if instructionDB == nil {
return nil, errors.New("instruction database not initialized")
}
query := `
SELECT id, name, text, description, CAST(dsl_json AS VARCHAR), created_at, updated_at
FROM instructions
WHERE dsl_json IS NOT NULL AND dsl_json != ''
ORDER BY updated_at DESC
LIMIT ?;
`
rows, err := instructionDB.Query(query, limit)
if err != nil {
log.Error().Err(err).Int("limit", limit).Msg("Error querying top instructions with DSL examples")
return nil, err
}
defer rows.Close()
var instructions []Instruction
for rows.Next() {
var i Instruction
var desc sql.NullString
err = rows.Scan(&i.ID, &i.Name, &i.Text, &desc, &i.DSLJSON, &i.CreatedAt, &i.UpdatedAt)
if err != nil {
log.Error().Err(err).Msg("Error scanning instruction row during GetTopInstructionsWithDSLExamples")
return nil, err
}
if desc.Valid {
i.Description = desc.String
}
if i.DSLJSON.Valid && i.DSLJSON.String != "" {
instructions = append(instructions, i)
}
}
if err = rows.Err(); err != nil {
log.Error().Err(err).Msg("Error iterating instruction rows for GetTopInstructionsWithDSLExamples")
return nil, err
}
log.Debug().Int("count", len(instructions)).Int("requested_limit", limit).Msg("Successfully retrieved top instructions with DSL examples")
return instructions, nil
}
func CreateInstructionWithPrecompiledDSL(ctx context.Context, name, scriptText, description, compiledDSLJSON string) (Instruction, error) {
if instructionDB == nil {
return Instruction{}, errors.New("instruction database not initialized")
}
id, err := createInstructionRecord(instructionDB, name, scriptText, description)
if err != nil {
return Instruction{}, fmt.Errorf("failed to create instruction record for '%s': %w", name, err)
}
if compiledDSLJSON != "" {
if updateErr := updateInstructionDSL(instructionDB, id, compiledDSLJSON); updateErr != nil {
log.Error().Err(updateErr).Int64("instruction_id", id).Msg("Failed to save pre-compiled DSL for new instruction")
}
} else {
log.Warn().Int64("instruction_id", id).Str("name", name).Msg("No pre-compiled DSL JSON provided; instruction created without DSL.")
}
finalInstruction, fetchErr := getInstructionByIDInternal(instructionDB, id)
if fetchErr != nil {
log.Error().Err(fetchErr).Int64("id", id).Msg("Failed to fetch newly created instruction after saving with pre-compiled DSL")
return Instruction{ID: id, Name: name, Text: scriptText, Description: description}, fmt.Errorf("instruction '%s' (ID: %d) created with pre-compiled DSL, but failed to fetch final record: %w", name, id, fetchErr)
}
return finalInstruction, nil
}
func UpdateInstructionWithPrecompiledDSL(ctx context.Context, id int64, name, scriptText, description, compiledDSLJSON string) (Instruction, error) {
if instructionDB == nil {
return Instruction{}, errors.New("instruction database not initialized")
}
stmt, err := instructionDB.Prepare(`
UPDATE instructions
SET text = ?, description = ?, dsl_json = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?;
`)
if err != nil {
log.Error().Err(err).Int64("id", id).Msg("Error preparing update instruction with precompiled DSL statement")
return Instruction{}, err
}
defer stmt.Close()
var dslForDB sql.NullString
if compiledDSLJSON != "" {
var jsonData map[string]interface{}
if err := json.Unmarshal([]byte(compiledDSLJSON), &jsonData); err != nil {
log.Error().Err(err).Int64("id", id).Str("inputDSL", compiledDSLJSON).Msg("compiledDSLJSON is not a valid JSON object")
return Instruction{}, fmt.Errorf("compiledDSLJSON is not a valid JSON object for instruction ID %d: %w", id, err)
}
dslForDB = sql.NullString{String: compiledDSLJSON, Valid: true}
} else {
dslForDB = sql.NullString{Valid: false}
}
var descForDB sql.NullString
if description != "" {
descForDB = sql.NullString{String: description, Valid: true}
} else {
descForDB = sql.NullString{Valid: false}
}
result, err := stmt.Exec(scriptText, descForDB, dslForDB, id)
if err != nil {
log.Error().Err(err).Int64("id", id).Str("name", name).Msg("Error executing update instruction with precompiled DSL statement")
return Instruction{}, err
}
rowsAffected, rerr := result.RowsAffected()
if rerr != nil {
log.Error().Err(rerr).Int64("id", id).Msg("Error getting rows affected after updating instruction with precompiled DSL")
}
if rowsAffected == 0 {
log.Warn().Int64("id", id).Str("name", name).Msg("Update instruction with precompiled DSL called, but no rows were affected (ID might not exist)")
return Instruction{}, fmt.Errorf("instruction with ID %d not found for update", id)
}
updatedInstruction, fetchErr := getInstructionByIDInternal(instructionDB, id)
if fetchErr != nil {
log.Error().Err(fetchErr).Int64("id", id).Msg("Failed to fetch instruction after updating with precompiled DSL")
return Instruction{ID: id, Name: name, Text: scriptText, Description: description, DSLJSON: dslForDB, UpdatedAt: time.Now()},
fmt.Errorf("instruction (ID: %d) updated with precompiled DSL, but failed to fetch final record: %w", id, fetchErr)
}
return updatedInstruction, nil
}