Skip to content

Commit

Permalink
Merge pull request #4143 from guardian/reaper/move-is-reapable-to-common
Browse files Browse the repository at this point in the history
  • Loading branch information
twrichards authored Oct 5, 2023
2 parents d55465c + c87b0ff commit 736956e
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ class CommonConfigWithElastic(resources: GridConfigResources) extends CommonConf
shards = string("es6.shards").toInt,
replicas = string("es6.replicas").toInt
)

val persistenceIdentifier = string("persistence.identifier")
val queriableIdentifiers = Seq(persistenceIdentifier)

val persistedRootCollections: List[String] = stringOpt("persistence.collections") match {
case Some(collections) => collections.split(',').toList
case None => List(s"${staffPhotographerOrganisation} Archive")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.gu.mediaservice.lib.elasticsearch

import com.gu.mediaservice.lib.ImageFields
import com.gu.mediaservice.model._
import scalaz.NonEmptyList
import scalaz.syntax.std.list._

object PersistedQueries extends ImageFields {
val photographerCategories = NonEmptyList(
StaffPhotographer.category,
ContractPhotographer.category,
CommissionedPhotographer.category
)

val illustratorCategories = NonEmptyList(
ContractIllustrator.category,
StaffIllustrator.category,
CommissionedIllustrator.category
)

val agencyCommissionedCategories = NonEmptyList(
CommissionedAgency.category
)

val hasCrops = filters.bool.must(filters.existsOrMissing("exports", exists = true))
val usedInContent = filters.nested("usages", filters.exists(NonEmptyList("usages")))

def existedPreGrid(persistenceIdentifier: String) = filters.exists(NonEmptyList(identifierField(persistenceIdentifier)))

val addedToLibrary = filters.bool.must(filters.boolTerm(editsField("archived"), value = true))
val hasUserEditsToImageMetadata = filters.exists(NonEmptyList(editsField("metadata")))
val hasPhotographerUsageRights = filters.bool.must(filters.terms(usageRightsField("category"), photographerCategories))
val hasIllustratorUsageRights = filters.bool.must(filters.terms(usageRightsField("category"), illustratorCategories))
val hasAgencyCommissionedUsageRights = filters.bool.must(filters.terms(usageRightsField("category"), agencyCommissionedCategories))

def addedGNMArchiveOrPersistedCollections(persistedRootCollections: List[String]) = filters.bool.must(filters.terms(collectionsField("path"), persistedRootCollections.toNel.get))

val addedToPhotoshoot = filters.exists(NonEmptyList(editsField("photoshoot")))
val hasLabels = filters.exists(NonEmptyList(editsField("labels")))
val hasLeases = filters.exists(NonEmptyList(leasesField("leases")))

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.gu.mediaservice.lib.elasticsearch

import com.sksamuel.elastic4s.ElasticDsl.matchAllQuery
import com.sksamuel.elastic4s.requests.searches.queries.Query
import org.joda.time.DateTime

trait ReapableEligibility {

val persistedRootCollections: List[String] // typically from config
val persistenceIdentifier: String // typically from config

private def moreThanTwentyDaysOld =
filters.date("uploadTime", None, Some(DateTime.now().minusDays(20))).getOrElse(matchAllQuery())

private lazy val persistedQueries = filters.or(
PersistedQueries.hasCrops,
PersistedQueries.usedInContent,
PersistedQueries.addedToLibrary,
PersistedQueries.hasUserEditsToImageMetadata,
PersistedQueries.hasPhotographerUsageRights,
PersistedQueries.hasIllustratorUsageRights,
PersistedQueries.hasAgencyCommissionedUsageRights,
PersistedQueries.addedToPhotoshoot,
PersistedQueries.hasLabels,
PersistedQueries.hasLeases,
PersistedQueries.existedPreGrid(persistenceIdentifier),
PersistedQueries.addedGNMArchiveOrPersistedCollections(persistedRootCollections)
)

def query: Query = filters.and(
moreThanTwentyDaysOld,
filters.not(persistedQueries)
)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package lib.elasticsearch
package com.gu.mediaservice.lib.elasticsearch

import com.gu.mediaservice.lib.formatting.printDateTime
import com.sksamuel.elastic4s.ElasticDsl
import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.requests.searches.queries.{NestedQuery, Query}
import com.sksamuel.elastic4s.requests.searches.queries.compound.BoolQuery
import com.sksamuel.elastic4s.requests.searches.queries.{NestedQuery, Query}
import com.sksamuel.elastic4s.requests.searches.term.TermQuery
import org.joda.time.DateTime
import scalaz.NonEmptyList
Expand Down
2 changes: 1 addition & 1 deletion media-api/app/lib/ImagePersistenceReasons.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lib

import com.gu.mediaservice.lib.elasticsearch.PersistedQueries
import com.gu.mediaservice.model.{CommissionedAgency, Illustrator, Image, ImageMetadata, Photographer}
import com.sksamuel.elastic4s.requests.searches.queries.Query
import lib.elasticsearch.PersistedQueries


case class ImagePersistenceReasons(persistedRootCollections: List[String], persistenceIdentifier: String) {
Expand Down
10 changes: 0 additions & 10 deletions media-api/app/lib/MediaApiConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ class MediaApiConfig(resources: GridConfigResources) extends CommonConfigWithEla

val requiredMetadata = List("credit", "description", "usageRights")

val persistenceIdentifier = string("persistence.identifier")
val queriableIdentifiers = Seq(persistenceIdentifier)

val persistedRootCollections: List[String] = stringOpt("persistence.collections") match {
case Some(collections) => collections.split(',').toList
case None => List(s"${staffPhotographerOrganisation} Archive")
}

def convertToInt(s: String): Option[Int] = Try { s.toInt }.toOption

val syndicationStartDate: Option[DateTime] = Try {
stringOpt("syndication.start").map(d => DateTime.parse(d).withTimeAtStartOfDay())
}.toOption.flatten
Expand Down
1 change: 1 addition & 0 deletions media-api/app/lib/elasticsearch/ElasticSearch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib.elasticsearch

import akka.actor.Scheduler
import com.gu.mediaservice.lib.ImageFields
import com.gu.mediaservice.lib.elasticsearch.filters
import com.gu.mediaservice.lib.auth.Authentication.Principal
import com.gu.mediaservice.lib.elasticsearch.{CompletionPreview, ElasticSearchClient, ElasticSearchConfig, MigrationStatusProvider, Running}
import com.gu.mediaservice.lib.logging.{GridLogging, MarkerMap}
Expand Down
24 changes: 3 additions & 21 deletions media-api/app/lib/elasticsearch/IsQueryFilter.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib.elasticsearch

import com.gu.mediaservice.lib.ImageFields
import com.gu.mediaservice.lib.elasticsearch.{ReapableEligibility, filters}
import com.gu.mediaservice.model._
import com.sksamuel.elastic4s.ElasticDsl.matchAllQuery
import com.sksamuel.elastic4s.requests.searches.queries.Query
Expand Down Expand Up @@ -67,25 +68,6 @@ case class IsDeleted(isDeleted: Boolean) extends IsQueryFilter {
)
}

case class IsReapable(persistedRootCollections: List[String], persistenceIdentifier: String) extends IsQueryFilter {
val moreThanTwentyDaysOld = filters.date("uploadTime", None, Some(DateTime.now().minusDays(20))).getOrElse(matchAllQuery())

val persistedQueries = filters.or(
PersistedQueries.hasCrops,
PersistedQueries.usedInContent,
PersistedQueries.addedToLibrary,
PersistedQueries.hasUserEditsToImageMetadata,
PersistedQueries.hasPhotographerUsageRights,
PersistedQueries.hasIllustratorUsageRights,
PersistedQueries.hasAgencyCommissionedUsageRights,
PersistedQueries.addedToPhotoshoot,
PersistedQueries.hasLabels,
PersistedQueries.hasLeases,
PersistedQueries.existedPreGrid(persistenceIdentifier),
PersistedQueries.addedGNMArchiveOrPersistedCollections(persistedRootCollections)
)
override def query: Query = filters.and(
moreThanTwentyDaysOld,
filters.not(persistedQueries)
)
case class IsReapable(persistedRootCollections: List[String], persistenceIdentifier: String)
extends IsQueryFilter with ReapableEligibility {
}
34 changes: 1 addition & 33 deletions media-api/app/lib/elasticsearch/SearchFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,12 @@ package lib.elasticsearch

import com.gu.mediaservice.lib.ImageFields
import com.gu.mediaservice.lib.auth.{Syndication, Tier}
import com.gu.mediaservice.lib.config.RuntimeUsageRightsConfig
import com.gu.mediaservice.lib.elasticsearch.filters
import com.gu.mediaservice.model._
import com.sksamuel.elastic4s.requests.searches.queries.Query
import lib.{ImagePersistenceReasons, MediaApiConfig, PersistenceReason}
import scalaz.NonEmptyList
import scalaz.syntax.std.list._

object PersistedQueries extends ImageFields {
val photographerCategories = NonEmptyList(
StaffPhotographer.category,
ContractPhotographer.category,
CommissionedPhotographer.category
)

val illustratorCategories = NonEmptyList(
ContractIllustrator.category,
StaffIllustrator.category,
CommissionedIllustrator.category
)

val agencyCommissionedCategories = NonEmptyList(
CommissionedAgency.category
)

val hasCrops = filters.bool.must(filters.existsOrMissing("exports", exists = true))
val usedInContent = filters.nested("usages", filters.exists(NonEmptyList("usages")))
def existedPreGrid(persistenceIdentifier: String) = filters.exists(NonEmptyList(identifierField(persistenceIdentifier)))
val addedToLibrary = filters.bool.must(filters.boolTerm(editsField("archived"), value = true))
val hasUserEditsToImageMetadata = filters.exists(NonEmptyList(editsField("metadata")))
val hasPhotographerUsageRights = filters.bool.must(filters.terms(usageRightsField("category"), photographerCategories))
val hasIllustratorUsageRights = filters.bool.must(filters.terms(usageRightsField("category"), illustratorCategories))
val hasAgencyCommissionedUsageRights = filters.bool.must(filters.terms(usageRightsField("category"), agencyCommissionedCategories))
def addedGNMArchiveOrPersistedCollections(persistedRootCollections: List[String]) = filters.bool.must(filters.terms(collectionsField("path"), persistedRootCollections.toNel.get))
val addedToPhotoshoot = filters.exists(NonEmptyList(editsField("photoshoot")))
val hasLabels = filters.exists(NonEmptyList(editsField("labels")))
val hasLeases = filters.exists(NonEmptyList(leasesField("leases")))

}

class SearchFilters(config: MediaApiConfig) extends ImageFields {

Expand Down
1 change: 1 addition & 0 deletions media-api/app/lib/elasticsearch/SyndicationFilter.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib.elasticsearch

import com.gu.mediaservice.lib.ImageFields
import com.gu.mediaservice.lib.elasticsearch.filters
import com.gu.mediaservice.model._
import com.gu.mediaservice.model.leases.{AllowSyndicationLease, DenySyndicationLease}
import com.gu.mediaservice.model.usage.SyndicationUsage
Expand Down

0 comments on commit 736956e

Please sign in to comment.