File size: 2,162 Bytes
d6f631f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package entity

import (
	"errors"
	"fmt"
	"time"

	"github.com/openmeterio/openmeter/pkg/models"
)

// ProgressID is the identifier for a progress
type ProgressID struct {
	models.NamespacedModel `json:"namespace,inline"`
	ID                     string
}

func (a *ProgressID) Validate() error {
	var errs []error

	if err := a.NamespacedModel.Validate(); err != nil {
		errs = append(errs, fmt.Errorf("namespaced model: %w", err))
	}

	if a.ID == "" {
		errs = append(errs, errors.New("id is required"))
	}

	return errors.Join(errs...)
}

// Progress represents the tracking details of an operation
type Progress struct {
	ProgressID `json:"id"`

	// Success is the number of items that succeeded
	Success uint64 `json:"success"`
	// Failed is the number of items that failed
	Failed uint64 `json:"failed"`
	// The total number of items to process
	Total uint64 `json:"total"`
	// The time the progress was last updated
	UpdatedAt time.Time `json:"updatedAt"`
}

func (a *Progress) Validate() error {
	var errs []error

	if err := a.ProgressID.Validate(); err != nil {
		errs = append(errs, fmt.Errorf("progress id: %w", err))
	}

	if a.Success+a.Failed > a.Total {
		errs = append(errs, errors.New("success and failed must be less than or equal to total"))
	}

	if a.Total == 0 && (a.Success > 0 || a.Failed > 0) {
		errs = append(errs, errors.New("success and failed must be zero when total is zero"))
	}

	if a.UpdatedAt.IsZero() {
		errs = append(errs, errors.New("updated at is required"))
	}

	return errors.Join(errs...)
}

// Get progress is the input for the GetProgress method
type GetProgressInput struct {
	ProgressID
}

func (a *GetProgressInput) Validate() error {
	var errs []error

	if err := a.ProgressID.Validate(); err != nil {
		errs = append(errs, fmt.Errorf("progress id: %w", err))
	}

	return errors.Join(errs...)
}

// UpsertProgressInput is the input for the UpsertProgress method
type UpsertProgressInput struct {
	Progress
}

func (a *UpsertProgressInput) Validate() error {
	var errs []error

	if err := a.Progress.Validate(); err != nil {
		errs = append(errs, fmt.Errorf("progress: %w", err))
	}

	return errors.Join(errs...)
}