diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/html/Html.java b/Plan/src/main/java/com/djrapitops/plan/ui/html/Html.java index 04655ff8e..f0dbb9605 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/html/Html.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/html/Html.java @@ -115,7 +115,7 @@ public enum Html { * @param localeFile */ public static void loadLocale(File localeFile) { - try (Scanner localeScanner = new Scanner(localeFile, "UTF-8")){ + try (Scanner localeScanner = new Scanner(localeFile, "UTF-8")) { List localeRows = new ArrayList<>(); boolean html = false; @@ -132,6 +132,7 @@ public enum Html { localeRows.add(line); } + for (String localeRow : localeRows) { try { String[] split = localeRow.split(" <> "); diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/html/RecentPlayersButtonsCreator.java b/Plan/src/main/java/com/djrapitops/plan/ui/html/RecentPlayersButtonsCreator.java index ddf5b3d35..b0822ad5c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/html/RecentPlayersButtonsCreator.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/html/RecentPlayersButtonsCreator.java @@ -24,15 +24,18 @@ public class RecentPlayersButtonsCreator { * @return html p-tag list of recent log-ins. */ public static String createRecentLoginsButtons(List names, int limit) { - StringBuilder html = new StringBuilder(); - html.append("

"); - for (int i = 0; i < names.size(); i++) { - if (i < limit) { - String name = names.get(i); - html.append(Html.BUTTON.parse(HtmlUtils.getRelativeInspectUrl(name), name)); - html.append(" "); + StringBuilder html = new StringBuilder("

"); + + int i = 0; + for (String name : names) { + if (i >= limit) { + break; } + + html.append(Html.BUTTON.parse(HtmlUtils.getRelativeInspectUrl(name), name)).append(" "); + i++; } + html.append("

"); return html.toString(); } diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/html/graphs/PunchCardGraphCreator.java b/Plan/src/main/java/com/djrapitops/plan/ui/html/graphs/PunchCardGraphCreator.java index d555b5b2d..96d5f4f74 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/html/graphs/PunchCardGraphCreator.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/html/graphs/PunchCardGraphCreator.java @@ -11,6 +11,7 @@ import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.analysis.AnalysisUtils; import main.java.com.djrapitops.plan.utilities.analysis.MathUtils; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Objects; @@ -43,8 +44,7 @@ public class PunchCardGraphCreator { } private static StringBuilder buildString(int[][] scaled) { - StringBuilder arrayBuilder = new StringBuilder(); - arrayBuilder.append("["); + StringBuilder arrayBuilder = new StringBuilder("["); for (int i = 0; i < 7; i++) { for (int j = 0; j < 24; j++) { int value = scaled[i][j]; @@ -68,6 +68,7 @@ public class PunchCardGraphCreator { int h = dAndH[1]; dataArray[d][h] = dataArray[d][h] + 1; } + if (Settings.ANALYSIS_REMOVE_OUTLIERS.isTrue()) { int avg = findAverage(dataArray); double standardDeviation = getStandardDeviation(dataArray, avg); @@ -93,8 +94,8 @@ public class PunchCardGraphCreator { } } - int size = array.length * array[0].length; - int sum = sum(valueMinusAvg); + double size = array.length * array[0].length; + double sum = sum(valueMinusAvg); return Math.sqrt(sum / size); } @@ -105,13 +106,7 @@ public class PunchCardGraphCreator { } private static int sum(int[][] array) { - int total = 0; - for (int[] is : array) { - for (int i : is) { - total += i; - } - } - return total; + return Arrays.stream(array).mapToInt(is -> is.length).sum(); } private static List getSessionStarts(Collection data) { diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java index 59761a207..0f13b8a22 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebServer.java @@ -11,7 +11,6 @@ import main.java.com.djrapitops.plan.database.tables.SecurityTable; import main.java.com.djrapitops.plan.ui.html.DataRequestHandler; import main.java.com.djrapitops.plan.ui.webserver.response.*; import main.java.com.djrapitops.plan.utilities.HtmlUtils; -import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.PassEncryptUtil; import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility; import org.bukkit.ChatColor; @@ -26,7 +25,9 @@ import java.security.cert.CertificateException; import java.util.Base64; import java.util.List; import java.util.UUID; -import java.util.concurrent.Executors; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * @author Rsl1122 @@ -75,7 +76,6 @@ public class WebServer { server.createContext("/", new HttpHandler() { @Override public void handle(HttpExchange exchange) throws IOException { - OutputStream os = null; try { URI uri = exchange.getRequestURI(); String target = uri.toString(); @@ -94,26 +94,25 @@ public class WebServer { Response response = getResponse(target, user); String content = response.getContent(); - exchange.sendResponseHeaders(response.getCode(), content.length()); - try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) { - try (ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes())) { - byte[] buffer = new byte[2048]; - int count; - while ((count = bis.read(buffer)) != -1) { - out.write(buffer, 0, count); - } + exchange.sendResponseHeaders(200, 0); + + try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody()); + ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes())) { + byte[] buffer = new byte[2048]; + int count; + while ((count = bis.read(buffer)) != -1) { + out.write(buffer, 0, count); } } } catch (Exception e) { Log.toLog(this.getClass().getName(), e); throw e; } finally { - MiscUtils.close(os); exchange.close(); } } }); - server.setExecutor(Executors.newSingleThreadExecutor()); + server.setExecutor(new ThreadPoolExecutor(4, 8, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100))); server.start(); enabled = true; diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java index b1d65a833..08dd0d9dc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/AnalysisUtils.java @@ -168,7 +168,7 @@ public class AnalysisUtils { .map(value -> (boolean) value) .collect(Collectors.toList()); long count = tempList.stream().filter(value -> value).count(); - return source.parseContainer(analysisType.getModifier(), ((double) (count / tempList.size()) * 100) + "%"); + return source.parseContainer(analysisType.getModifier(), (((double) count / tempList.size()) * 100) + "%"); } catch (Exception | NoClassDefFoundError | NoSuchFieldError e) { return logPluginDataCausedError(source, e); } diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/ui/graphs/PlayerActivityGraphCreatorTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/ui/graphs/PlayerActivityGraphCreatorTest.java index 4dccd24ab..ea2a8cb6e 100644 --- a/Plan/src/test/java/main/java/com/djrapitops/plan/ui/graphs/PlayerActivityGraphCreatorTest.java +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/ui/graphs/PlayerActivityGraphCreatorTest.java @@ -2,8 +2,6 @@ package test.java.main.java.com.djrapitops.plan.ui.graphs; import main.java.com.djrapitops.plan.data.SessionData; import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; import java.util.ArrayList; import java.util.Date; @@ -43,38 +41,4 @@ public class PlayerActivityGraphCreatorTest { @Before public void setUp() throws Exception { } - - /** - * - */ - @Test - @Ignore("Outdated") - public void testFilterSessions() { - List test = new ArrayList<>(); - SessionData invalid = new SessionData(0); - test.add(invalid); - long now = 10000L; - long nowMinusScale = now - 3000L; - SessionData valid1 = new SessionData(9000L, 11000L); - test.add(valid1); - SessionData valid2 = new SessionData(8000L, 10000L); - test.add(valid2); - SessionData valid3 = new SessionData(7000L, 9000L); - test.add(valid3); - SessionData invalid2 = new SessionData(5000L, 5500L); - test.add(invalid2); -// List> result = PlayerActivityGraphCreator.filterAndTransformSessions(test, nowMinusScale); -// List starts = result.get(0); -// List ends = result.get(1); -// assertTrue("Contained invalid session" + starts, !starts.contains(invalid.getSessionStart())); -// assertTrue("Contained invalid session" + starts, !starts.contains(invalid2.getSessionStart())); -// assertTrue("Contained invalid session" + ends, !ends.contains(invalid2.getSessionEnd())); -// assertTrue("Contained invalid session" + ends, !ends.contains(invalid2.getSessionEnd())); -// assertTrue("Did not contain valid session" + starts, starts.contains(valid1.getSessionStart())); -// assertTrue("Did not contain valid session" + ends, ends.contains(valid1.getSessionEnd())); -// assertTrue("Did not contain valid session" + starts, starts.contains(valid2.getSessionStart())); -// assertTrue("Did not contain valid session" + ends, ends.contains(valid2.getSessionEnd())); -// assertTrue("Did not contain valid session" + starts, starts.contains(valid3.getSessionStart())); -// assertTrue("Did not contain valid session" + ends, ends.contains(valid3.getSessionEnd())); - } }