-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
294 lines (238 loc) · 7.74 KB
/
app.go
File metadata and controls
294 lines (238 loc) · 7.74 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
package main
import (
"context"
"fmt"
"log"
"flowpulse/pkg/database"
"flowpulse/pkg/models"
"flowpulse/pkg/scheduler"
)
// App struct
type App struct {
ctx context.Context
db *database.DBService
scheduler *scheduler.SchedulerService
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Initialize the database
db, err := database.NewDBService()
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
a.db = db
// Initialize the scheduler
a.scheduler = scheduler.NewSchedulerService(db)
// Start all active jobs
if err := a.scheduler.StartAllJobs(); err != nil {
log.Printf("Failed to start jobs: %v", err)
}
log.Println("FlowPulse started successfully!")
}
// shutdown is called when the app is about to quit
func (a *App) shutdown(ctx context.Context) {
log.Println("Shutting down FlowPulse...")
if a.scheduler != nil {
a.scheduler.Shutdown()
}
if a.db != nil {
a.db.Close()
}
}
// APIs methods
// GetAllAPIs returns all APIs
func (a *App) GetAllAPIs() ([]models.API, error) {
return a.db.GetAllAPIs()
}
// GetAPIByID returns an API by ID
func (a *App) GetAPIByID(id int) (models.API, error) {
return a.db.GetAPIByID(id)
}
// CreateAPI creates a new API
func (a *App) CreateAPI(api models.API) (models.API, error) {
return a.db.CreateAPI(api)
}
// UpdateAPI updates an existing API
func (a *App) UpdateAPI(api models.API) (models.API, error) {
return a.db.UpdateAPI(api)
}
// DeleteAPI deletes an API by ID
func (a *App) DeleteAPI(id int) error {
return a.db.DeleteAPI(id)
}
// Collection methods
// GetAllCollections returns all collections
func (a *App) GetAllCollections() ([]models.Collection, error) {
return a.db.GetAllCollections()
}
// GetCollectionByID returns a collection by ID
func (a *App) GetCollectionByID(id int) (models.Collection, error) {
return a.db.GetCollectionByID(id)
}
// CreateCollection creates a new collection
func (a *App) CreateCollection(collection models.Collection) (models.Collection, error) {
return a.db.CreateCollection(collection)
}
// UpdateCollection updates an existing collection
func (a *App) UpdateCollection(collection models.Collection) (models.Collection, error) {
return a.db.UpdateCollection(collection)
}
// DeleteCollection deletes a collection by ID
func (a *App) DeleteCollection(id int) error {
return a.db.DeleteCollection(id)
}
// GetAPIsByCollectionID returns all APIs in a collection
func (a *App) GetAPIsByCollectionID(collectionID int) ([]models.API, error) {
return a.db.GetAPIsByCollectionID(collectionID)
}
// Analytics methods
// GetAPIAnalytics returns analytics for a specific API
func (a *App) GetAPIAnalytics(apiID int) (models.AnalyticsSummary, error) {
return a.db.GetAPIAnalytics(apiID)
}
// GetOverallAnalytics returns overall analytics for all APIs
func (a *App) GetOverallAnalytics() (models.AnalyticsSummary, error) {
return a.db.GetOverallAnalytics()
}
// GetExecutionStatusCounts returns counts of different status code ranges for an API
func (a *App) GetExecutionStatusCounts(apiID int) (map[string]int, error) {
logs, err := a.db.GetExecutionLogsByAPIID(apiID, 1000) // Get a large sample
if err != nil {
return nil, err
}
counts := map[string]int{
"success": 0, // 2xx
"redirect": 0, // 3xx
"client_error": 0, // 4xx
"server_error": 0, // 5xx
"other": 0, // Other codes
}
for _, log := range logs {
statusCode := log.StatusCode
if statusCode >= 200 && statusCode < 300 {
counts["success"]++
} else if statusCode >= 300 && statusCode < 400 {
counts["redirect"]++
} else if statusCode >= 400 && statusCode < 500 {
counts["client_error"]++
} else if statusCode >= 500 {
counts["server_error"]++
} else {
counts["other"]++
}
}
return counts, nil
}
// Schedules methods
// GetAllSchedules returns all schedules
func (a *App) GetAllSchedules() ([]models.Schedule, error) {
return a.db.GetAllSchedules()
}
// GetSchedulesByAPIID returns all schedules for an API
func (a *App) GetSchedulesByAPIID(apiID int) ([]models.Schedule, error) {
return a.db.GetSchedulesByAPIID(apiID)
}
// CreateSchedule creates a new schedule
func (a *App) CreateSchedule(schedule models.Schedule) (models.Schedule, error) {
newSchedule, err := a.db.CreateSchedule(schedule)
if err != nil {
return newSchedule, err
}
// If the schedule is active, schedule it
if newSchedule.IsActive {
if err := a.scheduler.ScheduleJob(newSchedule); err != nil {
return newSchedule, fmt.Errorf("schedule created but failed to start job: %w", err)
}
}
return newSchedule, nil
}
// UpdateSchedule updates an existing schedule
func (a *App) UpdateSchedule(schedule models.Schedule) error {
// Get the current state of the schedule
currentSchedule, err := a.db.GetScheduleByID(schedule.ID)
if err != nil {
return err
}
isCurrentlyActive := currentSchedule.IsActive
// Update the schedule in the database
if err := a.db.UpdateSchedule(schedule); err != nil {
return err
}
// Handle job scheduling based on active state changes
if isCurrentlyActive && !schedule.IsActive {
// Stop the job
if err := a.scheduler.StopJob(schedule.ID); err != nil {
return fmt.Errorf("schedule updated but failed to stop job: %w", err)
}
} else if !isCurrentlyActive && schedule.IsActive {
// Start the job
if err := a.scheduler.ScheduleJob(schedule); err != nil {
return fmt.Errorf("schedule updated but failed to start job: %w", err)
}
} else if isCurrentlyActive && schedule.IsActive {
// Update the job by stopping and restarting
if err := a.scheduler.StopJob(schedule.ID); err != nil {
log.Printf("Failed to stop existing job for schedule ID %d: %v", schedule.ID, err)
}
if err := a.scheduler.ScheduleJob(schedule); err != nil {
return fmt.Errorf("schedule updated but failed to restart job: %w", err)
}
}
return nil
}
// DeleteSchedule deletes a schedule by ID
func (a *App) DeleteSchedule(id int) error {
// Stop the job first
if err := a.scheduler.StopJob(id); err != nil {
log.Printf("Failed to stop job for schedule ID %d: %v", id, err)
}
// Delete from database
return a.db.DeleteSchedule(id)
}
// ToggleSchedule toggles the active state of a schedule
func (a *App) ToggleSchedule(id int, isActive bool) error {
schedule, err := a.db.GetScheduleByID(id)
if err != nil {
return fmt.Errorf("failed to get schedule: %w", err)
}
schedule.IsActive = isActive
return a.UpdateSchedule(schedule)
}
// CancelSchedule cancels a schedule permanently
func (a *App) CancelSchedule(id int) error {
// First get the schedule
schedule, err := a.db.GetScheduleByID(id)
if err != nil {
return fmt.Errorf("failed to get schedule: %w", err)
}
// Stop the job
if err := a.scheduler.StopJob(id); err != nil {
log.Printf("Failed to stop job for schedule ID %d: %v", id, err)
}
// Update to inactive status
schedule.IsActive = false
return a.db.UpdateSchedule(schedule)
}
// Logs methods
// GetExecutionLogsByAPIID returns execution logs for an API
func (a *App) GetExecutionLogsByAPIID(apiID int, limit int) ([]models.ExecutionLog, error) {
return a.db.GetExecutionLogsByAPIID(apiID, limit)
}
// GetAllExecutionLogs returns all execution logs with pagination
func (a *App) GetAllExecutionLogs(page, pageSize int) ([]models.ExecutionLog, error) {
return a.db.GetAllExecutionLogs(page, pageSize)
}
// GetRecentExecutions returns the most recent execution logs
func (a *App) GetRecentExecutions(limit int) ([]models.ExecutionLog, error) {
return a.db.GetRecentExecutions(limit)
}
// ExecuteAPIManually executes an API immediately (run now)
func (a *App) ExecuteAPIManually(apiID int) error {
return a.scheduler.ExecuteAPIManually(apiID)
}