<commit_before>package com.xing.qa.selenium.grid.hub;

import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.web.Hub;
import org.openqa.grid.web.servlet.RegistryBasedServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Console information nad more as JSON
 *
 * @author <NAME> (<EMAIL>)
 */
public class Console extends RegistryBasedServlet {

    private final Logger log = Logger.getLogger(getClass().getName());
    private String coreVersion;
    private String coreRevision;

    public Console() {
        this(null);
    }

    public Console(Registry registry) {
        super(registry);
        getVersion();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if ("application/json".equals(req.getHeader("Accept"))) {
            processApi(req, resp);
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "Only 'application/json' supported!");
        }
    }

    protected void processApi(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        try {
            JSONObject status = new JSONObject();

            Hub h = getRegistry().getHub();

            List<JSONObject> nodes = new ArrayList<JSONObject>();

            for (RemoteProxy proxy : getRegistry().getAllProxies()) {
                JSONRenderer beta = new WebProxyJsonRenderer(proxy);
                nodes.add(beta.render());
            }

            status.put("version", coreVersion + coreRevision);
            status.put("configuration", getRegistry().getConfiguration().getAllParams());
            status.put("host", h.getHost());
            status.put("port", h.getPort());
            status.put("registration_url", h.getRegistrationURL());
            status.put("nodes", nodes);

            response.setStatus(200);
            Writer w = response.getWriter();
            status.write(w);
        } catch (JSONException e) {
            response.setStatus(500);
            JSONObject error = new JSONObject();
            try {
                error.put("message", e.getMessage());
                error.put("location", e.getStackTrace());
                error.write(response.getWriter());
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        }
    }

    private void getVersion() {
        final Properties p = new Properties();

        InputStream stream =
                Thread.currentThread().getContextClassLoader().getResourceAsStream("VERSION.txt");
        if (stream == null) {
            log.severe("Couldn't determine version number");
            return;
        }
        try {
            p.load(stream);
        } catch (IOException e) {
            log.severe("Cannot load version from VERSION.txt" + e.getMessage());
        }

        coreVersion = p.getProperty("selenium.core.version");
        coreRevision = p.getProperty("selenium.core.revision");

        if (coreVersion == null) {
            log.severe("Cannot load selenium.core.version from VERSION.txt");
        }
    }

}<commit_msg>Added pending requests and more specific sub-call only returning pernding request data<commit_after>package com.xing.qa.selenium.grid.hub;

import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.web.Hub;
import org.openqa.grid.web.servlet.RegistryBasedServlet;
import org.openqa.selenium.Capabilities;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Console information nad more as JSON
 *
 * @author <NAME> (<EMAIL>)
 */
public class Console extends RegistryBasedServlet {

    private final Logger log = Logger.getLogger(getClass().getName());
    private String coreVersion;
    private String coreRevision;

    public Console() {
        this(null);
    }

    public Console(Registry registry) {
        super(registry);
        getVersion();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            if ("/requests".equals(req.getPathInfo())) {
                sendJson(pendingRequests(), req, resp);
            } else {
                sendJson(status(), req, resp);
            }
        } catch (JSONException je) {
            resp.setContentType("application/json");
            resp.setCharacterEncoding("UTF-8");
            resp.setStatus(500);
            JSONObject error = new JSONObject();

            try {
                error.put("message", je.getMessage());
                error.put("location", je.getStackTrace());
                error.write(resp.getWriter());
            } catch (JSONException e1) {
              log.log(Level.WARNING, "Failed to write error response", e1);
            }

        }

    }

    protected void sendJson(JSONObject jo, HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        resp.setStatus(200);
        Writer w = null;
        try {
            w = resp.getWriter();
            jo.write(w);
        } catch (IOException e) {
            log.log(Level.WARNING, "Error writing response", e);
        } catch (JSONException e) {
            log.log(Level.WARNING, "Failed to serialize JSON response", e);
        }
    }

    protected JSONObject pendingRequests() throws JSONException {
        JSONObject pending = new JSONObject();
        int p = getRegistry().getNewSessionRequestCount();
        int to = getRegistry().getNewSessionWaitTimeout();
        List<Map<String,?>> desired;

        if (p > 0) {
            desired = new ArrayList<Map<String, ?>>();
            for (Capabilities c: getRegistry().getDesiredCapabilities()) {
                desired.add(c.asMap());
            }
        } else {
            desired = Collections.emptyList();
        }

        pending.put("pending", p);
        pending.put("requested_capabilities", desired);
        pending.put("timeout", to);

        return pending;
    }

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

            Hub h = getRegistry().getHub();

            List<JSONObject> nodes = new ArrayList<JSONObject>();

            for (RemoteProxy proxy : getRegistry().getAllProxies()) {
                JSONRenderer beta = new WebProxyJsonRenderer(proxy);
                nodes.add(beta.render());
            }

            status.put("version", coreVersion + coreRevision);
            status.put("configuration", getRegistry().getConfiguration().getAllParams());
            status.put("host", h.getHost());
            status.put("port", h.getPort());
            status.put("registration_url", h.getRegistrationURL());
            status.put("nodes", nodes);
            status.put("requests", pendingRequests());

            return status;
    }

    private void getVersion() {
        final Properties p = new Properties();

        InputStream stream =
                Thread.currentThread().getContextClassLoader().getResourceAsStream("VERSION.txt");
        if (stream == null) {
            log.severe("Couldn't determine version number");
            return;
        }
        try {
            p.load(stream);
        } catch (IOException e) {
            log.severe("Cannot load version from VERSION.txt" + e.getMessage());
        }

        coreVersion = p.getProperty("selenium.core.version");
        coreRevision = p.getProperty("selenium.core.revision");

        if (coreVersion == null) {
            log.severe("Cannot load selenium.core.version from VERSION.txt");
        }
    }

}
{'fraction_non_alphanumeric': 0.11847507331378299, 'fraction_numerical': 0.002346041055718475, 'mean_word_length': 2.3712930011862396, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2978083', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2164, 'n_words': 478}