forked from vieter-v/vieter
more wip
parent
656ff2614d
commit
e8f4069faa
|
|
@ -1,25 +1,49 @@
|
||||||
module db
|
module db
|
||||||
|
|
||||||
import models { Target, TargetArch, TargetFilter, TargetSearchFilter }
|
import models { Target, TargetArch, TargetFilter }
|
||||||
import math
|
import math
|
||||||
|
|
||||||
// get_targets returns all targets in the database.
|
// get_targets returns all targets in the database.
|
||||||
pub fn (db &VieterDb) get_targets(filter TargetFilter) []Target {
|
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
|
window_size := 50
|
||||||
// around.
|
|
||||||
if filter.repo != '' {
|
mut out := []Target{}
|
||||||
res := sql db.conn {
|
mut targets := []Target{cap: window_size}
|
||||||
select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
if filtered_offset + u64(targets.len) > filter.offset {
|
||||||
|
start_index := math.max(left_to_skip, 0)
|
||||||
|
end_index := start_index +
|
||||||
|
math.min(filter.limit - u64(out.len), u64(targets.len) - start_index)
|
||||||
|
println('start = $start_index, end = $end_index')
|
||||||
|
|
||||||
|
out << targets[start_index..end_index]
|
||||||
|
}
|
||||||
|
|
||||||
|
filtered_offset += u64(targets.len)
|
||||||
}
|
}
|
||||||
|
|
||||||
res := sql db.conn {
|
return out
|
||||||
select from Target order by id limit filter.limit offset filter.offset
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_target tries to return a specific target.
|
// get_target tries to return a specific target.
|
||||||
|
|
@ -105,34 +129,3 @@ pub fn (db &VieterDb) target_exists(target_id int) bool {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (db &VieterDb) search_targets(filter TargetSearchFilter) []Target {
|
|
||||||
out := []Target{}
|
|
||||||
|
|
||||||
mut seen := u64(0)
|
|
||||||
mut offset := 0
|
|
||||||
|
|
||||||
for out.len < filter.limit {
|
|
||||||
targets := sql db.conn {
|
|
||||||
select from Target order by id limit 25 offset offset
|
|
||||||
}
|
|
||||||
offset += targets.len
|
|
||||||
|
|
||||||
if targets.len == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
matched_targets := targets.filter(it.url.contains(filter.query)
|
|
||||||
|| it.path.contains(filter.query))
|
|
||||||
seen += u64(matched_targets.len)
|
|
||||||
|
|
||||||
if seen > filter.offset {
|
|
||||||
start_index := u64(matched_targets.len) - math.min(seen - filter.offset, matched_targets.len)
|
|
||||||
end_index := start_index + math.min(filter.limit - seen, u64(matched_targets.len) - start_index)
|
|
||||||
|
|
||||||
out << matched_targets[start_index..end_index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -73,12 +73,5 @@ pub mut:
|
||||||
limit u64 = 25
|
limit u64 = 25
|
||||||
offset u64
|
offset u64
|
||||||
repo string
|
repo string
|
||||||
}
|
|
||||||
|
|
||||||
[params]
|
|
||||||
pub struct TargetSearchFilter {
|
|
||||||
pub mut:
|
|
||||||
limit u64 = 25
|
|
||||||
offset u64
|
|
||||||
query string
|
query string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,5 +82,5 @@ fn (mut app App) v1_patch_target(id int) web.Result {
|
||||||
return app.json(.ok, new_data_response(target))
|
return app.json(.ok, new_data_response(target))
|
||||||
}
|
}
|
||||||
|
|
||||||
['/api/v1/targets/search'; auth; markused; get]
|
['/api/v1/targets/search'; auth; get; markused]
|
||||||
fn (mut app App) v1_search_targets()
|
fn (mut app App) v1_search_targets()
|
||||||
|
|
|
||||||
|
|
@ -12,4 +12,4 @@ address = "http://localhost:8000"
|
||||||
api_update_frequency = 2
|
api_update_frequency = 2
|
||||||
image_rebuild_frequency = 1
|
image_rebuild_frequency = 1
|
||||||
max_concurrent_builds = 3
|
max_concurrent_builds = 3
|
||||||
max_log_age = 64
|
# max_log_age = 64
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue