Skip to content

Commit 0070969

Browse files
authored
feat: add reports, config, scm pagination (#246)
* feat: add report endpoint pagination * feat: add config endpoint pagination * chore: rename getPaginationParam to be more explicite * ci: run test in CI * chore: by default monitor last week report * fix: groupe route definition * feat: add scm endpoint pagination * chore: remove duplicated test instruction * fix: add missing operator
1 parent 48914c0 commit 0070969

12 files changed

Lines changed: 579 additions & 201 deletions

File tree

.github/workflows/go.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ jobs:
3737
install-only: true
3838
- name: Show GoReleaser version
3939
run: goreleaser --version
40+
- name: Test
41+
run: make test
4042
- name: Build
4143
run: make build
4244
# Codecov should only be updated if make test is executed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ app.build: ## Build application locally
1111
go build -o bin/udash \
1212
-ldflags='-w -s -X "github.com/updatecli/udash/pkg/version.BuildTime=$(shell date)" -X "github.com/updatecli/udash/pkg/version.GoVersion=$(shell go version)" -X "github.com/updatecli/udash/pkg/version.Version=42"'
1313

14+
.PHONY: server.start
1415
server.start: app.build ## Start application locally
1516
./bin/udash server start --debug
1617

@@ -22,6 +23,7 @@ build: ## Build updatecli as a "dirty snapshot" (no tag, no release, but all OS/
2223
build.all: ## Build updatecli for "release" (tag or release and all OS/arch combinations)
2324
goreleaser --clean --skip=publish,sign
2425

26+
.PHONY: clean
2527
clean: ## Clean go test cache
2628
go clean -testcache
2729

@@ -52,7 +54,6 @@ db.delete: ## Delete development database
5254
help: ## Show this Makefile's help
5355
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
5456

55-
5657
.PHONY: lint
5758
lint: ## Execute the Golang's linters on updatecli's source code
5859
golangci-lint run

pkg/database/config.go

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ func GetConfigKind(ctx context.Context, resourceType string) ([]string, error) {
155155
return results, nil
156156
}
157157

158-
// GetConfigSource returns a list of resource configurations from the database.
159-
func GetConfigSource(ctx context.Context, kind, id, config string) ([]model.ConfigSource, error) {
158+
// GetSourceConfigs returns a list of resource configurations from the database.
159+
func GetSourceConfigs(ctx context.Context, kind, id, config string, limit, page int) ([]model.ConfigSource, int, error) {
160160
table := configSourceTableName
161161

162162
// SELECT id, kind, created_at, updated_at, config FROM " + table
@@ -183,17 +183,39 @@ func GetConfigSource(ctx context.Context, kind, id, config string) ([]model.Conf
183183
)
184184
}
185185

186+
// Get total count of results
187+
totalCount := 0
188+
totalQuery := psql.Select(sm.From(query), sm.Columns("count(*)"))
189+
totalQueryString, totalArgs, err := totalQuery.Build(ctx)
190+
if err != nil {
191+
logrus.Errorf("building total count query failed: %s\n\t%s", totalQueryString, err)
192+
return nil, 0, err
193+
}
194+
195+
if err = DB.QueryRow(ctx, totalQueryString, totalArgs...).Scan(
196+
&totalCount,
197+
); err != nil {
198+
logrus.Errorf("parsing total count result: %s", err)
199+
}
200+
201+
if limit < totalCount && limit > 0 {
202+
query.Apply(
203+
sm.Limit(limit),
204+
sm.Offset((page-1)*limit),
205+
)
206+
}
207+
186208
queryString, args, err := query.Build(ctx)
187209
if err != nil {
188210
logrus.Errorf("building query failed: %s\n\t%s", queryString, err)
189-
return nil, err
211+
return nil, 0, err
190212
}
191213

192214
rows, err := DB.Query(context.Background(), queryString, args...)
193215

194216
if err != nil {
195217
logrus.Errorf("query failed: %q\n\t%s", queryString, err)
196-
return nil, err
218+
return nil, 0, err
197219
}
198220

199221
results := []model.ConfigSource{}
@@ -206,7 +228,7 @@ func GetConfigSource(ctx context.Context, kind, id, config string) ([]model.Conf
206228
err := rows.Scan(&r.ID, &r.Kind, &r.Created_at, &r.Updated_at, &config)
207229
if err != nil {
208230
logrus.Errorf("parsing Source result: %s", err)
209-
return nil, err
231+
return nil, 0, err
210232
}
211233

212234
err = json.Unmarshal([]byte(config), &r.Config)
@@ -218,11 +240,11 @@ func GetConfigSource(ctx context.Context, kind, id, config string) ([]model.Conf
218240
results = append(results, r)
219241
}
220242

221-
return results, nil
243+
return results, totalCount, nil
222244
}
223245

224-
// GetConfigCondition returns a list of resource configurations from the database.
225-
func GetConfigCondition(ctx context.Context, kind, id, config string) ([]model.ConfigCondition, error) {
246+
// GetConditionConfigs returns a list of resource configurations from the database.
247+
func GetConditionConfigs(ctx context.Context, kind, id, config string, limit, page int) ([]model.ConfigCondition, int, error) {
226248
table := configConditionTableName
227249

228250
// SELECT id, kind, created_at, updated_at, config FROM " + table
@@ -249,17 +271,39 @@ func GetConfigCondition(ctx context.Context, kind, id, config string) ([]model.C
249271
)
250272
}
251273

274+
totalCount := 0
275+
totalQuery := psql.Select(sm.From(query), sm.Columns("count(*)"))
276+
totalQueryString, totalArgs, err := totalQuery.Build(ctx)
277+
if err != nil {
278+
logrus.Errorf("building total count query failed: %s\n\t%s", totalQueryString, err)
279+
return nil, 0, err
280+
}
281+
282+
if err = DB.QueryRow(ctx, totalQueryString, totalArgs...).Scan(
283+
&totalCount,
284+
); err != nil {
285+
logrus.Errorf("parsing total count result: %s", err)
286+
}
287+
288+
// Apply pagination if limit and page are set
289+
if limit < totalCount && limit > 0 {
290+
query.Apply(
291+
sm.Limit(limit),
292+
sm.Offset((page-1)*limit),
293+
)
294+
}
295+
252296
queryString, args, err := query.Build(ctx)
253297
if err != nil {
254298
logrus.Errorf("building query failed: %s\n\t%s", queryString, err)
255-
return nil, err
299+
return nil, 0, err
256300
}
257301

258302
rows, err := DB.Query(context.Background(), queryString, args...)
259303

260304
if err != nil {
261305
logrus.Errorf("query failed: %q\n\t%s", queryString, err)
262-
return nil, err
306+
return nil, 0, err
263307
}
264308

265309
results := []model.ConfigCondition{}
@@ -275,7 +319,7 @@ func GetConfigCondition(ctx context.Context, kind, id, config string) ([]model.C
275319

276320
logrus.Errorf("Query: %q\n\t%s", queryString, err)
277321
logrus.Errorf("parsing condition result: %s", err)
278-
return nil, err
322+
return nil, 0, err
279323
}
280324

281325
err = json.Unmarshal([]byte(config), &r.Config)
@@ -287,11 +331,11 @@ func GetConfigCondition(ctx context.Context, kind, id, config string) ([]model.C
287331
results = append(results, r)
288332
}
289333

290-
return results, nil
334+
return results, totalCount, nil
291335
}
292336

293-
// GetConfigTarget returns a list of resource configurations from the database.
294-
func GetConfigTarget(ctx context.Context, kind, id, config string) ([]model.ConfigTarget, error) {
337+
// GetTargetConfigs returns a list of resource configurations from the database.
338+
func GetTargetConfigs(ctx context.Context, kind, id, config string, limit, page int) ([]model.ConfigTarget, int, error) {
295339
table := configTargetTableName
296340

297341
// SELECT id, kind, created_at, updated_at, config FROM " + table
@@ -318,17 +362,39 @@ func GetConfigTarget(ctx context.Context, kind, id, config string) ([]model.Conf
318362
)
319363
}
320364

365+
totalCount := 0
366+
totalQuery := psql.Select(sm.From(query), sm.Columns("count(*)"))
367+
totalQueryString, totalArgs, err := totalQuery.Build(ctx)
368+
if err != nil {
369+
logrus.Errorf("building total count query failed: %s\n\t%s", totalQueryString, err)
370+
return nil, 0, err
371+
}
372+
373+
if err = DB.QueryRow(ctx, totalQueryString, totalArgs...).Scan(
374+
&totalCount,
375+
); err != nil {
376+
logrus.Errorf("parsing total count result: %s", err)
377+
}
378+
379+
// Apply pagination if limit and page are set
380+
if limit < totalCount && limit > 0 {
381+
query.Apply(
382+
sm.Limit(limit),
383+
sm.Offset((page-1)*limit),
384+
)
385+
}
386+
321387
queryString, args, err := query.Build(ctx)
322388
if err != nil {
323389
logrus.Errorf("building query failed: %s\n\t%s", queryString, err)
324-
return nil, err
390+
return nil, 0, err
325391
}
326392

327393
rows, err := DB.Query(context.Background(), queryString, args...)
328394

329395
if err != nil {
330396
logrus.Errorf("query failed: %q\n\t%s", queryString, err)
331-
return nil, err
397+
return nil, 0, err
332398
}
333399

334400
results := []model.ConfigTarget{}
@@ -342,7 +408,7 @@ func GetConfigTarget(ctx context.Context, kind, id, config string) ([]model.Conf
342408
if err != nil {
343409
logrus.Errorf("Query: %q\n\t%s", queryString, err)
344410
logrus.Errorf("parsing target result: %s", err)
345-
return nil, err
411+
return nil, 0, err
346412
}
347413

348414
err = json.Unmarshal([]byte(config), &r.Config)
@@ -354,5 +420,5 @@ func GetConfigTarget(ctx context.Context, kind, id, config string) ([]model.Conf
354420
results = append(results, r)
355421
}
356422

357-
return results, nil
423+
return results, totalCount, nil
358424
}

0 commit comments

Comments
 (0)