Skip to content

Commit

Permalink
handle nodes without defined attributes
Browse files Browse the repository at this point in the history
closes #448
  • Loading branch information
lmenezes committed Jun 17, 2020
1 parent b0fc43b commit d0f3eaa
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 136 deletions.
23 changes: 23 additions & 0 deletions app/models/commons/NodeInfo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package models.commons

import play.api.libs.json.{JsObject, JsValue, Json}

trait NodeInfo {

private val InternalNodeAttributes = Seq(
"ml.machine_memory",
"xpack.installed",
"transform.node",
"ml.max_open_jobs"
)

def attrs(info: JsValue) = {
val map =
(info \ "attributes").asOpt[JsObject].map(_.value.filterNot {
case (attr, _) => InternalNodeAttributes.contains(attr)
}).getOrElse(Map())

JsObject(map.toSeq)
}

}
9 changes: 2 additions & 7 deletions app/models/nodes/Node.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package models.nodes

import models.commons.NodeRoles
import models.commons.{NodeInfo, NodeRoles}
import play.api.libs.json._

import scala.collection.Map

object Node {
object Node extends NodeInfo {

def apply(id: String, currentMaster: Boolean, info: JsValue, stats: JsValue): JsValue = {
val jvmVersion = (info \ "jvm" \ "version").asOpt[JsString].getOrElse(JsNull)
Expand Down Expand Up @@ -35,9 +33,6 @@ object Node {
)
}

def attrs(info: JsValue): Map[String, JsValue] =
(info \ "attributes").as[JsObject].value.filterKeys(_ != "xpack.installed")

private def cpu(stats: JsValue): JsValue = {
val load = (stats \ "os" \ "cpu" \ "load_average" \ "1m").asOpt[JsValue].getOrElse(// 5.X
(stats \ "os" \ "load_average").asOpt[JsValue].getOrElse(JsNull) // FIXME: 2.X
Expand Down
9 changes: 2 additions & 7 deletions app/models/overview/Node.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package models.overview

import models.commons.NodeRoles
import models.commons.{NodeInfo, NodeRoles}
import play.api.libs.json._

import scala.collection.Map

object Node {
object Node extends NodeInfo {

def apply(id: String, info: JsValue, stats: JsValue, masterNodeId: String) = {
val nodeRoles = NodeRoles(stats)
Expand Down Expand Up @@ -42,9 +40,6 @@ object Node {
)
}

def attrs(info: JsValue): Map[String, JsValue] =
(info \ "attributes").as[JsObject].value.filterKeys(_ != "xpack.installed")

def disk(stats: JsValue): JsObject = {
val totalInBytes = (stats \ "fs" \ "total" \ "total_in_bytes").asOpt[Long].getOrElse(0l)
val freeInBytes = (stats \ "fs" \ "total" \ "free_in_bytes").asOpt[Long].getOrElse(0l)
Expand Down
2 changes: 1 addition & 1 deletion public/nodes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<div class="row">
<div class="col-xs-12 node-attrs">
<span ng-repeat="(attr, value) in node.attributes">
<span class="label label-success">{{value}}</span>
<span class="label label-success" title="{{attr}}">{{value}}</span>
</span>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion public/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
<div class="row">
<div class="col-xs-12 node-attrs">
<span ng-repeat="(attr, value) in node.attributes">
<span class="label label-success">{{value}}</span>
<span class="label label-success" title="{{attr}}">{{value}}</span>
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package models.nodes
package models.commons

import play.api.libs.json.Json

object NodesInfo {
object NodeInfoData {

val nodeInfo5 = Json.parse(
"""
Expand Down Expand Up @@ -114,5 +114,43 @@ object NodesInfo {
""".stripMargin
)

val noAttributesNode = Json.parse(
"""
|{
| "build_hash": "5395e21",
| "jvm": {
| "mem": {
| "direct_max": "1.9gb",
| "direct_max_in_bytes": 2130051072,
| "heap_init": "2gb",
| "heap_init_in_bytes": 2147483648,
| "heap_max": "1.9gb",
| "heap_max_in_bytes": 2130051072,
| "non_heap_init": "2.4mb",
| "non_heap_init_in_bytes": 2555904,
| "non_heap_max": "0b",
| "non_heap_max_in_bytes": 0
| },
| "pid": 4463,
| "start_time": "2017-05-18T14:14:43.665Z",
| "start_time_in_millis": 1495116883665,
| "using_compressed_ordinary_object_pointers": "true"
| },
| "name": "007ywNv",
| "os": {
| "allocated_processors": 2,
| "available_processors": 2,
| "refresh_interval": "1s",
| "refresh_interval_in_millis": 1000
| },
| "roles": [
| "master",
| "data",
| "ingest"
| ],
| "version": "5.1.1"
|}
""".stripMargin
)

}
50 changes: 46 additions & 4 deletions test/models/nodes/NodeSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models.nodes

import models.commons.NodeInfoData
import models.overview.NodeSpec.noAttributesNode
import org.specs2.Specification
import play.api.libs.json.Json

Expand All @@ -9,8 +11,9 @@ object NodeSpec extends Specification {
s2"""
Node should

parse a >= 5.0 ES node $nodeInfo5
parse a AWS node $awsNode
parse a >= 5.0 ES node $nodeInfo5
parse a AWS node $awsNode
parse a node without attributes $noAttributesNode
"""

def nodeInfo5 = {
Expand Down Expand Up @@ -50,7 +53,7 @@ object NodeSpec extends Specification {
|}
""".stripMargin
)
val node = Node("nodeId", true, NodesInfo.nodeInfo5, NodeStats.nodeStats5)
val node = Node("nodeId", true, NodeInfoData.nodeInfo5, NodeStats.nodeStats5)
node mustEqual expected
}

Expand Down Expand Up @@ -91,7 +94,46 @@ object NodeSpec extends Specification {
|}
""".stripMargin
)
val node = Node("nodeId", true, NodesInfo.awsInfo, NodeStats.awsNodeStats5)
val node = Node("nodeId", true, NodeInfoData.awsInfo, NodeStats.awsNodeStats5)
node mustEqual expected
}

def noAttributesNode = {
val expected = Json.parse(
"""
|{
| "coordinating": false,
| "cpu": {
| "load": 0.02,
| "os": 0,
| "process": 0
| },
| "current_master": true,
| "data": true,
| "disk": {
| "available": 8744493056,
| "percent": 16,
| "total": 10434699264
| },
| "heap": {
| "max": "1.9gb",
| "percent": 30,
| "used": "629.2mb"
| },
| "id": "nodeId",
| "ingest": true,
| "jvm": null,
| "master": true,
| "name": "007ywNv",
| "uptime": 492790575,
| "version": "5.1.1",
| "host": null,
| "attributes": {
| }
|}
""".stripMargin
)
val node = Node("nodeId", true, NodeInfoData.noAttributesNode, NodeStats.awsNodeStats5)
node mustEqual expected
}

Expand Down
58 changes: 49 additions & 9 deletions test/models/overview/NodeSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package models.overview

import models.commons.NodeInfoData
import org.specs2.Specification
import play.api.libs.json.Json

Expand All @@ -9,8 +10,9 @@ object NodeSpec extends Specification {
s2"""
Node should

parse a >= 5.0 ES node $nodeInfo5
parse a AWS nods $awsNode
parse a >= 5.0 ES node $nodeInfo5
parse a AWS nods $awsNode
parse a node without attributes $noAttributesNode
"""

def nodeInfo5 = {
Expand All @@ -22,10 +24,10 @@ object NodeSpec extends Specification {
| "name": "Solara",
| "host": "127.0.0.1",
| "ip": "127.0.0.1",
| "es_version": "2.1.0",
| "jvm_version": "1.8.0_72",
| "es_version": "5.1.1",
| "jvm_version": "1.8.0_131",
| "load_average": 3.17138671875,
| "available_processors": 8,
| "available_processors": 4,
| "cpu_percent": 0,
| "master": true,
| "data": true,
Expand All @@ -49,7 +51,7 @@ object NodeSpec extends Specification {
|}
""".stripMargin
)
val node = Node("nodeId", NodesInfo.nodeInfo5, NodeStats.nodeStats5, "otherId")
val node = Node("nodeId", NodeInfoData.nodeInfo5, NodeStats.nodeStats5, "otherId")
node mustEqual expected
}

Expand All @@ -62,10 +64,10 @@ object NodeSpec extends Specification {
| "name": "Solara",
| "host": null,
| "ip": null,
| "es_version": "2.1.0",
| "es_version": "5.1.1",
| "jvm_version": null,
| "load_average": 3.17138671875,
| "available_processors": 8,
| "available_processors": 2,
| "cpu_percent": 0,
| "master": true,
| "data": true,
Expand All @@ -89,7 +91,45 @@ object NodeSpec extends Specification {
|}
""".stripMargin
)
val node = Node("nodeId", NodesInfo.awsInfo, NodeStats.awsNodeStats5, "otherId")
val node = Node("nodeId", NodeInfoData.awsInfo, NodeStats.awsNodeStats5, "otherId")
node mustEqual expected
}

def noAttributesNode = {
val expected = Json.parse(
"""
|{
| "id": "nodeId",
| "current_master": false,
| "name": "Solara",
| "host": "127.0.0.1",
| "ip": "127.0.0.1",
| "es_version": "5.1.1",
| "jvm_version": null,
| "load_average": 3.17138671875,
| "available_processors": 2,
| "cpu_percent": 0,
| "master": true,
| "data": true,
| "coordinating": false,
| "ingest": false,
| "heap": {
| "used": 28420720,
| "committed": 259522560,
| "used_percent": 2,
| "max": 1037959168
| },
| "disk": {
| "total": 249804886016,
| "free": 41567444992,
| "used_percent": 84
| },
| "attributes": {
| }
|}
""".stripMargin
)
val node = Node("nodeId", NodeInfoData.noAttributesNode, NodeStats.nodeStats5, "otherId")
node mustEqual expected
}

Expand Down
Loading

0 comments on commit d0f3eaa

Please sign in to comment.