-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.go
More file actions
124 lines (106 loc) · 3.23 KB
/
Copy pathapp.go
File metadata and controls
124 lines (106 loc) · 3.23 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
package main
import (
"context"
"errors"
"loadcell/engine"
"loadcell/importers"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App is the Wails-bound surface for the frontend.
type App struct {
ctx context.Context
engine *engine.Engine
requests *requestStore
runs *runStore
flows *flowStore
sample *sampleCache
}
// NewApp creates a new App application struct.
func NewApp() *App {
return &App{
engine: engine.New(),
requests: newRequestStore(),
runs: newRunStore(),
flows: newFlowStore(),
sample: newSampleCache(),
}
}
// startup is called when the app starts. The context is saved so we can call
// the Wails runtime methods.
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// StartTest validates cfg, starts the engine, and wires live metrics to a
// Wails event named "metrics". Returns an error if cfg is invalid or a test
// is already running.
func (a *App) StartTest(cfg engine.Config) error {
if a.ctx == nil {
return errors.New("app not started")
}
a.engine.OnMetrics(func(m engine.Metrics) {
runtime.EventsEmit(a.ctx, "metrics", m)
})
return a.engine.Start(cfg)
}
// StopTest cancels a running test. No-op if nothing is running.
func (a *App) StopTest() {
a.engine.Stop()
}
// ListRequests returns saved request templates, newest first.
func (a *App) ListRequests() ([]SavedRequest, error) {
return a.requests.List()
}
// SaveRequest creates (when ID is empty) or updates the given request, and
// returns the persisted version with timestamps and ID populated.
func (a *App) SaveRequest(req SavedRequest) (SavedRequest, error) {
return a.requests.Upsert(req)
}
// DeleteRequest removes a saved request by ID. Missing ID is not an error.
func (a *App) DeleteRequest(id string) error {
if id == "" {
return errors.New("id is required")
}
return a.requests.Delete(id)
}
// ListRuns returns persisted load-test runs, newest first.
func (a *App) ListRuns() ([]SavedRun, error) {
return a.runs.List()
}
// SaveRun persists a completed run and returns it with id/startedAt filled.
func (a *App) SaveRun(r SavedRun) (SavedRun, error) {
return a.runs.Save(r)
}
// ImportRun parses load-test results produced by an external tool (k6,
// vegeta, ...), persists the resulting run, and returns it. name is used as a
// fallback display name (typically the source filename). The format is
// auto-detected from contents.
func (a *App) ImportRun(name string, contents string) (SavedRun, error) {
run, err := importers.ImportRun(name, []byte(contents))
if err != nil {
return SavedRun{}, err
}
return a.runs.Save(run)
}
// DeleteRun removes a persisted run by ID.
func (a *App) DeleteRun(id string) error {
if id == "" {
return errors.New("id is required")
}
return a.runs.Delete(id)
}
// ListFlows returns saved flows newest-first.
func (a *App) ListFlows() ([]SavedFlow, error) {
return a.flows.List()
}
// SaveFlow creates (when ID is empty) or updates the given flow, and
// returns the persisted version with timestamps and ID populated.
func (a *App) SaveFlow(f SavedFlow) (SavedFlow, error) {
return a.flows.Upsert(f)
}
// DeleteFlow removes a saved flow by ID.
func (a *App) DeleteFlow(id string) error {
if id == "" {
return errors.New("id is required")
}
return a.flows.Delete(id)
}