Skip to content

Commit

Permalink
Implement bulk endpoints for game, study, team
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 10, 2024
1 parent 5961615 commit 832e6b1
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 6 deletions.
74 changes: 68 additions & 6 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, StoreBulkForum]
operations: [Search, Count, DeleteById, DeleteByIds, Mapping, Refresh, Store, StoreBulkForum, StoreBulkGame, StoreBulkStudy, StoreBulkTeam]
}

@readonly
Expand Down Expand Up @@ -65,6 +65,24 @@ operation StoreBulkForum {
errors: [InternalServerError]
}

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

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

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

structure SearchInput {

@required
Expand Down Expand Up @@ -130,17 +148,65 @@ structure StoreBulkForumInput {
sources: ForumSources
}

structure StoreBulkGameInput {
@required
sources: GameSources
}

structure StoreBulkStudyInput {
@required
sources: StudySources
}

structure StoreBulkTeamInput {
@required
sources: TeamSources
}

list ForumSources {
member: ForumSourceWithId
}

list GameSources {
member: GameSourceWithId
}

list StudySources {
member: StudySourceWithId
}

list TeamSources {
member: TeamSourceWithId
}

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

structure TeamSourceWithId {
@required
id: String
@required
source: TeamSource
}

structure StudySourceWithId {
@required
id: String
@required
source: StudySource
}

structure GameSourceWithId {
@required
id: String
@required
source: GameSource
}

structure Forum {
@required
text: String
Expand Down Expand Up @@ -261,19 +327,15 @@ structure GameSource {
@required
@jsonName("c")
winnerColor: Integer
@required
@jsonName("a")
averageRating: Integer
@required
@jsonName("i")
hasAi: Boolean
ai: Integer
@required
@jsonName("d")
date: Timestamp // or string?
@required
@jsonName("l")
duration: Integer
@required
@jsonName("ct")
clockInit: Integer
@jsonName("ci")
Expand Down
30 changes: 30 additions & 0 deletions modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@ class SearchServiceImpl(esClient: ESClient[IO])(using Logger[IO]) extends Search

import SearchServiceImpl.{ given, * }

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

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

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

override def storeBulkForum(sources: List[ForumSourceWithId]): IO[Unit] =
esClient
.storeBulk(
Expand Down
18 changes: 18 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,24 @@ class PlayClient(client: StandaloneWSClient, baseUrl: String)(using ExecutionCon

import implicits.given

override def storeBulkTeam(sources: List[TeamSourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/team")
.post(StoreBulkTeamInput(sources))
.map(_ => ())

override def storeBulkStudy(sources: List[StudySourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/study")
.post(StoreBulkStudyInput(sources))
.map(_ => ())

override def storeBulkGame(sources: List[GameSourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/game")
.post(StoreBulkGameInput(sources))
.map(_ => ())

override def storeBulkForum(sources: List[ForumSourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/forum")
Expand Down
41 changes: 41 additions & 0 deletions modules/e2e/src/test/scala/CompatSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,47 @@ object CompatSuite extends weaver.IOSuite:
)
IO.fromFuture(IO(client.storeBulkForum(sources))).map(expect.same(_, ()))

test("store bulk game endpoint"): client =>
val sources = List(
lila.search.spec.GameSourceWithId(
"id1",
lila.search.spec
.GameSource("status1", 1, true, "perf1", 1, Timestamp(0, 0), "w1", "b1")
),
lila.search.spec.GameSourceWithId(
"id2",
lila.search.spec
.GameSource("status2", 2, false, "perf2", 2, Timestamp(0, 0), "w2", "b2")
)
)
IO.fromFuture(IO(client.storeBulkGame(sources))).map(expect.same(_, ()))

test("store bulk study endpoint"): client =>
val sources = List(
lila.search.spec.StudySourceWithId(
"id1",
lila.search.spec.StudySource("name1", "owner1", Nil, "chapter names", "chapter texts", 12, true)
),
lila.search.spec.StudySourceWithId(
"id2",
lila.search.spec.StudySource("name2", "owner2", Nil, "chapter names", "chapter texts", 12, false)
)
)
IO.fromFuture(IO(client.storeBulkStudy(sources))).map(expect.same(_, ()))

test("store bulk team endpoint"): client =>
val sources = List(
lila.search.spec.TeamSourceWithId(
"id1",
lila.search.spec.TeamSource("names1", "desc1", 100)
),
lila.search.spec.TeamSourceWithId(
"id2",
lila.search.spec.TeamSource("names2", "desc2", 200)
)
)
IO.fromFuture(IO(client.storeBulkTeam(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 832e6b1

Please sign in to comment.