Changed InspectPageParser to use PlayerProfile

This commit is contained in:
Rsl1122 2017-11-20 15:53:17 +02:00
parent f77eb460cc
commit 8c889698f9
5 changed files with 101 additions and 80 deletions

View File

@ -156,7 +156,7 @@ public class PlayerProfile implements OfflinePlayer {
if (longestSession.isPresent()) {
return longestSession.getAsLong();
}
return 0L;
return -1;
}
public long getSessionMedian() {

View File

@ -11,17 +11,14 @@ import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.ParseException;
import main.java.com.djrapitops.plan.data.Action;
import main.java.com.djrapitops.plan.data.PlayerKill;
import main.java.com.djrapitops.plan.data.PlayerProfile;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.data.time.WorldTimes;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.SessionsTable;
import main.java.com.djrapitops.plan.database.tables.UsersTable;
import main.java.com.djrapitops.plan.utilities.FormatUtils;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.analysis.AnalysisUtils;
import main.java.com.djrapitops.plan.utilities.comparators.ActionComparator;
import main.java.com.djrapitops.plan.utilities.analysis.MathUtils;
import main.java.com.djrapitops.plan.utilities.comparators.SessionLengthComparator;
import main.java.com.djrapitops.plan.utilities.comparators.SessionStartComparator;
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
@ -32,7 +29,10 @@ import main.java.com.djrapitops.plan.utilities.html.graphs.ServerPreferencePieCr
import main.java.com.djrapitops.plan.utilities.html.graphs.WorldPieCreator;
import main.java.com.djrapitops.plan.utilities.html.tables.ActionsTableCreator;
import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
/**
@ -56,103 +56,121 @@ public class InspectPageParser extends PageParser {
Log.logDebug("Database", "Inspect Parse Fetch");
Benchmark.start("Inspect Parse, Fetch");
Database db = plugin.getDB();
SessionsTable sessionsTable = db.getSessionsTable();
UserInfo userInfo = db.getUserInfoTable().getUserInfo(uuid);
UsersTable usersTable = db.getUsersTable();
String playerName = usersTable.getPlayerName(uuid);
Optional<Long> registerDate = usersTable.getRegisterDate(uuid);
if (registerDate.isPresent()) {
addValue("registered", FormatUtils.formatTimeStampYear(registerDate.get()));
} else {
addValue("registered", "-");
}
addValue("playerName", playerName);
int timesKicked = usersTable.getTimesKicked(uuid);
long now = MiscUtils.getTime();
addValue("refresh", FormatUtils.formatTimeStamp(now));
addValue("version", MiscUtils.getPlanVersion());
addValue("timeZone", MiscUtils.getTimeZoneOffsetHours());
long lastSeen = sessionsTable.getLastSeen(uuid);
PlayerProfile profile = db.getPlayerProfile(uuid);
Benchmark.stop("Inspect Parse, Fetch");
String playerName = profile.getName();
long registered = profile.getRegistered();
int timesKicked = profile.getTimesKicked();
long lastSeen = profile.getLastSeen();
addValue("registered", FormatUtils.formatTimeStampYear(registered));
addValue("playerName", playerName);
addValue("kickCount", timesKicked);
if (lastSeen != 0) {
addValue("lastSeen", FormatUtils.formatTimeStampYear(lastSeen));
} else {
addValue("lastSeen", "-");
}
addValue("kickCount", timesKicked);
Map<String, Long> playtimeByServer = sessionsTable.getPlaytimeByServer(uuid);
addValue("serverPieSeries", ServerPreferencePieCreator.createSeriesData(playtimeByServer));
Map<UUID, String> serverNames = db.getServerTable().getServerNames();
Map<UUID, WorldTimes> worldTimesPerServer = profile.getWorldTimesPerServer();
addValue("serverPieSeries", ServerPreferencePieCreator.createSeriesData(serverNames, worldTimesPerServer));
addValue("worldPieColors", Settings.THEME_GRAPH_WORLD_PIE.toString());
addValue("gmPieColors", Settings.THEME_GRAPH_GM_PIE.toString());
addValue("serverPieColors", Settings.THEME_GRAPH_SERVER_PREF_PIE.toString());
// TODO IP Timestamp table
// List<String> geolocations = db.getIpsTable().getGeolocations(uuid);
List<String> nicknames = db.getNicknamesTable().getNicknames(uuid).stream()
List<String> nicknames = profile.getNicknames().stream()
.map(HtmlUtils::swapColorsToSpan)
.collect(Collectors.toList());
addValue("nicknames", HtmlStructure.createDotList(nicknames.toArray(new String[nicknames.size()])));
// addValue("geolocations", HtmlStructure.createDotList(geolocations.toArray(new String[geolocations.size()])));
Map<UUID, List<Session>> sessions = sessionsTable.getSessions(uuid);
List<Session> allSessions = sessions.values().stream()
.flatMap(Collection::stream)
.sorted(new SessionStartComparator()) // Sorted Newest first.
Map<UUID, List<Session>> sessions = profile.getSessions();
Map<String, List<Session>> sessionsByServerName = sessions.entrySet().stream()
.collect(Collectors.toMap(entry -> serverNames.get(entry.getKey()), Map.Entry::getValue));
List<Session> allSessions = profile.getAllSessions()
.sorted(new SessionStartComparator())
.collect(Collectors.toList());
// String[] sessionsTabContent = HtmlStructure.createSessionsTabContentInspectPage(sessions, allSessions, uuid);
// addValue("contentSessions", sessionsTabContent[0]);
// addValue("sessionTabGraphViewFunctions", sessionsTabContent[1]);
// addValue("contentServerOverview", HtmlStructure.createServerOverviewColumn(sessions));
long now = MiscUtils.getTime();
addValue("refresh", FormatUtils.formatTimeStamp(now));
String[] sessionsTabContent = HtmlStructure.createSessionsTabContentInspectPage(sessionsByServerName, allSessions, uuid);
addValue("contentSessions", sessionsTabContent[0]);
addValue("sessionTabGraphViewFunctions", sessionsTabContent[1]);
addValue("contentServerOverview", HtmlStructure.createServerOverviewColumn(sessionsByServerName));
long dayAgo = now - TimeAmount.DAY.ms();
long weekAgo = now - TimeAmount.WEEK.ms();
long monthAgo = now - TimeAmount.MONTH.ms();
List<Session> sessionsDay = allSessions.stream()
.filter(s -> s.getSessionStart() > dayAgo)
.sorted(new SessionLengthComparator())
.collect(Collectors.toList());
List<Session> sessionsWeek = allSessions.stream()
.filter(s -> s.getSessionStart() > weekAgo)
.sorted(new SessionLengthComparator())
.collect(Collectors.toList());
List<Session> sessionsDay = profile.getSessions(dayAgo, now).collect(Collectors.toList());
List<Session> sessionsWeek = profile.getSessions(weekAgo, now).collect(Collectors.toList());
List<Session> sessionsMonth = profile.getSessions(monthAgo, now).collect(Collectors.toList());
long playtime = PlayerProfile.getPlaytime(allSessions.stream());
long playtimeDay = PlayerProfile.getPlaytime(sessionsDay.stream());
long playtimeWeek = PlayerProfile.getPlaytime(sessionsWeek.stream());
long playtimeMonth = PlayerProfile.getPlaytime(sessionsMonth.stream());
long longestSession = PlayerProfile.getLongestSession(allSessions.stream());
long longestSessionDay = PlayerProfile.getLongestSession(sessionsDay.stream());
long longestSessionWeek = PlayerProfile.getLongestSession(sessionsWeek.stream());
long longestSessionMonth = PlayerProfile.getLongestSession(sessionsMonth.stream());
long sessionMedian = PlayerProfile.getSessionMedian(allSessions.stream());
long sessionMedianDay = PlayerProfile.getSessionMedian(sessionsDay.stream());
long sessionMedianWeek = PlayerProfile.getSessionMedian(sessionsWeek.stream());
long sessionMedianMonth = PlayerProfile.getSessionMedian(sessionsMonth.stream());
int sessionCount = allSessions.size();
int sessionCountDay = sessionsDay.size();
int sessionCountWeek = sessionsWeek.size();
long playtimeDay = AnalysisUtils.getTotalPlaytime(sessionsDay);
long playtimeWeek = AnalysisUtils.getTotalPlaytime(sessionsWeek);
int sessionCountMonth = sessionsMonth.size();
addValue("sessionLengthLongestDay", !sessionsDay.isEmpty() ? FormatUtils.formatTimeAmount(sessionsDay.get(0).getLength()) : "-");
addValue("sessionLengthLongestWeek", !sessionsWeek.isEmpty() ? FormatUtils.formatTimeAmount(sessionsWeek.get(0).getLength()) : "-");
long sessionAverage = MathUtils.averageLong(playtime, sessionCount);
long sessionAverageDay = MathUtils.averageLong(playtimeDay, sessionCountDay);
long sessionAverageWeek = MathUtils.averageLong(playtimeWeek, sessionCountWeek);
long sessionAverageMonth = MathUtils.averageLong(playtimeMonth, sessionCountMonth);
addValue("sessionCountDay", sessionCountDay);
addValue("sessionCountWeek", sessionCountWeek);
addValue("playtimeDay", FormatUtils.formatTimeAmount(playtimeDay));
addValue("playtimeWeek", FormatUtils.formatTimeAmount(playtimeWeek));
addValue("playtimeTotal", playtime > 0L ? FormatUtils.formatTimeAmount(playtime) : "-");
addValue("playtimeDay", playtimeDay > 0L ? FormatUtils.formatTimeAmount(playtimeDay) : "-");
addValue("playtimeWeek", playtimeWeek > 0L ? FormatUtils.formatTimeAmount(playtimeWeek) : "-");
addValue("playtimeMonth", playtimeMonth > 0L ? FormatUtils.formatTimeAmount(playtimeMonth) : "-");
List<Action> actions = db.getActionsTable().getActions(uuid);
actions.addAll(allSessions.stream()
.map(Session::getPlayerKills)
.flatMap(Collection::stream)
.map(PlayerKill::convertToAction)
.collect(Collectors.toList()));
actions.sort(new ActionComparator());
addValue("sessionLengthLongest", longestSession > 0L ? FormatUtils.formatTimeAmount(longestSession) : "-");
addValue("sessionLongestDay", longestSessionDay > 0L ? FormatUtils.formatTimeAmount(longestSessionDay) : "-");
addValue("sessionLongestWeek", longestSessionWeek > 0L ? FormatUtils.formatTimeAmount(longestSessionWeek) : "-");
addValue("sessionLongestMonth", longestSessionMonth > 0L ? FormatUtils.formatTimeAmount(longestSessionMonth) : "-");
addValue("tableBodyActions", ActionsTableCreator.createTable(actions));
addValue("sessionLengthMedian", sessionMedian > 0L ? FormatUtils.formatTimeAmount(sessionMedian) : "-");
addValue("sessionMedianDay", sessionMedianDay > 0L ? FormatUtils.formatTimeAmount(sessionMedianDay) : "-");
addValue("sessionMedianWeek", sessionMedianWeek > 0L ? FormatUtils.formatTimeAmount(sessionMedianWeek) : "-");
addValue("sessionMedianMonth", sessionMedianMonth > 0L ? FormatUtils.formatTimeAmount(sessionMedianMonth) : "-");
Benchmark.stop("Inspect Parse, Fetch");
addValue("sessionAverage", sessionAverage > 0L ? FormatUtils.formatTimeAmount(sessionAverage) : "-");
addValue("sessionAverageDay", sessionAverageDay > 0L ? FormatUtils.formatTimeAmount(sessionAverageDay) : "-");
addValue("sessionAverageWeek", sessionAverageWeek > 0L ? FormatUtils.formatTimeAmount(sessionAverageWeek) : "-");
addValue("sessionAverageMonth", sessionAverageMonth > 0L ? FormatUtils.formatTimeAmount(sessionAverageMonth) : "-");
long playTime = AnalysisUtils.getTotalPlaytime(allSessions);
int sessionCount = allSessions.size();
addValue("sessionCount", sessionCount);
addValue("playtimeTotal", FormatUtils.formatTimeAmount(playTime));
addValue("sessionCountDay", sessionCountDay);
addValue("sessionCountWeek", sessionCountWeek);
addValue("sessionCountMonth", sessionCountMonth);
List<Action> actions = profile.getAllActions();
addValue("tableBodyActions", ActionsTableCreator.createTable(actions));
String punchCardData = PunchCardGraphCreator.createDataSeries(allSessions);
WorldTimes worldTimes = db.getWorldTimesTable().getWorldTimesOfUser(uuid);
@ -185,13 +203,9 @@ public class InspectPageParser extends PageParser {
addValue("mobKillCount", mobKillCount);
addValue("deathCount", deathCount);
boolean isActive = AnalysisUtils.isActive(MiscUtils.getTime(), lastSeen, playTime, sessionCount);
boolean isActive = AnalysisUtils.isActive(MiscUtils.getTime(), lastSeen, playtime, sessionCount);
String active = isActive ? "Active" : "Inactive";
if (userInfo != null) {
playerClassification(userInfo, active);
} else {
addValue("playerClassification", active);
}
playerClassification(profile, active);
if (!plugin.getInfoManager().isUsingAnotherWebServer()) {
addValue("networkName", Settings.SERVER_NAME.toString());
@ -204,14 +218,13 @@ public class InspectPageParser extends PageParser {
}
}
private void playerClassification(UserInfo userInfo, String active) {
boolean isBanned = userInfo.isBanned();
boolean isOP = userInfo.isOpped();
private void playerClassification(PlayerProfile profile, String active) {
boolean isBanned = profile.isBanned();
boolean isOP = profile.isOp();
String banned = isBanned ? "Banned" : "";
String op = isOP ? "Operator (OP)" : "";
addValue("playerClassification", HtmlStructure.separateWithDots(active, banned, op));
}
}

View File

@ -95,6 +95,9 @@ public class MathUtils {
* @see #average(int, int)
*/
public static long averageLong(long total, long size) {
if (size == 0) {
return -1L;
}
return total / size;
}

View File

@ -58,6 +58,7 @@ public class HtmlStructure {
return builder.toString();
}
@Deprecated
public static String createServerOverviewColumn(Map<String, List<Session>> sessions) {
StringBuilder builder = new StringBuilder("<div class=\"column\">");
if (Verify.isEmpty(sessions)) {
@ -91,6 +92,7 @@ public class HtmlStructure {
return builder.toString();
}
@Deprecated
public static String[] createSessionsTabContentInspectPage(Map<String, List<Session>> sessions, List<Session> allSessions, UUID uuid) {
Map<UUID, Map<String, List<Session>>> map = new HashMap<>();
map.put(uuid, sessions);

View File

@ -1,6 +1,9 @@
package main.java.com.djrapitops.plan.utilities.html.graphs;
import main.java.com.djrapitops.plan.data.time.WorldTimes;
import java.util.Map;
import java.util.UUID;
public class ServerPreferencePieCreator {
@ -8,14 +11,14 @@ public class ServerPreferencePieCreator {
throw new IllegalStateException("Utility Class");
}
public static String createSeriesData(Map<String, Long> serverPlaytimes) {
public static String createSeriesData(Map<UUID, String> serverNames, Map<UUID, WorldTimes> serverWorldTimes) {
StringBuilder seriesBuilder = new StringBuilder("[");
int i = 0;
int size = serverPlaytimes.size();
for (Map.Entry<String, Long> server : serverPlaytimes.entrySet()) {
String serverName = server.getKey();
int size = serverWorldTimes.size();
for (Map.Entry<UUID, WorldTimes> server : serverWorldTimes.entrySet()) {
String serverName = serverNames.getOrDefault(server.getKey(), "Unknown");
seriesBuilder.append("{name:'").append(serverName)
.append("',y:").append(server.getValue());
.append("',y:").append(server.getValue().getTotal());
seriesBuilder.append("}");
if (i < size - 1) {