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

Renames TopMentionsTopicType to TopicType. #25166

Merged
merged 4 commits into from
Jun 30, 2022
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
12 changes: 6 additions & 6 deletions article/app/topics/TopicS3Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.amazonaws.services.s3.model.{GetObjectRequest, S3Object}
import com.amazonaws.util.IOUtils
import common.GuLogging
import conf.Configuration
import model.{TopMentionJsonParseException, TopicsApiResponse}
import model.{TopicsJsonParseException, TopicsApiResponse}
import play.api.libs.json.{JsError, JsSuccess, Json}
import services.S3
import topics.S3ObjectImplicits.RichS3Object
Expand Down Expand Up @@ -46,7 +46,7 @@ final class TopicS3ClientImpl extends TopicS3Client with S3 with GuLogging {
client.getObject(request).parseToTopicsApiResponse
}.flatten match {
case Success(value) =>
log.info(s"got topMentionResponse from S3 for key ${key}")
log.info(s"got topicApiResponse from S3 for key ${key}")
Future.successful(value)
case Failure(exception) =>
log.error(s"S3 retrieval failed for key ${key}", exception)
Expand Down Expand Up @@ -75,14 +75,14 @@ object S3ObjectImplicits {

Json.fromJson[TopicsApiResponse](json) match {
case JsSuccess(topMentionResponse, __) =>
log.debug(s"Parsed TopMentionsDetails from S3 for key ${s3Object.getKey}")
log.debug(s"Parsed TopicDetails from S3 for key ${s3Object.getKey}")
joecowton1 marked this conversation as resolved.
Show resolved Hide resolved
Success(topMentionResponse)
case JsError(errors) =>
val errorPaths = errors.map { error => error._1.toString() }.mkString(",")
log.error(s"Error parsing topMentionResponse from S3 for key ${s3Object.getKey} paths: ${errorPaths}")
log.error(s"Error parsing topicApiResponse from S3 for key ${s3Object.getKey} paths: ${errorPaths}")
Failure(
TopMentionJsonParseException(
s"could not parse S3 TopMentionsDetails json. Errors paths(s): $errors",
TopicsJsonParseException(
s"could not parse S3 TopicDetails json. Errors paths(s): $errors",
joecowton1 marked this conversation as resolved.
Show resolved Hide resolved
),
)
}
Expand Down
4 changes: 2 additions & 2 deletions article/app/topics/TopicService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class TopicService(topicS3Client: TopicS3Client) extends GuLogging {
private val topicsDetails = Box[Option[Map[String, TopicsApiResponse]]](None)

def refreshTopics()(implicit executionContext: ExecutionContext): Future[Unit] = {
val retrievedTopMentions = topicS3Client.getListOfKeys().map { key => key.map { retrieveTopicsDetails(_) } }
val retrievedTopics = topicS3Client.getListOfKeys().map { key => key.map { retrieveTopicsDetails(_) } }

retrievedTopMentions
retrievedTopics
.flatMap(Future.sequence(_))
.map(response => {
topicsDetails send Some(response.toMap)
Expand Down
28 changes: 14 additions & 14 deletions article/test/LiveBlogControllerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import play.api.test._
import play.api.test.Helpers._
import org.scalatest.{BeforeAndAfterAll, DoNotDiscover}
import org.scalatestplus.mockito.MockitoSugar
import model.{LiveBlogPage, TopicResult, SelectedTopic, TopMentionsTopicType, AvailableTopic, TopicsLiveBlog}
import model.{LiveBlogPage, TopicResult, SelectedTopic, TopicType, AvailableTopic, TopicsLiveBlog}
import topics.{TopicS3Client, TopicService}

import scala.concurrent.Future
Expand All @@ -33,27 +33,27 @@ import scala.concurrent.Future
var fakeDcr = new DCRFake()
val topicResult = TopicResult(
name = "Fifa",
`type` = TopMentionsTopicType.Org,
`type` = TopicType.Org,
blocks = Seq("56d08042e4b0d38537b1f70b"),
count = 1,
percentage_blocks = 1.2f,
)

val fakeAvailableTopics = Vector(
AvailableTopic(TopMentionsTopicType.Gpe, "United Kingdom", 6),
AvailableTopic(TopMentionsTopicType.Gpe, "Russia", 4),
AvailableTopic(TopMentionsTopicType.Org, "KPMG", 4),
AvailableTopic(TopMentionsTopicType.Gpe, "Ukraine", 3),
AvailableTopic(TopMentionsTopicType.Gpe, "China", 2),
AvailableTopic(TopMentionsTopicType.Gpe, "United States", 2),
AvailableTopic(TopMentionsTopicType.Loc, "Europe", 2),
AvailableTopic(TopMentionsTopicType.Gpe, "Moscow", 2),
AvailableTopic(TopMentionsTopicType.Org, "PZ Cussons", 2),
AvailableTopic(TopMentionsTopicType.Person, "Emmanuel Macron", 1),
AvailableTopic(TopicType.Gpe, "United Kingdom", 6),
AvailableTopic(TopicType.Gpe, "Russia", 4),
AvailableTopic(TopicType.Org, "KPMG", 4),
AvailableTopic(TopicType.Gpe, "Ukraine", 3),
AvailableTopic(TopicType.Gpe, "China", 2),
AvailableTopic(TopicType.Gpe, "United States", 2),
AvailableTopic(TopicType.Loc, "Europe", 2),
AvailableTopic(TopicType.Gpe, "Moscow", 2),
AvailableTopic(TopicType.Org, "PZ Cussons", 2),
AvailableTopic(TopicType.Person, "Emmanuel Macron", 1),
);

when(
fakeTopicService.getSelectedTopic(path, SelectedTopic(TopMentionsTopicType.Org, "Fifa")),
fakeTopicService.getSelectedTopic(path, SelectedTopic(TopicType.Org, "Fifa")),
) thenReturn Some(
topicResult,
)
Expand Down Expand Up @@ -314,7 +314,7 @@ import scala.concurrent.Future
assertDcrCalledForLiveBlogWithBlocks(fakeDcr, expectedBlocks = Seq("56d08042e4b0d38537b1f70b"))
}

"renderArticle" should "doesn't call getTopMentionsByTopic given filterKeyEvents and topics query params are provided" in new Setup {
"renderArticle" should "doesn't call getSelectedTopic given filterKeyEvents and topics query params are provided" in new Setup {
val fakeRequest = FakeRequest(GET, s"${path}").withHeaders("host" -> "localhost:9000")

val result = liveBlogController.renderArticle(
Expand Down
16 changes: 8 additions & 8 deletions article/test/model/LiveBlogCurrentPageTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

import model.liveblog.BodyBlock.{KeyEvent, SummaryEvent}
import model.liveblog._
import model.TopMentionsTopicType.TopMentionsTopicType
import model.TopicType.TopicType
import org.joda.time.DateTime
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
Expand Down Expand Up @@ -65,7 +65,7 @@ class LiveBlogCurrentPageTest extends AnyFlatSpec with Matchers {
filterKeyEvents = false,
topicResult = Some(
TopicResult(
`type` = TopMentionsTopicType.Org,
`type` = TopicType.Org,
name = "someName",
blocks = Seq(),
count = 0,
Expand Down Expand Up @@ -376,7 +376,7 @@ class LiveBlogCurrentPageTest extends AnyFlatSpec with Matchers {
}

it should "only filters blocks by key events given both key events and top mentions are provided" in {
val topicResult = getFakeTopicResult(TopMentionsTopicType.Org, "tfl", Seq("1", "3"))
val topicResult = getFakeTopicResult(TopicType.Org, "tfl", Seq("1", "3"))
val testFakeBlocks = TestFakeBlocks(numberOfBlocks = 5, numberOfKeyEventsBlocks = 2, None)
val result =
LiveBlogCurrentPage.firstPage(
Expand All @@ -400,7 +400,7 @@ class LiveBlogCurrentPageTest extends AnyFlatSpec with Matchers {
}

it should "returns the 1st page of the topic filtered blocks" in {
val topicResult = getFakeTopicResult(TopMentionsTopicType.Org, "tfl", Seq("1", "2", "3", "4"))
val topicResult = getFakeTopicResult(TopicType.Org, "tfl", Seq("1", "2", "3", "4"))
val testFakeBlocks = TestFakeBlocks(numberOfBlocks = 5, numberOfKeyEventsBlocks = 2, None)
val result =
LiveBlogCurrentPage.firstPage(
Expand Down Expand Up @@ -670,7 +670,7 @@ class LiveBlogCurrentPageTest extends AnyFlatSpec with Matchers {
}

it should "returns the correct 2nd page of the topic filtered blocks" in {
val topicResult = getFakeTopicResult(TopMentionsTopicType.Org, "tfl", Seq("2", "3", "4", "5", "6"))
val topicResult = getFakeTopicResult(TopicType.Org, "tfl", Seq("2", "3", "4", "5", "6"))
val testFakeBlocks = TestFakeBlocks(numberOfBlocks = 8, numberOfKeyEventsBlocks = 2, None)

val expectedFirstPage = FirstPage(
Expand Down Expand Up @@ -707,7 +707,7 @@ class LiveBlogCurrentPageTest extends AnyFlatSpec with Matchers {
}

"updates" should "return only the topic filtered blocks after the lastUpdated block given lastUpdated is also a topic block" in {
val topicResult = getFakeTopicResult(TopMentionsTopicType.Org, "tfl", Seq("2", "3", "4", "5", "6"))
val topicResult = getFakeTopicResult(TopicType.Org, "tfl", Seq("2", "3", "4", "5", "6"))
val sinceBlock = SinceBlockId("5")
val testFakeBlocks = TestFakeBlocks(numberOfBlocks = 8, numberOfKeyEventsBlocks = 2, Some("5"))

Expand All @@ -723,7 +723,7 @@ class LiveBlogCurrentPageTest extends AnyFlatSpec with Matchers {
}

"updates" should "return only the topic filtered blocks after the lastUpdated block given lastUpdated is NOT a topic block" in {
val topicResult = getFakeTopicResult(TopMentionsTopicType.Org, "tfl", Seq("2", "3", "5", "6"))
val topicResult = getFakeTopicResult(TopicType.Org, "tfl", Seq("2", "3", "5", "6"))
val sinceBlockId = "4"
val testFakeBlocks = TestFakeBlocks(numberOfBlocks = 8, numberOfKeyEventsBlocks = 2, Some(sinceBlockId))

Expand All @@ -740,7 +740,7 @@ class LiveBlogCurrentPageTest extends AnyFlatSpec with Matchers {
}

private def getFakeTopicResult(
topicType: TopMentionsTopicType,
topicType: TopicType,
topicName: String,
blocks: Seq[String],
) = {
Expand Down
26 changes: 13 additions & 13 deletions article/test/topics/TopicServiceTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package topics

import model.{TopicsApiResponse, TopicResult, SelectedTopic, TopMentionsTopicType, AvailableTopic}
import model.{TopicsApiResponse, TopicResult, SelectedTopic, TopicType, AvailableTopic}
import org.scalatest.{BeforeAndAfterAll}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -22,7 +22,7 @@ class TopicServiceTest
val topicResult =
TopicResult(
name = "name1",
`type` = TopMentionsTopicType.Org,
`type` = TopicType.Org,
blocks = Seq("blockId1"),
count = 1,
percentage_blocks = 1.2f,
Expand All @@ -31,24 +31,24 @@ class TopicServiceTest
val topicResults = Seq(
TopicResult(
name = "name1",
`type` = TopMentionsTopicType.Org,
`type` = TopicType.Org,
blocks = Seq("blockId1"),
count = 1,
percentage_blocks = 1.2f,
),
TopicResult(
name = "name2",
`type` = TopMentionsTopicType.Person,
`type` = TopicType.Person,
blocks = Seq("blockId1"),
count = 10,
percentage_blocks = 1.2f,
),
)
val successResponse =
TopicsApiResponse(entity_types = Seq(TopMentionsTopicType.Org), results = Seq(topicResult), model = "model")
TopicsApiResponse(entity_types = Seq(TopicType.Org), results = Seq(topicResult), model = "model")

val successMultiResponse =
TopicsApiResponse(entity_types = Seq(TopMentionsTopicType.Org), results = topicResults, model = "model")
TopicsApiResponse(entity_types = Seq(TopicType.Org), results = topicResults, model = "model")

"refreshTopics" should "return successful future given getListOfKeys s3 call fails" in {
when(fakeClient.getListOfKeys()) thenReturn Future.failed(new Throwable(""))
Expand Down Expand Up @@ -95,7 +95,7 @@ class TopicServiceTest
val topicService = new TopicService(fakeClient)
val refreshJob = Await.result(topicService.refreshTopics(), 1.second)

val result = topicService.getSelectedTopic("key1", SelectedTopic(TopMentionsTopicType.Org, "name1"))
val result = topicService.getSelectedTopic("key1", SelectedTopic(TopicType.Org, "name1"))

result.get should equal(topicResult)
}
Expand All @@ -107,7 +107,7 @@ class TopicServiceTest
val topicService = new TopicService(fakeClient)
val refreshJob = Await.result(topicService.refreshTopics(), 1.second)

val result = topicService.getSelectedTopic("key1", SelectedTopic(TopMentionsTopicType.Org, "NAME1"))
val result = topicService.getSelectedTopic("key1", SelectedTopic(TopicType.Org, "NAME1"))

result should equal(None)
}
Expand All @@ -119,7 +119,7 @@ class TopicServiceTest
val topicService = new TopicService(fakeClient)
val refreshJob = Await.result(topicService.refreshTopics(), 1.second)

val result = topicService.getSelectedTopic("key2", SelectedTopic(TopMentionsTopicType.Org, "name1"))
val result = topicService.getSelectedTopic("key2", SelectedTopic(TopicType.Org, "name1"))

result should equal(None)
}
Expand All @@ -132,7 +132,7 @@ class TopicServiceTest
val refreshJob = Await.result(topicService.refreshTopics(), 1.second)

val result =
topicService.getSelectedTopic("key1", SelectedTopic(TopMentionsTopicType.Person, "Boris"))
topicService.getSelectedTopic("key1", SelectedTopic(TopicType.Person, "Boris"))

result should equal(None)
}
Expand All @@ -145,7 +145,7 @@ class TopicServiceTest
val refreshJob = Await.result(topicService.refreshTopics(), 1.second)

val result =
topicService.getSelectedTopic("key1", SelectedTopic(TopMentionsTopicType.Org, "someRandomOrg"))
topicService.getSelectedTopic("key1", SelectedTopic(TopicType.Org, "someRandomOrg"))

result should equal(None)
}
Expand All @@ -156,8 +156,8 @@ class TopicServiceTest
val expectedTopics =
Some(
List(
AvailableTopic(TopMentionsTopicType.Org, "name1", 1),
AvailableTopic(TopMentionsTopicType.Person, "name2", 10),
AvailableTopic(TopicType.Org, "name1", 1),
AvailableTopic(TopicType.Person, "name2", 10),
),
)

Expand Down
20 changes: 10 additions & 10 deletions common/app/model/TopMentionsModel.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package model

import common.GuLogging
import model.TopMentionsTopicType.TopMentionsTopicType
import model.TopicType.TopicType
import play.api.libs.json.{Format, Json}

case class TopicResult(
name: String,
`type`: TopMentionsTopicType,
`type`: TopicType,
blocks: Seq[String],
count: Int,
percentage_blocks: Float,
)
case class TopicsApiResponse(entity_types: Seq[TopMentionsTopicType], results: Seq[TopicResult], model: String)
case class TopicsApiResponse(entity_types: Seq[TopicType], results: Seq[TopicResult], model: String)

case class TopMentionJsonParseException(message: String) extends Exception(message)
case class TopicsJsonParseException(message: String) extends Exception(message)

object TopicsApiResponse {
implicit val TopicResultJf: Format[TopicResult] = Json.format[TopicResult]
implicit val TopicsApiResponseJf: Format[TopicsApiResponse] = Json.format[TopicsApiResponse]
}

case class AvailableTopic(
`type`: TopMentionsTopicType,
`type`: TopicType,
value: String,
count: Int,
)
Expand All @@ -30,7 +30,7 @@ object AvailableTopic {
implicit val AvailableTopicJf: Format[AvailableTopic] = Json.format[AvailableTopic]
}

case class SelectedTopic(`type`: TopMentionsTopicType, value: String)
case class SelectedTopic(`type`: TopicType, value: String)

object SelectedTopic extends GuLogging {

Expand All @@ -40,7 +40,7 @@ object SelectedTopic extends GuLogging {
topic.flatMap { f =>
val filterEntity = f.split(":")
if (filterEntity.length == 2) {
val entityType = TopMentionsTopicType.withNameOpt(filterEntity(0))
val entityType = TopicType.withNameOpt(filterEntity(0))
if (entityType.isEmpty) {
log.warn(s"topics query parameter entity ${filterEntity(0)} is invalid")
None
Expand All @@ -56,8 +56,8 @@ object SelectedTopic extends GuLogging {
}
}

object TopMentionsTopicType extends Enumeration {
type TopMentionsTopicType = Value
object TopicType extends Enumeration {
type TopicType = Value

val Org = Value(1, "ORG")
val Product = Value(2, "PRODUCT")
Expand All @@ -68,5 +68,5 @@ object TopMentionsTopicType extends Enumeration {

def withNameOpt(s: String): Option[Value] = values.find(_.toString == s.toUpperCase)

implicit val format: Format[TopMentionsTopicType] = Json.formatEnum(this)
implicit val format: Format[TopicType] = Json.formatEnum(this)
}
14 changes: 7 additions & 7 deletions common/test/model/TopMentionsModelTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package model
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class TopMentionsModelTest extends AnyFlatSpec with Matchers {
class TopicsModelTest extends AnyFlatSpec with Matchers {
"getFilterEntityFromTopicsParam" should "return none when no topic is provided" in {
val result = SelectedTopic.fromString(None)

Expand All @@ -17,7 +17,7 @@ class TopMentionsModelTest extends AnyFlatSpec with Matchers {
result should be(None)
}

it should "return none when the provided topic does not exist as a valid TopMentionsTopicType" in {
it should "return none when the provided topic does not exist as a valid TopicType" in {
val topic = Some("organization:nhs")
val result = SelectedTopic.fromString(topic)

Expand All @@ -26,11 +26,11 @@ class TopMentionsModelTest extends AnyFlatSpec with Matchers {

it should "be case insensitive on topic type" in {
val topics = Seq(
TestCase("ORG:someEntityValue", SelectedTopic(TopMentionsTopicType.Org, "someEntityValue")),
TestCase("Org:someEntityValue", SelectedTopic(TopMentionsTopicType.Org, "someEntityValue")),
TestCase("Product:someEntityValue", SelectedTopic(TopMentionsTopicType.Product, "someEntityValue")),
TestCase("PRODUCT:someEntityValue", SelectedTopic(TopMentionsTopicType.Product, "someEntityValue")),
TestCase("person:someEntityValue", SelectedTopic(TopMentionsTopicType.Person, "someEntityValue")),
TestCase("ORG:someEntityValue", SelectedTopic(TopicType.Org, "someEntityValue")),
TestCase("Org:someEntityValue", SelectedTopic(TopicType.Org, "someEntityValue")),
TestCase("Product:someEntityValue", SelectedTopic(TopicType.Product, "someEntityValue")),
TestCase("PRODUCT:someEntityValue", SelectedTopic(TopicType.Product, "someEntityValue")),
TestCase("person:someEntityValue", SelectedTopic(TopicType.Person, "someEntityValue")),
)
topics foreach { topic =>
val result = SelectedTopic.fromString(Some(topic.topic))
Expand Down