Skip to content

Commit dcc24cf

Browse files
authored
Snappier search: facets only for a new query, prefetched pages, a progress bar (#5)
Next was slow because every page asked cvcpkg.org for facets, which makes it count every match: 1.2 s for page 2 of everything and 2.4 s for page 2 of zlib, against 0.2 s and 0.4 s without. Now only a new query or filter asks for facets (the honest package count); paging reuses that count. After showing a page the search module prefetches the next one in the background and keeps the page it left, so Next and Prev are answered with no request. Requests wait in 50 ms slices (http-get-async and await with a timeout), and between slices the module draws an indeterminate progress bar at the bottom of its view; the package module does too. lib.ari gains search-key, body-text and progress-bar, and search-query takes facets. The fixture logs its requests (/_requests) and answers a slow: name after a second. Module tests show a new query asks for facets and paging doesn't, Next and Prev make no request, and the bar draws during a slow request and clears after; with prefetch disabled they fail.
1 parent ab13004 commit dcc24cf

7 files changed

Lines changed: 251 additions & 16 deletions

File tree

‎share/cypkg/lib.ari‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,33 @@ functions: |
4444
;; A filter's value as the query sends it: "all" means no filter.
4545
(defun filter-value (v) (if (= v "all") "" v))
4646

47-
;; The query for one page of a search, as the web page sends it. facets=true: without it the
48-
;; server counts packages on the returned page only.
49-
(defun search-query (q platform arch link page offset)
47+
;; The query for one page of a search. facets is #t only for a new query or filters: it makes
48+
;; the server count every match (seconds on cvcpkg.org, against a fraction of one without), and
49+
;; it's the only way to get an honest package count; without it the count is page-local, so a
50+
;; page reuses the first page's.
51+
(defun search-query (q platform arch link page offset facets)
5052
(dict "q" q "platform" (filter-value platform) "arch" (filter-value arch)
51-
"link" (filter-value link) "limit" page "offset" offset "facets" "true"))
53+
"link" (filter-value link) "limit" page "offset" offset
54+
"facets" (if facets "true" "false")))
55+
56+
;; What makes two searches the same search, paging aside.
57+
(defun search-key (q platform arch link) (string.join (list q platform arch link) "|"))
58+
59+
;; A reply's body as text (a reply's body is bytes).
60+
(defun body-text (r)
61+
(let ((b (get-attr r "body"))) (if (is-bytes b) (bytes.decode b) (text b))))
62+
63+
;; An indeterminate progress bar, width cells wide (at most 160), for the nth step of a wait: a
64+
;; block that sweeps across and back. "" when n is 0 (nothing in flight). Three slices of a
65+
;; constant, not a loop: a tick redraws it 20 times a second.
66+
(defun progress-bar (n width)
67+
(if (= n 0) ""
68+
(let ((span (- width 6)))
69+
(let ((pos (% n (* 2 span))))
70+
(let ((at (if (> pos span) (- (* 2 span) pos) pos))
71+
(dots "································································································································································"))
72+
(str-concat (string.substring dots 0 at) "██████"
73+
(string.substring dots 0 (- span at))))))))
5274

5375
;; The status line for n rows from offset off, of total, in pc packages (nil: unknown).
5476
(defun search-status (off n total pc)

‎share/cypkg/package.ari‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ init: |
2626
(state-set "pkg.total" 0)
2727
(state-set "pkg.status" "")
2828
(state-set "pkg.seq" 0)
29+
(state-set "pkg.busy" 0)
30+
(state-set "pkg.bar" "")
2931
(state-set "req.pkgpage" "0"))
3032

3133
# pkg.seq counts the opens and pages that have finished, so a test can wait for one.
@@ -43,7 +45,19 @@ on_tick: |
4345
(state-set "pkg.total" 0)
4446
(state-set "pkg.status" "loading...")
4547
(state-set "view" "package")
46-
(set r (http-get (str-concat (state-get "args.server") "/v1/packages/" (url-encode name))))
48+
;; Wait in 50 ms slices, drawing a progress bar in pkg.bar between them.
49+
(set f (http-get-async (str-concat (state-get "args.server") "/v1/packages/"
50+
(url-encode name))))
51+
(set r nil)
52+
(state-set "pkg.busy" 1)
53+
(while (= r nil)
54+
(set r (try (await f 0.05) (catch (TimeoutError e) nil)))
55+
(if (= r nil)
56+
(begin
57+
(state-set "pkg.busy" (+ (int (state-get "pkg.busy")) 1))
58+
(state-set "pkg.bar" (progress-bar (int (state-get "pkg.busy")) 96)))))
59+
(state-set "pkg.busy" 0)
60+
(state-set "pkg.bar" "")
4761
(set rows (reply-rows (reply-object r)))
4862
(if (if (is-list rows) (> (length rows) 0) #f)
4963
(begin

‎share/cypkg/search.ari‎

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ init: |
3030
(state-set "search.status" "")
3131
(state-set "search.sent" "")
3232
(state-set "search.seq" 0)
33+
(state-set "search.key" "")
34+
(state-set "search.pc" "")
35+
(state-set "search.busy" 0)
36+
(state-set "search.bar" "")
37+
(state-set "search.next.key" "")
38+
(state-set "search.prev.key" "")
3339
(state-set "req.search" "1"))
3440

3541
# search.seq counts the searches that have finished, so a test can wait for one.
@@ -45,31 +51,92 @@ on_tick: |
4551
(state-set "search.sent" filters)
4652
(state-set "req.search" "1")))
4753

48-
;; A search: one page of rows.
54+
;; A request with a progress bar: wait in 50 ms slices, and between them count the wait in
55+
;; search.busy and draw it in search.bar, which the view shows. The tick is parked, not
56+
;; blocked, while it waits, so frames keep drawing.
57+
(defun fetch (url query)
58+
(set f (http-get-async url (dict "query" query)))
59+
(set r nil)
60+
(state-set "search.busy" 1)
61+
(while (= r nil)
62+
(set r (try (await f 0.05) (catch (TimeoutError e) nil)))
63+
(if (= r nil)
64+
(begin
65+
(state-set "search.busy" (+ (int (state-get "search.busy")) 1))
66+
(state-set "search.bar" (progress-bar (int (state-get "search.busy")) 96)))))
67+
(state-set "search.busy" 0)
68+
(state-set "search.bar" "")
69+
r)
70+
(set url (str-concat (state-get "args.server") "/v1/search"))
71+
(set page (int-or (state-get "args.page") 10))
72+
(set key (search-key (state-get "search.q") (state-get "search.platform")
73+
(state-get "search.arch") (state-get "search.link")))
74+
;; A page kept in slot (next or prev) for this search at offset off, as reply text, or nil.
75+
(defun kept (slot off)
76+
(if (if (= (state-get (str-concat "search." slot ".key")) key)
77+
(= (int-or (state-get (str-concat "search." slot ".offset")) -1) off) #f)
78+
(state-get (str-concat "search." slot ".body"))
79+
nil))
80+
(defun keep (slot off body)
81+
(state-set (str-concat "search." slot ".key") key)
82+
(state-set (str-concat "search." slot ".offset") off)
83+
(state-set (str-concat "search." slot ".body") body))
84+
85+
;; A search: one page of rows. A new query or filter asks the server for facets (the honest
86+
;; package count); paging doesn't, and reuses that count. A page already fetched (the next
87+
;; one, prefetched, or the one just left) is parsed from its text with no request.
4988
(if (= (state-get "req.search") "1")
5089
(begin
5190
(state-set "req.search" "0")
52-
(state-set "search.status" "searching...")
53-
(set r (http-get (str-concat (state-get "args.server") "/v1/search")
54-
(dict "query" (search-query (state-get "search.q") (state-get "search.platform")
55-
(state-get "search.arch") (state-get "search.link")
56-
(int-or (state-get "args.page") 10) (state-get "search.offset")))))
91+
(set off (int (state-get "search.offset")))
92+
(set fresh (!= key (state-get "search.key")))
93+
(set body (if fresh nil (kept "next" off)))
94+
(if (= body nil) (set body (if fresh nil (kept "prev" off))))
95+
(set r (if (= body nil)
96+
(begin
97+
(state-set "search.status" "searching...")
98+
(fetch url (search-query (state-get "search.q") (state-get "search.platform")
99+
(state-get "search.arch") (state-get "search.link")
100+
page off fresh)))
101+
(dict "status" 200 "body" body "error" "")))
57102
(set d (reply-object r))
58103
(set rows (reply-rows d))
59104
(if (is-list rows)
60105
(begin
106+
;; The page being left stays at hand for Prev.
107+
(if (if (not fresh) (!= (state-get "search.key") "") #f)
108+
(keep "prev" (int (state-get "search.shown")) (state-get "search.body")))
61109
(state-set-tree "search.res" d)
110+
(state-set "search.body" (body-text r))
111+
(state-set "search.shown" off)
112+
(state-set "search.key" key)
113+
(if fresh (state-set "search.pc" (text (field d "package_count"))))
62114
(set n (length rows))
63115
(set total (if (= (field d "total") nil) n (int (field d "total"))))
64116
(state-set "search.count" n)
65117
(state-set "search.total" total)
66118
(state-set "search.status"
67-
(search-status (int (state-get "search.offset")) n total (field d "package_count"))))
119+
(search-status off n total (if (= (state-get "search.pc") "") nil
120+
(int (state-get "search.pc"))))))
68121
(begin
122+
(state-set "search.key" "")
69123
(state-set "search.count" 0)
70124
(state-set "search.total" 0)
71125
(state-set "search.status" (search-failure r))))
72-
(state-set "search.seq" (+ (int (state-get "search.seq")) 1)))))
126+
(state-set "search.seq" (+ (int (state-get "search.seq")) 1))
127+
128+
;; Prefetch the next page in the background, so Next is instant. A failure is left for
129+
;; the click to find.
130+
(if (if (is-list rows) (< (+ off page) (int (state-get "search.total"))) #f)
131+
(if (= (kept "next" (+ off page)) nil)
132+
(begin
133+
(set nf (http-get-async url (dict "query"
134+
(search-query (state-get "search.q") (state-get "search.platform")
135+
(state-get "search.arch") (state-get "search.link")
136+
page (+ off page) #f))))
137+
(set nr (await nf))
138+
(if (= (get-attr nr "status") 200)
139+
(keep "next" (+ off page) (body-text nr)))))))))
73140

74141
root:
75142
- include: search_view

‎share/cypkg/views.ari‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# search.res.packages.{i}.name .version .platform .arch .link .size_bytes
1313
# pkg.name .desc .version .license .homepage .tags .install .status .offset .count .total
1414
# pkg.page.{i}.version .where .kind .size .date
15+
# search.bar, pkg.bar a progress bar while a request is in flight ("" otherwise)
1516
# The views write:
1617
# search.q .platform .arch .link .offset, and req.search "1" to ask for a search
1718
# nav.open, and view "package", to open a package (the package module reads nav.open)
@@ -103,6 +104,10 @@ units:
103104
(state-set "search.offset"
104105
(+ (int (state-get "search.offset")) (int (state-get "args.page"))))
105106
(state-set "req.search" "1"))
107+
# The progress bar: the search module draws search.bar while a request is in flight.
108+
- text: ""
109+
bind: search.bar
110+
106111

107112
package_view:
108113
type: group
@@ -178,3 +183,6 @@ units:
178183
(state-set "pkg.offset"
179184
(+ (int (state-get "pkg.offset")) (int (state-get "args.page"))))
180185
(state-set "req.pkgpage" "1"))
186+
# The progress bar: the package module draws pkg.bar while a request is in flight.
187+
- text: ""
188+
bind: pkg.bar

‎tests/lib/lib_test.ari‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,35 @@ tests:
5656

5757
search-query sends the filters, the page and facets: |
5858
(begin
59-
(set q (search-query "zlib" "all" "arm64" "static" 10 20))
59+
(set q (search-query "zlib" "all" "arm64" "static" 10 20 #t))
6060
(assert (= (get-attr q "q") "zlib"))
6161
(assert (= (get-attr q "platform") ""))
6262
(assert (= (get-attr q "arch") "arm64"))
6363
(assert (= (get-attr q "link") "static"))
6464
(assert (= (get-attr q "limit") 10))
6565
(assert (= (get-attr q "offset") 20))
66-
(assert (= (get-attr q "facets") "true")))
66+
(assert (= (get-attr q "facets") "true"))
67+
(assert (= (get-attr (search-query "zlib" "all" "all" "all" 10 10 #f) "facets") "false")))
68+
69+
search-key ignores paging and names the filters: |
70+
(begin
71+
(assert (= (search-key "zlib" "all" "all" "all") "zlib|all|all|all"))
72+
(assert (!= (search-key "zlib" "linux" "all" "all") (search-key "zlib" "all" "all" "all"))))
73+
74+
body-text decodes bytes and leaves text: |
75+
(begin
76+
(assert (= (body-text (dict "body" (string.encode "{}"))) "{}"))
77+
(assert (= (body-text (dict "body" "{}")) "{}"))
78+
(assert (= (body-text (dict "body" nil)) "")))
79+
80+
progress-bar sweeps a block across and back: |
81+
(begin
82+
(assert (= (progress-bar 0 20) "") "idle")
83+
(assert (= (string.length (progress-bar 1 20)) 20))
84+
(assert (= (progress-bar 1 20) "·██████·············"))
85+
(assert (= (progress-bar 14 20) "··············██████") "the far end")
86+
(assert (= (progress-bar 15 20) "·············██████·") "and back")
87+
(assert (= (progress-bar 28 20) "██████··············") "home again"))
6788

6889
search-status counts rows and packages: |
6990
(begin

‎tests/modules/search_test.ari‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ meta:
1313

1414
requires: [net]
1515

16+
# lib.ari's functions (text and the like) for the tests' own programs.
17+
import: ../../share/cypkg/lib.ari
18+
1619
args:
1720
server: {type: string, default: "@SERVER@"}
1821
q: {type: string, default: ""}
@@ -61,6 +64,32 @@ tests:
6164
(assert (= (state-get "includes.s.search.status") "builds 1-10 of 25, 1 package")
6265
(state-get "includes.s.search.status")))
6366

67+
Next is answered from the prefetched page, with no request: |
68+
(begin
69+
(set count (lambda ()
70+
(let ((r (http-get (str-concat (state-get "args.server") "/_requests"))))
71+
(length (get-attr (json-parse (get-attr r "body")) "requests")))))
72+
;; The module prefetches page 2 right after showing page 1.
73+
(set i 0)
74+
(while (if (!= (text (state-get "includes.s.search.next.offset")) "10") (< i 500) #f)
75+
(sleep 0.02) (set i (+ i 1)))
76+
(assert (= (text (state-get "includes.s.search.next.offset")) "10") "page 2 was never prefetched")
77+
(set before (count))
78+
(set n (int (state-get "includes.s.search.seq")))
79+
(state-set "includes.s.search.offset" 10)
80+
(state-set "includes.s.req.search" "1")
81+
(set i 0)
82+
(while (if (<= (int (state-get "includes.s.search.seq")) n) (< i 500) #f) (sleep 0.02) (set i (+ i 1)))
83+
(assert (= (state-get "includes.s.search.status") "builds 11-20 of 25, 1 package")
84+
(state-get "includes.s.search.status"))
85+
;; Showing page 2 prefetches page 3; that one request is the only new one.
86+
(assert (<= (count) (+ before 1)) "Next fetched page 2 again")
87+
(state-set "includes.s.search.offset" 0)
88+
(state-set "includes.s.req.search" "1")
89+
(set n (int (state-get "includes.s.search.seq")))
90+
(set i 0)
91+
(while (if (<= (int (state-get "includes.s.search.seq")) n) (< i 500) #f) (sleep 0.02) (set i (+ i 1))))
92+
6493
Next pages the search: |
6594
(begin
6695
(set n (int (state-get "includes.s.search.seq")))
@@ -82,6 +111,35 @@ tests:
82111
(assert (= (state-get "includes.s.search.count") "5") (state-get "includes.s.search.count"))
83112
(assert (= (state-get "includes.s.search.status") "builds 21-25 of 25, 1 package")))
84113

114+
paging asked the server for no facets, and only the new query did: |
115+
(begin
116+
(set r (http-get (str-concat (state-get "args.server") "/_requests")))
117+
(set with (list))
118+
(set without (list))
119+
(for x (get-attr (json-parse (get-attr r "body")) "requests")
120+
(if (= (get-attr x "q") "zstd")
121+
(if (= (get-attr x "facets") "true")
122+
(set with (append with x))
123+
(set without (append without x)))))
124+
(assert (= (length with) 1) (json-encode with))
125+
(assert (= (get-attr (nth with 0) "offset") "0") "facets only for the query's first page")
126+
(assert (> (length without) 0) "pages were fetched without facets"))
127+
128+
Prev is answered from the page just left, with no request: |
129+
(begin
130+
(set count (lambda ()
131+
(let ((r (http-get (str-concat (state-get "args.server") "/_requests"))))
132+
(length (get-attr (json-parse (get-attr r "body")) "requests")))))
133+
(set before (count))
134+
(set n (int (state-get "includes.s.search.seq")))
135+
(state-set "includes.s.search.offset" 10)
136+
(state-set "includes.s.req.search" "1")
137+
(set i 0)
138+
(while (if (<= (int (state-get "includes.s.search.seq")) n) (< i 500) #f) (sleep 0.02) (set i (+ i 1)))
139+
(assert (= (state-get "includes.s.search.status") "builds 11-20 of 25, 1 package")
140+
(state-get "includes.s.search.status"))
141+
(assert (= (count) before) "Prev made a request"))
142+
85143
a filter change searches again from the first row: |
86144
(begin
87145
(set n (int (state-get "includes.s.search.seq")))
@@ -131,3 +189,16 @@ tests:
131189
(assert (= (state-get "includes.s.search.total") "0"))
132190
(assert (= (state-get "includes.s.search.status")
133191
"search failed: the registry's reply was not a search result")))
192+
193+
a slow request shows the progress bar until its reply: |
194+
(begin
195+
(set n (int (state-get "includes.s.search.seq")))
196+
(state-set "includes.s.search.q" "slow:zstd")
197+
(state-set "includes.s.req.search" "1")
198+
(sleep 0.5)
199+
(assert (string.contains (text (state-get "includes.s.search.bar")) "█")
200+
(str-concat "bar: " (text (state-get "includes.s.search.bar"))))
201+
(set i 0)
202+
(while (if (<= (int (state-get "includes.s.search.seq")) n) (< i 500) #f) (sleep 0.02) (set i (+ i 1)))
203+
(assert (= (text (state-get "includes.s.search.bar")) "") "the bar stays after the reply")
204+
(assert (string.starts-with (state-get "includes.s.search.status") "builds 1-10 of 25")))

0 commit comments

Comments
 (0)