Skip to content

Commit

Permalink
Implement store bulk forum endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 10, 2024
1 parent 8afbc94 commit 5961615
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
25 changes: 23 additions & 2 deletions modules/api/src/main/smithy/search.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use smithy.api#jsonName
@simpleRestJson
service SearchService {
version: "3.0.0"
operations: [Search, Count, DeleteById, DeleteByIds, Mapping, Refresh, Store]
operations: [Search, Count, DeleteById, DeleteByIds, Mapping, Refresh, Store, StoreBulkForum]
}

@readonly
Expand Down Expand Up @@ -59,6 +59,12 @@ operation Store {
errors: [InternalServerError]
}

@http(method: "POST", uri: "/store-bulk/forum", code: 200)
operation StoreBulkForum {
input: StoreBulkForumInput
errors: [InternalServerError]
}

structure SearchInput {

@required
Expand Down Expand Up @@ -119,6 +125,22 @@ structure StoreInput {
id: String
}

structure StoreBulkForumInput {
@required
sources: ForumSources
}

list ForumSources {
member: ForumSourceWithId
}

structure ForumSourceWithId {
@required
id: String
@required
source: ForumSource
}

structure Forum {
@required
text: String
Expand Down Expand Up @@ -300,7 +322,6 @@ structure TeamSource {
nbMembers: Integer
}

@adt
union Source {
forum: ForumSource
game: GameSource
Expand Down
45 changes: 26 additions & 19 deletions modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ class SearchServiceImpl(esClient: ESClient[IO])(using Logger[IO]) extends Search

import SearchServiceImpl.{ given, * }

override def storeBulkForum(sources: List[ForumSourceWithId]): IO[Unit] =
esClient
.storeBulk(
Index.Forum.transform,
sources.map(s => s.id -> s.source)
)
.handleErrorWith: e =>
error"Error in storeBulkForum: sources=$sources" *>
IO.raiseError(InternalServerError("Internal server error"))

override def store(source: Source, id: String): IO[Unit] =
val (index, src) = source.extract
esClient
.store(source.index, Id(id), source)
.store(index, Id(id), src)
.handleErrorWith: e =>
error"Error in store: source=$source, id=$id" *>
IO.raiseError(InternalServerError("Internal server error"))
Expand Down Expand Up @@ -118,25 +129,21 @@ object SearchServiceImpl:
case q: Query.Study => lila.search.Index("study")
case q: Query.Team => lila.search.Index("team")

// given Indexable[Source] = (s: Source) => writeToString(s)

import smithy4s.json.Json.given
import com.github.plokhotnyuk.jsoniter_scala.core._
given Schema[Source.ForumSource] = lila.search.spec.Source.ForumSource.schema
given Schema[Source.GameSource] = lila.search.spec.Source.GameSource.schema
given Schema[Source.StudySource] = lila.search.spec.Source.StudySource.schema
given Schema[Source.TeamSource] = lila.search.spec.Source.TeamSource.schema

given Indexable[Source] = (s: Source) =>
s match
case s: Source.ForumSource => writeToString[Source.ForumSource](s)
case s: Source.GameSource => writeToString[Source.GameSource](s)
case s: Source.StudySource => writeToString[Source.StudySource](s)
case s: Source.TeamSource => writeToString[Source.TeamSource](s)

given [A: Schema]: Indexable[A] = (a: A) => writeToString(a)
given Indexable[ForumSource | GameSource | StudySource | TeamSource] =
(a: ForumSource | GameSource | StudySource | TeamSource) =>
a match
case f: ForumSource => writeToString(f)
case g: GameSource => writeToString(g)
case s: StudySource => writeToString(s)
case t: TeamSource => writeToString(t)

extension (source: Source)
def index = source match
case s: Source.ForumSource => lila.search.Index("forum")
case s: Source.GameSource => lila.search.Index("game")
case s: Source.StudySource => lila.search.Index("study")
case s: Source.TeamSource => lila.search.Index("team")
def extract = source match
case s: Source.ForumCase => lila.search.Index("forum") -> s.forum
case s: Source.GameCase => lila.search.Index("game") -> s.game
case s: Source.StudyCase => lila.search.Index("study") -> s.study
case s: Source.TeamCase => lila.search.Index("team") -> s.team
6 changes: 6 additions & 0 deletions modules/client/src/main/scala/PlayClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class PlayClient(client: StandaloneWSClient, baseUrl: String)(using ExecutionCon

import implicits.given

override def storeBulkForum(sources: List[ForumSourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/forum")
.post(StoreBulkForumInput(sources))
.map(_ => ())

override def store(source: Source, id: String): Future[Unit] =
client
.url(s"$baseUrl/store/$id")
Expand Down
16 changes: 15 additions & 1 deletion modules/e2e/src/test/scala/CompatSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import play.api.libs.ws.ahc.*
import lila.search.spec.{ Query, Index as SpecIndex, Source }
import scala.concurrent.ExecutionContext.Implicits.*
import com.sksamuel.elastic4s.Indexable
import smithy4s.Timestamp

object CompatSuite extends weaver.IOSuite:

Expand Down Expand Up @@ -51,9 +52,22 @@ object CompatSuite extends weaver.IOSuite:
IO.fromFuture(IO(client.refresh(SpecIndex.Forum))).map(expect.same(_, ()))

test("store endpoint"): client =>
val source = Source.teamSource("names", "desc", 100)
val source = Source.team(lila.search.spec.TeamSource("names", "desc", 100))
IO.fromFuture(IO(client.store(source, "id"))).map(expect.same(_, ()))

test("store bulk forum endpoint"): client =>
val sources = List(
lila.search.spec.ForumSourceWithId(
"id1",
lila.search.spec.ForumSource("body1", "topic1", "topid1", true, Timestamp(0, 0))
),
lila.search.spec.ForumSourceWithId(
"id2",
lila.search.spec.ForumSource("body2", "topic2", "topid2", true, Timestamp(0, 0))
)
)
IO.fromFuture(IO(client.storeBulkForum(sources))).map(expect.same(_, ()))

def testAppConfig = AppConfig(
server = HttpServerConfig(ip"0.0.0.0", port"9999", shutdownTimeout = 1),
elastic = ElasticConfig("http://0.0.0.0:9200")
Expand Down

0 comments on commit 5961615

Please sign in to comment.