-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulnerability.sql.go
246 lines (226 loc) · 6 KB
/
vulnerability.sql.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Code generated by sqlc. DO NOT EDIT.
// source: vulnerability.sql
package gensql
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/nais/api/internal/slug"
)
const createDependencytrackProject = `-- name: CreateDependencytrackProject :exec
INSERT INTO dependencytrack_projects (id, environment, team_slug, app)
VALUES ($1, $2, $3, $4)
ON CONFLICT DO NOTHING
`
type CreateDependencytrackProjectParams struct {
ID uuid.UUID
Environment string
TeamSlug slug.Slug
App string
}
func (q *Queries) CreateDependencytrackProject(ctx context.Context, arg CreateDependencytrackProjectParams) error {
_, err := q.db.Exec(ctx, createDependencytrackProject,
arg.ID,
arg.Environment,
arg.TeamSlug,
arg.App,
)
return err
}
const dailyVulnerabilityForTeam = `-- name: DailyVulnerabilityForTeam :many
SELECT
sum(critical) AS critical,
sum(high) AS high,
sum(medium) AS medium,
sum(low) AS low,
sum(unassigned) AS unassigned,
sum(risk_score) AS risk_score,
COUNT(DISTINCT dependencytrack_project_id) AS count,
date
FROM
vulnerability_metrics
WHERE
date >= $1::date
AND date <= $2::date
AND dependencytrack_project_id IN (
SELECT id
FROM dependencytrack_projects
WHERE team_slug = $3
)
GROUP BY
date
ORDER BY
date ASC
`
type DailyVulnerabilityForTeamParams struct {
FromDate pgtype.Date
ToDate pgtype.Date
TeamSlug slug.Slug
}
type DailyVulnerabilityForTeamRow struct {
Critical int64
High int64
Medium int64
Low int64
Unassigned int64
RiskScore int64
Count int64
Date pgtype.Date
}
// DailyVulnerabilityForTeam will return the metrics for the given team from first to last date.
func (q *Queries) DailyVulnerabilityForTeam(ctx context.Context, arg DailyVulnerabilityForTeamParams) ([]*DailyVulnerabilityForTeamRow, error) {
rows, err := q.db.Query(ctx, dailyVulnerabilityForTeam, arg.FromDate, arg.ToDate, arg.TeamSlug)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*DailyVulnerabilityForTeamRow{}
for rows.Next() {
var i DailyVulnerabilityForTeamRow
if err := rows.Scan(
&i.Critical,
&i.High,
&i.Medium,
&i.Low,
&i.Unassigned,
&i.RiskScore,
&i.Count,
&i.Date,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const dailyVulnerabilityForTeamAndEnvironment = `-- name: DailyVulnerabilityForTeamAndEnvironment :many
SELECT
sum(critical) AS critical,
sum(high) AS high,
sum(medium) AS medium,
sum(low) AS low,
sum(unassigned) AS unassigned,
sum(risk_score) AS risk_score,
COUNT(DISTINCT dependencytrack_project_id) AS count,
date
FROM
vulnerability_metrics
WHERE
date >= $1::date
AND date <= $2::date
AND dependencytrack_project_id IN (
SELECT id
FROM dependencytrack_projects
WHERE
team_slug = $3
AND environment = $4
)
GROUP BY
date
ORDER BY
date ASC
`
type DailyVulnerabilityForTeamAndEnvironmentParams struct {
FromDate pgtype.Date
ToDate pgtype.Date
TeamSlug slug.Slug
Environment string
}
type DailyVulnerabilityForTeamAndEnvironmentRow struct {
Critical int64
High int64
Medium int64
Low int64
Unassigned int64
RiskScore int64
Count int64
Date pgtype.Date
}
// DailyVulnerabilityForTeamAndEnvironment will return the metrics for the given team and environment from first to last date.
func (q *Queries) DailyVulnerabilityForTeamAndEnvironment(ctx context.Context, arg DailyVulnerabilityForTeamAndEnvironmentParams) ([]*DailyVulnerabilityForTeamAndEnvironmentRow, error) {
rows, err := q.db.Query(ctx, dailyVulnerabilityForTeamAndEnvironment,
arg.FromDate,
arg.ToDate,
arg.TeamSlug,
arg.Environment,
)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*DailyVulnerabilityForTeamAndEnvironmentRow{}
for rows.Next() {
var i DailyVulnerabilityForTeamAndEnvironmentRow
if err := rows.Scan(
&i.Critical,
&i.High,
&i.Medium,
&i.Low,
&i.Unassigned,
&i.RiskScore,
&i.Count,
&i.Date,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const vulnerabilityMetricsDateRangeForTeam = `-- name: VulnerabilityMetricsDateRangeForTeam :one
SELECT
MIN(date)::date AS from_date,
MAX(date)::date AS to_date
FROM
vulnerability_metrics
WHERE
dependencytrack_project_id IN (
SELECT id
FROM dependencytrack_projects
WHERE team_slug = $1
)
`
type VulnerabilityMetricsDateRangeForTeamRow struct {
FromDate pgtype.Date
ToDate pgtype.Date
}
// VulnerabilityMetricsDateRangeForTeam will return the first and last date that has a metrics.
func (q *Queries) VulnerabilityMetricsDateRangeForTeam(ctx context.Context, teamSlug slug.Slug) (*VulnerabilityMetricsDateRangeForTeamRow, error) {
row := q.db.QueryRow(ctx, vulnerabilityMetricsDateRangeForTeam, teamSlug)
var i VulnerabilityMetricsDateRangeForTeamRow
err := row.Scan(&i.FromDate, &i.ToDate)
return &i, err
}
const vulnerabilityMetricsMaxDate = `-- name: VulnerabilityMetricsMaxDate :one
SELECT
MAX(date)::date AS to_date
FROM
vulnerability_metrics
WHERE
dependencytrack_project_id = ANY(
SELECT id
FROM dependencytrack_projects
WHERE
environment = $1
AND app = $2
AND team_slug = $3
)
`
type VulnerabilityMetricsMaxDateParams struct {
Environment string
App string
TeamSlug slug.Slug
}
// VulnerabilityMetricsMaxDate will return the last date that has a metrics for the given project id.
func (q *Queries) VulnerabilityMetricsMaxDate(ctx context.Context, arg VulnerabilityMetricsMaxDateParams) (pgtype.Date, error) {
row := q.db.QueryRow(ctx, vulnerabilityMetricsMaxDate, arg.Environment, arg.App, arg.TeamSlug)
var to_date pgtype.Date
err := row.Scan(&to_date)
return to_date, err
}