-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
205 lines (183 loc) · 4.77 KB
/
batch.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Code generated by sqlc. DO NOT EDIT.
// source: batch.go
package gensql
import (
"context"
"errors"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/nais/api/internal/slug"
)
var (
ErrBatchAlreadyClosed = errors.New("batch already closed")
)
const costUpsert = `-- name: CostUpsert :batchexec
INSERT INTO cost (environment, team_slug, app, cost_type, date, daily_cost)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT ON CONSTRAINT daily_cost_key DO
UPDATE SET daily_cost = EXCLUDED.daily_cost
`
type CostUpsertBatchResults struct {
br pgx.BatchResults
tot int
closed bool
}
type CostUpsertParams struct {
Environment *string
TeamSlug slug.Slug
App string
CostType string
Date pgtype.Date
DailyCost float32
}
// CostUpsert will insert or update a cost record. If there is a conflict on the daily_cost_key constrant, the
// daily_cost column will be updated.
func (q *Queries) CostUpsert(ctx context.Context, arg []CostUpsertParams) *CostUpsertBatchResults {
batch := &pgx.Batch{}
for _, a := range arg {
vals := []interface{}{
a.Environment,
a.TeamSlug,
a.App,
a.CostType,
a.Date,
a.DailyCost,
}
batch.Queue(costUpsert, vals...)
}
br := q.db.SendBatch(ctx, batch)
return &CostUpsertBatchResults{br, len(arg), false}
}
func (b *CostUpsertBatchResults) Exec(f func(int, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
if b.closed {
if f != nil {
f(t, ErrBatchAlreadyClosed)
}
continue
}
_, err := b.br.Exec()
if f != nil {
f(t, err)
}
}
}
func (b *CostUpsertBatchResults) Close() error {
b.closed = true
return b.br.Close()
}
const resourceUtilizationUpsert = `-- name: ResourceUtilizationUpsert :batchexec
INSERT INTO resource_utilization_metrics (timestamp, environment, team_slug, app, resource_type, usage, request)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT ON CONSTRAINT resource_utilization_metric DO NOTHING
`
type ResourceUtilizationUpsertBatchResults struct {
br pgx.BatchResults
tot int
closed bool
}
type ResourceUtilizationUpsertParams struct {
Timestamp pgtype.Timestamptz
Environment string
TeamSlug slug.Slug
App string
ResourceType ResourceType
Usage float64
Request float64
}
// ResourceUtilizationUpsert will insert or update resource utilization records.
func (q *Queries) ResourceUtilizationUpsert(ctx context.Context, arg []ResourceUtilizationUpsertParams) *ResourceUtilizationUpsertBatchResults {
batch := &pgx.Batch{}
for _, a := range arg {
vals := []interface{}{
a.Timestamp,
a.Environment,
a.TeamSlug,
a.App,
a.ResourceType,
a.Usage,
a.Request,
}
batch.Queue(resourceUtilizationUpsert, vals...)
}
br := q.db.SendBatch(ctx, batch)
return &ResourceUtilizationUpsertBatchResults{br, len(arg), false}
}
func (b *ResourceUtilizationUpsertBatchResults) Exec(f func(int, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
if b.closed {
if f != nil {
f(t, ErrBatchAlreadyClosed)
}
continue
}
_, err := b.br.Exec()
if f != nil {
f(t, err)
}
}
}
func (b *ResourceUtilizationUpsertBatchResults) Close() error {
b.closed = true
return b.br.Close()
}
const vulnerabilityMetricsUpsert = `-- name: VulnerabilityMetricsUpsert :batchexec
INSERT INTO vulnerability_metrics (date, dependencytrack_project_id, critical, high, medium, low, unassigned, risk_score)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT ON CONSTRAINT vulnerability_metric DO NOTHING
`
type VulnerabilityMetricsUpsertBatchResults struct {
br pgx.BatchResults
tot int
closed bool
}
type VulnerabilityMetricsUpsertParams struct {
Date pgtype.Date
DependencytrackProjectID uuid.UUID
Critical int32
High int32
Medium int32
Low int32
Unassigned int32
RiskScore float64
}
func (q *Queries) VulnerabilityMetricsUpsert(ctx context.Context, arg []VulnerabilityMetricsUpsertParams) *VulnerabilityMetricsUpsertBatchResults {
batch := &pgx.Batch{}
for _, a := range arg {
vals := []interface{}{
a.Date,
a.DependencytrackProjectID,
a.Critical,
a.High,
a.Medium,
a.Low,
a.Unassigned,
a.RiskScore,
}
batch.Queue(vulnerabilityMetricsUpsert, vals...)
}
br := q.db.SendBatch(ctx, batch)
return &VulnerabilityMetricsUpsertBatchResults{br, len(arg), false}
}
func (b *VulnerabilityMetricsUpsertBatchResults) Exec(f func(int, error)) {
defer b.br.Close()
for t := 0; t < b.tot; t++ {
if b.closed {
if f != nil {
f(t, ErrBatchAlreadyClosed)
}
continue
}
_, err := b.br.Exec()
if f != nil {
f(t, err)
}
}
}
func (b *VulnerabilityMetricsUpsertBatchResults) Close() error {
b.closed = true
return b.br.Close()
}