-
Notifications
You must be signed in to change notification settings - Fork 554
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
Facia presses 'lite' versions of fronts as well as full versions #18364
Changes from 5 commits
de61634
0f50c3e
5f0a9ea
1fdfcc0
94c4b14
0abee2d
6d6b169
c9337a6
5547069
84ecd67
7d557d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ class Application(liveFapiFrontPress: LiveFapiFrontPress, draftFapiFrontPress: D | |
|
||
def generateLivePressedFor(path: String): Action[AnyContent] = Action.async { request => | ||
liveFapiFrontPress.getPressedFrontForPath(path) | ||
.map(_.full) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a route like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can do, I will add it |
||
.map(Json.toJson(_)) | ||
.map(Json.prettyPrint) | ||
.map(Ok.apply(_)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} | ||
|
@@ -14,20 +15,22 @@ 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 | ||
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} | ||
|
||
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, pressedType: PressedPageType): Unit = S3FrontsApi.putLiveFapiPressedJson(path, json, pressedType) | ||
override def isLiveContent: Boolean = true | ||
|
||
override implicit val capiClient: ContentApiClientLogic = CircuitBreakingContentApiClient( | ||
|
@@ -55,7 +58,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, pressedType: PressedPageType): Unit = S3FrontsApi.putDraftFapiPressedJson(path, json, pressedType) | ||
override def isLiveContent: Boolean = false | ||
|
||
override def collectionContentWithSnaps( | ||
|
@@ -80,7 +83,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, pressedType: PressedPageType): Unit | ||
def isLiveContent: Boolean | ||
|
||
def collectionContentWithSnaps( | ||
|
@@ -112,10 +115,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, FullType) | ||
putPressedPage(path, pressedFronts.lite, LiteType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happen if one of the put fails? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then the two pressed files will not match and the error will be logged. This shouldn't be a problem. It's already possible for the pressed page to be updated between the browser retrieving the initial pressed page, and the show-more request. |
||
}.fold( | ||
e => { | ||
StatusNotification.notifyFailedJob(path, isLive = isLiveContent, e) | ||
|
@@ -148,7 +150,19 @@ trait FapiFrontPress extends Logging { | |
pressFuture | ||
} | ||
|
||
def generateCollectionJsonFromFapiClient(collectionId: String)(implicit executionContext: ExecutionContext): Response[PressedCollection] = | ||
private def putPressedPage(path: String, pressedFront: PressedPage, pressedType: PressedPageType): Unit = { | ||
val json: String = Json.stringify(Json.toJson(pressedFront)) | ||
|
||
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] = { | ||
for { | ||
collection <- FAPI.getCollection(collectionId) | ||
curated <- getCurated(collection) | ||
|
@@ -160,17 +174,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 +288,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) | ||
pressedCollections <- Response.traverse(collectionIds.map(generateCollectionJsonFromFapiClient)) | ||
seoWithProperties <- Response.Async.Right(getFrontSeoAndProperties(path)) | ||
} yield seoWithProperties match { | ||
case (seoData, frontProperties) => PressedPageVersions.fromPressedCollections(path, seoData, frontProperties, pressedCollections) | ||
} | ||
} | ||
|
||
private def getFrontSeoAndProperties(path: String)(implicit executionContext: ExecutionContext): Future[(SeoData, FrontProperties)] = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
storiesCount
would be a better name for this function?