Skip to content

Sessions stats calculation #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions config/src/main/java/ru/qatools/gridrouter/config/WithHosts.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package ru.qatools.gridrouter.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Dmitry Baev charlie@yandex-team.ru
* @author Innokenty Shuvalov innokenty@yandex-team.ru
*/
public interface WithRoutesMap extends WithHosts {
public interface WithRoutesMap {

List<Browser> getBrowsers();

default Map<String, String> getRoutesMap() {
Map<String, String> routes = new HashMap<>();
getHosts().forEach(h -> routes.put(h.getRouteId(), h.getRoute()));
HashMap<String, String> routes = new HashMap<>();
getBrowsers().stream()
.flatMap(b -> b.getVersions().stream())
.flatMap(v -> v.getRegions().stream())
.flatMap(r -> r.getHosts().stream())
.forEach(h -> routes.put(h.getRouteId(), h.getRoute()));
return routes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public Map<String, Browsers> getQuotaMap() {
return userBrowsers;
}

public Map<String, String> getRoutes() {
return routes;
public String getRoute(String routeId) {
return routes.get(routeId);
}

public Version findVersion(String user, JsonCapabilities caps) {
Expand Down
70 changes: 70 additions & 0 deletions proxy/src/main/java/ru/qatools/gridrouter/JsonWireUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ru.qatools.gridrouter;

import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLDecoder;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.http.HttpMethod.DELETE;

/**
* @author Alexander Andyashin aandryashin@yandex-team.ru
* @author Innokenty Shuvalov innokenty@yandex-team.ru
* @author Dmitry Baev charlie@yandex-team.ru
* @author Artem Eroshenko eroshenkoam@yandex-team.ru
*/
public final class JsonWireUtils {

private static final Logger LOGGER = LoggerFactory.getLogger(JsonWireUtils.class);

public static final String WD_HUB_SESSION = "/wd/hub/session/";

public static final int SESSION_HASH_LENGTH = 32;

private JsonWireUtils() {
}

public static boolean isUriValid(String uri) {
return uri.length() > getUriPrefixLength();
}

public static boolean isSessionDeleteRequest(HttpServletRequest request, String command) {
return DELETE.name().equalsIgnoreCase(request.getMethod()) && !command.contains("/");
}

public static String getSessionHash(String uri) {
return uri.substring(WD_HUB_SESSION.length(), getUriPrefixLength());
}

public static String getFullSessionId(String uri) {
String tail = uri.substring(WD_HUB_SESSION.length());
int end = tail.indexOf('/');
if (end < 0) {
return tail;
}
return tail.substring(0, end);
}

public static int getUriPrefixLength() {
return WD_HUB_SESSION.length() + SESSION_HASH_LENGTH;
}

public static String redirectionUrl(String host, String command) throws URISyntaxException {
return new URIBuilder(host).setPath(WD_HUB_SESSION + command).build().toString();
}

public static String getCommand(String uri) {
String encodedCommand = uri.substring(getUriPrefixLength());
try {
return URLDecoder.decode(encodedCommand, UTF_8.name());
} catch (UnsupportedEncodingException e) {
LOGGER.error("[UNABLE_TO_DECODE_COMMAND] - could not decode command: {}", encodedCommand, e);
return encodedCommand;
}
}
}
59 changes: 9 additions & 50 deletions proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package ru.qatools.gridrouter;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.utils.URIBuilder;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.util.StringContentProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import ru.qatools.gridrouter.json.GridStats;
import ru.qatools.gridrouter.json.JsonMessage;
import ru.qatools.gridrouter.json.JsonMessageFactory;
import ru.qatools.gridrouter.sessions.StatsCounter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
Expand All @@ -18,13 +17,10 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLDecoder;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.http.HttpMethod.DELETE;
import static org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnServletContext;
import static ru.qatools.gridrouter.JsonWireUtils.*;
import static ru.qatools.gridrouter.RequestUtils.getRemoteHost;

/**
Expand All @@ -34,7 +30,7 @@
* @author Artem Eroshenko eroshenkoam@yandex-team.ru
*/
@WebServlet(
urlPatterns = {ProxyServlet.WD_HUB_SESSION + "*"},
urlPatterns = {WD_HUB_SESSION + "*"},
asyncSupported = true,
initParams = {
@WebInitParam(name = "timeout", value = "300000"),
Expand All @@ -45,15 +41,11 @@ public class ProxyServlet extends org.eclipse.jetty.proxy.ProxyServlet {

private static final Logger LOGGER = LoggerFactory.getLogger(ProxyServlet.class);

public static final String WD_HUB_SESSION = "/wd/hub/session/";

public static final int SESSION_HASH_LENGTH = 32;

@Autowired
private ConfigRepository config;
private transient ConfigRepository config;

@Autowired
private GridStats stats;
private transient StatsCounter statsCounter;

@Override
public void init(ServletConfig config) throws ServletException {
Expand Down Expand Up @@ -85,7 +77,7 @@ protected String rewriteTarget(HttpServletRequest request) {
return null;
}

String route = getRoute(uri);
String route = config.getRoute(getSessionHash(uri));
String command = getCommand(uri);

if (route == null) {
Expand All @@ -95,7 +87,9 @@ protected String rewriteTarget(HttpServletRequest request) {

if (isSessionDeleteRequest(request, command)) {
LOGGER.info("[SESSION_DELETED] [{}] [{}] [{}]", remoteHost, route, command);
stats.stopSession();
statsCounter.deleteSession(getFullSessionId(uri), route);
} else {
statsCounter.updateSession(getFullSessionId(uri), route);
}

try {
Expand Down Expand Up @@ -132,39 +126,4 @@ private String removeSessionIdSafe(String content, String remoteHost) {
}
return content;
}

protected String redirectionUrl(String host, String command) throws URISyntaxException {
return new URIBuilder(host).setPath(WD_HUB_SESSION + command).build().toString();
}

protected String getRoute(String uri) {
return config.getRoutes().get(getSessionHash(uri));
}

protected String getCommand(String uri) {
String encodedCommand = uri.substring(getUriPrefixLength());
try {
return URLDecoder.decode(encodedCommand, UTF_8.name());
} catch (UnsupportedEncodingException e) {
LOGGER.error("[UNABLE_TO_DECODE_COMMAND] - could not decode command: {}", encodedCommand, e);
return encodedCommand;
}
}

protected boolean isUriValid(String uri) {
return uri.length() > getUriPrefixLength();
}

protected boolean isSessionDeleteRequest(HttpServletRequest request, String command) {
return DELETE.name().equalsIgnoreCase(request.getMethod())
&& !command.contains("/");
}

protected String getSessionHash(String uri) {
return uri.substring(WD_HUB_SESSION.length(), getUriPrefixLength());
}

protected int getUriPrefixLength() {
return WD_HUB_SESSION.length() + SESSION_HASH_LENGTH;
}
}
13 changes: 2 additions & 11 deletions proxy/src/main/java/ru/qatools/gridrouter/QuotaServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
Expand All @@ -17,24 +15,17 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.context.support.SpringBeanAutowiringSupport.processInjectionBasedOnServletContext;
import static ru.qatools.gridrouter.json.JsonFormatter.toJson;

/**
* @author Innokenty Shuvalov innokenty@yandex-team.ru
*/
@WebServlet(urlPatterns = {"/quota"}, asyncSupported = true)
@ServletSecurity(value = @HttpConstraint(rolesAllowed = {"user"}))
public class QuotaServlet extends HttpServlet {
public class QuotaServlet extends SpringHttpServlet {

@Autowired
private ConfigRepository config;

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
processInjectionBasedOnServletContext(this, config.getServletContext());
}
private transient ConfigRepository config;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Expand Down
Loading