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

IMAGEDAM 2050: Graphics usage placeholder #4386

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ object MappingTest {
)),
downloadUsageMetadata = Some(DownloadUsageMetadata(
downloadedBy = "me"
)),
graphicsUsageMetadata = Some(GraphicsUsageMetadata(
addedBy = "me"
))
)),
leases = LeasesByMedia.build(List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ object Mappings {
keywordField("downloadedBy")
))

def graphicsUsageMetadata(name: String): ObjectField = nonDynamicObjectField(name).copy(properties = Seq(
keywordField("addedBy")
))

def usagesMapping(name: String): NestedField = nestedField(name).copy(properties = Seq(
keywordField("id"),
sStemmerAnalysed("title"),
Expand All @@ -310,7 +314,8 @@ object Mappings {
digitalUsageMetadata("digitalUsageMetadata"),
syndicationUsageMetadata("syndicationUsageMetadata"),
frontUsageMetadata("frontUsageMetadata"),
downloadUsageMetadata("downloadUsageMetadata")
downloadUsageMetadata("downloadUsageMetadata"),
graphicsUsageMetadata("graphicsUsageMetadata")
))

def leaseMapping(name: String): ObjectField = nonDynamicObjectField(name).copy(properties = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ object ItemToMediaUsage {
.map(_.asScala.toMap).flatMap(buildFront),
Option(item.getMap[Any]("download_metadata"))
.map(_.asScala.toMap).flatMap(buildDownload),
Option(item.getMap[Any]("graphics_metadata"))
.map(_.asScala.toMap).flatMap(buildGraphics),
new DateTime(item.getLong("last_modified")),
Try {
item.getLong("date_added")
Expand Down Expand Up @@ -99,4 +101,12 @@ object ItemToMediaUsage {
)
}.toOption
}

private def buildGraphics(metadataMap: Map[String, Any]): Option[GraphicsUsageMetadata] = {
Try {
GraphicsUsageMetadata(
metadataMap("addedBy").asInstanceOf[String]
)
}
}.toOption
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ object UsageBuilder {
usage.digitalUsageMetadata,
usage.syndicationUsageMetadata,
usage.frontUsageMetadata,
usage.downloadUsageMetadata
usage.downloadUsageMetadata,
usage.graphicsUsageMetadata
)

private def buildStatusString(usage: MediaUsage): UsageStatus = if (usage.isRemoved) RemovedUsageStatus else usage.status
Expand All @@ -28,11 +29,13 @@ object UsageBuilder {
}

private def buildUsageReference(usage: MediaUsage): List[UsageReference] = {

usage.usageType match {
case DigitalUsage => buildDigitalUsageReference(usage)
case PrintUsage => buildPrintUsageReference(usage)
case SyndicationUsage => buildSyndicationUsageReference(usage)
case DownloadUsage => buildDownloadUsageReference(usage)
case GraphicsUsage => buildGraphicsUsageReference(usage)
}
}

Expand Down Expand Up @@ -80,4 +83,14 @@ object UsageBuilder {
}).getOrElse(
List[UsageReference]()
)

private def buildGraphicsUsageReference(usage: MediaUsage): List[UsageReference] = usage.graphicsUsageMetadata.map (metadata => {
List(
UsageReference(
GraphicsUsageReference,None, name = Some(metadata.addedBy)
)
)
}).getOrElse(
List[UsageReference]()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gu.mediaservice.model.usage

import play.api.libs.json.{Json, Reads, Writes}

case class GraphicsUsageMetadata(
addedBy: String
) extends UsageMetadata {
override def toMap: Map[String, Any] = Map(
"addedBy" -> addedBy
)
}

object GraphicsUsageMetadata {
implicit val reader: Reads[GraphicsUsageMetadata] = Json.reads[GraphicsUsageMetadata]
implicit val writer: Writes[GraphicsUsageMetadata] = Json.writes[GraphicsUsageMetadata]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ case class MediaUsageKey(
grouping: String,
)
case class MediaUsage(
usageId: UsageId,
grouping: String,
mediaId: String,
usageType: UsageType,
mediaType: String,
status: UsageStatus,
printUsageMetadata: Option[PrintUsageMetadata],
digitalUsageMetadata: Option[DigitalUsageMetadata],
syndicationUsageMetadata: Option[SyndicationUsageMetadata],
frontUsageMetadata: Option[FrontUsageMetadata],
downloadUsageMetadata: Option[DownloadUsageMetadata],
lastModified: DateTime,
dateAdded: Option[DateTime] = None,
dateRemoved: Option[DateTime] = None
usageId: UsageId,
grouping: String,
mediaId: String,
usageType: UsageType,
mediaType: String,
status: UsageStatus,
printUsageMetadata: Option[PrintUsageMetadata],
digitalUsageMetadata: Option[DigitalUsageMetadata],
syndicationUsageMetadata: Option[SyndicationUsageMetadata],
frontUsageMetadata: Option[FrontUsageMetadata],
downloadUsageMetadata: Option[DownloadUsageMetadata],
graphicsUsageMetadata: Option[GraphicsUsageMetadata],
lastModified: DateTime,
dateAdded: Option[DateTime] = None,
dateRemoved: Option[DateTime] = None
) extends GridLogging {

def isGridLikeId(implicit logMarker: LogMarker): Boolean = {
Expand Down Expand Up @@ -60,7 +61,8 @@ case class MediaUsage(
digitalUsageMetadata,
syndicationUsageMetadata,
frontUsageMetadata,
downloadUsageMetadata
downloadUsageMetadata,
graphicsUsageMetadata
// NOTE that we don't compare any date fields
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import org.joda.time.DateTime
import play.api.libs.functional.syntax._

case class Usage(
id: String,
references: List[UsageReference],
platform: UsageType,
media: String,
status: UsageStatus,
dateAdded: Option[DateTime],
dateRemoved: Option[DateTime],
lastModified: DateTime,
id: String,
references: List[UsageReference],
platform: UsageType,
media: String,
status: UsageStatus,
dateAdded: Option[DateTime],
dateRemoved: Option[DateTime],
lastModified: DateTime,

// TODO collapse this field into an `Option[UsageMetadata]`
printUsageMetadata: Option[PrintUsageMetadata] = None,
digitalUsageMetadata: Option[DigitalUsageMetadata] = None,
syndicationUsageMetadata: Option[SyndicationUsageMetadata] = None,
frontUsageMetadata: Option[FrontUsageMetadata] = None,
downloadUsageMetadata: Option[DownloadUsageMetadata] = None
// TODO collapse this field into an `Option[UsageMetadata]`
printUsageMetadata: Option[PrintUsageMetadata] = None,
digitalUsageMetadata: Option[DigitalUsageMetadata] = None,
syndicationUsageMetadata: Option[SyndicationUsageMetadata] = None,
frontUsageMetadata: Option[FrontUsageMetadata] = None,
downloadUsageMetadata: Option[DownloadUsageMetadata] = None,
graphicsUsageMetadata: Option[GraphicsUsageMetadata] = None
)
object Usage {
import com.gu.mediaservice.lib.formatting._
Expand All @@ -37,7 +38,8 @@ object Usage {
(__ \ "digitalUsageMetadata").writeNullable[DigitalUsageMetadata] ~
(__ \ "syndicationUsageMetadata").writeNullable[SyndicationUsageMetadata] ~
(__ \ "frontUsageMetadata").writeNullable[FrontUsageMetadata] ~
(__ \ "downloadUsageMetadata").writeNullable[DownloadUsageMetadata]
(__ \ "downloadUsageMetadata").writeNullable[DownloadUsageMetadata] ~
(__ \ "graphicsUsageMetadata").writeNullable[GraphicsUsageMetadata]
)(unlift(Usage.unapply))

implicit val reads: Reads[Usage] = (
Expand All @@ -53,6 +55,7 @@ object Usage {
(__ \ "digitalUsageMetadata").readNullable[DigitalUsageMetadata] ~
(__ \ "syndicationUsageMetadata").readNullable[SyndicationUsageMetadata] ~
(__ \ "frontUsageMetadata").readNullable[FrontUsageMetadata] ~
(__ \ "downloadUsageMetadata").readNullable[DownloadUsageMetadata]
(__ \ "downloadUsageMetadata").readNullable[DownloadUsageMetadata] ~
(__ \ "graphicsUsageMetadata").readNullable[GraphicsUsageMetadata]
)(Usage.apply _)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ trait UsageReferenceType {
case SyndicationUsageReference => "syndication"
case FrontUsageReference => "front"
case DownloadUsageReference => "download"
case GraphicsUsageReference => "graphics"
}
}

Expand All @@ -24,6 +25,7 @@ object UsageReferenceType {
case "syndication" => SyndicationUsageReference
case "front" => FrontUsageReference
case "download" => DownloadUsageReference
case "graphics" => GraphicsUsageReference
}
}

Expand All @@ -33,3 +35,4 @@ object ComposerUsageReference extends UsageReferenceType
object SyndicationUsageReference extends UsageReferenceType
object FrontUsageReference extends UsageReferenceType
object DownloadUsageReference extends UsageReferenceType
object GraphicsUsageReference extends UsageReferenceType
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sealed trait UsageStatus {
case DownloadedUsageStatus => "downloaded"
case FailedUsageStatus => "failed"
case UnknownUsageStatus => "unknown"
case GraphicsUsageStatus => "graphics"
}
}

Expand All @@ -23,6 +24,7 @@ object UsageStatus {
case "downloaded" => DownloadedUsageStatus
case "failed" => FailedUsageStatus
case "unknown" => UnknownUsageStatus
case "graphics" => GraphicsUsageStatus
case _ => throw new IllegalArgumentException("Invalid usage status")
}

Expand All @@ -39,6 +41,7 @@ object RemovedUsageStatus extends UsageStatus
object SyndicatedUsageStatus extends UsageStatus
object DownloadedUsageStatus extends UsageStatus
object FailedUsageStatus extends UsageStatus
object GraphicsUsageStatus extends UsageStatus

// For Fronts usages as we don't know if a front is in draft or is live
// TODO remove this once we do!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ trait UsageType {
case DigitalUsage => "digital"
case SyndicationUsage => "syndication"
case DownloadUsage => "download"
case GraphicsUsage => "graphics"
}
}

Expand All @@ -20,10 +21,12 @@ object UsageType {
case "digital" => DigitalUsage
case "syndication" => SyndicationUsage
case "download" => DownloadUsage
case "graphics" => GraphicsUsage
}
}

object PrintUsage extends UsageType
object DigitalUsage extends UsageType
object SyndicationUsage extends UsageType
object DownloadUsage extends UsageType
object GraphicsUsage extends UsageType
24 changes: 24 additions & 0 deletions usage/app/controllers/UsageApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,30 @@ class UsageApi(
)
}}

def setGraphicsUsages() = auth(parse.json) { req =>

val request = (req.body \ "data").validate[GraphicsUsageRequest]
request.fold(
e => respondError(
BadRequest,
errorKey = "graphics-usage-parse-failed",
errorMessage = JsError.toJson(e).toString
),
usageRequest => {
implicit val logMarker: LogMarker = MarkerMap(
"requestType" -> "set-graphics-usages",
"requestId" -> RequestLoggingFilter.getRequestId(req),
"image-id" -> usageRequest.mediaId,
) ++ apiKeyMarkers(req.user.accessor)
logger.info(logMarker, "recording graphics usage")
val group = usageGroupOps.build(usageRequest)
usageApiSubject.onNext(WithLogMarker.includeUsageGroup(group))
Accepted
}
)

}

def updateUsageStatus(mediaId: String, usageId: String) = auth.async(parse.json) {req => {
val request = (req.body \ "data").validate[UsageStatus]
request.fold(
Expand Down
25 changes: 25 additions & 0 deletions usage/app/lib/MediaUsageBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object MediaUsageBuilder {
None,
None,
None,
None,
printUsage.dateAdded
)

Expand All @@ -36,6 +37,7 @@ object MediaUsageBuilder {
None,
None,
None,
None,
lastModified = mediaWrapper.lastModified
)
}
Expand All @@ -54,6 +56,7 @@ object MediaUsageBuilder {
syndicationUsageMetadata = Some(syndicationUsageRequest.metadata),
None,
None,
None,
lastModified = syndicationUsageRequest.dateAdded
)
}
Expand All @@ -73,6 +76,7 @@ object MediaUsageBuilder {
syndicationUsageMetadata = None,
frontUsageMetadata = Some(frontUsageRequest.metadata),
None,
None,
lastModified = frontUsageRequest.dateAdded
)
}
Expand All @@ -92,7 +96,28 @@ object MediaUsageBuilder {
syndicationUsageMetadata = None,
frontUsageMetadata = None,
downloadUsageMetadata = Some(downloadUsageRequest.metadata),
None,
lastModified = downloadUsageRequest.dateAdded
)
}

def build(graphicsUsageRequest: GraphicsUsageRequest, groupId: String): MediaUsage = {
val usageId = UsageIdBuilder.build(graphicsUsageRequest)

MediaUsage (
usageId,
groupId,
graphicsUsageRequest.mediaId,
GraphicsUsage,
mediaType = "image",
graphicsUsageRequest.status,
printUsageMetadata = None,
digitalUsageMetadata = None,
syndicationUsageMetadata = None,
frontUsageMetadata = None,
downloadUsageMetadata = None,
graphicsUsageMetadata = Some(graphicsUsageRequest.metadata),
lastModified = graphicsUsageRequest.dateAdded
)
}
}
2 changes: 1 addition & 1 deletion usage/app/lib/UsageRecorder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class UsageRecorder(

val dbUpdateStream: Observable[WithLogMarker[Set[String]]] = getUpdatesStream(dbMatchStream)

val notificationStream: Observable[WithLogMarker[UsageNotice]] = getNotificationStream(dbUpdateStream)
val notificationStream: Observable[WithLogMarker[UsageNotice]] = getNotificationStream(dbUpdateStream)

val notifiedStream: Observable[LogMarker] = notificationStream.map(usageNotifier.send)

Expand Down
21 changes: 21 additions & 0 deletions usage/app/model/GraphicsUsageRequest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model

import com.gu.mediaservice.model.usage.{GraphicsUsageMetadata, GraphicsUsageStatus, UsageStatus}
import org.joda.time.DateTime
import play.api.libs.json.{JodaReads, JodaWrites, Json, Reads, Writes}

case class GraphicsUsageRequest(
dateAdded: DateTime,
addedBy: String,
mediaId: String
) {
val metadata: GraphicsUsageMetadata = GraphicsUsageMetadata(addedBy)
val status: UsageStatus = GraphicsUsageStatus
}
object GraphicsUsageRequest {
import JodaWrites._
import JodaReads._

implicit val reads: Reads[GraphicsUsageRequest] = Json.reads[GraphicsUsageRequest]
implicit val writes: Writes[GraphicsUsageRequest] = Json.writes[GraphicsUsageRequest]
}
Loading