Skip to content

Commit

Permalink
Merge pull request #124 from opentargets/rm-bug-lit-filter
Browse files Browse the repository at this point in the history
Count filtered data and update query
  • Loading branch information
mbdebian authored Feb 8, 2023
2 parents a5bc6c7 + e7ea9f2 commit 766019b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
9 changes: 8 additions & 1 deletion app/models/Backend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import java.time.LocalDate
import java.time.format.DateTimeFormatter
import scala.collection.immutable.ArraySeq
import scala.concurrent._
import scala.concurrent.impl.Promise
import scala.util.{Failure, Success}

class Backend @Inject() (implicit
ec: ExecutionContext,
Expand Down Expand Up @@ -816,7 +818,12 @@ class Backend @Inject() (implicit
val npag = pag.next
Helpers.Cursor.from(Some(Json.toJson(npag)))
}
Publications(total, year, nCursor, pubs)

val result = dbRetriever.executeQuery[Int, Query](simQ.filteredTotalQ).map { v2 =>
Publications(total, year, nCursor, pubs, v2.head)
}

result.await
}

dbRetriever.executeQuery[Long, Query](simQ.total).flatMap {
Expand Down
76 changes: 50 additions & 26 deletions app/models/db/QLITAGG.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ case class QLITAGG(
val year: Column = column("year")
val month: Column = column("month")
val day: Column = column("day")

val T: Column = column(tableName)
val TIdx: Column = column(indexTableName)

Expand All @@ -35,16 +34,51 @@ case class QLITAGG(
PreWhere(F.in(key, F.set(ids.map(literal).toSeq))),
GroupBy(pmid.name :: Nil),
Having(F.greaterOrEquals(F.count(pmid.name), literal(ids.size))),
OrderBy(F.sum(relevance.name).desc :: F.any(date.name).desc :: Nil),
Limit(offset, size)
OrderBy(F.sum(relevance.name).desc :: F.any(date.name).desc :: Nil)
)

val litQ: Q = Q(
Select(pmid :: pmcid :: date :: year :: month :: Nil),
From(T),
PreWhere(F.in(pmid, pmidsQ(pmid :: Nil).toColumn(None)))
private def pmidsQNord(select: Seq[Column]): Q = Q(
Select(select),
From(TIdx),
PreWhere(F.in(key, F.set(ids.map(literal).toSeq))),
GroupBy(pmid.name :: Nil),
Having(F.greaterOrEquals(F.count(pmid.name), literal(ids.size)))
)

val filteredTotalQ: Q = {
val preCountQ = filterDate match {
case Some(value) =>
Q(
Select(literal(1) :: Nil),
From(T),
PreWhere(F.in(pmid, pmidsQNord(pmid :: Nil).toColumn(None))),
Where(
F.and(
F.greaterOrEquals(
F.plus(F.multiply(year, literal(100)), month),
literal((value._1 * 100) + value._2)
),
F.lessOrEquals(
F.plus(F.multiply(year, literal(100)), month),
literal((filterDate.get._3 * 100) + filterDate.get._4)
)
)
)
)
case _ =>
Q(
Select(literal(1) :: Nil),
From(T),
PreWhere(F.in(pmid, pmidsQNord(pmid :: Nil).toColumn(None)))
)
}

Q(
Select(F.count(Column.star) :: Nil),
From(preCountQ.toColumn(None))
)
}

val total: Q = {
val countQ = Q(
Select(literal(1) :: Nil),
Expand All @@ -67,8 +101,8 @@ case class QLITAGG(
val minDate: Q = {
val q = Q(
Select(F.min(year) :: Nil),
From(pmidsQ(pmid :: Nil).toColumn(None), Some("L")),
Join(litQ.toColumn(None), Some("left"), Some("any"), global = false, Some("L"), pmid :: Nil)
From(T),
PreWhere(F.in(pmid, pmidsQ(pmid :: Nil).toColumn(None)))
)

logger.debug(q.toString)
Expand All @@ -82,14 +116,8 @@ case class QLITAGG(
case Some(value) =>
Q(
Select(pmid :: pmcid :: date :: year :: month :: Nil),
From(pmidsQ(pmid :: Nil).toColumn(None), Some("L")),
Join(litQ.toColumn(None),
Some("left"),
Some("any"),
global = false,
Some("L"),
pmid :: Nil
),
From(T),
PreWhere(F.in(pmid, pmidsQ(pmid :: Nil).toColumn(None))),
Where(
F.and(
F.greaterOrEquals(
Expand All @@ -101,19 +129,15 @@ case class QLITAGG(
literal((filterDate.get._3 * 100) + filterDate.get._4)
)
)
)
),
Limit(offset, size)
)
case _ =>
Q(
Select(pmid :: pmcid :: date :: year :: month :: Nil),
From(pmidsQ(pmid :: Nil).toColumn(None), Some("L")),
Join(litQ.toColumn(None),
Some("left"),
Some("any"),
global = false,
Some("L"),
pmid :: Nil
)
From(T),
PreWhere(F.in(pmid, pmidsQ(pmid :: Nil).toColumn(None))),
Limit(offset, size)
)
}

Expand Down
4 changes: 3 additions & 1 deletion app/models/entities/Publications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import sangria.schema.{
case class Publications(count: Long,
lowYear: Int,
cursor: Option[String],
rows: IndexedSeq[JsValue]
rows: IndexedSeq[JsValue],
filteredCount: Long = 0
)

object Publications {
Expand All @@ -29,6 +30,7 @@ object Publications {
"Publication list",
fields[Backend, Publications](
Field("count", LongType, description = None, resolve = _.value.count),
Field("filteredCount", LongType, description = None, resolve = _.value.filteredCount),
Field("earliestPubYear",
IntType,
description = Some("Earliest publication year."),
Expand Down

0 comments on commit 766019b

Please sign in to comment.