From de6163439a5a0151e82b07fd006d2a4fc0a5cd97 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Tue, 28 Nov 2017 14:43:32 +0000 Subject: [PATCH 1/9] press lite versions of front pages --- common/app/layout/Front.scala | 5 +- common/app/layout/slices/Container.scala | 13 +++- common/app/model/PressedPage.scala | 16 +++++ facia-press/app/controllers/Application.scala | 1 + .../app/frontpress/FapiFrontPress.scala | 59 ++++++++++++------- 5 files changed, 71 insertions(+), 23 deletions(-) diff --git a/common/app/layout/Front.scala b/common/app/layout/Front.scala index 4eb9b1067b9a..fa7f84b9408f 100644 --- a/common/app/layout/Front.scala +++ b/common/app/layout/Front.scala @@ -297,7 +297,10 @@ object Front extends implicits.Collections { type TrailUrl = String def itemsVisible(containerDefinition: ContainerDefinition): Int = - containerDefinition.slices.flatMap(_.layout.columns.map(_.numItems)).sum + itemsVisible(containerDefinition.slices) + + def itemsVisible(slices: Seq[Slice]): Int = + slices.flatMap(_.layout.columns.map(_.numItems)).sum // Never de-duplicate snaps. def participatesInDeduplication(faciaContent: PressedContent): Boolean = faciaContent.properties.embedType.isEmpty diff --git a/common/app/layout/slices/Container.scala b/common/app/layout/slices/Container.scala index 7b7a42ffdf41..33e043942dbf 100644 --- a/common/app/layout/slices/Container.scala +++ b/common/app/layout/slices/Container.scala @@ -1,7 +1,8 @@ package layout.slices -import model.pressed.CollectionConfig +import model.pressed.{CollectionConfig, PressedContent} import common.Logging +import layout.Front import model.facia.PressedCollection import scala.collection.immutable.Iterable @@ -31,6 +32,16 @@ object Container extends Logging { def fromConfig(collectionConfig: CollectionConfig): Container = resolve(collectionConfig.collectionType) + def maxStories(collectionConfig: CollectionConfig, items: Seq[PressedContent]): Option[Int] = { + resolve(collectionConfig.collectionType) match { + case Dynamic(dynamicPackage) => dynamicPackage + .slicesFor(items.map(Story.fromFaciaContent)) + .map(Front.itemsVisible) + case Fixed(fixedContainer) => Some(Front.itemsVisible(fixedContainer.slices)) + case _ => None + } + } + def fromPressedCollection(pressedCollection: PressedCollection, omitMPU: Boolean, adFree: Boolean): Container = { val container = resolve(pressedCollection.collectionType) container match { diff --git a/common/app/model/PressedPage.scala b/common/app/model/PressedPage.scala index 388baade3bfe..da348d67b46f 100644 --- a/common/app/model/PressedPage.scala +++ b/common/app/model/PressedPage.scala @@ -60,6 +60,22 @@ object PressedPage { } } +case class PressedPageVersions(lite: PressedPage, full: PressedPage) + +object PressedPageVersions { + def fromPressedCollections(id: String, + seoData: SeoData, + frontProperties: FrontProperties, + pressedCollections: List[PressedCollectionVersions]): PressedPageVersions = { + PressedPageVersions( + PressedPage(id, seoData, frontProperties, pressedCollections.map(_.lite)), + PressedPage(id, seoData, frontProperties, pressedCollections.map(_.full)) + ) + } +} + +case class PressedCollectionVersions(lite: PressedCollection, full: PressedCollection) + case class PressedPage ( id: String, seoData: SeoData, diff --git a/facia-press/app/controllers/Application.scala b/facia-press/app/controllers/Application.scala index ca36c515b186..162691066b53 100644 --- a/facia-press/app/controllers/Application.scala +++ b/facia-press/app/controllers/Application.scala @@ -23,6 +23,7 @@ class Application(liveFapiFrontPress: LiveFapiFrontPress, draftFapiFrontPress: D def generateLivePressedFor(path: String): Action[AnyContent] = Action.async { request => liveFapiFrontPress.getPressedFrontForPath(path) + .map(_.full) .map(Json.toJson(_)) .map(Json.prettyPrint) .map(Ok.apply(_)) diff --git a/facia-press/app/frontpress/FapiFrontPress.scala b/facia-press/app/frontpress/FapiFrontPress.scala index 4e2176c50bd1..acd001eb64a6 100644 --- a/facia-press/app/frontpress/FapiFrontPress.scala +++ b/facia-press/app/frontpress/FapiFrontPress.scala @@ -14,7 +14,7 @@ import conf.Configuration import conf.switches.Switches.FaciaInlineEmbeds import contentapi.{CapiHttpClient, CircuitBreakingContentApiClient, ContentApiClient, QueryDefaults} import services.fronts.FrontsApi -import model._ +import model.{PressedPage, _} import model.facia.PressedCollection import model.pressed._ import org.joda.time.DateTime @@ -22,6 +22,7 @@ import play.api.libs.json._ import play.api.libs.ws.WSClient import services.{ConfigAgent, S3FrontsApi} import implicits.Booleans._ +import layout.slices.Container import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success} @@ -112,10 +113,9 @@ trait FapiFrontPress extends Logging { val stopWatch: StopWatch = new StopWatch val pressFuture = getPressedFrontForPath(path) - .map { pressedFront: PressedPage => - val json: String = Json.stringify(Json.toJson(pressedFront)) - FaciaPressMetrics.FrontPressContentSize.recordSample(json.getBytes.length, new DateTime()) - putPressedJson(path, json) + .map { pressedFronts: PressedPageVersions => + putPressedPage(path, pressedFronts.full) + putPressedPage(s"$path.lite", pressedFronts.lite) }.fold( e => { StatusNotification.notifyFailedJob(path, isLive = isLiveContent, e) @@ -148,7 +148,13 @@ trait FapiFrontPress extends Logging { pressFuture } - def generateCollectionJsonFromFapiClient(collectionId: String)(implicit executionContext: ExecutionContext): Response[PressedCollection] = + private def putPressedPage(path: String, pressedFront: PressedPage) = { + val json: String = Json.stringify(Json.toJson(pressedFront)) + FaciaPressMetrics.FrontPressContentSize.recordSample(json.getBytes.length, new DateTime()) + putPressedJson(path, json) + } + + def generateCollectionJsonFromFapiClient(collectionId: String)(implicit executionContext: ExecutionContext): Response[PressedCollectionVersions] = { for { collection <- FAPI.getCollection(collectionId) curated <- getCurated(collection) @@ -160,17 +166,28 @@ trait FapiFrontPress extends Logging { .contains(collection.collectionConfig.collectionType) .toOption(curated.length + backfill.length) .getOrElse(Configuration.facia.collectionCap) - val trimmedCurated = curated.take(maxStories) - val trimmedBackfill = backfill.take(maxStories - trimmedCurated.length) - PressedCollection.fromCollectionWithCuratedAndBackfill( - collection, - trimmedCurated, - trimmedBackfill, - treats + + val storyCountLite = Container.maxStories(CollectionConfig.make(collection.collectionConfig), curated ++ backfill).getOrElse(maxStories) + + PressedCollectionVersions( + pressCollection(collection, curated, backfill, treats, storyCountLite), + pressCollection(collection, curated, backfill, treats, maxStories) ) } + } + private def pressCollection(collection: Collection, curated: List[PressedContent], backfill: List[PressedContent], treats: List[PressedContent], storyCount: Int) = { + val trimmedCurated = curated.take(storyCount) + val trimmedBackfill = backfill.take(storyCount - trimmedCurated.length) + PressedCollection.fromCollectionWithCuratedAndBackfill( + collection, + trimmedCurated, + trimmedBackfill, + treats + ) + } + private def getCurated(collection: Collection)(implicit executionContext: ExecutionContext): Response[List[PressedContent]] = { // Map initial PressedContent to enhanced content which contains pre-fetched embed content. val initialContent = collectionContentWithSnaps(collection, searchApiQuery, itemApiQuery) @@ -263,14 +280,14 @@ trait FapiFrontPress extends Logging { } } - def getPressedFrontForPath(path: String)(implicit executionContext: ExecutionContext): Response[PressedPage] = { - val collectionIds = getCollectionIdsForPath(path) - collectionIds - .flatMap(c => Response.traverse(c.map(generateCollectionJsonFromFapiClient))) - .flatMap(result => - Response.Async.Right(getFrontSeoAndProperties(path).map{ - case (seoData, frontProperties) => PressedPage(path, seoData, frontProperties, result) - })) + def getPressedFrontForPath(path: String)(implicit executionContext: ExecutionContext): Response[PressedPageVersions] = { + for { + collectionIds <- getCollectionIdsForPath(path) + pressedCollectionVersions <- Response.traverse(collectionIds.map(generateCollectionJsonFromFapiClient)) + seoWithProperties <- Response.Async.Right(getFrontSeoAndProperties(path)) + } yield seoWithProperties match { + case (seoData, frontProperties) => PressedPageVersions.fromPressedCollections(path, seoData, frontProperties, pressedCollectionVersions) + } } private def getFrontSeoAndProperties(path: String)(implicit executionContext: ExecutionContext): Future[(SeoData, FrontProperties)] = From 0f50c3e19061c017e88f9332deba24e9c075148d Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Tue, 28 Nov 2017 15:14:53 +0000 Subject: [PATCH 2/9] set .lite suffix on output front --- common/app/services/S3.scala | 15 ++++----------- .../app/frontpress/FapiFrontPress.scala | 18 +++++++++--------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/common/app/services/S3.scala b/common/app/services/S3.scala index e66b1117bb5b..26df91c21ba3 100644 --- a/common/app/services/S3.scala +++ b/common/app/services/S3.scala @@ -158,19 +158,12 @@ object S3FrontsApi extends S3 { lazy val stage = Configuration.facia.stage.toUpperCase val namespace = "frontsapi" lazy val location = s"$stage/$namespace" - private val filename = "pressed.v2.json" - def getLiveFapiPressedKeyForPath(path: String): String = - s"$location/pressed/live/$path/fapi/$filename" + private def putFapiPressedJson(live: String, path: String, json: String, suffix: String): Unit = + putPrivateGzipped(s"$location/pressed/$live/$path/fapi/pressed.v2$suffix.json", json, "application/json") - def getDraftFapiPressedKeyForPath(path: String): String = - s"$location/pressed/draft/$path/fapi/$filename" - - def putLiveFapiPressedJson(path: String, json: String): Unit = - putPrivateGzipped(getLiveFapiPressedKeyForPath(path), json, "application/json") - - def putDraftFapiPressedJson(path: String, json: String): Unit = - putPrivateGzipped(getDraftFapiPressedKeyForPath(path), json, "application/json") + def putLiveFapiPressedJson(path: String, json: String, suffix: String): Unit = putFapiPressedJson("live", path, json, suffix) + def putDraftFapiPressedJson(path: String, json: String, suffix: String): Unit = putFapiPressedJson("draft", path, json, suffix) } object S3Archive extends S3 { diff --git a/facia-press/app/frontpress/FapiFrontPress.scala b/facia-press/app/frontpress/FapiFrontPress.scala index acd001eb64a6..4f832cbf8a0f 100644 --- a/facia-press/app/frontpress/FapiFrontPress.scala +++ b/facia-press/app/frontpress/FapiFrontPress.scala @@ -28,7 +28,7 @@ import scala.util.{Failure, Success} class LiveFapiFrontPress(val wsClient: WSClient, val capiClientForFrontsSeo: ContentApiClient)(implicit ec: ExecutionContext) extends FapiFrontPress { - override def putPressedJson(path: String, json: String): Unit = S3FrontsApi.putLiveFapiPressedJson(path, json) + override def putPressedJson(path: String, json: String, suffix: String): Unit = S3FrontsApi.putLiveFapiPressedJson(path, json, suffix) override def isLiveContent: Boolean = true override implicit val capiClient: ContentApiClientLogic = CircuitBreakingContentApiClient( @@ -56,7 +56,7 @@ class DraftFapiFrontPress(val wsClient: WSClient, val capiClientForFrontsSeo: Co implicit val fapiClient: ApiClient = FrontsApi.crossAccountClient - override def putPressedJson(path: String, json: String): Unit = S3FrontsApi.putDraftFapiPressedJson(path, json) + override def putPressedJson(path: String, json: String, suffix: String): Unit = S3FrontsApi.putDraftFapiPressedJson(path, json, suffix) override def isLiveContent: Boolean = false override def collectionContentWithSnaps( @@ -81,7 +81,7 @@ trait FapiFrontPress extends Logging { implicit def fapiClient: ApiClient val capiClientForFrontsSeo: ContentApiClient val wsClient: WSClient - def putPressedJson(path: String, json: String): Unit + def putPressedJson(path: String, json: String, suffix: String): Unit def isLiveContent: Boolean def collectionContentWithSnaps( @@ -114,8 +114,8 @@ trait FapiFrontPress extends Logging { val pressFuture = getPressedFrontForPath(path) .map { pressedFronts: PressedPageVersions => - putPressedPage(path, pressedFronts.full) - putPressedPage(s"$path.lite", pressedFronts.lite) + putPressedPage(path, pressedFronts.full, "") + putPressedPage(path, pressedFronts.lite, ".lite") }.fold( e => { StatusNotification.notifyFailedJob(path, isLive = isLiveContent, e) @@ -148,10 +148,10 @@ trait FapiFrontPress extends Logging { pressFuture } - private def putPressedPage(path: String, pressedFront: PressedPage) = { + private def putPressedPage(path: String, pressedFront: PressedPage, suffix: String) = { val json: String = Json.stringify(Json.toJson(pressedFront)) FaciaPressMetrics.FrontPressContentSize.recordSample(json.getBytes.length, new DateTime()) - putPressedJson(path, json) + putPressedJson(path, json, suffix) } def generateCollectionJsonFromFapiClient(collectionId: String)(implicit executionContext: ExecutionContext): Response[PressedCollectionVersions] = { @@ -283,10 +283,10 @@ trait FapiFrontPress extends Logging { def getPressedFrontForPath(path: String)(implicit executionContext: ExecutionContext): Response[PressedPageVersions] = { for { collectionIds <- getCollectionIdsForPath(path) - pressedCollectionVersions <- Response.traverse(collectionIds.map(generateCollectionJsonFromFapiClient)) + pressedCollections <- Response.traverse(collectionIds.map(generateCollectionJsonFromFapiClient)) seoWithProperties <- Response.Async.Right(getFrontSeoAndProperties(path)) } yield seoWithProperties match { - case (seoData, frontProperties) => PressedPageVersions.fromPressedCollections(path, seoData, frontProperties, pressedCollectionVersions) + case (seoData, frontProperties) => PressedPageVersions.fromPressedCollections(path, seoData, frontProperties, pressedCollections) } } From 5f0a9eadca7a81ff394984074c0be65c0c5457c9 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Tue, 28 Nov 2017 15:59:35 +0000 Subject: [PATCH 3/9] use withS3Result to return 404 on not found --- common/app/services/S3.scala | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/common/app/services/S3.scala b/common/app/services/S3.scala index 26df91c21ba3..550c1ae3cb56 100644 --- a/common/app/services/S3.scala +++ b/common/app/services/S3.scala @@ -96,13 +96,8 @@ trait S3 extends Logging { putGzipped(key, value, contentType, Private) } - def getGzipped(key: String)(implicit codec: Codec): Option[String] = { - val request = new GetObjectRequest(bucket, key) - client.map { client => - val result = client.getObject(request) - val gzippedStream = new GZIPInputStream(result.getObjectContent) - Source.fromInputStream(gzippedStream).mkString - } + def getGzipped(key: String)(implicit codec: Codec): Option[String] = withS3Result(key) { result => + Source.fromInputStream(new GZIPInputStream(result.getObjectContent)).mkString } private def putGzipped(key: String, value: String, contentType: String, accessControlList: CannedAccessControlList) { From 1fdfcc0b37ec673ab23ae38ac8720902547a0036 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Wed, 29 Nov 2017 12:14:16 +0000 Subject: [PATCH 4/9] add metrics to facia-press for lite fronts. Use sealed trait for pressed page types --- common/app/common/metrics.scala | 1 + common/app/model/PressedPage.scala | 12 ++++++++++ common/app/services/S3.scala | 5 ++-- .../app/frontpress/FapiFrontPress.scala | 24 ++++++++++++------- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/common/app/common/metrics.scala b/common/app/common/metrics.scala index efeb4269984b..45b93bd8e847 100644 --- a/common/app/common/metrics.scala +++ b/common/app/common/metrics.scala @@ -138,6 +138,7 @@ object FaciaPressMetrics { val AuPressLatencyMetric = DurationMetric("au-press-latency", StandardUnit.Milliseconds) val AllFrontsPressLatencyMetric = DurationMetric("front-press-latency", StandardUnit.Milliseconds) val FrontPressContentSize = SamplerMetric("front-press-content-size", StandardUnit.Bytes) + val FrontPressContentSizeLite = SamplerMetric("front-press-content-size-lite", StandardUnit.Bytes) val FrontDecodingLatency = DurationMetric("front-decoding-latency", StandardUnit.Milliseconds) } diff --git a/common/app/model/PressedPage.scala b/common/app/model/PressedPage.scala index da348d67b46f..7e4e600b9f3e 100644 --- a/common/app/model/PressedPage.scala +++ b/common/app/model/PressedPage.scala @@ -60,6 +60,18 @@ object PressedPage { } } +sealed trait PressedPageType { + def suffix: String +} + +case object FullType extends PressedPageType { + override def suffix = "" +} + +case object LiteType extends PressedPageType { + override def suffix = ".lite" +} + case class PressedPageVersions(lite: PressedPage, full: PressedPage) object PressedPageVersions { diff --git a/common/app/services/S3.scala b/common/app/services/S3.scala index 550c1ae3cb56..0df373495d73 100644 --- a/common/app/services/S3.scala +++ b/common/app/services/S3.scala @@ -12,6 +12,7 @@ import com.amazonaws.services.s3.model._ import com.amazonaws.util.StringInputStream import common.Logging import conf.Configuration +import model.PressedPageType import org.joda.time.{DateTime, DateTimeZone} import play.api.libs.ws.{WSClient, WSRequest} import sun.misc.BASE64Encoder @@ -157,8 +158,8 @@ object S3FrontsApi extends S3 { private def putFapiPressedJson(live: String, path: String, json: String, suffix: String): Unit = putPrivateGzipped(s"$location/pressed/$live/$path/fapi/pressed.v2$suffix.json", json, "application/json") - def putLiveFapiPressedJson(path: String, json: String, suffix: String): Unit = putFapiPressedJson("live", path, json, suffix) - def putDraftFapiPressedJson(path: String, json: String, suffix: String): Unit = putFapiPressedJson("draft", path, json, suffix) + def putLiveFapiPressedJson(path: String, json: String, pressedType: PressedPageType): Unit = putFapiPressedJson("live", path, json, pressedType.suffix) + def putDraftFapiPressedJson(path: String, json: String, pressedType: PressedPageType): Unit = putFapiPressedJson("draft", path, json, pressedType.suffix) } object S3Archive extends S3 { diff --git a/facia-press/app/frontpress/FapiFrontPress.scala b/facia-press/app/frontpress/FapiFrontPress.scala index 4f832cbf8a0f..80bd8fca8e8b 100644 --- a/facia-press/app/frontpress/FapiFrontPress.scala +++ b/facia-press/app/frontpress/FapiFrontPress.scala @@ -1,5 +1,6 @@ package frontpress +import metrics.SamplerMetric import com.gu.contentapi.client.ContentApiClientLogic import com.gu.contentapi.client.model.v1.ItemResponse import com.gu.contentapi.client.model.{ItemQuery, SearchQuery} @@ -23,12 +24,13 @@ import play.api.libs.ws.WSClient import services.{ConfigAgent, S3FrontsApi} import implicits.Booleans._ import layout.slices.Container + import scala.concurrent.{ExecutionContext, Future} import scala.util.{Failure, Success} class LiveFapiFrontPress(val wsClient: WSClient, val capiClientForFrontsSeo: ContentApiClient)(implicit ec: ExecutionContext) extends FapiFrontPress { - override def putPressedJson(path: String, json: String, suffix: String): Unit = S3FrontsApi.putLiveFapiPressedJson(path, json, suffix) + override def putPressedJson(path: String, json: String, pressedType: PressedPageType): Unit = S3FrontsApi.putLiveFapiPressedJson(path, json, pressedType) override def isLiveContent: Boolean = true override implicit val capiClient: ContentApiClientLogic = CircuitBreakingContentApiClient( @@ -56,7 +58,7 @@ class DraftFapiFrontPress(val wsClient: WSClient, val capiClientForFrontsSeo: Co implicit val fapiClient: ApiClient = FrontsApi.crossAccountClient - override def putPressedJson(path: String, json: String, suffix: String): Unit = S3FrontsApi.putDraftFapiPressedJson(path, json, suffix) + override def putPressedJson(path: String, json: String, pressedType: PressedPageType): Unit = S3FrontsApi.putDraftFapiPressedJson(path, json, pressedType) override def isLiveContent: Boolean = false override def collectionContentWithSnaps( @@ -81,7 +83,7 @@ trait FapiFrontPress extends Logging { implicit def fapiClient: ApiClient val capiClientForFrontsSeo: ContentApiClient val wsClient: WSClient - def putPressedJson(path: String, json: String, suffix: String): Unit + def putPressedJson(path: String, json: String, pressedType: PressedPageType): Unit def isLiveContent: Boolean def collectionContentWithSnaps( @@ -114,8 +116,8 @@ trait FapiFrontPress extends Logging { val pressFuture = getPressedFrontForPath(path) .map { pressedFronts: PressedPageVersions => - putPressedPage(path, pressedFronts.full, "") - putPressedPage(path, pressedFronts.lite, ".lite") + putPressedPage(path, pressedFronts.full, FullType) + putPressedPage(path, pressedFronts.lite, LiteType) }.fold( e => { StatusNotification.notifyFailedJob(path, isLive = isLiveContent, e) @@ -148,10 +150,16 @@ trait FapiFrontPress extends Logging { pressFuture } - private def putPressedPage(path: String, pressedFront: PressedPage, suffix: String) = { + private def putPressedPage(path: String, pressedFront: PressedPage, pressedType: PressedPageType): Unit = { val json: String = Json.stringify(Json.toJson(pressedFront)) - FaciaPressMetrics.FrontPressContentSize.recordSample(json.getBytes.length, new DateTime()) - putPressedJson(path, json, suffix) + + val metric: SamplerMetric = pressedType match { + case FullType => FaciaPressMetrics.FrontPressContentSize + case LiteType => FaciaPressMetrics.FrontPressContentSizeLite + } + + metric.recordSample(json.getBytes.length, new DateTime()) + putPressedJson(path, json, pressedType) } def generateCollectionJsonFromFapiClient(collectionId: String)(implicit executionContext: ExecutionContext): Response[PressedCollectionVersions] = { From 94c4b14b8e464b35d7b1c5e378d17038dfba31c7 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Wed, 29 Nov 2017 12:25:42 +0000 Subject: [PATCH 5/9] set 24 parallel json presses (same as master) --- facia/app/controllers/front/FrontJsonFapi.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facia/app/controllers/front/FrontJsonFapi.scala b/facia/app/controllers/front/FrontJsonFapi.scala index cb963bd73b6c..61be863adf35 100644 --- a/facia/app/controllers/front/FrontJsonFapi.scala +++ b/facia/app/controllers/front/FrontJsonFapi.scala @@ -11,7 +11,7 @@ import scala.concurrent.{ExecutionContext, Future} trait FrontJsonFapi extends Logging { lazy val stage: String = Configuration.facia.stage.toUpperCase val bucketLocation: String - val parallelJsonPresses = 16 + val parallelJsonPresses = 24 val futureSemaphore = new FutureSemaphore(parallelJsonPresses) def blockingOperations: BlockingOperations From 0abee2db9281479ef7cc2a26fcff4ee31263b4b4 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Thu, 30 Nov 2017 11:30:50 +0000 Subject: [PATCH 6/9] generate lite pressed front route --- facia-press/app/controllers/Application.scala | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/facia-press/app/controllers/Application.scala b/facia-press/app/controllers/Application.scala index 162691066b53..6a992a7a9bb4 100644 --- a/facia-press/app/controllers/Application.scala +++ b/facia-press/app/controllers/Application.scala @@ -1,10 +1,11 @@ package controllers +import com.gu.facia.api.Response import common.ImplicitControllerExecutionContext import conf.Configuration import conf.switches.Switches.FaciaPressOnDemand import frontpress.{DraftFapiFrontPress, LiveFapiFrontPress} -import model.NoCache +import model.{NoCache, PressedPage} import play.api.libs.json.Json import play.api.mvc._ import services.ConfigAgent @@ -21,17 +22,26 @@ class Application(liveFapiFrontPress: LiveFapiFrontPress, draftFapiFrontPress: D NoCache(Ok(ConfigAgent.contentsAsJsonString).withHeaders("Content-Type" -> "application/json")) } - def generateLivePressedFor(path: String): Action[AnyContent] = Action.async { request => - liveFapiFrontPress.getPressedFrontForPath(path) - .map(_.full) - .map(Json.toJson(_)) + def generateLivePressedFor(path: String): Action[AnyContent] = Action.async { _ => + val front = liveFapiFrontPress.getPressedFrontForPath(path).map(_.full) + frontToResult(front) + } + + def generateLiteLivePressedFor(path: String): Action[AnyContent] = Action.async { _ => + val front = liveFapiFrontPress.getPressedFrontForPath(path).map(_.lite) + frontToResult(front) + } + + private def frontToResult(front: Response[PressedPage]): Future[Result] = { + front.map(Json.toJson(_)) .map(Json.prettyPrint) .map(Ok.apply(_)) .map(NoCache.apply) .fold( apiError => InternalServerError(apiError.message), successJson => successJson.withHeaders("Content-Type" -> "application/json") - )} + ) + } private def handlePressRequest(path: String, liveOrDraft: String)(f: (String) => Future[_]): Future[Result] = if (FaciaPressOnDemand.isSwitchedOn) { From 6d6b1694799318d5d5c346016a451602b845f272 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Thu, 30 Nov 2017 11:31:50 +0000 Subject: [PATCH 7/9] rename stories count function --- common/app/layout/slices/Container.scala | 2 +- facia-press/app/frontpress/FapiFrontPress.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/app/layout/slices/Container.scala b/common/app/layout/slices/Container.scala index 33e043942dbf..e081bd3e49db 100644 --- a/common/app/layout/slices/Container.scala +++ b/common/app/layout/slices/Container.scala @@ -32,7 +32,7 @@ object Container extends Logging { def fromConfig(collectionConfig: CollectionConfig): Container = resolve(collectionConfig.collectionType) - def maxStories(collectionConfig: CollectionConfig, items: Seq[PressedContent]): Option[Int] = { + def storiesCount(collectionConfig: CollectionConfig, items: Seq[PressedContent]): Option[Int] = { resolve(collectionConfig.collectionType) match { case Dynamic(dynamicPackage) => dynamicPackage .slicesFor(items.map(Story.fromFaciaContent)) diff --git a/facia-press/app/frontpress/FapiFrontPress.scala b/facia-press/app/frontpress/FapiFrontPress.scala index 80bd8fca8e8b..cdad2c119598 100644 --- a/facia-press/app/frontpress/FapiFrontPress.scala +++ b/facia-press/app/frontpress/FapiFrontPress.scala @@ -175,7 +175,7 @@ trait FapiFrontPress extends Logging { .toOption(curated.length + backfill.length) .getOrElse(Configuration.facia.collectionCap) - val storyCountLite = Container.maxStories(CollectionConfig.make(collection.collectionConfig), curated ++ backfill).getOrElse(maxStories) + val storyCountLite = Container.storiesCount(CollectionConfig.make(collection.collectionConfig), curated ++ backfill).getOrElse(maxStories) PressedCollectionVersions( pressCollection(collection, curated, backfill, treats, storyCountLite), From c9337a684fb921102a8cd6382a206e608768b935 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Thu, 30 Nov 2017 15:55:37 +0000 Subject: [PATCH 8/9] add lite press facia press route --- facia-press/conf/routes | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/facia-press/conf/routes b/facia-press/conf/routes index 55ca209823d5..931a3947061a 100644 --- a/facia-press/conf/routes +++ b/facia-press/conf/routes @@ -2,7 +2,8 @@ GET /_healthcheck controllers.HealthCheck.healthCheck() GET / controllers.Application.index GET /debug/config controllers.Application.showCurrentConfig -GET /pressed/live/*path controllers.Application.generateLivePressedFor(path) +GET /pressed/live/*path controllers.Application.generateLivePressedFor(path) +GET /pressed/live/*path.lite controllers.Application.generateLiteLivePressedFor(path) POST /press/live/*path controllers.Application.pressLiveForPath(path) POST /press/draft/all controllers.Application.pressDraftForAll() From 7d557d81fb816fbd289684d2c51e4e0f6c578862 Mon Sep 17 00:00:00 2001 From: Peter Colley Date: Fri, 1 Dec 2017 11:21:17 +0000 Subject: [PATCH 9/9] switch order of routes test --- facia-press/conf/routes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facia-press/conf/routes b/facia-press/conf/routes index 931a3947061a..a80e3f10d33e 100644 --- a/facia-press/conf/routes +++ b/facia-press/conf/routes @@ -2,8 +2,8 @@ GET /_healthcheck controllers.HealthCheck.healthCheck() GET / controllers.Application.index GET /debug/config controllers.Application.showCurrentConfig -GET /pressed/live/*path controllers.Application.generateLivePressedFor(path) GET /pressed/live/*path.lite controllers.Application.generateLiteLivePressedFor(path) +GET /pressed/live/*path controllers.Application.generateLivePressedFor(path) POST /press/live/*path controllers.Application.pressLiveForPath(path) POST /press/draft/all controllers.Application.pressDraftForAll()