Skip to content

Commit

Permalink
add simplified status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Feb 27, 2020
1 parent 3080886 commit d70ba99
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ A call to http://localhost:4444/grid/admin/Console will return a full status rep

The call `http://localhost:4444/grid/admin/Console/requests` will just return a list of the pending requests of the connected nodes.

### Simplified Status

The call `http://localhost:4444/grid/admin/Console/simple` will just return a map of pending request count, free browsers count, and in-use browsers count, for each browser name.

## ForceDeregister

The servlet `com.xing.qa.selenium.grid.hub.ForceDeregister` implements an endpoint to forcibly deregister a given node from the Hub's GridRegistry. This immediately cleans up an test sessions that were using the node and then removes it from the Grid.
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/xing/qa/selenium/grid/hub/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import org.json.JSONObject;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.internal.TestSession;
import org.openqa.grid.internal.TestSlot;
import org.openqa.grid.web.Hub;
import org.openqa.grid.web.servlet.RegistryBasedServlet;
import org.openqa.selenium.BuildInfo;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -16,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -46,6 +51,8 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
try {
if ("/requests".equals(req.getPathInfo())) {
sendJson(pendingRequests(), req, resp);
} else if ("/simple".equals(req.getPathInfo())) {
sendJson(simpleStatus(), req, resp);
} else {
sendJson(status(), req, resp);
}
Expand Down Expand Up @@ -127,4 +134,63 @@ protected JSONObject status()

return status;
}

protected JSONObject simpleStatus() throws JSONException {
JSONObject status = new JSONObject();

Hub h = getRegistry().getHub();

Map<String, JSONObject> nodes = new HashMap<String, JSONObject>();
AutoIncrementMap inUse = new AutoIncrementMap();
AutoIncrementMap notInUse = new AutoIncrementMap();
AutoIncrementMap pending = new AutoIncrementMap();

for (RemoteProxy proxy : getRegistry().getAllProxies()) {
for (TestSlot slot : proxy.getTestSlots()) {
String browserName = slot.getCapabilities().get(CapabilityType.BROWSER_NAME).toString().replaceAll("\\s+", "");
TestSession session = slot.getSession();
if (session != null) {
inUse.increment(browserName);
} else {
notInUse.increment(browserName);
}
}
}

if (getRegistry().getNewSessionRequestCount() > 0) {
for (Capabilities c: getRegistry().getDesiredCapabilities()) {
pending.increment(c.getBrowserName());
}
}

status.put("browsers_in_use", inUse.getAsJsonObject());
status.put("browsers_free", notInUse.getAsJsonObject());
status.put("pending_requests", pending.getAsJsonObject());
return status;
}
}


class AutoIncrementMap {
private Map<String, Integer> map = new HashMap<String, Integer>();

public void increment(String k) {
if (!map.containsKey(k)) {
map.put(k, 1);
} else {
map.put(k, map.get(k) + 1);
}
}

public int get(String k) {
return map.containsKey(k) ? map.get(k) : 0;
}

public JSONObject getAsJsonObject() throws JSONException {
JSONObject val = new JSONObject();
for (String k: map.keySet()) {
val.put(k, map.get(k));
}
return val;
}
}

0 comments on commit d70ba99

Please sign in to comment.