Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Return search results inside an object #36

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
solr-delete-core
solr-recreate-core
solr-recreate-dbtests-cores
solr-logs
];
};
vm = pkgs.mkShell {
Expand Down Expand Up @@ -119,6 +120,7 @@
vm-solr-create-core
vm-solr-delete-core
vm-solr-recreate-core
vm-solr-logs
solr-recreate-dbtests-cores
];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package io.renku.search.api.data
import io.bullet.borer.Encoder
import io.bullet.borer.derivation.MapBasedCodecs
import io.bullet.borer.Decoder
import sttp.tapir.Schema

final case class PageDef(
limit: Int,
Expand All @@ -40,3 +41,4 @@ object PageDef:

given Encoder[PageDef] = MapBasedCodecs.deriveEncoder
given Decoder[PageDef] = MapBasedCodecs.deriveDecoder
given Schema[PageDef] = Schema.derived
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,29 @@

package io.renku.search.api.data

import io.bullet.borer.{Decoder, Encoder}
import io.bullet.borer.NullOptions.given
import io.bullet.borer.derivation.MapBasedCodecs
import sttp.tapir.Schema

final case class PageWithTotals(
page: PageDef,
prevPage: Option[Int],
nextPage: Option[Int],
totalResult: Long,
totalPages: Int
totalPages: Int,
prevPage: Option[Int] = None,
nextPage: Option[Int] = None
)

object PageWithTotals:
given Encoder[PageWithTotals] = MapBasedCodecs.deriveEncoder
given Decoder[PageWithTotals] = MapBasedCodecs.deriveDecoder
given Schema[PageWithTotals] = Schema.derived

def apply(page: PageDef, totalResults: Long, hasMore: Boolean): PageWithTotals =
PageWithTotals(
page,
Option(page.page - 1).filter(_ > 0),
Option(page.page + 1).filter(_ => hasMore),
totalResults,
math.ceil(totalResults.toDouble / page.limit).toInt
math.ceil(totalResults.toDouble / page.limit).toInt,
Option(page.page - 1).filter(_ > 0),
Option(page.page + 1).filter(_ => hasMore)
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@

package io.renku.search.api.data

import io.bullet.borer.derivation.MapBasedCodecs
import io.bullet.borer.Encoder
import io.bullet.borer.Decoder
import sttp.tapir.Schema

final case class SearchResult(
items: Seq[SearchEntity],
pagingInfo: PageWithTotals
)

object SearchResult:
given Encoder[SearchResult] = MapBasedCodecs.deriveEncoder
given Decoder[SearchResult] = MapBasedCodecs.deriveDecoder
given Schema[SearchResult] = Schema.derived
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ object Params extends TapirCodecs with TapirBorerJson {
val total: EndpointOutput[Long] = header[Long]("x-total")
val totalPages: EndpointOutput[Int] = header[Int]("x-total-pages")

pageDef.and(prevPage).and(nextPage).and(total).and(totalPages).mapTo[PageWithTotals]
pageDef.and(total).and(totalPages).and(prevPage).and(nextPage).mapTo[PageWithTotals]
}

val searchItems: EndpointOutput[Seq[SearchEntity]] =
borerJsonBody[Seq[SearchEntity]]

val searchResult: EndpointOutput[SearchResult] =
searchItems.and(pagingInfo).mapTo[SearchResult]
borerJsonBody[SearchResult].and(pagingInfo).map(_._1)(r => (r, r.pagingInfo))
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ trait TapirCodecs:
Codec.string.mapEither(Query.parse(_))(_.render)

given Schema[Query] = Schema.anyObject[Query]
given Schema[PageDef] = Schema.derived
given Schema[QueryInput] = Schema.derived
12 changes: 10 additions & 2 deletions nix/dev-scripts.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
concatTextFile,
writeShellScriptBin,
}: rec {
}: let key = ./dev-vm-key; in rec {
redis-push = concatTextFile {
name = "redis-push";
files = [./scripts/redis-push];
Expand Down Expand Up @@ -37,6 +37,10 @@
${solr-create-core}/bin/solr-create-core "$1"
'';

solr-logs = writeShellScriptBin "solr-logs" ''
sudo nixos-container run ''${RS_CONTAINER:-rsdev} -- journalctl -efu solr.service
'';

vm-build = writeShellScriptBin "vm-build" ''
nix build .#nixosConfigurations.dev-vm.config.system.build.vm
'';
Expand All @@ -46,7 +50,11 @@
'';

vm-ssh = writeShellScriptBin "vm-ssh" ''
ssh -p $VM_SSH_PORT root@localhost "$@"
ssh -i ${key} -p $VM_SSH_PORT root@localhost "$@"
'';

vm-solr-logs = writeShellScriptBin "solr-logs" ''
${vm-ssh}/bin/vm-ssh journalctl -efu solr.service
'';

vm-solr-create-core = writeShellScriptBin "solr-create-core" ''
Expand Down
7 changes: 7 additions & 0 deletions nix/dev-vm-key
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACASODG0t0zVHAKwE/CSlmCpNlR/XiWBcsFXA9gDBMCfHwAAAJDN6ZQezemU
HgAAAAtzc2gtZWQyNTUxOQAAACASODG0t0zVHAKwE/CSlmCpNlR/XiWBcsFXA9gDBMCfHw
AAAEBoaSefL4ulXiGquSLqHHQ9rj+aZZ+YffV49VwEwrduBRI4MbS3TNUcArAT8JKWYKk2
VH9eJYFywVcD2AMEwJ8fAAAACmVpa2VAcG9yb3MBAgM=
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions nix/dev-vm-key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBI4MbS3TNUcArAT8JKWYKk2VH9eJYFywVcD2AMEwJ8f
1 change: 1 addition & 0 deletions nix/dev-vm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

users.users.root = {
password = "root";
openssh.authorizedKeys.keyFiles = [ ./dev-vm-key.pub ];
};
i18n = {defaultLocale = "de_DE.UTF-8";};
console.keyMap = "de";
Expand Down
2 changes: 1 addition & 1 deletion nix/openapi-doc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: 'http://localhost:8080/search/spec.json',
url: 'http://localhost:8080/apiv2/search/spec.json',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
Expand Down
Loading