Skip to content

Commit

Permalink
Merge pull request #18365 from guardian/pcolley/facia_use_lite_fronts
Browse files Browse the repository at this point in the history
Use lite pressed fronts in Facia
  • Loading branch information
TBonnin authored Dec 6, 2017
2 parents 72e25be + ea53427 commit e83f728
Show file tree
Hide file tree
Showing 28 changed files with 255 additions and 123 deletions.
3 changes: 2 additions & 1 deletion applications/app/services/NewspaperQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ class NewspaperQuery(contentApiClient: ContentApiClient) extends Dates with Logg
index,
Fixed(containerDefinition),
CollectionConfigWithId(dataId.getOrElse(""), CollectionConfig.empty.copy(displayName = containerName, description = containerDescription)),
CollectionEssentials(trails, linkSnaps, containerName, dataId, None, None)
CollectionEssentials(trails, linkSnaps, containerName, dataId, None, None),
hasMore = false
).copy(hasShowMoreEnabled = false)
}

Expand Down
1 change: 1 addition & 0 deletions common/app/common/metrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
46 changes: 26 additions & 20 deletions common/app/layout/ContainerLayout.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ case class IndexedTrail(faciaContent: PressedContent, index: Int)

object ContainerLayout extends implicits.Collections {
def apply(
sliceDefinitions: Seq[Slice],
items: Seq[PressedContent],
initialContext: ContainerLayoutContext,
config: ContainerDisplayConfig,
mobileShowMore: MobileShowMore
sliceDefinitions: Seq[Slice],
items: Seq[PressedContent],
initialContext: ContainerLayoutContext,
config: ContainerDisplayConfig,
mobileShowMore: MobileShowMore,
hasMore: Boolean
): (ContainerLayout, ContainerLayoutContext) = {
val indexedTrails = items.zipWithIndex.map((IndexedTrail.apply _).tupled)

Expand All @@ -32,7 +33,7 @@ object ContainerLayout extends implicits.Collections {
(slicesSoFar :+ slice, remainingTrails, newContext)
}

(ContainerLayout(slices, showMore map { case IndexedTrail(trail, index) =>
val remainingCards = showMore.map { case IndexedTrail(trail, index) =>
FaciaCardAndIndex(
index,
FaciaCard.fromTrail(
Expand All @@ -43,7 +44,9 @@ object ContainerLayout extends implicits.Collections {
),
hideUpTo = Some(Desktop)
)
}), finalContext)
}

(ContainerLayout(slices, remainingCards, hasMore), finalContext)
}

def singletonFromContainerDefinition(
Expand All @@ -58,27 +61,30 @@ object ContainerLayout extends implicits.Collections {
)._1

def fromContainerDefinition(
containerDefinition: ContainerDefinition,
containerLayoutContext: ContainerLayoutContext,
config: ContainerDisplayConfig,
items: Seq[PressedContent]
containerDefinition: ContainerDefinition,
containerLayoutContext: ContainerLayoutContext,
config: ContainerDisplayConfig,
items: Seq[PressedContent],
hasMore: Boolean = false
): (ContainerLayout, ContainerLayoutContext) = apply(
containerDefinition.slices,
items,
containerLayoutContext,
config,
containerDefinition.mobileShowMore
containerDefinition.mobileShowMore,
hasMore
)

def fromContainer(
container: Container,
containerLayoutContext: ContainerLayoutContext,
config: ContainerDisplayConfig,
items: Seq[PressedContent]
container: Container,
containerLayoutContext: ContainerLayoutContext,
config: ContainerDisplayConfig,
items: Seq[PressedContent],
hasMore: Boolean
): Option[(ContainerLayout, ContainerLayoutContext)] =
ContainerDefinition.fromContainer(container, items) map {
definition: ContainerDefinition =>
fromContainerDefinition(definition, containerLayoutContext, config, items)
fromContainerDefinition(definition, containerLayoutContext, config, items, hasMore)
}

def forHtmlBlobs(sliceDefinitions: Seq[Slice], blobs: Seq[HtmlAndClasses]): ContainerLayout = {
Expand Down Expand Up @@ -110,7 +116,8 @@ object ContainerLayout extends implicits.Collections {

case class ContainerLayout(
slices: Seq[SliceWithCards],
remainingCards: Seq[FaciaCardAndIndex]
remainingCards: Seq[FaciaCardAndIndex],
hasMore: Boolean = false
) {
def transformCards(f: ContentCard => ContentCard): ContainerLayout = copy(
slices = slices.map(_.transformCards(f)),
Expand All @@ -120,8 +127,7 @@ case class ContainerLayout(
def hasMobileShowMore: Boolean =
slices.flatMap(_.columns.flatMap(_.cards)).exists(_.hideUpTo.contains(Mobile))

def hasDesktopShowMore: Boolean =
remainingCards.nonEmpty
def hasDesktopShowMore: Boolean = remainingCards.nonEmpty || hasMore

def hasShowMore: Boolean = hasDesktopShowMore || hasMobileShowMore
}
27 changes: 14 additions & 13 deletions common/app/layout/Front.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ object FaciaContainer {
container: Container,
config: CollectionConfigWithId,
collectionEssentials: CollectionEssentials,
hasMore: Boolean,
componentId: Option[String] = None
): FaciaContainer = {
apply(
index,
container,
ContainerDisplayConfig.withDefaults(config),
collectionEssentials,
componentId
componentId,
hasMore
)
}

Expand All @@ -122,7 +124,8 @@ object FaciaContainer {
container: Container,
config: ContainerDisplayConfig,
collectionEssentials: CollectionEssentials,
componentId: Option[String]
componentId: Option[String],
hasMore: Boolean
): FaciaContainer = fromConfig(
index,
container,
Expand All @@ -132,7 +135,8 @@ object FaciaContainer {
container,
ContainerLayoutContext.empty,
config,
collectionEssentials.items
collectionEssentials.items,
hasMore
).map(_._1),
componentId
)
Expand Down Expand Up @@ -179,6 +183,7 @@ object FaciaContainer {
container = Fixed(ContainerDefinition.fastForNumberOfItems(items.size)),
config = ContainerDisplayConfig.withDefaults(CollectionConfigWithId(dataId, CollectionConfig.empty)),
collectionEssentials = CollectionEssentials(items take 8, Nil, Some(title), href, None, None),
hasMore = false,
componentId = None
).withTimeStamps
}
Expand Down Expand Up @@ -297,7 +302,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
Expand Down Expand Up @@ -374,7 +382,7 @@ object Front extends implicits.Collections {
case Nil => accumulation
case ((config, collection), container) :: remainingConfigs =>
val (newSeen, newItems, _) = deduplicate(seenTrails, container, collection.items)
val layoutMaybe = ContainerLayout.fromContainer(container, context, config, newItems)
val layoutMaybe = ContainerLayout.fromContainer(container, context, config, newItems, hasMore = false)
val newContext = layoutMaybe.map(_._2).getOrElse(context)
val faciaContainer = FaciaContainer.fromConfig(
index,
Expand All @@ -393,13 +401,6 @@ object Front extends implicits.Collections {
)
}

def fromConfigs(configs: Seq[(CollectionConfigWithId, CollectionEssentials)]): Front = {
fromConfigsAndContainers(configs.map {
case (config, collectionEssentials) =>
((ContainerDisplayConfig.withDefaults(config), collectionEssentials), Container.fromConfig(config.config))
})
}

case class ContainersWithDeduped(containers: Vector[FaciaContainer], deduped: DedupedFrontResult)

def fromPressedPageWithDeduped(pressedPage: PressedPage,
Expand Down Expand Up @@ -428,7 +429,7 @@ object Front extends implicits.Collections {
val collectionEssentials = CollectionEssentials.fromPressedCollection(pressedCollection)
val containerDisplayConfig = ContainerDisplayConfig.withDefaults(pressedCollection.collectionConfigWithId)

val containerLayoutMaybe: Option[(ContainerLayout, ContainerLayoutContext)] = ContainerLayout.fromContainer(container, context, containerDisplayConfig, newItems)
val containerLayoutMaybe: Option[(ContainerLayout, ContainerLayoutContext)] = ContainerLayout.fromContainer(container, context, containerDisplayConfig, newItems, pressedCollection.hasMore)
val newContext: ContainerLayoutContext = containerLayoutMaybe.map(_._2).getOrElse(context)
val faciaContainer = FaciaContainer.fromConfig(
index,
Expand Down
13 changes: 12 additions & 1 deletion common/app/layout/slices/Container.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +32,16 @@ object Container extends Logging {
def fromConfig(collectionConfig: CollectionConfig): Container =
resolve(collectionConfig.collectionType)

def storiesCount(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 {
Expand Down
28 changes: 28 additions & 0 deletions common/app/model/PressedPage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ 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 {
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,
Expand Down
11 changes: 8 additions & 3 deletions common/app/model/facia/PressedCollection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ case class PressedCollection(
hideKickers: Boolean,
showDateHeader: Boolean,
showLatestUpdate: Boolean,
config: CollectionConfig
config: CollectionConfig,
hasMore: Boolean
) {

lazy val collectionConfigWithId = CollectionConfigWithId(id, config)
Expand All @@ -52,7 +53,9 @@ object PressedCollection {
collection: com.gu.facia.api.models.Collection,
curated: List[PressedContent],
backfill: List[PressedContent],
treats: List[PressedContent]): PressedCollection =
treats: List[PressedContent],
hasMore: Boolean
): PressedCollection =
PressedCollection(
collection.id,
collection.displayName,
Expand All @@ -72,5 +75,7 @@ object PressedCollection {
collection.collectionConfig.hideKickers,
collection.collectionConfig.showDateHeader,
collection.collectionConfig.showLatestUpdate,
CollectionConfig.make(collection.collectionConfig))
CollectionConfig.make(collection.collectionConfig),
hasMore
)
}
16 changes: 5 additions & 11 deletions common/app/services/S3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,19 +154,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, 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 {
Expand Down
3 changes: 2 additions & 1 deletion common/test/common/commercial/ContainerModelTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class ContainerModelTest extends FlatSpec with Matchers with OptionValues {
hideKickers = false,
showDateHeader = false,
showLatestUpdate = false,
config
config,
hasMore = false
)
}

Expand Down
Loading

0 comments on commit e83f728

Please sign in to comment.