From 74baccb507523a10ce3514807a9c4e8433ab36a0 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 19 May 2022 22:43:38 +0200 Subject: [PATCH 1/3] WIP: BuildLogFilter --- src/db/logs.v | 6 +++--- src/models/logs.v | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/db/logs.v b/src/db/logs.v index 129ec4e..5c64e1f 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -1,11 +1,11 @@ module db -import models { BuildLog } +import models { BuildLog, BuildLogFilter } // get_build_logs returns all BuildLog's in the database. -pub fn (db &VieterDb) get_build_logs() []BuildLog { +pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { res := sql db.conn { - select from BuildLog order by id + select from BuildLog where filter.repo == 0 || repo_id == filter.repo order by id } return res diff --git a/src/models/logs.v b/src/models/logs.v index 173336f..0124d4e 100644 --- a/src/models/logs.v +++ b/src/models/logs.v @@ -26,3 +26,13 @@ pub fn (bl &BuildLog) str() string { return str } + +[params] +pub struct BuildLogFilter { +pub mut: + repo int + before time.Time + after time.Time + failed_only bool + arch string +} From a374924ea1e2bad90bcb4b3c801e2977d3ea4be4 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 22 May 2022 20:29:16 +0200 Subject: [PATCH 2/3] WIP: BuildLog filters --- CHANGELOG.md | 6 ++++++ src/db/logs.v | 28 ++++++++++++++++++++++++++-- src/models/logs.v | 13 ++++++++----- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c336fe..170c97c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * Database migrations +* Query parameters for GitRepo API to filter responses +* Respective CLI flags for new GitRepo API parameters + +### Changed + +* Refactor of main types into `models` module ## [0.3.0-alpha.2](https://git.rustybever.be/vieter/vieter/src/tag/0.3.0-alpha.2) diff --git a/src/db/logs.v b/src/db/logs.v index 5c64e1f..49f905b 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -1,13 +1,37 @@ module db import models { BuildLog, BuildLogFilter } +import time // get_build_logs returns all BuildLog's in the database. pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { - res := sql db.conn { - select from BuildLog where filter.repo == 0 || repo_id == filter.repo order by id + mut where_parts := []string{} + + if filter.repo != 0 { + where_parts << 'repo_id == $filter.repo' } + if filter.before != time.Time{} { + where_parts << 'start_time < $filter.before.unix_time()' + } + + if filter.after != time.Time{} { + where_parts << 'start_time < $filter.after.unix_time()' + } + + where_str := if where_parts.len > 0 { + ' where ' + where_parts.map('($it)').join(' and ') + } else { + '' + } + + query := 'select from BuildLog' + where_str + res := db.conn.exec(query) + +/* res := sql db.conn { */ +/* select from BuildLog where filter.repo == 0 || repo_id == filter.repo order by id */ +/* } */ + return res } diff --git a/src/models/logs.v b/src/models/logs.v index 0124d4e..1bfc7a6 100644 --- a/src/models/logs.v +++ b/src/models/logs.v @@ -30,9 +30,12 @@ pub fn (bl &BuildLog) str() string { [params] pub struct BuildLogFilter { pub mut: - repo int - before time.Time - after time.Time - failed_only bool - arch string + limit u64 = 25 + offset u64 + repo int + before time.Time + after time.Time + exit_codes_whitelist []u8 + exit_codes_blacklist []u8 + arch string } From 546d79ed2ef34edb5f563c4b9d7628f8078da09d Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 28 May 2022 22:22:55 +0200 Subject: [PATCH 3/3] feat(db): added function to convert sqlite output to struct --- src/db/db.v | 19 +++++++++++++++++++ src/db/logs.v | 17 +++++++++-------- src/models/logs.v | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/db/db.v b/src/db/db.v index 9a06a03..d6cc057 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -1,6 +1,7 @@ module db import sqlite +import time struct VieterDb { conn sqlite.DB @@ -66,3 +67,21 @@ pub fn init(db_path string) ?VieterDb { conn: conn } } + +pub fn row_into(row sqlite.Row) T { + mut i := 0 + mut out := T{} + + $for field in T.fields { + $if field.typ is string { + out.$(field.name) = row.vals[i] + } $else $if field.typ is int { + out.$(field.name) = row.vals[i].int() + } $else $if field.typ is time.Time { + out.$(field.name) = time.unix(row.vals[i].int()) + } + + i += 1 + } + return out +} diff --git a/src/db/logs.v b/src/db/logs.v index 49f905b..4ec48c7 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -19,18 +19,19 @@ pub fn (db &VieterDb) get_build_logs(filter BuildLogFilter) []BuildLog { where_parts << 'start_time < $filter.after.unix_time()' } - where_str := if where_parts.len > 0 { - ' where ' + where_parts.map('($it)').join(' and ') - } else { - '' + mut where_str := '' + + if where_parts.len > 0 { + where_str = ' where ' + where_parts.map('($it)').join(' and ') } query := 'select from BuildLog' + where_str - res := db.conn.exec(query) + rows, _ := db.conn.exec(query) + res := rows.map(row_into(it)) -/* res := sql db.conn { */ -/* select from BuildLog where filter.repo == 0 || repo_id == filter.repo order by id */ -/* } */ + // res := sql db.conn { + // select from BuildLog where filter.repo == 0 || repo_id == filter.repo order by id + // } return res } diff --git a/src/models/logs.v b/src/models/logs.v index 1bfc7a6..82fc52f 100644 --- a/src/models/logs.v +++ b/src/models/logs.v @@ -3,7 +3,7 @@ module models import time pub struct BuildLog { -pub: +pub mut: id int [primary; sql: serial] repo_id int [nonull] start_time time.Time [nonull]