Skip to content

Commit

Permalink
Merge pull request #25164 from guardian/jc/rename-top-mentions-services
Browse files Browse the repository at this point in the history
Jc/rename top mentions services
  • Loading branch information
joecowton1 authored Jun 30, 2022
2 parents ce033ca + bc466e6 commit a4498ba
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 219 deletions.
8 changes: 4 additions & 4 deletions article/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ import router.Routes
import services.ophan.SurgingContentAgentLifecycle
import services.{NewspaperBooksAndSectionsAutoRefresh, OphanApi, SkimLinksCacheLifeCycle}
import jobs.{StoreNavigationLifecycleComponent, TopicLifecycle}
import topmentions.{TopicS3Client, TopicS3ClientImpl, TopicService}
import topics.{TopicS3Client, TopicS3ClientImpl, TopicService}

class AppLoader extends FrontendApplicationLoader {
override def buildComponents(context: Context): FrontendComponents =
new BuiltInComponentsFromContext(context) with AppComponents
}

trait TopMentionsServices {
trait TopicServices {
lazy val topicS3Client: TopicS3Client = wire[TopicS3ClientImpl]
lazy val topMentionsService = wire[TopicService]
lazy val topicService = wire[TopicService]
}

trait AppComponents extends FrontendComponents with ArticleControllers with TopMentionsServices {
trait AppComponents extends FrontendComponents with ArticleControllers with TopicServices {

lazy val capiHttpClient: HttpClient = wire[CapiHttpClient]
lazy val contentApiClient = wire[ContentApiClient]
Expand Down
4 changes: 2 additions & 2 deletions article/app/controllers/ArticleControllers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import play.api.libs.ws.WSClient
import play.api.mvc.ControllerComponents
import renderers.DotcomRenderingService
import services.{NewspaperBookSectionTagAgent, NewspaperBookTagAgent}
import topmentions.{TopicS3Client, TopicService}
import topics.{TopicS3Client, TopicService}

trait ArticleControllers {
def contentApiClient: ContentApiClient
def controllerComponents: ControllerComponents
def wsClient: WSClient
def remoteRender: DotcomRenderingService
def topicS3Client: TopicS3Client
def topMentionsService: TopicService
def topicService: TopicService
implicit def appContext: ApplicationContext
lazy val bookAgent: NewspaperBookTagAgent = wire[NewspaperBookTagAgent]
lazy val bookSectionAgent: NewspaperBookSectionTagAgent = wire[NewspaperBookSectionTagAgent]
Expand Down
80 changes: 40 additions & 40 deletions article/app/controllers/LiveBlogController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import play.twirl.api.Html
import renderers.DotcomRenderingService
import services.CAPILookup
import services.dotcomponents.DotcomponentsLogger
import topmentions.TopicService
import topics.TopicService
import views.support.RenderOtherStatus
import scala.concurrent.Future

Expand All @@ -30,7 +30,7 @@ class LiveBlogController(
val controllerComponents: ControllerComponents,
ws: WSClient,
remoteRenderer: renderers.DotcomRenderingService = DotcomRenderingService(),
topMentionsService: TopicService,
topicService: TopicService,
)(implicit context: ApplicationContext)
extends BaseController
with GuLogging
Expand All @@ -45,7 +45,7 @@ class LiveBlogController(

def renderEmail(path: String): Action[AnyContent] = {
Action.async { implicit request =>
mapModel(path, ArticleBlocks, topMentionResult = None) {
mapModel(path, ArticleBlocks, topicResult = None) {
case (minute: MinutePage, _) =>
Future.successful(common.renderEmail(ArticleEmailHtmlPage.html(minute), minute))
case (blog: LiveBlogPage, _) => Future.successful(common.renderEmail(LiveBlogHtmlPage.html(blog), blog))
Expand All @@ -62,16 +62,16 @@ class LiveBlogController(
): Action[AnyContent] = {
Action.async { implicit request =>
val filter = shouldFilter(filterKeyEvents)
val topMentions = if (filter) None else getTopMentions(path, topics)
val availableTopics = topMentionsService.getTopics(path)
val topicResult = if (filter) None else getTopicResult(path, topics)
val availableTopics = topicService.getAvailableTopics(path)

page.map(ParseBlockId.fromPageParam) match {
case Some(ParsedBlockId(id)) =>
renderWithRange(
path,
PageWithBlock(id),
filter,
topMentions,
topicResult,
availableTopics,
selectedTopics = topics,
) // we know the id of a block
Expand All @@ -80,7 +80,7 @@ class LiveBlogController(
Cached(10)(WithoutRevalidationResult(NotFound)),
) // page param there but couldn't extract a block id
case None => {
topMentions match {
topicResult match {
case Some(value) =>
renderWithRange(
path,
Expand Down Expand Up @@ -116,16 +116,16 @@ class LiveBlogController(
): Action[AnyContent] = {
Action.async { implicit request: Request[AnyContent] =>
val filter = shouldFilter(filterKeyEvents)
val topMentionResult = getTopMentions(path, topics)
val range = getRange(lastUpdate, page, topMentionResult)
val availableTopics = topMentionsService.getTopics(path)
val topicResult = getTopicResult(path, topics)
val range = getRange(lastUpdate, page, topicResult)
val availableTopics = topicService.getAvailableTopics(path)

mapModel(path, range, filter, topMentionResult) {
mapModel(path, range, filter, topicResult) {
case (blog: LiveBlogPage, _) if rendered.contains(false) => getJsonForFronts(blog)
case (blog: LiveBlogPage, blocks) if request.forceDCR && lastUpdate.isEmpty =>
Future.successful(renderGuuiJson(blog, blocks, filter, availableTopics, selectedTopics = topics))
case (blog: LiveBlogPage, blocks) =>
getJson(blog, range, isLivePage, filter, blocks.requestedBodyBlocks.getOrElse(Map.empty), topMentionResult)
getJson(blog, range, isLivePage, filter, blocks.requestedBodyBlocks.getOrElse(Map.empty), topicResult)
case (minute: MinutePage, _) =>
Future.successful(common.renderJson(views.html.fragments.minuteBody(minute), minute))
case _ =>
Expand All @@ -140,13 +140,13 @@ class LiveBlogController(
path: String,
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
availableTopics: Option[Seq[AvailableTopic]],
selectedTopics: Option[String],
)(implicit
request: RequestHeader,
): Future[Result] = {
mapModel(path, range, filterKeyEvents, topMentionResult) { (page, blocks) =>
mapModel(path, range, filterKeyEvents, topicResult) { (page, blocks) =>
{
val isAmpSupported = page.article.content.shouldAmplify
val pageType: PageType = PageType(page, request, context)
Expand Down Expand Up @@ -203,9 +203,9 @@ class LiveBlogController(
private[this] def getRange(
lastUpdate: Option[String],
page: Option[String],
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
): BlockRange = {
(lastUpdate.map(ParseBlockId.fromBlockId), page.map(ParseBlockId.fromPageParam), topMentionResult) match {
(lastUpdate.map(ParseBlockId.fromBlockId), page.map(ParseBlockId.fromPageParam), topicResult) match {
case (Some(ParsedBlockId(id)), _, _) => SinceBlockId(id)
case (_, Some(ParsedBlockId(id)), _) => PageWithBlock(id)
case (_, _, Some(_)) => TopicsLiveBlog
Expand All @@ -225,7 +225,7 @@ class LiveBlogController(
isLivePage: Option[Boolean],
filterKeyEvents: Boolean,
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]] = Map.empty,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
)(implicit request: RequestHeader): Future[Result] = {
val remoteRender = !request.forceDCROff

Expand All @@ -238,7 +238,7 @@ class LiveBlogController(
filterKeyEvents,
remoteRender,
requestedBodyBlocks,
topMentionResult,
topicResult,
)
case _ => Future.successful(common.renderJson(views.html.liveblog.liveBlogBody(liveblog), liveblog))
}
Expand All @@ -248,7 +248,7 @@ class LiveBlogController(
page: PageWithStoryPackage,
lastUpdateBlockId: SinceBlockId,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
): Seq[BodyBlock] = {
val requestedBlocks = page.article.fields.blocks.toSeq.flatMap {
_.requestedBodyBlocks.getOrElse(lastUpdateBlockId.around, Seq())
Expand All @@ -260,8 +260,8 @@ class LiveBlogController(

if (filterKeyEvents) {
latestBlocks.filter(block => block.eventType == KeyEvent || block.eventType == SummaryEvent)
} else if (topMentionResult.isDefined) {
latestBlocks.filter(block => topMentionResult.get.blocks.contains(block.id))
} else if (topicResult.isDefined) {
latestBlocks.filter(block => topicResult.get.blocks.contains(block.id))
} else latestBlocks

}
Expand All @@ -270,7 +270,7 @@ class LiveBlogController(
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]],
lastUpdateBlockId: SinceBlockId,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
): Seq[Block] = {
val blocksAround = requestedBodyBlocks.getOrElse(lastUpdateBlockId.around, Seq.empty).takeWhile { block =>
block.id != lastUpdateBlockId.lastUpdate
Expand All @@ -280,8 +280,8 @@ class LiveBlogController(
blocksAround.filter(block =>
block.attributes.keyEvent.getOrElse(false) || block.attributes.summary.getOrElse(false),
)
} else if (topMentionResult.isDefined) {
blocksAround.filter(block => topMentionResult.get.blocks.contains(block.id))
} else if (topicResult.isDefined) {
blocksAround.filter(block => topicResult.get.blocks.contains(block.id))
} else blocksAround
}

Expand All @@ -300,10 +300,10 @@ class LiveBlogController(
filterKeyEvents: Boolean,
remoteRender: Boolean,
requestedBodyBlocks: scala.collection.Map[String, Seq[Block]],
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
)(implicit request: RequestHeader): Future[Result] = {
val newBlocks = getNewBlocks(page, lastUpdateBlockId, filterKeyEvents, topMentionResult)
val newCapiBlocks = getNewBlocks(requestedBodyBlocks, lastUpdateBlockId, filterKeyEvents, topMentionResult)
val newBlocks = getNewBlocks(page, lastUpdateBlockId, filterKeyEvents, topicResult)
val newCapiBlocks = getNewBlocks(requestedBodyBlocks, lastUpdateBlockId, filterKeyEvents, topicResult)

val timelineHtml = views.html.liveblog.keyEvents(
"",
Expand Down Expand Up @@ -361,13 +361,13 @@ class LiveBlogController(
path: String,
range: BlockRange,
filterKeyEvents: Boolean = false,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
)(
render: (PageWithStoryPackage, Blocks) => Future[Result],
)(implicit request: RequestHeader): Future[Result] = {
capiLookup
.lookup(path, Some(range))
.map(responseToModelOrResult(range, filterKeyEvents, topMentionResult))
.map(responseToModelOrResult(range, filterKeyEvents, topicResult))
.recover(convertApiExceptions)
.flatMap {
case Left((model, blocks)) => render(model, blocks)
Expand All @@ -378,7 +378,7 @@ class LiveBlogController(
private[this] def responseToModelOrResult(
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
)(response: ItemResponse)(implicit request: RequestHeader): Either[(PageWithStoryPackage, Blocks), Result] = {
val supportedContent: Option[ContentType] = response.content.filter(isSupported).map(Content(_))
val supportedContentResult: Either[ContentType, Result] = ModelOrResult(supportedContent, response)
Expand All @@ -395,7 +395,7 @@ class LiveBlogController(
response,
range,
filterKeyEvents,
topMentionResult,
topicResult,
).left
.map(_ -> blocks)
case unknown =>
Expand All @@ -410,17 +410,17 @@ class LiveBlogController(
filterKeyEvents.getOrElse(false)
}

def getTopMentions(blogId: String, maybeTopic: Option[String]) = {
val topMentionsResult = for {
selectedTopic <- SelectedTopic.fromString(maybeTopic)
topMentions <- topMentionsService.getSelectedTopic(blogId, selectedTopic)
} yield topMentions
def getTopicResult(blogId: String, topic: Option[String]) = {
val topicResult = for {
selectedTopic <- SelectedTopic.fromString(topic)
topicResult <- topicService.getSelectedTopic(blogId, selectedTopic)
} yield topicResult

topMentionsResult match {
case Some(_) => log.info(s"top mention result was successfully retrieved for ${maybeTopic.get}")
case None => if (maybeTopic.isDefined) log.warn(s"top mention result couldn't be retrieved for ${maybeTopic.get}")
topicResult match {
case Some(_) => log.info(s"topic was successfully retrieved for ${topic.get}")
case None => if (topic.isDefined) log.warn(s"topic result couldn't be retrieved for ${topic.get}")
}

topMentionsResult
topicResult
}
}
8 changes: 4 additions & 4 deletions article/app/jobs/TopicLifecycle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jobs
import app.LifecycleComponent
import common.{AkkaAsync, JobScheduler}
import play.api.inject.ApplicationLifecycle
import topmentions.TopicService
import topics.TopicService

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
Expand All @@ -12,7 +12,7 @@ class TopicLifecycle(
appLifeCycle: ApplicationLifecycle,
jobs: JobScheduler,
akkaAsync: AkkaAsync,
topMentionService: TopicService,
topicService: TopicService,
)(implicit ec: ExecutionContext)
extends LifecycleComponent {

Expand All @@ -27,14 +27,14 @@ class TopicLifecycle(

// refresh top mentions when app starts
akkaAsync.after1s {
topMentionService.refreshTopics()
topicService.refreshTopics()
}
}

private def scheduleJobs(): Unit = {
// This job runs every 2 minutes
jobs.scheduleEvery("TopMentionsAgentRefreshJob", 2.minutes) {
topMentionService.refreshTopics()
topicService.refreshTopics()
}
}

Expand Down
8 changes: 4 additions & 4 deletions article/app/model/LiveBlogHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object LiveBlogHelpers extends GuLogging {
liveBlog: Article,
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
): Option[LiveBlogCurrentPage] = {

val pageSize = if (liveBlog.content.tags.tags.map(_.id).contains("sport/sport")) 30 else 10
Expand All @@ -51,7 +51,7 @@ object LiveBlogHelpers extends GuLogging {
_,
range,
filterKeyEvents,
topMentionResult,
topicResult,
),
)

Expand All @@ -64,7 +64,7 @@ object LiveBlogHelpers extends GuLogging {
response: ItemResponse,
range: BlockRange,
filterKeyEvents: Boolean,
topMentionResult: Option[TopMentionsResult],
topicResult: Option[TopicResult],
): Either[LiveBlogPage, Status] = {

val pageSize = if (liveBlog.content.tags.tags.map(_.id).contains("sport/sport")) 30 else 10
Expand All @@ -76,7 +76,7 @@ object LiveBlogHelpers extends GuLogging {
blocks,
range,
filterKeyEvents,
topMentionResult,
topicResult,
)
} getOrElse None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package topmentions
package topics

import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.{GetObjectRequest, S3Object}
Expand All @@ -8,7 +8,7 @@ import conf.Configuration
import model.{TopMentionJsonParseException, TopicsApiResponse}
import play.api.libs.json.{JsError, JsSuccess, Json}
import services.S3
import topmentions.S3ObjectImplicits.RichS3Object
import topics.S3ObjectImplicits.RichS3Object
import model.TopicsApiResponse._

import scala.collection.JavaConverters._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package topmentions
package topics

import common.{Box, GuLogging}
import model.{TopicsApiResponse, TopMentionsResult, SelectedTopic, AvailableTopic}
import model.{TopicsApiResponse, TopicResult, SelectedTopic, AvailableTopic}

import scala.concurrent.{ExecutionContext, Future}

Expand All @@ -28,7 +28,7 @@ class TopicService(topicS3Client: TopicS3Client) extends GuLogging {
topicsDetails.get().flatMap(_.get(blogId))
}

def getTopics(blogId: String): Option[Seq[AvailableTopic]] = {
def getAvailableTopics(blogId: String): Option[Seq[AvailableTopic]] = {
getBlogTopicsApiResponse(blogId).map(mentions =>
mentions.results.map(mention => AvailableTopic(mention.`type`, mention.name, mention.count)),
)
Expand All @@ -41,7 +41,7 @@ class TopicService(topicS3Client: TopicS3Client) extends GuLogging {
def getSelectedTopic(
blogId: String,
topMentionEntity: SelectedTopic,
): Option[TopMentionsResult] = {
): Option[TopicResult] = {
getBlogTopicsApiResponse(blogId).flatMap(_.results.find(result => {
result.`type` == topMentionEntity.`type` && result.name == topMentionEntity.value
}))
Expand Down
Loading

0 comments on commit a4498ba

Please sign in to comment.