Skip to content

Commit

Permalink
Convert tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Dec 29, 2024
1 parent beac919 commit 8f72df9
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 552 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.env
.idea
/storage
/test/storage
build
erl_crash.dump
secrets.env
2 changes: 1 addition & 1 deletion src/ethos.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub type BagTable(k, v)

@external(erlang, "ethos_ffi", "bag_get")
@external(erlang, "ethos_ffi", "bag_new")
pub fn new() -> BagTable(k, v)

@external(erlang, "ethos_ffi", "bag_get")
Expand Down
2 changes: 1 addition & 1 deletion src/ethos_ffi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bag_new() ->

bag_get(Bag, Key) ->
Items1 = ets:lookup(Bag, Key),
Items2 = lists:map(fun(Elem) -> element(1, Elem) end, Items1),
Items2 = lists:map(fun(Elem) -> element(2, Elem) end, Items1),
{ok, Items2}.

bag_insert(Bag, Key, Value) ->
Expand Down
30 changes: 17 additions & 13 deletions src/packages.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import gleam/erlang/process
import gleam/io
import gleam/otp/actor
import gleam/otp/supervisor
import gleam/result
import mist
import packages/error.{type Error}
import packages/periodic
import packages/router
import packages/storage
import packages/syncing
import packages/text_search
import packages/web
import wisp
import wisp/wisp_mist
Expand Down Expand Up @@ -42,18 +42,22 @@ fn server() {
// generated anew each time
let secret_key_base = wisp.random_string(64)

// // Initialisation that is run per-request
// let make_context = fn() {
// web.Context(db: database, static_directory: static_directory)
// }
//
// // Start the web server
// let assert Ok(_) =
// router.handle_request(_, make_context)
// |> wisp_mist.handler(secret_key_base)
// |> mist.new
// |> mist.port(3000)
// |> mist.start_http
// Initialisation that is run per-request
let make_context = fn() {
web.Context(
db: database,
search_index: text_search.new(),
static_directory: static_directory,
)
}

// Start the web server
let assert Ok(_) =
router.handle_request(_, make_context)
|> wisp_mist.handler(secret_key_base)
|> mist.new
|> mist.port(3000)
|> mist.start_http

// Start syncing new releases periodically
let assert Ok(_) = start_hex_syncer(database, key)
Expand Down
1 change: 1 addition & 0 deletions src/packages/storage.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ fn package_to_json(package: Package) -> Json {
#("description", json.string(package.description)),
#("inserted_in_hex_at", json.int(package.inserted_in_hex_at)),
#("updated_in_hex_at", json.int(package.updated_in_hex_at)),
#("latest_version", json.string(package.latest_version)),
#("docs_url", json.string(package.docs_url)),
#("downloads_all", json.int(package.downloads_all)),
#("downloads_recent", json.int(package.downloads_recent)),
Expand Down
23 changes: 13 additions & 10 deletions src/packages/text_search.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub fn insert(
|> string.append(" ")
|> string.append(description)
|> stem_words
|> list.try_each(fn(word) { insert(index, word, name) })
|> list.try_each(fn(word) { ethos.insert(index.table, word, name) })
|> result.replace_error(error.EtsTableError)
}

pub fn update(
Expand All @@ -41,16 +42,18 @@ pub fn lookup(
index: TextSearchIndex,
phrase: String,
) -> Result(List(String), Nil) {
let words = stem_words(phrase)
use names <- result.map(list.try_map(words, ethos.get(index.table, _)))
names
|> list.flatten
|> list.fold(dict.new(), fn(counters, name) {
dict.upsert(counters, name, fn(x) { option.unwrap(x, 0) + 1 })
stem_words(phrase)
|> list.try_map(ethos.get(index.table, _))
|> result.map(fn(names) {
names
|> list.flatten
|> list.fold(dict.new(), fn(counters, name) {
dict.upsert(counters, name, fn(x) { option.unwrap(x, 0) + 1 })
})
|> dict.to_list
|> list.sort(fn(a, b) { int.compare(b.1, a.1) })
|> list.map(fn(pair) { pair.0 })
})
|> dict.to_list
|> list.sort(fn(a, b) { int.compare(a.1, b.1) })
|> list.map(fn(pair) { pair.0 })
}

fn remove(index: TextSearchIndex, name: String) -> Result(Nil, Error) {
Expand Down
Loading

0 comments on commit 8f72df9

Please sign in to comment.