/
run.go
267 lines (244 loc) · 7.01 KB
/
run.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
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
package fixtures
import (
"context"
"fmt"
"strings"
"github.com/google/uuid"
"github.com/rotisserie/eris"
"gorm.io/gorm"
"github.com/G-Research/fasttrackml/pkg/api/mlflow/dao/models"
"github.com/G-Research/fasttrackml/pkg/api/mlflow/dao/repositories"
)
// RunFixtures represents data fixtures object.
type RunFixtures struct {
baseFixtures
runRepository repositories.RunRepositoryProvider
tagRepository repositories.TagRepositoryProvider
metricRepository repositories.MetricRepositoryProvider
}
// NewRunFixtures creates new instance of RunFixtures.
func NewRunFixtures(db *gorm.DB) (*RunFixtures, error) {
return &RunFixtures{
baseFixtures: baseFixtures{db: db},
runRepository: repositories.NewRunRepository(db),
tagRepository: repositories.NewTagRepository(db),
metricRepository: repositories.NewMetricRepository(db),
}, nil
}
// CreateRun creates a new test Run.
func (f RunFixtures) CreateRun(
ctx context.Context, run *models.Run,
) (*models.Run, error) {
if err := f.runRepository.Create(ctx, run); err != nil {
return nil, eris.Wrap(err, "error creating test run")
}
return run, nil
}
// ArchiveRun archive existing runs by their ids.
func (f RunFixtures) ArchiveRun(ctx context.Context, namespaceID uint, ids []string) error {
return f.runRepository.ArchiveBatch(ctx, namespaceID, ids)
}
// UpdateRun updates existing Run.
func (f RunFixtures) UpdateRun(
ctx context.Context, run *models.Run,
) error {
if err := f.runRepository.Update(ctx, run); err != nil {
return eris.Wrap(err, "error updating test run")
}
return nil
}
// ArchiveRuns soft-deletes existing Runs.
func (f RunFixtures) ArchiveRuns(
ctx context.Context, namespaceID uint, runIDs []string,
) error {
if err := f.runRepository.ArchiveBatch(ctx, namespaceID, runIDs); err != nil {
return eris.Wrap(err, "error archiving runs")
}
return nil
}
// CreateExampleRun creates one example run belonging to the experiment, with tags and metrics.
func (f RunFixtures) CreateExampleRun(
ctx context.Context, exp *models.Experiment,
) (*models.Run, error) {
runs, err := f.CreateExampleRuns(ctx, exp, 1)
if err != nil {
return nil, err
}
return runs[0], err
}
// CreateExampleRuns creates some example runs belonging to the experiment, with tags and metrics.
func (f RunFixtures) CreateExampleRuns(
ctx context.Context, exp *models.Experiment, num int,
) ([]*models.Run, error) {
var runs []*models.Run
// create runs for the experiment
for i := 0; i < num; i++ {
run := &models.Run{
ID: strings.ReplaceAll(uuid.New().String(), "-", ""),
Name: fmt.Sprintf("TestRun_%d", i),
Status: models.StatusRunning,
SourceType: "JOB",
ExperimentID: *exp.ID,
ArtifactURI: "artifact_uri",
LifecycleStage: models.LifecycleStageActive,
}
run, err := f.CreateRun(ctx, run)
if err != nil {
return nil, err
}
run.Experiment = *exp
tag := models.Tag{
Key: "my tag key",
Value: "my tag value",
RunID: run.ID,
}
err = f.CreateTag(ctx, tag)
if err != nil {
return nil, err
}
run.Tags = []models.Tag{tag}
err = f.CreateMetrics(ctx, run, 2)
if err != nil {
return nil, err
}
err = f.CreateParams(ctx, run, 2)
if err != nil {
return nil, err
}
runs = append(runs, run)
}
return runs, nil
}
// GetRun returns run by requested run id.
func (f RunFixtures) GetRun(ctx context.Context, runID string) (*models.Run, error) {
run, err := f.runRepository.GetByID(ctx, runID)
if err != nil {
return nil, eris.Wrap(err, "error getting test run")
}
return run, nil
}
// GetRuns fetches all runs for an experiment.
func (f RunFixtures) GetRuns(
ctx context.Context, experimentID int32,
) ([]models.Run, error) {
var runs []models.Run
if err := f.db.WithContext(ctx).Where(
"experiment_id = ?", experimentID,
).Order(
"start_time desc",
).Find(
&runs,
).Error; err != nil {
return nil, eris.Wrapf(err, "error getting `run` entities by experiment id: %v", experimentID)
}
return runs, nil
}
// FindMinMaxRowNums finds min and max rownum for an experiment's runs.
func (f RunFixtures) FindMinMaxRowNums(
ctx context.Context, experimentID int32,
) (int64, int64, error) {
runs, err := f.GetRuns(ctx, experimentID)
if err != nil {
return 0, 0, eris.Wrap(err, "error fetching test runs")
}
var min, max models.RowNum
for _, run := range runs {
if run.RowNum < min {
min = run.RowNum
}
if run.RowNum > max {
max = run.RowNum
}
}
return int64(min), int64(max), nil
}
// CreateTag creates a new Tag for a run.
func (f RunFixtures) CreateTag(
ctx context.Context, tag models.Tag,
) error {
if err := f.tagRepository.CreateRunTagWithTransaction(ctx, f.db, tag.RunID, tag.Key, tag.Value); err != nil {
return eris.Wrap(err, "error creating run tag")
}
return nil
}
// CreateMetric creates a new test Metric.
func (f RunFixtures) CreateMetric(ctx context.Context, metric *models.Metric) error {
defaultContext := models.DefaultContext
if err := f.baseFixtures.db.WithContext(
ctx,
).FirstOrCreate(
&defaultContext, defaultContext,
).Error; err != nil {
return eris.Wrap(err, "error creating or finding default context")
}
if metric.Context.Json == nil {
metric.ContextID = defaultContext.ID
}
if err := f.baseFixtures.db.WithContext(ctx).Create(metric).Error; err != nil {
return eris.Wrap(err, "error creating test metric")
}
return nil
}
// CreateMetrics creates some example metrics for a Run, up to count.
func (f RunFixtures) CreateMetrics(
ctx context.Context, run *models.Run, count int,
) error {
defaultContext := models.DefaultContext
if err := f.baseFixtures.db.WithContext(
ctx,
).FirstOrCreate(
&defaultContext, defaultContext,
).Error; err != nil {
return eris.Wrap(err, "error creating or finding default context")
}
for i := 1; i <= count; i++ {
// create test `metric` and test `latest metric` and connect to run.
for iter := 1; iter <= count; iter++ {
if err := f.baseFixtures.db.WithContext(
ctx,
).Create(&models.Metric{
Key: fmt.Sprintf("key%d", i),
Value: 123.1 + float64(iter),
Timestamp: 1234567890 + int64(iter),
RunID: run.ID,
Step: int64(iter),
IsNan: false,
Iter: int64(iter),
ContextID: defaultContext.ID,
}).Error; err != nil {
return err
}
}
if err := f.baseFixtures.db.WithContext(
ctx,
).Create(&models.LatestMetric{
Key: fmt.Sprintf("key%d", i),
Value: 123.1 + float64(count),
Timestamp: 1234567890 + int64(count),
Step: int64(count),
IsNan: false,
RunID: run.ID,
LastIter: int64(count),
ContextID: defaultContext.ID,
}).Error; err != nil {
return err
}
}
return nil
}
// CreateParams creates some example params for a Run, up to count.
func (f RunFixtures) CreateParams(
ctx context.Context, run *models.Run, count int,
) error {
for i := 1; i <= count; i++ {
err := f.baseFixtures.db.WithContext(ctx).Create(&models.Param{
Key: fmt.Sprintf("key%d", i),
Value: fmt.Sprintf("val%d", i),
RunID: run.ID,
}).Error
if err != nil {
return err
}
}
return nil
}