From 393e641a7666682c1e4ff38b83affbf596c461d6 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 7 May 2022 15:31:01 +0200 Subject: [PATCH] feat(server): allow filtering of builds per repo --- src/db/logs.v | 12 +++++++++++- src/server/logs.v | 16 +++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/db/logs.v b/src/db/logs.v index 3e5b600..9a2405b 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -4,7 +4,7 @@ import time pub struct BuildLog { id int [primary; sql: serial] - repo GitRepo [nonull] + repo_id int [nonull] start_time time.Time [nonull] end_time time.Time [nonull] exit_code int [nonull] @@ -19,6 +19,16 @@ pub fn (db &VieterDb) get_build_logs() []BuildLog { return res } +// get_build_logs_for_repo returns all BuildLog's in the database for a given +// repo. +pub fn (db &VieterDb) get_build_logs_for_repo(repo_id int) []BuildLog { + res := sql db.conn { + select from BuildLog where repo_id == repo_id order by id + } + + return res +} + // get_build_log tries to return a specific BuildLog. pub fn (db &VieterDb) get_build_log(id int) ?BuildLog { res := sql db.conn { diff --git a/src/server/logs.v b/src/server/logs.v index 01116b4..8b0f297 100644 --- a/src/server/logs.v +++ b/src/server/logs.v @@ -15,7 +15,11 @@ fn (mut app App) get_logs() web.Result { return app.json(http.Status.unauthorized, new_response('Unauthorized.')) } - logs := app.db.get_build_logs() + logs := if 'repo' in app.query { + app.db.get_build_logs_for_repo(app.query['repo'].int()) + } else { + app.db.get_build_logs() + } return app.json(http.Status.ok, new_data_response(logs)) } @@ -56,13 +60,15 @@ fn (mut app App) post_log() web.Result { arch := app.query['arch'] - repo := app.db.get_git_repo(app.query['repo'].int()) or { - return app.json(http.Status.bad_request, new_response('Unknown repo.')) + repo := app.query['repo'].int() + + if repo == 0 { + return app.json(http.Status.bad_request, new_response('Invalid Git repo.')) } // Store log in db log := db.BuildLog{ - repo: repo + repo_id: repo start_time: start_time end_time: end_time exit_code: exit_code @@ -70,7 +76,7 @@ fn (mut app App) post_log() web.Result { app.db.add_build_log(log) - repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, repo.id.str(), arch) + repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, repo.str(), arch) // Create the logs directory of it doesn't exist if !os.exists(repo_logs_dir) {