-
Notifications
You must be signed in to change notification settings - Fork 568
/
model.go
188 lines (162 loc) · 4.68 KB
/
model.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
package pfsdb
import (
"database/sql"
"strings"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/pachyderm/pachyderm/v2/src/pfs"
)
// A separate type is defined for safety so row ids must be explicitly cast for use in another table.
// ProjectID is the row id for a project entry in postgres.
type ProjectID uint64
// RepoID is the row id for a repo entry in postgres.
type RepoID uint64
// CommitID is the row id for a commit entry in postgres.
type CommitID uint64
// BranchID is the row id for a branch entry in postgres.
type BranchID uint64
type CreatedAtUpdatedAt struct {
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type Project struct {
ID ProjectID `db:"id"`
Name string `db:"name"`
Description string `db:"description"`
CreatedAtUpdatedAt
}
func (project *Project) Pb() *pfs.Project {
return &pfs.Project{
Name: project.Name,
}
}
func (project *Project) PbInfo() *pfs.ProjectInfo {
return &pfs.ProjectInfo{
Project: &pfs.Project{
Name: project.Name,
},
Description: project.Description,
CreatedAt: timestamppb.New(project.CreatedAt),
}
}
func (project Project) GetCreatedAtUpdatedAt() CreatedAtUpdatedAt {
return project.CreatedAtUpdatedAt
}
// Repo is a row in the pfs.repos table.
type Repo struct {
ID RepoID `db:"id"`
Project Project `db:"project"`
Name string `db:"name"`
Type string `db:"type"`
Description string `db:"description"`
CreatedAtUpdatedAt
BranchesNames string `db:"branches"`
}
func (repo Repo) GetCreatedAtUpdatedAt() CreatedAtUpdatedAt {
return repo.CreatedAtUpdatedAt
}
func (repo *Repo) Pb() *pfs.Repo {
return &pfs.Repo{
Name: repo.Name,
Type: repo.Type,
Project: repo.Project.Pb(),
}
}
func (repo *Repo) PbInfo() (*pfs.RepoInfo, error) {
branches, err := parseBranches(repo)
if err != nil {
return nil, err
}
return &pfs.RepoInfo{
Repo: repo.Pb(),
Description: repo.Description,
Branches: branches,
Created: timestamppb.New(repo.CreatedAt),
}, nil
}
func parseBranches(repo *Repo) ([]*pfs.Branch, error) {
var branches []*pfs.Branch
if repo.BranchesNames == noBranches {
return branches, nil
}
// after aggregation, braces, quotes, and leading hex prefixes need to be removed from the encoded branch strings.
for _, branchName := range strings.Split(strings.Trim(repo.BranchesNames, "{}"), ",") {
branch := &pfs.Branch{
Name: branchName,
Repo: repo.Pb(),
}
branches = append(branches, branch)
}
return branches, nil
}
type Commit struct {
ID CommitID `db:"int_id"`
CommitSetID string `db:"commit_set_id"`
CommitID string `db:"commit_id"`
Origin string `db:"origin"`
Description string `db:"description"`
StartTime sql.NullTime `db:"start_time"`
FinishingTime sql.NullTime `db:"finishing_time"`
FinishedTime sql.NullTime `db:"finished_time"`
CompactingTime sql.NullInt64 `db:"compacting_time_s"`
ValidatingTime sql.NullInt64 `db:"validating_time_s"`
Error string `db:"error"`
Size int64 `db:"size"`
// BranchName is used to derive the BranchID in commit related queries.
BranchName sql.NullString `db:"branch_name"`
BranchID sql.NullInt64 `db:"branch_id"`
Repo Repo `db:"repo"`
CreatedAtUpdatedAt
}
func (commit Commit) GetCreatedAtUpdatedAt() CreatedAtUpdatedAt {
return commit.CreatedAtUpdatedAt
}
func (commit *Commit) Pb() *pfs.Commit {
pb := &pfs.Commit{
Id: commit.CommitSetID,
Repo: commit.Repo.Pb(),
}
if commit.BranchName.Valid {
pb.Branch = &pfs.Branch{
Repo: pb.Repo,
Name: commit.BranchName.String,
}
}
return pb
}
// Branch is a row in the pfs.branches table.
type Branch struct {
ID BranchID `db:"id"`
Head Commit `db:"head"`
Repo Repo `db:"repo"`
Name string `db:"name"`
CreatedAtUpdatedAt
}
func (branch Branch) GetCreatedAtUpdatedAt() CreatedAtUpdatedAt {
return branch.CreatedAtUpdatedAt
}
func (branch *Branch) Pb() *pfs.Branch {
return &pfs.Branch{
Name: branch.Name,
Repo: branch.Repo.Pb(),
}
}
type BranchTrigger struct {
FromBranch Branch `db:"from_branch"`
ToBranch Branch `db:"to_branch"`
CronSpec string `db:"cron_spec"`
RateLimitSpec string `db:"rate_limit_spec"`
Size string `db:"size"`
NumCommits int64 `db:"num_commits"`
AllConditions bool `db:"all_conditions"`
}
func (trigger *BranchTrigger) Pb() *pfs.Trigger {
return &pfs.Trigger{
Branch: trigger.ToBranch.Name,
CronSpec: trigger.CronSpec,
RateLimitSpec: trigger.RateLimitSpec,
Size: trigger.Size,
Commits: trigger.NumCommits,
All: trigger.AllConditions,
}
}