diff --git a/app/controllers/ClusterChangesController.scala b/app/controllers/ClusterChangesController.scala index 41cb4b6a69ac68d1b47cd662459ea4f98c638fc1..8fc9e646633fdd818dcf307d83508c2b9e40e0ba 100644 --- a/app/controllers/ClusterChangesController.scala +++ b/app/controllers/ClusterChangesController.scala @@ -3,19 +3,36 @@ package controllers import javax.inject.Inject import controllers.auth.AuthenticationModule -import elastic.ElasticClient +import elastic.{ElasticClient, Error} +import models.commons.{Indices, Nodes} import models.{CerebroResponse, Hosts} -import services.changes.DataService +import play.api.libs.json.Json import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.Future class ClusterChangesController @Inject()(val authentication: AuthenticationModule, val hosts: Hosts, - client: ElasticClient, - data: DataService) extends BaseController { + client: ElasticClient) extends BaseController { - def get = process { - request => data.getData(request.target).map(CerebroResponse(200, _)) + def get = process { request => + Future.sequence(Seq( + client.getIndices(request.target), + client.getNodes(request.target), + client.main(request.target) + )).map { responses => + val failed = responses.find(_.isInstanceOf[Error]) + failed match { + case None => + val body = Json.obj( + "indices" -> Indices(responses(0).body), + "nodes" -> Nodes(responses(1).body), + "cluster_name" -> (responses(2).body \ "cluster_name").as[String] + ) + CerebroResponse(200, body) + case Some(f) => + CerebroResponse(f.status, f.body) + } + } } - } diff --git a/app/services/changes/DataService.scala b/app/services/changes/DataService.scala deleted file mode 100644 index 97f22df07f2b4e188aaefbc3311bff102d57dd9c..0000000000000000000000000000000000000000 --- a/app/services/changes/DataService.scala +++ /dev/null @@ -1,36 +0,0 @@ -package services.changes - -import com.google.inject.Inject -import elastic.{ElasticClient, Error} -import models.ElasticServer -import models.commons.{Indices, Nodes} -import play.api.libs.json.{JsValue, Json} -import services.exception.RequestFailedException - -import scala.concurrent.ExecutionContext.Implicits.global -import scala.concurrent.Future - -class DataService @Inject()(client: ElasticClient) { - - val apis = Seq( - "_cat/nodes?format=json&h=name", // Node names - "_cat/indices?format=json&h=index", // Index names - "_cat/health?h=cluster&format=json" // Cluster name - ) - - def getData(target: ElasticServer): Future[JsValue] = - Future.sequence(apis.map(client.executeRequest("GET", _, None, target))).map { responses => - responses.zipWithIndex.find(_._1.isInstanceOf[Error]) match { - case Some((response, idx)) => - throw RequestFailedException(apis(idx), response.status, response.body.toString()) - - case None => - Json.obj( - "nodes" -> Nodes(responses(0).body), - "indices" -> Indices(responses(1).body), - "cluster_name" -> (responses(2).body \\ "cluster").head - ) - } - } - -} diff --git a/test/controllers/ClusterChangesControllerSpec.scala b/test/controllers/ClusterChangesControllerSpec.scala index ae9d95a91acaf9c00600dc0031e0fa64e29587e9..d7a9c7f7c1fe141aa0ee9efe8650b5521667b5e4 100644 --- a/test/controllers/ClusterChangesControllerSpec.scala +++ b/test/controllers/ClusterChangesControllerSpec.scala @@ -1,6 +1,7 @@ package controllers -import elastic.Success +import controllers.AnalysisControllerSpec.application +import elastic.{ElasticResponse, Success} import models.ElasticServer import play.api.libs.json.Json import play.api.test.FakeRequest @@ -27,27 +28,26 @@ object ClusterChangesControllerSpec extends MockedServices { |} """.stripMargin ) - val healthResponse = Json.parse("""[{"cluster": "elasticsearch"}]""") + val mainResponse = Json.parse("""{"cluster_name": "elasticsearch"}""") val indicesResponse = Json.parse( """ |[ - | {"index":"index1"}, - | {"index":"index2"} + | {"health":"green","status":"open","index":"index1","pri":"10","rep":"0","docs.count":"4330","docs.deleted":"10","store.size":"4.1mb","pri.store.size":"4.1mb"}, + | {"health":"green","status":"closed","index":"index2","pri":"10","rep":"0","docs.count":"1497203","docs.deleted":"5048","store.size":"860.9mb","pri.store.size":"860.9mb"} |] """.stripMargin ) val nodesResponse = Json.parse( """ |[ - | {"name":"Shriek"}, - | {"name":"Jimaine Szardos"} + | {"host":"127.0.0.1","ip":"127.0.0.1","name":"Shriek"}, + | {"host":"127.0.0.1","ip":"127.0.0.1","name":"Jimaine Szardos"} |] """.stripMargin ) - - client.executeRequest("GET", "_cat/health?h=cluster&format=json", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, healthResponse)) - client.executeRequest("GET", "_cat/nodes?format=json&h=name", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, nodesResponse)) - client.executeRequest("GET", "_cat/indices?format=json&h=index", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, indicesResponse)) + client.main(ElasticServer("somehost", None)) returns Future.successful(Success(200, mainResponse)) + client.getNodes(ElasticServer("somehost", None)) returns Future.successful(Success(200, nodesResponse)) + client.getIndices(ElasticServer("somehost", None)) returns Future.successful(Success(200, indicesResponse)) val response = route(application, FakeRequest(POST, "/cluster_changes").withBody(Json.obj("host" -> "somehost"))).get ensure(response, 200, expectedResponse) }