From 4eee0e069b71f78160c75b2018e8c8f68c389c0c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 31 Dec 2022 10:19:59 +0100 Subject: [PATCH] feat(api): add search query to targets --- src/console/targets/targets.v | 11 ++++++++ src/db/targets.v | 53 +++++++++++++++++++++++++++-------- src/models/targets.v | 1 + src/server/api_targets.v | 3 ++ vieter.toml | 2 +- 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 3c0d755..dfc3792 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -40,6 +40,12 @@ pub fn cmd() cli.Command { description: 'Only return targets that publish to this repo.' flag: cli.FlagType.string }, + cli.Flag{ + name: 'query' + abbrev: 'q' + description: 'Search string to filter targets by.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ! { config_file := cmd.flags.get_string('config-file')! @@ -62,6 +68,11 @@ pub fn cmd() cli.Command { filter.repo = repo } + query := cmd.flags.get_string('query')! + if query != '' { + filter.query = query + } + raw := cmd.flags.get_bool('raw')! list(conf, filter, raw)! diff --git a/src/db/targets.v b/src/db/targets.v index 41e56df..fba227e 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -1,24 +1,55 @@ module db import models { Target, TargetArch, TargetFilter } +import math // get_targets returns all targets in the database. pub fn (db &VieterDb) get_targets(filter TargetFilter) []Target { - // This seems to currently be blocked by a bug in the ORM, I'll have to ask - // around. - if filter.repo != '' { - res := sql db.conn { - select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset + window_size := 32 + + mut out := []Target{} + mut targets := []Target{cap: window_size} + + mut offset := 0 + mut filtered_offset := u64(0) + + for out.len < filter.limit { + targets = sql db.conn { + select from Target order by id limit window_size offset offset + } + offset += targets.len + + if targets.len == 0 { + break } - return res + if filter.repo != '' { + targets = targets.filter(it.repo == filter.repo) + } + + if filter.query != '' { + targets = targets.filter(it.url.contains(filter.query) || it.path.contains(filter.query) + || it.branch.contains(filter.query)) + } + + if filtered_offset > filter.offset { + end_index := math.min(filter.limit - u64(out.len), u64(targets.len)) + + out << targets[0..end_index] + } + // We start counting targets in the middle of the current window + else if filtered_offset + u64(targets.len) > filter.offset { + start_index := filter.offset - filtered_offset + end_index := start_index + + math.min(filter.limit - u64(out.len), u64(targets.len) - start_index) + + out << targets[start_index..end_index] + } + + filtered_offset += u64(targets.len) } - res := sql db.conn { - select from Target order by id limit filter.limit offset filter.offset - } - - return res + return out } // get_target tries to return a specific target. diff --git a/src/models/targets.v b/src/models/targets.v index af3cb0d..612f7fa 100644 --- a/src/models/targets.v +++ b/src/models/targets.v @@ -73,4 +73,5 @@ pub mut: limit u64 = 25 offset u64 repo string + query string } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 4bb7d12..f04fdae 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -81,3 +81,6 @@ fn (mut app App) v1_patch_target(id int) web.Result { return app.json(.ok, new_data_response(target)) } + +['/api/v1/targets/search'; auth; get; markused] +fn (mut app App) v1_search_targets() diff --git a/vieter.toml b/vieter.toml index 1f839f0..b1dbb94 100644 --- a/vieter.toml +++ b/vieter.toml @@ -12,4 +12,4 @@ address = "http://localhost:8000" api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 -max_log_age = 64 +# max_log_age = 64