Skip to content

Commit

Permalink
fix: ignore RC and use correct ordering according to version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Jul 26, 2024
1 parent 451f1d2 commit a59b924
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
45 changes: 29 additions & 16 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import gleam/dict.{type Dict}
import gleam/dynamic
import gleam/hexpm
import gleam/int
import gleam/io
import gleam/json
import gleam/list
import gleam/option.{type Option, None, Some}
Expand Down Expand Up @@ -422,7 +423,7 @@ pub fn upsert_package_type_fun_signature(

pub fn name_search(db: pgo.Connection, query: String) {
let query = pgo.text(query)
"SELECT DISTINCT ON (package_rank, type_name, signature_kind, module_name)
"SELECT DISTINCT ON (package_rank, ordering, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
Expand All @@ -441,7 +442,7 @@ pub fn name_search(db: pgo.Connection, query: String) {
JOIN package p
ON p.id = r.package_id
WHERE s.name = $1
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC
ORDER BY package_rank DESC, ordering DESC, type_name, signature_kind, module_name
LIMIT 100"
|> pgo.execute(db, [query], decode_type_search)
|> result.map_error(error.DatabaseError)
Expand All @@ -451,7 +452,7 @@ pub fn name_search(db: pgo.Connection, query: String) {
pub fn module_and_name_search(db: pgo.Connection, query: String) {
let query = pgo.text(query)
"WITH splitted_name AS (SELECT string_to_array($1, '.') AS full_name)
SELECT DISTINCT ON (package_rank, type_name, signature_kind, module_name)
SELECT DISTINCT ON (package_rank, ordering, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
Expand All @@ -473,7 +474,7 @@ pub fn module_and_name_search(db: pgo.Connection, query: String) {
ON true
WHERE s.name = s_n.full_name[2]
AND m.name LIKE '%' || s_n.full_name[1] || '%'
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC
ORDER BY package_rank DESC, ordering DESC, type_name, signature_kind, module_name
LIMIT 100"
|> pgo.execute(db, [query], decode_type_search)
|> result.map_error(error.DatabaseError)
Expand All @@ -494,7 +495,7 @@ fn transform_query(q: String) {
pub fn content_search(db: pgo.Connection, query: String) {
let pattern = pgo.text(transform_query(query))
let query = pgo.text(query)
"SELECT DISTINCT ON (package_rank, type_name, signature_kind, module_name)
"SELECT DISTINCT ON (package_rank, ordering, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
Expand All @@ -519,7 +520,7 @@ pub fn content_search(db: pgo.Connection, query: String) {
OR replace(s.signature_, ' ', '') ILIKE '%' || $1 || '%'
OR replace(s.signature_, ' ', '') LIKE '%' || replace($2, ' ', '%') || '%'
)
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC
ORDER BY package_rank DESC, ordering DESC, type_name, signature_kind, module_name
LIMIT 100"
|> pgo.execute(db, [query, pattern], decode_type_search)
|> result.map_error(error.DatabaseError)
Expand Down Expand Up @@ -556,7 +557,7 @@ pub fn type_search_to_json(item) {

pub fn signature_search(db: pgo.Connection, q: String) {
let query = pgo.text(q)
"SELECT DISTINCT ON (package_rank, type_name, signature_kind, module_name)
"SELECT DISTINCT ON (package_rank, ordering, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
Expand All @@ -575,7 +576,7 @@ pub fn signature_search(db: pgo.Connection, q: String) {
JOIN package p
ON p.id = r.package_id
WHERE to_tsvector('english', s.signature_) @@ websearch_to_tsquery($1)
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC
ORDER BY package_rank DESC, ordering DESC, type_name, signature_kind, module_name
LIMIT 100"
|> pgo.execute(db, [query], decode_type_search)
|> result.map_error(error.DatabaseError)
Expand All @@ -584,7 +585,7 @@ pub fn signature_search(db: pgo.Connection, q: String) {

pub fn documentation_search(db: pgo.Connection, q: String) {
let query = pgo.text(q)
"SELECT DISTINCT ON (package_rank, type_name, signature_kind, module_name)
"SELECT DISTINCT ON (package_rank, ordering, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
Expand All @@ -603,7 +604,7 @@ pub fn documentation_search(db: pgo.Connection, q: String) {
JOIN package p
ON p.id = r.package_id
WHERE to_tsvector('english', s.documentation) @@ websearch_to_tsquery($1)
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC
ORDER BY package_rank DESC, ordering DESC, type_name, signature_kind, module_name
LIMIT 100"
|> pgo.execute(db, [query], decode_type_search)
|> result.map_error(error.DatabaseError)
Expand All @@ -612,7 +613,7 @@ pub fn documentation_search(db: pgo.Connection, q: String) {

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)
"SELECT DISTINCT ON (package_rank, ordering, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
Expand All @@ -631,8 +632,16 @@ pub fn module_search(db: pgo.Connection, q: String) {
JOIN package p
ON p.id = r.package_id
WHERE m.name LIKE '%' || $1 || '%'
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC
LIMIT 100"
AND r.version = (
SELECT MAX(version)
FROM package_release r
JOIN package_module m
ON m.package_release_id = r.id
WHERE m.name LIKE '%' || 'cake' || '%'
AND version SIMILAR TO '[0-9]*.[0-9]*.[0-9]*'
AND r.id = m.package_release_id
)
ORDER BY package_rank DESC, ordering DESC, type_name, signature_kind, module_name"
|> pgo.execute(db, [query], decode_type_search)
|> result.map_error(error.DatabaseError)
|> result.map(fn(r) { r.rows })
Expand All @@ -643,7 +652,8 @@ pub fn exact_type_search(db: pgo.Connection, q: List(Int)) {
let ids =
list.index_map(q, fn(_, idx) { "$" <> int.to_string(idx + 1) })
|> string.join(", ")
{ "SELECT DISTINCT ON (package_rank, type_name, signature_kind, module_name)
{
"SELECT DISTINCT ON (package_rank, ordering, type_name, signature_kind, module_name)
s.name type_name,
s.documentation,
s.kind signature_kind,
Expand All @@ -661,8 +671,11 @@ pub fn exact_type_search(db: pgo.Connection, q: List(Int)) {
ON m.package_release_id = r.id
JOIN package p
ON p.id = r.package_id
WHERE s.id IN (" <> ids <> ")
ORDER BY package_rank DESC, type_name, signature_kind, module_name, ordering DESC" }
WHERE s.id IN ("
<> ids
<> ")
ORDER BY package_rank DESC, ordering DESC, type_name, signature_kind, module_name"
}
|> pgo.execute(db, list.map(q, pgo.int), decode_type_search)
|> result.map_error(error.DatabaseError)
|> result.map(fn(r) { r.rows })
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import backend/postgres/queries
import backend/web
import cors_builder as cors
import gleam/erlang/process
import gleam/function
import gleam/http
import gleam/int
import gleam/io
import gleam/json
import gleam/list
import gleam/option
Expand Down Expand Up @@ -93,6 +95,7 @@ fn search(query: String, ctx: Context) {
queries.module_search(ctx.db, query)
|> result.map_error(error.debug_log)
|> result.unwrap([])
|> function.tap(fn(s) { io.debug(list.length(s)) })
|> list.filter(fn(i) {
!list.contains(
list.concat([
Expand Down
42 changes: 26 additions & 16 deletions apps/frontend/src/data/model.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import gleam/int
import gleam/list
import gleam/option.{type Option}
import gleam/pair
import gleam/regex
import gleam/result
import gleam/string
import lustre/element.{type Element}
Expand Down Expand Up @@ -161,24 +162,33 @@ fn extract_package_version(
acc: Dict(String, String),
search_result: search_result.SearchResult,
) -> Dict(String, String) {
case dict.get(acc, search_result.package_name) {
Error(_) ->
dict.insert(acc, search_result.package_name, search_result.version)
Ok(content) -> {
let old =
string.split(content, ".")
|> list.map(int.parse)
|> list.map(result.unwrap(_, 0))
let new =
string.split(search_result.version, ".")
|> list.map(int.parse)
|> list.map(result.unwrap(_, 0))
case new |> is_higher(old) {
True ->
let assert Ok(re) = regex.from_string("^[0-9]*.[0-9]*.[0-9]*$")
case regex.check(re, search_result.version) {
False -> acc
True ->
case dict.get(acc, search_result.package_name) {
Error(_) ->
dict.insert(acc, search_result.package_name, search_result.version)
False -> acc
Ok(content) -> {
let old =
string.split(content, ".")
|> list.map(int.parse)
|> list.map(result.unwrap(_, 0))
let new =
string.split(search_result.version, ".")
|> list.map(int.parse)
|> list.map(result.unwrap(_, 0))
case new |> is_higher(old) {
True ->
dict.insert(
acc,
search_result.package_name,
search_result.version,
)
False -> acc
}
}
}
}
}
}

Expand Down

0 comments on commit a59b924

Please sign in to comment.