Skip to content

Commit

Permalink
feat: look for module name search
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 21, 2024
1 parent 8a4aca2 commit 69a7075
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
28 changes: 28 additions & 0 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,34 @@ pub fn documentation_search(db: pgo.Connection, q: String) {
|> result.map(fn(r) { r.rows })
}

pub fn module_search(db: pgo.Connection, q: String) {
let query = pgo.text(q)
"SELECT DISTINCT ON (package_rank, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
s.metadata,
s.json_signature,
m.name module_name,
p.name,
r.version,
p.rank package_rank,
string_to_array(regexp_replace(r.version, '([0-9]+).([0-9]+).([0-9]+).*', '\\1.\\2.\\3'), '.')::int[] AS ordering
FROM package_type_fun_signature s
JOIN package_module m
ON m.id = s.package_module_id
JOIN package_release r
ON m.package_release_id = r.id
JOIN package p
ON p.id = r.package_id
WHERE m.name = $1
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC
LIMIT 100"
|> pgo.execute(db, [query], decode_type_search)
|> result.map_error(error.DatabaseError)
|> result.map(fn(r) { r.rows })
}

pub fn select_gleam_toml(db: pgo.Connection, offset: Int) {
"SELECT gleam_toml
FROM package_release
Expand Down
20 changes: 18 additions & 2 deletions apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ fn isolate_filters(query: String) -> #(String, List(String)) {
string.split(query, " ")
|> list.fold(#([], []), fn(acc, val) {
case val {
"in:signature" | "in:name" | "in:documentation" -> #(acc.0, [val, ..acc.1])
"in:signature" | "in:name" | "in:documentation" | "in:module" -> #(acc.0, [
val,
..acc.1
])
_ -> #([val, ..acc.0], acc.1)
}
})
Expand All @@ -35,7 +38,7 @@ fn isolate_filters(query: String) -> #(String, List(String)) {
|> pair.map_second(fn(filters) {
let no_filters = list.is_empty(filters)
use <- bool.guard(when: no_filters, return: [
"in:signature", "in:name", "in:documentation",
"in:signature", "in:name", "in:documentation", "in:module",
])
filters
})
Expand Down Expand Up @@ -79,13 +82,26 @@ fn search(query: String, ctx: Context) {
!list.contains(list.append(exact_matches, matches), i)
})
}
let module_searches = case list.contains(filters, "in:module") {
False -> []
True ->
queries.module_search(ctx.db, query)
|> result.map_error(error.debug_log)
|> result.unwrap([])
|> list.filter(fn(i) {
!list.contains(list.append(exact_matches, matches), i)
})
}
json.object([
#("exact-matches", json.array(exact_matches, queries.type_search_to_json)),
#("matches", json.array(matches, queries.type_search_to_json)),
#("searches", json.array(signature_searches, queries.type_search_to_json)),
#("docs-searches", {
json.array(documentation_searches, queries.type_search_to_json)
}),
#("module-searches", {
json.array(module_searches, queries.type_search_to_json)
}),
])
}

Expand Down
9 changes: 5 additions & 4 deletions apps/frontend/src/data/model.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub fn update_search_results(model: Model, search_results: SearchResults) {
let index = compute_index(search_results)
let view_cache = case search_results {
search_result.Start | search_result.InternalServerError -> element.none()
search_result.SearchResults(e, m, s, d) ->
cache.cache_search_results(index, e, m, s, d)
search_result.SearchResults(e, m, s, d, mods) ->
cache.cache_search_results(index, e, m, s, d, mods)
}
Model(
..model,
Expand All @@ -63,7 +63,7 @@ pub fn update_search_results(model: Model, search_results: SearchResults) {

pub fn reset(_model: Model) {
Model(
search_results: search_result.SearchResults([], [], [], []),
search_results: search_result.SearchResults([], [], [], [], []),
input: "",
index: [],
loading: False,
Expand All @@ -75,12 +75,13 @@ pub fn reset(_model: Model) {
fn compute_index(search_results: SearchResults) -> Index {
case search_results {
search_result.Start | search_result.InternalServerError -> []
search_result.SearchResults(exact, others, searches, docs) -> {
search_result.SearchResults(exact, others, searches, docs, modules) -> {
[]
|> insert_module_names(exact)
|> insert_module_names(others)
|> insert_module_names(searches)
|> insert_module_names(docs)
|> insert_module_names(modules)
|> list.map(fn(i) { pair.map_second(i, list.reverse) })
}
}
Expand Down
4 changes: 3 additions & 1 deletion apps/frontend/src/data/search_result.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub type SearchResults {
matches: List(SearchResult),
signature_searches: List(SearchResult),
docs_searches: List(SearchResult),
module_searches: List(SearchResult),
)
}

Expand All @@ -47,12 +48,13 @@ pub fn decode_search_results(dyn) {
dynamic.decode1(fn(_) { InternalServerError }, {
dynamic.field("error", dynamic.string)
}),
dynamic.decode4(
dynamic.decode5(
SearchResults,
dynamic.field("exact-matches", dynamic.list(decode_search_result)),
dynamic.field("matches", dynamic.list(decode_search_result)),
dynamic.field("searches", dynamic.list(decode_search_result)),
dynamic.field("docs-searches", dynamic.list(decode_search_result)),
dynamic.field("module-searches", dynamic.list(decode_search_result)),
),
])(dyn)
}
Expand Down
2 changes: 2 additions & 0 deletions apps/frontend/src/frontend/strings.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub const searches_match = "Matched from a document search in functions, constan

pub const docs_match = "Matched from a documentation search in functions, constants or types."

pub const modules_match = "Matched from a module name"

pub const retry_query = "Retry with a different query. You can match functions, types or constants names, as well as functions types directly!"

pub const internal_server_error = "Internal server error. The error should be fixed soon. Please, retry later."
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/src/frontend/view/body/body.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ pub fn body(model: Model) {
title: "Internal server error",
content: frontend_strings.internal_server_error,
)
search_result.SearchResults([], [], [], []) ->
search_result.SearchResults([], [], [], [], []) ->
empty_state(
image: images.shadow_lucy,
title: "No match found!",
content: frontend_strings.retry_query,
)
search_result.SearchResults(_, _, _, _) -> model.view_cache
search_result.SearchResults(_, _, _, _, _) -> model.view_cache
}
},
])
Expand Down
7 changes: 7 additions & 0 deletions apps/frontend/src/frontend/view/body/cache.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub fn cache_search_results(
others: List(search_result.SearchResult),
searches: List(search_result.SearchResult),
docs_searches: List(search_result.SearchResult),
modules_searches: List(search_result.SearchResult),
) {
s.search_results_wrapper([], [
sidebar(index),
Expand All @@ -128,6 +129,12 @@ pub fn cache_search_results(
frontend_strings.docs_match,
),
view_search_results(docs_searches),
match_title(
modules_searches,
"Module matches",
frontend_strings.modules_match,
),
view_search_results(modules_searches),
]),
])
}

0 comments on commit 69a7075

Please sign in to comment.