-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
419 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// Comment to get more information during initialization | ||
logLevel := Level.Info | ||
|
||
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.22") | ||
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.23") | ||
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.1") | ||
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package controllers | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
import play.api.http.Status | ||
import play.api.libs.json.{JsNumber, JsObject, Json} | ||
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents} | ||
|
||
import akka.stream.Materializer | ||
import akka.stream.scaladsl.Sink | ||
import com.sksamuel.elastic4s.http.ElasticDsl.{search, termsAggregation} | ||
import javax.inject.{Inject, Singleton} | ||
import models.Roles | ||
|
||
import org.elastic4play.NotFoundError | ||
import org.elastic4play.controllers.Authenticated | ||
import org.elastic4play.database.DBFind | ||
import org.elastic4play.services.DBLists | ||
import org.elastic4play.services.QueryDSL._ | ||
|
||
@Singleton | ||
class CustomFieldsCtrl @Inject()( | ||
authenticated: Authenticated, | ||
dbfind: DBFind, | ||
dblists: DBLists, | ||
components: ControllerComponents, | ||
implicit val ec: ExecutionContext, | ||
implicit val mat: Materializer | ||
) extends AbstractController(components) | ||
with Status { | ||
|
||
def useCount(customField: String): Action[AnyContent] = | ||
authenticated(Roles.read) | ||
.async { | ||
dblists("custom_fields") | ||
.getItems[JsObject] | ||
._1 | ||
.collect { | ||
case (_, value) if (value \ "reference").asOpt[String].contains(customField) ⇒ (value \ "type").as[String] | ||
} | ||
.runWith(Sink.head) | ||
.recoverWith { case _ ⇒ Future.failed(NotFoundError(s"CustomField $customField not found")) } | ||
.flatMap { customFieldType ⇒ | ||
val filter = and("relations" in ("case", "alert", "caseTemplate"), contains(s"customFields.$customField.$customFieldType")) | ||
dbfind( | ||
indexName ⇒ search(indexName).query(filter.query).aggregations(termsAggregation("t").field("relations")) | ||
).map { searchResponse ⇒ | ||
val buckets = searchResponse.aggregations.terms("t").buckets | ||
val total = buckets.map(_.docCount).sum | ||
val result = buckets.map(b ⇒ b.key → JsNumber(b.docCount)) :+ ("total" → JsNumber(total)) | ||
Ok(JsObject(result)) | ||
} | ||
.recover { case _ ⇒ Ok(Json.obj("total" → 0)) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.