Added ServerStatsRequestHandler.
This commit is contained in:
@@ -26,6 +26,7 @@ public class RootUriRequestHandler implements UriRequestHandler {
|
||||
private RegisterRequestHandler _registerRequestHandler;
|
||||
private ReplayRequestHandler _replayRequestHandler;
|
||||
private GameHistoryRequestHandler _gameHistoryRequestHandler;
|
||||
private ServerStatsRequestHandler _serverStatsRequestHandler;
|
||||
|
||||
public RootUriRequestHandler(Map<Type, Object> context) {
|
||||
_hallRequestHandler = new HallRequestHandler(context);
|
||||
@@ -42,6 +43,7 @@ public class RootUriRequestHandler implements UriRequestHandler {
|
||||
_registerRequestHandler = new RegisterRequestHandler(context);
|
||||
_replayRequestHandler = new ReplayRequestHandler(context);
|
||||
_gameHistoryRequestHandler = new GameHistoryRequestHandler(context);
|
||||
_serverStatsRequestHandler = new ServerStatsRequestHandler(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,6 +64,8 @@ public class RootUriRequestHandler implements UriRequestHandler {
|
||||
_replayRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+6), request, context, responseWriter, e);
|
||||
} else if (uri.startsWith(_serverContextPath+"gameHistory")) {
|
||||
_gameHistoryRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+11), request, context, responseWriter, e);
|
||||
} else if (uri.startsWith(_serverContextPath+"stats")) {
|
||||
_serverStatsRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+5), request, context, responseWriter, e);
|
||||
} else if (uri.startsWith(_serverContextPath+"admin")) {
|
||||
_adminRequestHandler.handleRequest(uri.substring(_serverContextPath.length()+5), request, context, responseWriter, e);
|
||||
} else if (uri.startsWith(_serverContextPath + "chat")) {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.gempukku.lotro.async.handler;
|
||||
|
||||
import com.gempukku.lotro.async.HttpProcessingException;
|
||||
import com.gempukku.lotro.async.ResponseWriter;
|
||||
import com.gempukku.lotro.game.GameHistoryService;
|
||||
import com.gempukku.lotro.game.GameHistoryStatistics;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
import org.jboss.netty.channel.MessageEvent;
|
||||
import org.jboss.netty.handler.codec.http.HttpMethod;
|
||||
import org.jboss.netty.handler.codec.http.HttpRequest;
|
||||
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.lang.reflect.Type;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class ServerStatsRequestHandler extends LotroServerRequestHandler implements UriRequestHandler {
|
||||
private GameHistoryService _gameHistoryService;
|
||||
|
||||
public ServerStatsRequestHandler(Map<Type, Object> context) {
|
||||
super(context);
|
||||
|
||||
_gameHistoryService = extractObject(context, GameHistoryService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequest(String uri, HttpRequest request, Map<Type, Object> context, ResponseWriter responseWriter, MessageEvent e) throws Exception {
|
||||
if (uri.equals("") && request.getMethod() == HttpMethod.GET) {
|
||||
QueryStringDecoder queryDecoder = new QueryStringDecoder(request.getUri());
|
||||
String participantId = getQueryParameterSafely(queryDecoder, "participantId");
|
||||
String startDay = getQueryParameterSafely(queryDecoder, "startDay");
|
||||
String length = getQueryParameterSafely(queryDecoder, "length");
|
||||
|
||||
Player resourceOwner = getResourceOwnerSafely(request, participantId);
|
||||
|
||||
try {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
format.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
long from = format.parse(startDay).getTime();
|
||||
Date to = format.parse(startDay);
|
||||
if (length.equals("month"))
|
||||
to.setMonth(to.getMonth() + 1);
|
||||
else if (length.equals("week"))
|
||||
to.setDate(to.getDate() + 7);
|
||||
else if (length.equals("day"))
|
||||
to.setDate(to.getDate() + 1);
|
||||
else
|
||||
throw new WebApplicationException(Response.Status.BAD_REQUEST);
|
||||
long duration = to.getTime() - from;
|
||||
|
||||
int activePlayers = _gameHistoryService.getActivePlayersCount(from, duration);
|
||||
int gamesCount = _gameHistoryService.getGamesPlayedCount(from, duration);
|
||||
|
||||
GameHistoryStatistics gameHistoryStatistics = _gameHistoryService.getGameHistoryStatistics(from, duration);
|
||||
|
||||
DecimalFormat percFormat = new DecimalFormat("#0.0%");
|
||||
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document doc = documentBuilder.newDocument();
|
||||
Element stats = doc.createElement("stats");
|
||||
stats.setAttribute("activePlayers", String.valueOf(activePlayers));
|
||||
stats.setAttribute("gamesCount", String.valueOf(gamesCount));
|
||||
stats.setAttribute("start", format.format(new Date(from)));
|
||||
stats.setAttribute("end", format.format(new Date(from + duration - 1)));
|
||||
for (GameHistoryStatistics.FormatStat formatStat : gameHistoryStatistics.getFormatStats()) {
|
||||
Element formatStatElem = doc.createElement("formatStat");
|
||||
formatStatElem.setAttribute("format", formatStat.getFormat());
|
||||
formatStatElem.setAttribute("count", String.valueOf(formatStat.getCount()));
|
||||
formatStatElem.setAttribute("perc", percFormat.format(formatStat.getPercentage()));
|
||||
stats.appendChild(formatStatElem);
|
||||
}
|
||||
|
||||
doc.appendChild(stats);
|
||||
|
||||
responseWriter.writeResponse(doc);
|
||||
} catch (ParseException exp) {
|
||||
throw new HttpProcessingException(400);
|
||||
}
|
||||
} else {
|
||||
responseWriter.writeError(404);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user