-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
twitter-server: Restructure LoadBalancersHandler Logic
Problem / Solution LoadBalancer data can be rendered as either JSON or HTML, and all the logic for doing so is contained within the `LoadBalancerHandler`. As we restructure endpoints within the Twitter Server Admin Interface, let's separate out the action, from the data model, from the rendered content to make it easier to work with moving forward. JIRA Issues: CSL-9014 Differential Revision: https://phabricator.twitter.biz/D392168
- Loading branch information
1 parent
4e8df0a
commit a5bce43
Showing
4 changed files
with
93 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
server/src/main/scala/com/twitter/server/view/BalancerHtmlView.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.twitter.server.view | ||
|
||
import com.twitter.finagle.loadbalancer.Metadata | ||
import com.twitter.server.util.HtmlUtils.escapeHtml | ||
|
||
private[server] class BalancerHtmlView(balancer: Option[Metadata], routePath: String) extends View { | ||
|
||
private[view] def renderNoBalancerDetails: String = | ||
"Load balancer not found in registry." | ||
|
||
private[view] def renderBalancerDetails(md: Metadata): String = | ||
s""" | ||
|<table> | ||
| <tr><td>Label</td><td>${escapeHtml(md.label)}</td></tr> | ||
| <tr><td>Balancer Class</td><td>${escapeHtml(md.balancerClass)}</td></tr> | ||
| <tr><td>Status</td><td>${escapeHtml(md.status)}</td></tr> | ||
| <tr><td>Number Nodes Available</td><td>${md.numAvailable}</td></tr> | ||
| <tr><td>Number Nodes Busy</td><td>${md.numBusy}</td></tr> | ||
| <tr><td>Number Nodes Closed</td><td>${md.numClosed}</td></tr> | ||
| <tr><td>Total pending requests</td><td>${md.totalPending}</td></tr> | ||
| <tr><td>Total load</td><td>${md.totalLoad}</td></tr> | ||
| <tr><td>Size</td><td>${md.size}</td></tr> | ||
| <tr><td><a href="$routePath?label=${escapeHtml(md.label)}">More details</a></td><td></td></tr> | ||
|</table> | ||
""".stripMargin | ||
|
||
private[view] def renderDetails: String = balancer match { | ||
case None => renderNoBalancerDetails | ||
case Some(metadata) => renderBalancerDetails(metadata) | ||
} | ||
|
||
def render: String = | ||
s""" | ||
|<div class="row"> | ||
| <div class="col-md-12"> | ||
| <a name="load_balancer"></a> | ||
| <h3>Load Balancer</h3> | ||
| ${renderDetails} | ||
| </div> | ||
|</div> | ||
""".stripMargin | ||
} |
30 changes: 30 additions & 0 deletions
30
server/src/main/scala/com/twitter/server/view/BalancersJsonView.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.twitter.server.view | ||
|
||
import com.twitter.finagle.loadbalancer.Metadata | ||
import com.twitter.server.util.JsonConverter | ||
|
||
private[server] class BalancersJsonView(balancers: Seq[Metadata]) extends View { | ||
|
||
private[view] def renderBalancer(balancer: Metadata): Map[String, Object] = | ||
Map( | ||
"label" -> balancer.label, | ||
"info" -> Map( | ||
"balancer_class" -> balancer.balancerClass, | ||
"status" -> balancer.status, | ||
"number_available" -> balancer.numAvailable, | ||
"number_busy" -> balancer.numBusy, | ||
"number_closed" -> balancer.numClosed, | ||
"total_pending" -> balancer.totalPending, | ||
"total_load" -> balancer.totalLoad, | ||
"size" -> balancer.size, | ||
"additional_info" -> balancer.additionalInfo | ||
) | ||
) | ||
|
||
def render: String = { | ||
val clients = balancers.map(renderBalancer) | ||
val asMap: Map[String, Object] = Map("clients" -> clients) | ||
JsonConverter.writeToString(asMap) | ||
} | ||
|
||
} |