forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_factory.go
131 lines (106 loc) · 2.59 KB
/
job_factory.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
package db
import (
sq "github.com/Masterminds/squirrel"
"github.com/concourse/atc/db/lock"
)
//go:generate counterfeiter . JobFactory
type JobFactory interface {
VisibleJobs([]string) (Dashboard, error)
}
type jobFactory struct {
conn Conn
lockFactory lock.LockFactory
}
func NewJobFactory(conn Conn, lockFactory lock.LockFactory) JobFactory {
return &jobFactory{
conn: conn,
lockFactory: lockFactory,
}
}
func (j *jobFactory) VisibleJobs(teamNames []string) (Dashboard, error) {
rows, err := jobsQuery.
Where(sq.Eq{
"t.name": teamNames,
"j.active": true,
}).
OrderBy("j.id ASC").
RunWith(j.conn).
Query()
if err != nil {
return nil, err
}
currentTeamJobs, err := scanJobs(j.conn, j.lockFactory, rows)
if err != nil {
return nil, err
}
rows, err = jobsQuery.
Where(sq.NotEq{
"t.name": teamNames,
}).
Where(sq.Eq{
"p.public": true,
"j.active": true,
}).
OrderBy("j.id ASC").
RunWith(j.conn).
Query()
if err != nil {
return nil, err
}
otherTeamPublicJobs, err := scanJobs(j.conn, j.lockFactory, rows)
if err != nil {
return nil, err
}
jobs := append(currentTeamJobs, otherTeamPublicJobs...)
var jobIDs []int
for _, job := range jobs {
jobIDs = append(jobIDs, job.ID())
}
nextBuilds, err := j.getBuildsFrom("next_builds_per_job", jobIDs)
if err != nil {
return nil, err
}
finishedBuilds, err := j.getBuildsFrom("latest_completed_builds_per_job", jobIDs)
if err != nil {
return nil, err
}
transitionBuilds, err := j.getBuildsFrom("transition_builds_per_job", jobIDs)
if err != nil {
return nil, err
}
dashboard := Dashboard{}
for _, job := range jobs {
dashboardJob := DashboardJob{Job: job}
if nextBuild, found := nextBuilds[job.ID()]; found {
dashboardJob.NextBuild = nextBuild
}
if finishedBuild, found := finishedBuilds[job.ID()]; found {
dashboardJob.FinishedBuild = finishedBuild
}
if transitionBuild, found := transitionBuilds[job.ID()]; found {
dashboardJob.TransitionBuild = transitionBuild
}
dashboard = append(dashboard, dashboardJob)
}
return dashboard, nil
}
func (j *jobFactory) getBuildsFrom(view string, jobIDs []int) (map[int]Build, error) {
rows, err := buildsQuery.
From(view + " b").
Where(sq.Eq{"j.id": jobIDs}).
RunWith(j.conn).Query()
if err != nil {
return nil, err
}
defer Close(rows)
builds := make(map[int]Build)
for rows.Next() {
build := &build{conn: j.conn, lockFactory: j.lockFactory}
err := scanBuild(build, rows, j.conn.EncryptionStrategy())
if err != nil {
return nil, err
}
builds[build.JobID()] = build
}
return builds, nil
}