Add classifier settings

This commit is contained in:
Eike Kettner
2020-08-28 22:17:49 +02:00
parent 53fdb100ab
commit 8c4f2e702b
17 changed files with 649 additions and 56 deletions

View File

@ -0,0 +1,9 @@
CREATE TABLE `classifier_setting` (
`cid` varchar(254) not null primary key,
`enabled` boolean not null,
`schedule` varchar(254) not null,
`category` varchar(254) not null,
`file_id` varchar(254),
`created` timestamp not null,
foreign key (`cid`) references `collective`(`cid`)
);

View File

@ -0,0 +1,11 @@
CREATE TABLE "classifier_setting" (
"cid" varchar(254) not null primary key,
"enabled" boolean not null,
"schedule" varchar(254) not null,
"category" varchar(254) not null,
"item_count" int not null,
"file_id" varchar(254),
"created" timestamp not null,
foreign key ("cid") references "collective"("cid"),
foreign key ("file_id") references "filemeta"("id")
);

View File

@ -0,0 +1,106 @@
package docspell.store.records
import cats.implicits._
import docspell.common._
import docspell.store.impl.Implicits._
import docspell.store.impl._
import com.github.eikek.calev._
import doobie._
import doobie.implicits._
case class RClassifierSetting(
cid: Ident,
enabled: Boolean,
schedule: CalEvent,
category: String,
itemCount: Int,
fileId: Option[Ident],
created: Timestamp
) {}
object RClassifierSetting {
val table = fr"classifier_setting"
object Columns {
val cid = Column("cid")
val enabled = Column("enabled")
val schedule = Column("schedule")
val category = Column("category")
val itemCount = Column("item_count")
val fileId = Column("file_id")
val created = Column("created")
val all = List(cid, enabled, schedule, category, itemCount, fileId, created)
}
import Columns._
def insert(v: RClassifierSetting): ConnectionIO[Int] = {
val sql =
insertRow(
table,
all,
fr"${v.cid},${v.enabled},${v.schedule},${v.category},${v.itemCount},${v.fileId},${v.created}"
)
sql.update.run
}
def updateAll(v: RClassifierSetting): ConnectionIO[Int] = {
val sql = updateRow(
table,
cid.is(v.cid),
commas(
enabled.setTo(v.enabled),
schedule.setTo(v.schedule),
category.setTo(v.category),
itemCount.setTo(v.itemCount),
fileId.setTo(v.fileId)
)
)
sql.update.run
}
def updateSettings(v: RClassifierSetting): ConnectionIO[Int] =
for {
n1 <- updateRow(
table,
cid.is(v.cid),
commas(
enabled.setTo(v.enabled),
schedule.setTo(v.schedule),
itemCount.setTo(v.itemCount),
category.setTo(v.category)
)
).update.run
n2 <- if (n1 <= 0) insert(v) else 0.pure[ConnectionIO]
} yield n1 + n2
def findById(id: Ident): ConnectionIO[Option[RClassifierSetting]] = {
val sql = selectSimple(all, table, cid.is(id))
sql.query[RClassifierSetting].option
}
def delete(coll: Ident): ConnectionIO[Int] =
deleteFrom(table, cid.is(coll)).update.run
case class Classifier(
enabled: Boolean,
schedule: CalEvent,
itemCount: Int,
category: Option[String]
) {
def toRecord(coll: Ident, created: Timestamp): RClassifierSetting =
RClassifierSetting(
coll,
enabled,
schedule,
category.getOrElse(""),
itemCount,
None,
created
)
}
}

View File

@ -61,14 +61,47 @@ object RCollective {
updateRow(table, id.is(cid), language.setTo(lang)).update.run
def updateSettings(cid: Ident, settings: Settings): ConnectionIO[Int] =
updateRow(
table,
id.is(cid),
commas(
language.setTo(settings.language),
integration.setTo(settings.integrationEnabled)
)
).update.run
for {
n1 <- updateRow(
table,
id.is(cid),
commas(
language.setTo(settings.language),
integration.setTo(settings.integrationEnabled)
)
).update.run
cls <-
Timestamp
.current[ConnectionIO]
.map(now => settings.classifier.map(_.toRecord(cid, now)))
n2 <- cls match {
case Some(cr) =>
RClassifierSetting.updateSettings(cr)
case None =>
RClassifierSetting.delete(cid)
}
} yield n1 + n2
def getSettings(coll: Ident): ConnectionIO[Option[Settings]] = {
val cId = id.prefix("c")
val CS = RClassifierSetting.Columns
val csCid = CS.cid.prefix("cs")
val cols = Seq(
language.prefix("c"),
integration.prefix("c"),
CS.enabled.prefix("cs"),
CS.schedule.prefix("cs"),
CS.itemCount.prefix("cs"),
CS.category.prefix("cs")
)
val from = table ++ fr"c LEFT JOIN" ++
RClassifierSetting.table ++ fr"cs ON" ++ csCid.is(cId)
selectSimple(cols, from, cId.is(coll))
.query[Settings]
.option
}
def findById(cid: Ident): ConnectionIO[Option[RCollective]] = {
val sql = selectSimple(all, table, id.is(cid))
@ -112,5 +145,10 @@ object RCollective {
selectSimple(all.map(_.prefix("c")), from, aId.is(attachId)).query[RCollective].option
}
case class Settings(language: Language, integrationEnabled: Boolean)
case class Settings(
language: Language,
integrationEnabled: Boolean,
classifier: Option[RClassifierSetting.Classifier]
)
}