Add a route to return used tags

This is part of the `/insights` route without queries for file usage.
This commit is contained in:
Eike Kettner
2020-08-08 07:56:55 +02:00
parent a4796f3f7f
commit 1c8b66194b
5 changed files with 52 additions and 32 deletions

View File

@ -11,7 +11,7 @@ import doobie._
import doobie.implicits._
object QCollective {
case class TagCount(id: Ident, name: String, category: Option[String], count: Int)
case class TagCount(tag: RTag, count: Int)
case class InsightData(
incoming: Int,
@ -22,8 +22,6 @@ object QCollective {
def getInsights(coll: Ident): ConnectionIO[InsightData] = {
val IC = RItem.Columns
val TC = RTag.Columns
val RC = RTagItem.Columns
val q0 = selectCount(
IC.id,
RItem.table,
@ -52,25 +50,33 @@ object QCollective {
inner join filemeta m on m.id = a.file_id where a.id in (select aid from attachs)
) as t""".query[Option[Long]].unique
for {
n0 <- q0
n1 <- q1
n2 <- fileSize
n3 <- tagCloud(coll)
} yield InsightData(n0, n1, n2.getOrElse(0L), n3)
}
def tagCloud(coll: Ident): ConnectionIO[List[TagCount]] = {
val TC = RTag.Columns
val RC = RTagItem.Columns
val q3 = fr"SELECT" ++ commas(
TC.tid.prefix("t").f,
TC.name.prefix("t").f,
TC.category.prefix("t").f,
fr"count(" ++ RC.itemId.prefix("r").f ++ fr")"
TC.all.map(_.prefix("t").f) ++ Seq(fr"count(" ++ RC.itemId.prefix("r").f ++ fr")")
) ++
fr"FROM" ++ RTagItem.table ++ fr"r" ++
fr"INNER JOIN" ++ RTag.table ++ fr"t ON" ++ RC.tagId
.prefix("r")
.is(TC.tid.prefix("t")) ++
fr"WHERE" ++ TC.cid.prefix("t").is(coll) ++
fr"GROUP BY" ++ commas(TC.name.prefix("t").f, TC.tid.prefix("t").f, TC.category.prefix("t").f)
fr"GROUP BY" ++ commas(
TC.name.prefix("t").f,
TC.tid.prefix("t").f,
TC.category.prefix("t").f
)
for {
n0 <- q0
n1 <- q1
n2 <- fileSize
n3 <- q3.query[TagCount].to[List]
} yield InsightData(n0, n1, n2.getOrElse(0L), n3)
q3.query[TagCount].to[List]
}
def getContacts(