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

Do not lookup pressed fronts in S3 if front does not exist #27152

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/app/services/ConfigAgentTrait.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ object ConfigAgent extends GuLogging {
def shouldServeFront(id: String)(implicit context: ApplicationContext): Boolean =
getPathIds.contains(id) && (context.isPreview || !isFrontHidden(id))

def frontExistsInConfig(id: String)(implicit context: ApplicationContext): Boolean =
getPathIds.contains(id)

def shouldServeEditionalisedFront(edition: Edition, id: String)(implicit context: ApplicationContext): Boolean = {
shouldServeFront(s"${edition.id.toLowerCase}/$id")
}
Expand Down
94 changes: 55 additions & 39 deletions facia/app/controllers/FaciaController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ trait FaciaController
FrontHeadline.headlineNotFound
}

frontJsonFapi
.get(path, liteRequestType)
.map(_.fold[CacheableResult](notFound())(FrontHeadline.renderEmailHeadline))
.map(Cached(CacheTime.Facia))
if (!ConfigAgent.frontExistsInConfig(path)) {
successful(Cached(CacheTime.Facia)(notFound()))
} else {
frontJsonFapi
.get(path, liteRequestType)
.map(_.fold[CacheableResult](notFound())(FrontHeadline.renderEmailHeadline))
.map(Cached(CacheTime.Facia))
}

}

def renderFront(path: String): Action[AnyContent] =
Expand Down Expand Up @@ -170,11 +175,17 @@ trait FaciaController
// see https://github.com/guardian/pressreader
def renderFrontJsonMinimal(path: String): Action[AnyContent] =
Action.async { implicit request =>
frontJsonFapi.get(path, liteRequestType).map { resp =>
Cached(CacheTime.Facia)(JsonComponent.fromWritable(resp match {
case Some(pressedPage) => FapiFrontJsonMinimal.get(pressedPage)
case None => JsObject(Nil)
}))
if (!ConfigAgent.frontExistsInConfig(path)) {
successful(
Cached(CacheTime.Facia)(JsonComponent.fromWritable(JsObject(Nil))),
)
} else {
frontJsonFapi.get(path, liteRequestType).map { resp =>
Cached(CacheTime.Facia)(JsonComponent.fromWritable(resp match {
case Some(pressedPage) => FapiFrontJsonMinimal.get(pressedPage)
case None => JsObject(Nil)
}))
}
}
}

Expand Down Expand Up @@ -340,6 +351,7 @@ trait FaciaController
RevalidatableResult(Ok(showcaseWithoutDcDates).as("text/xml; charset=utf-8"), showcaseWithoutDcDates)
}

// Used by dev-build only
def renderFrontPress(path: String): Action[AnyContent] =
Action.async { implicit request => renderFrontPressResult(path) }

Expand Down Expand Up @@ -392,41 +404,45 @@ trait FaciaController

def renderShowMore(path: String, collectionId: String): Action[AnyContent] =
Action.async { implicit request =>
frontJsonFapi.get(path, fullRequestType).flatMap {
case Some(pressedPage) if request.forceDCR =>
val maybeResponse = for {
collection <- pressedPage.collections.find(_.id == collectionId)
} yield {
successful(Cached(CacheTime.Facia) {
val cards = collection.curated ++ collection.backfill

val adFreeFilteredCards = cards.filter(c => !(c.properties.isPaidFor && request.isAdFree))

implicit val pressedContentFormat = PressedContentFormat.format
JsonComponent.fromWritable(Json.toJson(adFreeFilteredCards))
})
}
maybeResponse.getOrElse { successful(Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound))) }
case Some(pressedPage) =>
val containers = Front.fromPressedPage(pressedPage, Edition(request), adFree = request.isAdFree).containers
val maybeResponse =
for {
(container, index) <- containers.zipWithIndex.find(_._1.dataId == collectionId)
containerLayout <- container.containerLayout
if (!ConfigAgent.frontExistsInConfig(path)) {
successful(Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound)))
} else {
frontJsonFapi.get(path, fullRequestType).flatMap {
case Some(pressedPage) if request.forceDCR =>
val maybeResponse = for {
collection <- pressedPage.collections.find(_.id == collectionId)
} yield {
val remainingCards: Seq[FaciaCardAndIndex] = containerLayout.remainingCards.map(_.withFromShowMore)
val adFreeFilteredCards: Seq[FaciaCardAndIndex] = if (request.isAdFree) {
remainingCards.filter(c => !checkIfPaid(c.item))
} else {
remainingCards
}
successful(Cached(CacheTime.Facia) {
JsonComponent(views.html.fragments.containers.facia_cards.showMore(adFreeFilteredCards, index))
val cards = collection.curated ++ collection.backfill

val adFreeFilteredCards = cards.filter(c => !(c.properties.isPaidFor && request.isAdFree))

implicit val pressedContentFormat = PressedContentFormat.format
JsonComponent.fromWritable(Json.toJson(adFreeFilteredCards))
})
}
maybeResponse.getOrElse { successful(Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound))) }
case Some(pressedPage) =>
val containers = Front.fromPressedPage(pressedPage, Edition(request), adFree = request.isAdFree).containers
val maybeResponse =
for {
(container, index) <- containers.zipWithIndex.find(_._1.dataId == collectionId)
containerLayout <- container.containerLayout
} yield {
val remainingCards: Seq[FaciaCardAndIndex] = containerLayout.remainingCards.map(_.withFromShowMore)
val adFreeFilteredCards: Seq[FaciaCardAndIndex] = if (request.isAdFree) {
remainingCards.filter(c => !checkIfPaid(c.item))
} else {
remainingCards
}
successful(Cached(CacheTime.Facia) {
JsonComponent(views.html.fragments.containers.facia_cards.showMore(adFreeFilteredCards, index))
})
}

maybeResponse getOrElse successful(Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound)))
case None => successful(Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound)))
maybeResponse getOrElse successful(Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound)))
case None => successful(Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound)))
}
}
}

Expand Down