Skip to content
Snippets Groups Projects
Commit b9d2c220 authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

optimised cluster changes api

parent a36eb5c5
No related branches found
No related tags found
No related merge requests found
...@@ -3,36 +3,19 @@ package controllers ...@@ -3,36 +3,19 @@ package controllers
import javax.inject.Inject import javax.inject.Inject
import controllers.auth.AuthenticationModule import controllers.auth.AuthenticationModule
import elastic.{ElasticClient, Error} import elastic.ElasticClient
import models.commons.{Indices, Nodes}
import models.{CerebroResponse, Hosts} import models.{CerebroResponse, Hosts}
import play.api.libs.json.Json import services.changes.DataService
import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class ClusterChangesController @Inject()(val authentication: AuthenticationModule, class ClusterChangesController @Inject()(val authentication: AuthenticationModule,
val hosts: Hosts, val hosts: Hosts,
client: ElasticClient) extends BaseController { client: ElasticClient,
data: DataService) extends BaseController {
def get = process { request => def get = process {
Future.sequence(Seq( request => data.getData(request.target).map(CerebroResponse(200, _))
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)
}
}
} }
} }
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
)
}
}
}
package controllers package controllers
import controllers.AnalysisControllerSpec.application import elastic.Success
import elastic.{ElasticResponse, Success}
import models.ElasticServer import models.ElasticServer
import play.api.libs.json.Json import play.api.libs.json.Json
import play.api.test.FakeRequest import play.api.test.FakeRequest
...@@ -28,26 +27,27 @@ object ClusterChangesControllerSpec extends MockedServices { ...@@ -28,26 +27,27 @@ object ClusterChangesControllerSpec extends MockedServices {
|} |}
""".stripMargin """.stripMargin
) )
val mainResponse = Json.parse("""{"cluster_name": "elasticsearch"}""") val healthResponse = Json.parse("""[{"cluster": "elasticsearch"}]""")
val indicesResponse = Json.parse( val indicesResponse = Json.parse(
""" """
|[ |[
| {"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"}, | {"index":"index1"},
| {"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"} | {"index":"index2"}
|] |]
""".stripMargin """.stripMargin
) )
val nodesResponse = Json.parse( val nodesResponse = Json.parse(
""" """
|[ |[
| {"host":"127.0.0.1","ip":"127.0.0.1","name":"Shriek"}, | {"name":"Shriek"},
| {"host":"127.0.0.1","ip":"127.0.0.1","name":"Jimaine Szardos"} | {"name":"Jimaine Szardos"}
|] |]
""".stripMargin """.stripMargin
) )
client.main(ElasticServer("somehost", None)) returns Future.successful(Success(200, mainResponse))
client.getNodes(ElasticServer("somehost", None)) returns Future.successful(Success(200, nodesResponse)) client.executeRequest("GET", "_cat/health?h=cluster&format=json", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, healthResponse))
client.getIndices(ElasticServer("somehost", None)) returns Future.successful(Success(200, indicesResponse)) 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))
val response = route(application, FakeRequest(POST, "/cluster_changes").withBody(Json.obj("host" -> "somehost"))).get val response = route(application, FakeRequest(POST, "/cluster_changes").withBody(Json.obj("host" -> "somehost"))).get
ensure(response, 200, expectedResponse) ensure(response, 200, expectedResponse)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment