Skip to content

Commit

Permalink
#689 Add stats API for task logs
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Sep 3, 2018
1 parent 11c4ec4 commit f18ccbc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
16 changes: 11 additions & 5 deletions thehive-backend/app/controllers/LogCtrl.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package controllers

import javax.inject.{ Inject, Singleton }

import scala.concurrent.ExecutionContext

import play.api.http.Status
import play.api.mvc._

import javax.inject.{ Inject, Singleton }
import models.Roles
import services.LogSrv

import org.elastic4play.Timed
import org.elastic4play.controllers.{ Authenticated, Fields, FieldsBodyParser, Renderer }
import org.elastic4play.models.JsonFormat.baseModelEntityWrites
import org.elastic4play.services.JsonFormat.queryReads
import org.elastic4play.services.{ QueryDSL, QueryDef }
import org.elastic4play.services.JsonFormat.{ aggReads, queryReads }
import org.elastic4play.services.{ Agg, QueryDSL, QueryDef }
import org.elastic4play.{ BadRequestError, Timed }

@Singleton
class LogCtrl @Inject() (
Expand Down Expand Up @@ -70,4 +69,11 @@ class LogCtrl @Inject() (
val (logs, total) = logSrv.find(query, range, sort)
renderer.toOutput(OK, logs, total)
}

@Timed
def stats(): Action[Fields] = authenticated(Roles.read).async(fieldsBodyParser) { implicit request
val query = request.body.getValue("query").fold[QueryDef](QueryDSL.any)(_.as[QueryDef])
val aggs = request.body.getValue("stats").getOrElse(throw BadRequestError("Parameter \"stats\" is missing")).as[Seq[Agg]]
logSrv.stats(query, aggs).map(s Ok(s))
}
}
2 changes: 1 addition & 1 deletion thehive-backend/app/services/LogSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ class LogSrv @Inject() (
findSrv[LogModel, Log](logModel, queryDef, range, sortBy)
}

def stats(queryDef: QueryDef, agg: Agg*): Future[JsObject] = findSrv(logModel, queryDef, agg: _*)
def stats(queryDef: QueryDef, agg: Seq[Agg]): Future[JsObject] = findSrv(logModel, queryDef, agg: _*)
}
1 change: 1 addition & 0 deletions thehive-backend/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ POST /api/case/:caseId/task controllers.TaskCtrl.create(ca
GET /api/case/task/:taskId/log controllers.LogCtrl.findInTask(taskId)
POST /api/case/task/:taskId/log/_search controllers.LogCtrl.findInTask(taskId)
POST /api/case/task/log/_search controllers.LogCtrl.find()
POST /api/case/task/log/_stats controllers.LogCtrl.stats()
POST /api/case/task/:taskId/log controllers.LogCtrl.create(taskId)
PATCH /api/case/task/log/:logId controllers.LogCtrl.update(logId)
DELETE /api/case/task/log/:logId controllers.LogCtrl.delete(logId)
Expand Down

0 comments on commit f18ccbc

Please sign in to comment.