feat(api): add search query to targets

Jef Roosens 2022-12-31 10:19:59 +01:00 committed by Chewing_Bever
parent 60d5fb77e0
commit f8f611f5c5
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 58 additions and 12 deletions

View File

@ -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)!

View File

@ -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.
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
}
if filter.repo != '' {
res := sql db.conn {
select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset
targets = targets.filter(it.repo == filter.repo)
}
return res
if filter.query != '' {
targets = targets.filter(it.url.contains(filter.query) || it.path.contains(filter.query)
|| it.branch.contains(filter.query))
}
res := sql db.conn {
select from Target order by id limit filter.limit offset filter.offset
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]
}
return res
filtered_offset += u64(targets.len)
}
return out
}
// get_target tries to return a specific target.

View File

@ -73,4 +73,5 @@ pub mut:
limit u64 = 25
offset u64
repo string
query string
}

View File

@ -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()

View File

@ -12,5 +12,5 @@ address = "http://localhost:8000"
api_update_frequency = 2
image_rebuild_frequency = 1
max_concurrent_builds = 3
max_log_age = 64
# max_log_age = 64
collect_metrics = true