From fa627f13fcbb021ac5b6f31fa5dd3fbf040212c0 Mon Sep 17 00:00:00 2001 From: To-om Date: Tue, 10 Nov 2020 14:43:00 +0100 Subject: [PATCH] #1635 Add more handling duration properties --- .../thp/thehive/controllers/v0/CaseCtrl.scala | 49 ++++++++++++- .../thehive/controllers/v1/Properties.scala | 68 +++++++++++++++++++ 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala b/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala index aa94cfbd82..c48612d9af 100644 --- a/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala +++ b/thehive/app/org/thp/thehive/controllers/v0/CaseCtrl.scala @@ -338,9 +338,24 @@ 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( - _.coalesce( + _.coalesceIdent( _.has(_.endDate) .sack( (_: JLong, endDate: JLong) => endDate, @@ -349,7 +364,37 @@ class PublicCase @Inject() ( .sack((_: Long) - (_: JLong), _.by(_.value(_.startDate).graphMap[Long, JLong, Converter[Long, JLong]](_.getTime, Converter.long))) .sack((_: Long) / (_: Long), _.by(_.constant(3600000L))) .sack[Long], - 0L + _.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 ) diff --git a/thehive/app/org/thp/thehive/controllers/v1/Properties.scala b/thehive/app/org/thp/thehive/controllers/v1/Properties.scala index f435c9b3d3..0a3a7c8619 100644 --- a/thehive/app/org/thp/thehive/controllers/v1/Properties.scala +++ b/thehive/app/org/thp/thehive/controllers/v1/Properties.scala @@ -1,5 +1,6 @@ package org.thp.thehive.controllers.v1 +import java.lang.{Long => JLong} import java.util.Date import javax.inject.{Inject, Named, Singleton} @@ -18,6 +19,7 @@ import org.thp.thehive.services.CaseTemplateOps._ import org.thp.thehive.services.CustomFieldOps._ import org.thp.thehive.services.LogOps._ import org.thp.thehive.services.ObservableOps._ +import org.thp.thehive.services.OrganisationOps._ import org.thp.thehive.services.TagOps._ import org.thp.thehive.services.TaskOps._ import org.thp.thehive.services.UserOps._ @@ -233,6 +235,72 @@ 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("viewingOrganisation", UMapping.string)( + _.authSelect((cases, authContext) => cases.organisations.visible(authContext).value(_.name)).readonly + ) + .property("owningOrganisation", UMapping.string)( + _.authSelect((cases, authContext) => cases.origin.visible(authContext).value(_.name)).readonly + ) .build lazy val caseTemplate: PublicProperties =