Skip to content

Commit

Permalink
#1734 Add handlingDuration properties in alert
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jan 7, 2021
1 parent 20e7add commit 8f1028a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ScalliGraph
10 changes: 7 additions & 3 deletions thehive/app/org/thp/thehive/controllers/v0/AlertCtrl.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.thp.thehive.controllers.v0

import java.util.{Base64, List => JList, Map => JMap}

import io.scalaland.chimney.dsl._
import javax.inject.{Inject, Named, Singleton}
import org.apache.tinkerpop.gremlin.structure.Graph
import org.thp.scalligraph.auth.AuthContext
import org.thp.scalligraph.controllers._
Expand All @@ -27,6 +24,8 @@ import org.thp.thehive.services._
import play.api.libs.json.{JsArray, JsObject, Json}
import play.api.mvc.{Action, AnyContent, Results}

import java.util.{Base64, List => JList, Map => JMap}
import javax.inject.{Inject, Named, Singleton}
import scala.util.{Failure, Success, Try}

@Singleton
Expand Down Expand Up @@ -451,5 +450,10 @@ class PublicAlert @Inject() (
})
.property("case", db.idMapping)(_.select(_.`case`._id).readonly)
.property("importDate", UMapping.date.optional)(_.select(_.importDate).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)
.build
}
9 changes: 7 additions & 2 deletions thehive/app/org/thp/thehive/controllers/v1/Properties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Properties @Inject() (
case CustomFieldType.integer => new Converter[Any, JsValue] { def apply(x: JsValue): Any = x.as[Long] }
case CustomFieldType.string => new Converter[Any, JsValue] { def apply(x: JsValue): Any = x.as[String] }
}
.getOrElse(new Converter[Any, JsValue] { def apply(x: JsValue): Any = x })
.getOrElse((x: JsValue) => x)
case _ => (x: JsValue) => x
}
.custom {
Expand All @@ -143,6 +143,11 @@ class Properties @Inject() (
case _ => Failure(BadRequestError("Invalid custom fields format"))
})
.property("importDate", UMapping.date.optional)(_.select(_.importDate).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)
.build

lazy val audit: PublicProperties =
Expand Down Expand Up @@ -259,7 +264,7 @@ class Properties @Inject() (
case CustomFieldType.integer => new Converter[Any, JsValue] { def apply(x: JsValue): Any = x.as[Long] }
case CustomFieldType.string => new Converter[Any, JsValue] { def apply(x: JsValue): Any = x.as[String] }
}
.getOrElse(new Converter[Any, JsValue] { def apply(x: JsValue): Any = x })
.getOrElse((x: JsValue) => x)
case _ => (x: JsValue) => x
}
.custom {
Expand Down
12 changes: 12 additions & 0 deletions thehive/app/org/thp/thehive/services/AlertSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ object AlertOps {
def importDate: Traversal[Date, Date, Converter[Date, Date]] =
traversal.outE[AlertCase].value(_._createdAt)

def handlingDuration: Traversal[Long, Long, IdentityConverter[Long]] =
traversal.coalesceIdent(
_.filter(_.outE[AlertCase])
.sack(
(_: JLong, importDate: JLong) => importDate,
_.by(_.importDate.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)
)

def similarCases(maybeCaseFilter: Option[Traversal.V[Case] => Traversal.V[Case]])(implicit
authContext: AuthContext
): Traversal[(RichCase, SimilarStats), JMap[String, Any], Converter[(RichCase, SimilarStats), JMap[String, Any]]] = {
Expand Down

0 comments on commit 8f1028a

Please sign in to comment.