Skip to content

Commit 306879e

Browse files
committed
Performance improvements.
1 parent 3c5926f commit 306879e

6 files changed

Lines changed: 55 additions & 20 deletions

File tree

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ lazy val commonSettings = Seq(
1313
resolvers += Resolver.bintrayRepo("jrouly", "sbt-release"),
1414
organization := "net.rouly",
1515
scalaVersion := "2.11.12",
16-
version := "1.1",
16+
version := "1.2",
1717
name := s"employability-${name.value}"
1818
) ++ Bintray.settings
1919

web/app/net/rouly/employability/web/api/ApiController.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
package net.rouly.employability.web.api
22

3+
import net.rouly.common.server.play.implicits.ResultBodyImplicits._
34
import net.rouly.employability.models.Topic
45
import net.rouly.employability.web.elasticsearch.{DocumentService, TopicService}
56
import play.api.libs.json.{Format, Json}
67
import play.api.mvc.{AbstractController, ControllerComponents}
78

9+
import scala.concurrent.ExecutionContext
10+
811
class ApiController(
912
cc: ControllerComponents,
1013
documentService: DocumentService,
1114
topicService: TopicService
12-
) extends AbstractController(cc) {
15+
)(implicit ec: ExecutionContext)
16+
extends AbstractController(cc) {
1317

1418
def allTopics = {
1519
Action(topicService.topicSource.chunkedResponse)
1620
}
1721

1822
def topicById(id: String) = {
19-
Action {
23+
Action.async {
2024
topicService
21-
.topicSource
22-
.filter(_.id == id)
23-
.take(1)
24-
.chunkedResponse
25+
.topicById(id)
26+
.toJsonResult
2527
}
2628
}
2729

2830
def docsByTopicId(id: String) = {
29-
Action(documentService.documentsByTopic(id).chunkedResponse)
31+
Action.async {
32+
documentService
33+
.documentById(id)
34+
.toJsonResult
35+
}
3036
}
3137

3238
}

web/app/net/rouly/employability/web/application/ElasticsearchController.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ class ElasticsearchController(
3939

4040
def allTopics = {
4141
Action.async {
42-
for {
43-
topics <- topicService.topicSource.runWith(Sink.collection)
44-
} yield Ok(application.topics(topics.toList.sortBy(_.id.toInt))).cached
42+
topicService
43+
.topicSource
44+
.runWith(Sink.collection)
45+
.map(_.toList.sortBy(_.id.toInt))
46+
.as(application.topics.apply)
47+
.cached
4548
}
4649
}
4750

4851
def topicById(id: String) = {
4952
Action.async {
5053
for {
51-
topic <- topicService.topicSource.filter(_.id == id).runWith(Sink.headOption)
54+
topic <- topicService.topicById(id)
5255
docs <- documentService.documentsByTopic(id).take(10).runWith(Sink.collection)
53-
} yield {
54-
topic.render(application.topic(_, docs.toList)).cached
55-
}
56+
} yield Ok(application.topic(topic, docs.toList)).cached
5657
}
5758
}
5859

@@ -67,9 +68,10 @@ class ElasticsearchController(
6768

6869
def docById(id: String) = {
6970
Action.async {
70-
for {
71-
doc <- documentService.documentSource.filter(_.id == id).runWith(Sink.headOption)
72-
} yield doc.render(application.document.apply).cached
71+
documentService
72+
.documentById(id)
73+
.as(application.document.apply)
74+
.cached
7375
}
7476
}
7577

web/app/net/rouly/employability/web/application/package.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,33 @@ package net.rouly.employability.web
33
import play.api.mvc.{Result, Results}
44
import play.twirl.api.Html
55

6+
import scala.concurrent.{ExecutionContext, Future}
7+
68
package object application {
79

8-
implicit class OptionalRender[T](option: Option[T]) {
9-
def render(r: T => Html): Result = option match {
10+
implicit class RenderOption[T](option: Option[T]) {
11+
def as(r: T => Html): Result = option match {
1012
case Some(t) => Results.Ok(r(t))
1113
case None => Results.NotFound
1214
}
1315
}
1416

17+
implicit class RenderT[T](t: T) {
18+
def as(r: T => Html): Result = Results.Ok(r(t))
19+
}
20+
21+
implicit class RenderFutureT[T](ft: Future[T]) {
22+
def as(r: T => Html)(implicit ec: ExecutionContext): Future[Result] = ft.map(t => Results.Ok(r(t)))
23+
}
24+
1525
implicit class RichResult(result: Result) {
1626
def cached: Result = result.withHeaders("Cache-Control" -> "public")
1727
def notCached: Result = result.withHeaders("Cache-Control" -> "no-store")
1828
}
1929

30+
implicit class RichFutureResult(fresult: Future[Result]) {
31+
def cached(implicit ec: ExecutionContext): Future[Result] = fresult.map(_.withHeaders("Cache-Control" -> "public"))
32+
def notCached(implicit ec: ExecutionContext): Future[Result] = fresult.map(_.withHeaders("Cache-Control" -> "no-store"))
33+
}
34+
2035
}

web/app/net/rouly/employability/web/elasticsearch/DocumentService.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class DocumentService(elasticsearch: ElasticsearchModule)(implicit ec: Execution
2626
.map(_.to[ModeledDocument])
2727
}
2828

29+
def documentById(docId: String): Future[ModeledDocument] = {
30+
execute {
31+
get(docId).from(elasticsearch.config.modeledDocumentIndex)
32+
}.map(_.result.to[ModeledDocument])
33+
}
34+
2935
def documentsByTopic(topicId: String): Source[ModeledDocument, NotUsed] = {
3036
val weightQuery = rangeQuery("weightedTopics.weight").gt(0.1)
3137
val topicIdQuery = termQuery("weightedTopics.topic.id", topicId)

web/app/net/rouly/employability/web/elasticsearch/TopicService.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ class TopicService(elasticsearch: ElasticsearchModule)(implicit ec: ExecutionCon
2525
.map(_.to[Topic])
2626
}
2727

28+
def topicById(topicId: String): Future[Topic] = {
29+
execute {
30+
get(topicId).from(elasticsearch.config.topicIndex)
31+
}.map(_.result.to[Topic])
32+
}
33+
2834
}

0 commit comments

Comments
 (0)