Skip to content

Commit

Permalink
#1734 Rewrite case handling duration
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jan 7, 2021
1 parent 8f1028a commit bfda484
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 123 deletions.
65 changes: 5 additions & 60 deletions thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -345,66 +345,11 @@ class PublicCase @Inject() (
} yield Json.obj("customFields" -> values)
case _ => Failure(BadRequestError("Invalid custom fields format"))
})
.property("computed.handlingDurationInDays", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(86400000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDurationInHours", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(3600000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDurationInMinutes", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(60000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDurationInSeconds", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(1000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDuration", UMapping.long)(_.select(_.handlingDuration).readonly)
.property("computed.handlingDurationInSeconds", UMapping.long)(_.select(_.handlingDuration.math("_ / 1000").domainMap(_.toLong)).readonly)
.property("computed.handlingDurationInMinutes", UMapping.long)(_.select(_.handlingDuration.math("_ / 60000").domainMap(_.toLong)).readonly)
.property("computed.handlingDurationInHours", UMapping.long)(_.select(_.handlingDuration.math("_ / 3600000").domainMap(_.toLong)).readonly)
.property("computed.handlingDurationInDays", UMapping.long)(_.select(_.handlingDuration.math("_ / 86400000").domainMap(_.toLong)).readonly)
.property("viewingOrganisation", UMapping.string)(
_.authSelect((cases, authContext) => cases.organisations.visible(authContext).value(_.name)).readonly
)
Expand Down
65 changes: 5 additions & 60 deletions thehive/app/org/thp/thehive/controllers/v1/Properties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -281,66 +281,11 @@ class Properties @Inject() (
} yield Json.obj("customFields" -> values)
case _ => Failure(BadRequestError("Invalid custom fields format"))
})
.property("computed.handlingDurationInDays", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(86400000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDurationInHours", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(3600000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDurationInMinutes", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(60000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDurationInSeconds", UMapping.long)(
_.select(
_.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, endDate: JLong) => endDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack((_: Long) / (_: Long), _.by(_.constant(1000L)))
.sack[Long],
_.constant(0L)
)
).readonly
)
.property("computed.handlingDuration", UMapping.long)(_.select(_.handlingDuration).readonly)
.property("computed.handlingDurationInSeconds", UMapping.long)(_.select(_.handlingDuration.math("_ / 1000").domainMap(_.toLong)).readonly)
.property("computed.handlingDurationInMinutes", UMapping.long)(_.select(_.handlingDuration.math("_ / 60000").domainMap(_.toLong)).readonly)
.property("computed.handlingDurationInHours", UMapping.long)(_.select(_.handlingDuration.math("_ / 3600000").domainMap(_.toLong)).readonly)
.property("computed.handlingDurationInDays", UMapping.long)(_.select(_.handlingDuration.math("_ / 86400000").domainMap(_.toLong)).readonly)
.property("viewingOrganisation", UMapping.string)(
_.authSelect((cases, authContext) => cases.organisations.visible(authContext).value(_.name)).readonly
)
Expand Down
16 changes: 13 additions & 3 deletions thehive/app/org/thp/thehive/services/CaseSrv.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.thp.thehive.services

import java.util.{Map => JMap}
import java.lang.{Long => JLong}
import akka.actor.ActorRef

import javax.inject.{Inject, Named, Singleton}
Expand All @@ -12,7 +13,7 @@ import org.thp.scalligraph.models._
import org.thp.scalligraph.query.PropertyUpdater
import org.thp.scalligraph.services._
import org.thp.scalligraph.traversal.TraversalOps._
import org.thp.scalligraph.traversal.{Converter, StepLabel, Traversal}
import org.thp.scalligraph.traversal.{Converter, IdentityConverter, StepLabel, Traversal}
import org.thp.scalligraph.{CreateError, EntityIdOrName, EntityName, RichOptionTry, RichSeq}
import org.thp.thehive.controllers.v1.Conversion._
import org.thp.thehive.dto.v1.InputCustomFieldValue
Expand Down Expand Up @@ -557,9 +558,18 @@ object CaseOps {
def isActionRequired(implicit authContext: AuthContext): Traversal[Boolean, Boolean, Converter.Identity[Boolean]] =
traversal.choose(_.share(authContext).outE[ShareTask].has(_.actionRequired, true), true, false)

def handlingDuration: Traversal[Long, Long, IdentityConverter[Long]] =
traversal.coalesceIdent(
_.has(_.endDate)
.sack(
(_: JLong, importDate: JLong) => importDate,
_.by(_.value(_.endDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))
)
.sack((_: Long) - (_: JLong), _.by(_._createdAt.graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long)))
.sack[Long],
_.constant(0L)
)
}

// implicit class CaseCustomFieldsOpsDefs(traversal: Traversal.E[CaseCustomField]) extends CustomFieldValueOpsDefs(traversal)
}

class CaseIntegrityCheckOps @Inject() (@Named("with-thehive-schema") val db: Database, val service: CaseSrv) extends IntegrityCheckOps[Case] {
Expand Down

0 comments on commit bfda484

Please sign in to comment.