Skip to content

Commit

Permalink
PMM-12460 Make start and end time optional (nullable)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Nov 2, 2023
1 parent f052600 commit f20a686
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 41 deletions.
4 changes: 2 additions & 2 deletions managed/models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,8 @@ var databaseSchema = [][]string{
id VARCHAR NOT NULL,
status VARCHAR NOT NULL CHECK (status <> ''),
service_names VARCHAR[],
start_time TIMESTAMP NOT NULL,
end_time TIMESTAMP NOT NULL,
start_time TIMESTAMP,
end_time TIMESTAMP,
export_qan BOOLEAN NOT NULL,
ignore_load BOOLEAN NOT NULL,
created_at TIMESTAMP NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions managed/models/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type Dump struct {
ID string `reform:"id,pk"`
Status DumpStatus `reform:"status"`
ServiceNames pq.StringArray `reform:"service_names"`
StartTime time.Time `reform:"start_time"`
EndTime time.Time `reform:"end_time"`
StartTime *time.Time `reform:"start_time"`
EndTime *time.Time `reform:"end_time"`
ExportQAN bool `reform:"export_qan"`
IgnoreLoad bool `reform:"ignore_load"`
CreatedAt time.Time `reform:"created_at"`
Expand Down
16 changes: 14 additions & 2 deletions managed/models/dump_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,25 @@ type DumpFilters struct {

type CreateDumpParams struct {
ServiceNames []string
StartTime time.Time
EndTime time.Time
StartTime *time.Time
EndTime *time.Time
ExportQAN bool
IgnoreLoad bool
}

func (p *CreateDumpParams) Validate() error {
if p.StartTime != nil && p.EndTime != nil && p.StartTime.After(*p.EndTime) {
return errors.Errorf("dump start time can't be greater than end time")
}

return nil
}

func CreateDump(q *reform.Querier, params CreateDumpParams) (*Dump, error) {
if err := params.Validate(); err != nil {
return nil, errors.Wrap(err, "invalid dump creation params")
}

id := uuid.New().String()
if err := checkUniqueDumpID(q, id); err != nil {
return nil, err
Expand Down
68 changes: 45 additions & 23 deletions managed/models/dump_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,68 @@ func TestDumps(t *testing.T) {
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, tx.Rollback())
require.NoError(t, sqlDB.Close())
})

t.Run("create", func(t *testing.T) {
createDumpParams := models.CreateDumpParams{
ServiceNames: []string{"foo", "bar"},
StartTime: time.Now(),
EndTime: time.Now().Add(10 * time.Minute),
ExportQAN: false,
IgnoreLoad: true,
}
dump, err := models.CreateDump(tx.Querier, createDumpParams)
require.NoError(t, err)
assert.NotEmpty(t, dump.ID)
assert.Equal(t, models.DumpStatusInProgress, dump.Status)
assert.ElementsMatch(t, createDumpParams.ServiceNames, dump.ServiceNames)
assert.Equal(t, createDumpParams.StartTime, dump.StartTime)
assert.Equal(t, createDumpParams.EndTime, dump.EndTime)
assert.Equal(t, createDumpParams.ExportQAN, dump.ExportQAN)
assert.Equal(t, createDumpParams.IgnoreLoad, dump.IgnoreLoad)
t.Run("normal", func(t *testing.T) {
endTime := time.Now()
startTime := endTime.Add(-10 * time.Minute)

createDumpParams := models.CreateDumpParams{
ServiceNames: []string{"foo", "bar"},
StartTime: &startTime,
EndTime: &endTime,
ExportQAN: false,
IgnoreLoad: true,
}
dump, err := models.CreateDump(tx.Querier, createDumpParams)
require.NoError(t, err)
assert.NotEmpty(t, dump.ID)
assert.Equal(t, models.DumpStatusInProgress, dump.Status)
assert.ElementsMatch(t, createDumpParams.ServiceNames, dump.ServiceNames)
assert.Equal(t, createDumpParams.StartTime, dump.StartTime)
assert.Equal(t, createDumpParams.EndTime, dump.EndTime)
assert.Equal(t, createDumpParams.ExportQAN, dump.ExportQAN)
assert.Equal(t, createDumpParams.IgnoreLoad, dump.IgnoreLoad)
})

t.Run("invalid start and end time", func(t *testing.T) {
endTime := time.Now()
startTime := endTime.Add(10 * time.Minute)

createDumpParams := models.CreateDumpParams{
ServiceNames: []string{"foo", "bar"},
StartTime: &startTime,
EndTime: &endTime,
ExportQAN: false,
IgnoreLoad: true,
}
_, err := models.CreateDump(tx.Querier, createDumpParams)
require.EqualError(t, err, "invalid dump creation params: dump start time can't be greater than end time")
})
})

t.Run("find", func(t *testing.T) {
findTX, err := db.Begin()
require.NoError(t, err)
defer findTX.Rollback() //nolint:errcheck

endTime := time.Now()
startTime := endTime.Add(-10 * time.Minute)

dump1, err := models.CreateDump(findTX.Querier, models.CreateDumpParams{
ServiceNames: []string{"foo", "bar"},
StartTime: time.Now(),
EndTime: time.Now().Add(10 * time.Minute),
StartTime: &startTime,
EndTime: &endTime,
ExportQAN: false,
IgnoreLoad: true,
})
require.NoError(t, err)

dump2, err := models.CreateDump(findTX.Querier, models.CreateDumpParams{
ServiceNames: []string{"foo", "bar"},
StartTime: time.Now(),
EndTime: time.Now().Add(10 * time.Minute),
StartTime: &startTime,
EndTime: &endTime,
ExportQAN: false,
IgnoreLoad: true,
})
Expand All @@ -87,8 +109,8 @@ func TestDumps(t *testing.T) {

dump3, err := models.CreateDump(findTX.Querier, models.CreateDumpParams{
ServiceNames: []string{"foo", "bar"},
StartTime: time.Now(),
EndTime: time.Now().Add(10 * time.Minute),
StartTime: &startTime,
EndTime: &endTime,
ExportQAN: false,
IgnoreLoad: true,
})
Expand Down
4 changes: 2 additions & 2 deletions managed/models/dump_reform.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions managed/services/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ type Params struct {
User string
Password string
ServiceNames []string
StartTime time.Time
EndTime time.Time
StartTime *time.Time
EndTime *time.Time
ExportQAN bool
IgnoreLoad bool
}
Expand Down Expand Up @@ -123,11 +123,11 @@ func (s *Service) StartDump(params *Params) (string, error) {
pmmDumpCmd.Args = append(pmmDumpCmd.Args, fmt.Sprintf("--instance=%s", serviceName))
}

if !params.StartTime.IsZero() {
if params.StartTime != nil {
pmmDumpCmd.Args = append(pmmDumpCmd.Args, fmt.Sprintf("--start-ts=%s", params.StartTime.Format(time.RFC3339)))
}

if !params.EndTime.IsZero() {
if params.EndTime != nil {
pmmDumpCmd.Args = append(pmmDumpCmd.Args, fmt.Sprintf("--end-ts=%s", params.EndTime.Format(time.RFC3339)))
}

Expand Down
28 changes: 22 additions & 6 deletions managed/services/management/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,19 @@ func (s *Service) StartDump(ctx context.Context, req *dumpv1beta1.StartDumpReque
}

if req.StartTime != nil {
params.StartTime = req.StartTime.AsTime()
startTime := req.StartTime.AsTime()
params.StartTime = &startTime
}

if req.EndTime != nil {
params.EndTime = req.EndTime.AsTime()
endTime := req.EndTime.AsTime()
params.EndTime = &endTime
}

if params.StartTime != nil && params.EndTime != nil {
if params.StartTime.After(*params.EndTime) {
return nil, status.Error(codes.InvalidArgument, "Dump start time can't be greater enc time")
}
}

dumpID, err := s.dumpService.StartDump(params)
Expand Down Expand Up @@ -262,14 +270,22 @@ func convertDump(dump *models.Dump) (*dumpv1beta1.Dump, error) {
return nil, errors.Wrap(err, "failed to convert dump ds")
}

return &dumpv1beta1.Dump{
d := &dumpv1beta1.Dump{
DumpId: dump.ID,
Status: ds,
ServiceNames: dump.ServiceNames,
StartTime: timestamppb.New(dump.StartTime),
EndTime: timestamppb.New(dump.EndTime),
CreatedAt: timestamppb.New(dump.CreatedAt),
}, nil
}

if dump.StartTime != nil {
d.StartTime = timestamppb.New(*dump.StartTime)
}

if dump.EndTime != nil {
d.EndTime = timestamppb.New(*dump.EndTime)
}

return d, nil
}

func convertDumpStatus(status models.DumpStatus) (dumpv1beta1.DumpStatus, error) {
Expand Down

0 comments on commit f20a686

Please sign in to comment.