Skip to content

Commit

Permalink
PMM-12460 Add dump model, implement ListDumps method
Browse files Browse the repository at this point in the history
  • Loading branch information
artemgavrilov committed Sep 26, 2023
1 parent e66ba6b commit 9201b52
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 0 deletions.
76 changes: 76 additions & 0 deletions managed/models/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package models

import (
"time"

"gopkg.in/reform.v1"
)

//go:generate ../../bin/reform

type DumpStatus string

const (
DumpStatusInProgress = DumpStatus("in_progress")
DumpStatusSuccess = DumpStatus("success")
DumpStatusError = DumpStatus("error")
)

// Validate validates Dumps status.
func (ds DumpStatus) Validate() error {
switch ds {
case DumpStatusInProgress:
case DumpStatusSuccess:
case DumpStatusError:
default:
return NewInvalidArgumentError("invalid dump status '%s'", ds)
}

return nil
}

// Pointer returns a pointer to status value.
func (ds DumpStatus) Pointer() *DumpStatus {
return &ds
}

// Dump represents pmm dump artifact.
//
//reform:dumps
type Dump struct {
ID string `reform:"id,pk"`
Status DumpStatus `reform:"status"`
NodeIDs []string `reform:"node_ids"`
StartTime time.Time `reform:"start_time"`
EndTime time.Time `reform:"end_time"`
CreatedAt time.Time `reform:"created_at"`
UpdatedAt time.Time `reform:"updated_at"`
}

// BeforeInsert implements reform.BeforeInserter interface.
func (d *Dump) BeforeInsert() error {
now := Now()
d.CreatedAt = now
d.UpdatedAt = now
return nil
}

// BeforeUpdate implements reform.BeforeUpdater interface.
func (d *Dump) BeforeUpdate() error {
d.UpdatedAt = Now()
return nil
}

// AfterFind implements reform.AfterFinder interface.
func (d *Dump) AfterFind() error {
d.CreatedAt = d.CreatedAt.UTC()
d.UpdatedAt = d.UpdatedAt.UTC()
return nil
}

// check interfaces.
var (
_ reform.BeforeInserter = (*Dump)(nil)
_ reform.BeforeUpdater = (*Dump)(nil)
_ reform.AfterFinder = (*Dump)(nil)
)
84 changes: 84 additions & 0 deletions managed/models/dump_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package models

import (
"fmt"
"strings"

"github.com/pkg/errors"
"gopkg.in/reform.v1"
)

// DumpFilters represents filters for artifacts list.
type DumpFilters struct {
// Return only artifacts by specified status.
Status BackupStatus
}

// FindDumps returns dumps list sorted by creation time in DESCENDING order.
func FindDumps(q *reform.Querier, filters DumpFilters) ([]*Dump, error) {
var conditions []string
var args []interface{}
var idx int

if filters.Status != "" {
idx++
conditions = append(conditions, fmt.Sprintf("status = %s", q.Placeholder(idx)))
args = append(args, filters.Status)
}

var whereClause string
if len(conditions) != 0 {
whereClause = fmt.Sprintf("WHERE %s", strings.Join(conditions, " AND "))
}
rows, err := q.SelectAllFrom(DumpTable, fmt.Sprintf("%s ORDER BY created_at DESC", whereClause), args...)
if err != nil {
return nil, errors.Wrap(err, "failed to select dumps")
}

dumps := make([]*Dump, 0, len(rows))
for _, r := range rows {
dumps = append(dumps, r.(*Dump)) //nolint:forcetypeassert
}

return dumps, nil
}

// FindDumpByID returns dump by given ID if found, ErrNotFound if not.
func FindDumpByID(q *reform.Querier, id string) (*Dump, error) {
if id == "" {
return nil, errors.New("provided dump id is empty")
}

dump := &Dump{ID: id}
err := q.Reload(dump)
if err != nil {
if errors.Is(err, reform.ErrNoRows) {
return nil, errors.Wrapf(ErrNotFound, "dump by id '%s'", id)
}
return nil, errors.WithStack(err)
}

return dump, nil
}

// DeleteDump removes dump by ID.
func DeleteDump(q *reform.Querier, id string) error {
if _, err := FindDumpByID(q, id); err != nil {
return err
}

if err := q.Delete(&Dump{ID: id}); err != nil {
return errors.Wrapf(err, "failed to delete dump by id '%s'", id)
}
return nil
}

// IsDumpFinalStatus checks if dump status is one of the final ones.
func IsDumpFinalStatus(dumpStatus DumpStatus) bool {
switch dumpStatus {
case DumpStatusSuccess, DumpStatusError:
return true
default:
return false
}
}
161 changes: 161 additions & 0 deletions managed/models/dump_reform.go

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

Loading

0 comments on commit 9201b52

Please sign in to comment.