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

Revert "optimised cluster changes api"

This reverts commit b9d2c220.
parent 57ae79f4
No related branches found
No related tags found
No related merge requests found
...@@ -3,19 +3,36 @@ package controllers ...@@ -3,19 +3,36 @@ package controllers
import javax.inject.Inject import javax.inject.Inject
import controllers.auth.AuthenticationModule import controllers.auth.AuthenticationModule
import elastic.ElasticClient import elastic.{ElasticClient, Error}
import models.commons.{Indices, Nodes}
import models.{CerebroResponse, Hosts} import models.{CerebroResponse, Hosts}
import services.changes.DataService import play.api.libs.json.Json
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, client: ElasticClient) extends BaseController {
data: DataService) extends BaseController {
def get = process { def get = process { request =>
request => data.getData(request.target).map(CerebroResponse(200, _)) 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)
}
}
} }
} }
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 elastic.Success import controllers.AnalysisControllerSpec.application
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
...@@ -27,27 +28,26 @@ object ClusterChangesControllerSpec extends MockedServices { ...@@ -27,27 +28,26 @@ object ClusterChangesControllerSpec extends MockedServices {
|} |}
""".stripMargin """.stripMargin
) )
val healthResponse = Json.parse("""[{"cluster": "elasticsearch"}]""") val mainResponse = Json.parse("""{"cluster_name": "elasticsearch"}""")
val indicesResponse = Json.parse( val indicesResponse = Json.parse(
""" """
|[ |[
| {"index":"index1"}, | {"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":"index2"} | {"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 """.stripMargin
) )
val nodesResponse = Json.parse( val nodesResponse = Json.parse(
""" """
|[ |[
| {"name":"Shriek"}, | {"host":"127.0.0.1","ip":"127.0.0.1","name":"Shriek"},
| {"name":"Jimaine Szardos"} | {"host":"127.0.0.1","ip":"127.0.0.1","name":"Jimaine Szardos"}
|] |]
""".stripMargin """.stripMargin
) )
client.main(ElasticServer("somehost", None)) returns Future.successful(Success(200, mainResponse))
client.executeRequest("GET", "_cat/health?h=cluster&format=json", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, healthResponse)) client.getNodes(ElasticServer("somehost", None)) returns Future.successful(Success(200, nodesResponse))
client.executeRequest("GET", "_cat/nodes?format=json&h=name", None, ElasticServer("somehost", None)) returns Future.successful(Success(200, nodesResponse)) client.getIndices(ElasticServer("somehost", None)) returns Future.successful(Success(200, indicesResponse))
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