[2.6.0-DEV] Added SessionData, ParallelStream to Analysis, Incomplete implementation

This commit is contained in:
Rsl1122 2017-02-13 15:02:45 +02:00
parent b6d245725f
commit ae94733da5
8 changed files with 328 additions and 94 deletions

View File

@ -38,7 +38,6 @@ import java.net.URL;
import java.util.Date;
import main.java.com.djrapitops.plan.ui.webserver.WebSocketServer;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -63,6 +62,21 @@ Fix any bugs that come up
- New Players not counted for some reason.
Sortable player table.
Add -n argument for nickname search.
Online activity revamp
- Create new SessionData
- Data saved to UserData
- Database table
- Data saved to database
- Data retrieval from db
- Remove ServerData
-> Move commanduse responsibility to a new handler
- Online player analysis
- New player analysis (Registered)
Kill table
- weapon
- KillData
- Saving
- DB
*/
/**
*

View File

@ -0,0 +1,189 @@
package main.java.com.djrapitops.plan.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
* @author Rsl1122
*/
public class RawAnalysisData {
private long gmZero;
private long gmOne;
private long gmTwo;
private long gmThree;
private long totalLoginTimes;
private long totalPlaytime;
private int totalBanned;
private int active;
private int joinleaver;
private int inactive;
private long totalKills;
private long totalMobKills;
private long totalDeaths;
private int ops;
private List<Integer> ages;
private HashMap<String, Long> latestLogins;
private HashMap<String, Long> playtimes;
private List<SessionData> sessiondata;
private HashMap<String, Integer> commandUse;
public RawAnalysisData() {
gmZero = 0;
gmOne = 0;
gmTwo = 0;
gmThree = 0;
totalLoginTimes = 0;
totalPlaytime = 0;
totalBanned = 0;
active = 0;
joinleaver = 0;
inactive = 0;
totalKills = 0;
totalMobKills = 0;
ops = 0;
ages = new ArrayList<>();
latestLogins = new HashMap<>();
playtimes = new HashMap<>();
sessiondata = new ArrayList<>();
commandUse = new HashMap<>();
}
public void addToGmZero(long gmZero) {
this.gmZero += gmZero;
}
public void addToGmOne(long gmOne) {
this.gmOne += gmOne;
}
public void addToGmTwo(long gmTwo) {
this.gmTwo += gmTwo;
}
public void addGmThree(long gmThree) {
this.gmThree += gmThree;
}
public void addTotalLoginTimes(long totalLoginTimes) {
this.totalLoginTimes += totalLoginTimes;
}
public void addTotalPlaytime(long totalPlaytime) {
this.totalPlaytime += totalPlaytime;
}
public void addTotalBanned(int totalBanned) {
this.totalBanned += totalBanned;
}
public void addActive(int active) {
this.active += active;
}
public void addJoinleaver(int joinleaver) {
this.joinleaver += joinleaver;
}
public void addInactive(int inactive) {
this.inactive += inactive;
}
public void addTotalKills(long totalKills) {
this.totalKills += totalKills;
}
public void addTotalMobKills(long totalMobKills) {
this.totalMobKills += totalMobKills;
}
public void addTotalDeaths(long totalDeaths) {
this.totalDeaths += totalDeaths;
}
public void addOps(int ops) {
this.ops += ops;
}
public long getGmZero() {
return gmZero;
}
public long getGmOne() {
return gmOne;
}
public long getGmTwo() {
return gmTwo;
}
public long getGmThree() {
return gmThree;
}
public long getTotalLoginTimes() {
return totalLoginTimes;
}
public long getTotalPlaytime() {
return totalPlaytime;
}
public int getTotalBanned() {
return totalBanned;
}
public int getActive() {
return active;
}
public int getJoinleaver() {
return joinleaver;
}
public int getInactive() {
return inactive;
}
public long getTotalKills() {
return totalKills;
}
public long getTotalMobKills() {
return totalMobKills;
}
public long getTotalDeaths() {
return totalDeaths;
}
public int getOps() {
return ops;
}
public List<Integer> getAges() {
return ages;
}
public HashMap<String, Long> getLatestLogins() {
return latestLogins;
}
public HashMap<String, Long> getPlaytimes() {
return playtimes;
}
public List<SessionData> getSessiondata() {
return sessiondata;
}
public void setCommandUse(HashMap<String, Integer> commandUse) {
this.commandUse = commandUse;
}
public HashMap<String, Integer> getCommandUse() {
return commandUse;
}
}

View File

@ -0,0 +1,33 @@
package main.java.com.djrapitops.plan.data;
/**
*
* @author Rsl1122
*/
public class SessionData {
private long sessionStart;
private long sessionEnd;
public SessionData(long sessionStart) {
this.sessionStart = sessionStart;
this.sessionEnd = -1;
}
public SessionData(long sessionStart, long sessionEnd) {
this.sessionStart = sessionStart;
this.sessionEnd = sessionEnd;
}
public void endSession(long endOfSession) {
sessionEnd = endOfSession;
}
public long getSessionStart() {
return sessionStart;
}
public long getSessionEnd() {
return sessionEnd;
}
}

View File

@ -9,6 +9,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import main.java.com.djrapitops.plan.data.PlanLitePlayerData;
import main.java.com.djrapitops.plan.data.SessionData;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
@ -34,7 +35,7 @@ public class UserData {
private boolean isOp;
private boolean isBanned;
private DemographicsData demData;
private int mobKills;
private int playerKills;
private int deaths;
@ -45,6 +46,9 @@ public class UserData {
private String name;
private boolean isOnline;
private SessionData currentSession;
private List<SessionData> sessions;
public UserData(Player player, DemographicsData demData, Database db) {
uuid = player.getUniqueId();
registered = player.getFirstPlayed();
@ -67,6 +71,7 @@ public class UserData {
isBanned = player.isBanned();
name = player.getName();
isOnline = player.isOnline();
sessions = new ArrayList<>();
}
public UserData(OfflinePlayer player, DemographicsData demData, Database db) {
@ -89,6 +94,7 @@ public class UserData {
isBanned = player.isBanned();
name = player.getName();
isOnline = player.isOnline();
sessions = new ArrayList<>();
}
public void addIpAddress(InetAddress ip) {
@ -138,6 +144,27 @@ public class UserData {
}
}
public void addSession(SessionData session) {
sessions.add(session);
}
public void addSessions(Collection<SessionData> sessions) {
this.sessions.addAll(sessions);
}
public void startSession(long startTime) {
currentSession = new SessionData(startTime);
}
public void endSession(long endTime) {
if (currentSession != null) {
currentSession.endSession(endTime);
addSession(currentSession);
} else {
System.out.println("Player's session was initialized in a wrong way! (" + name + ")");
}
}
public void updateBanned(Player p) {
isBanned = p.isBanned();
}
@ -327,4 +354,8 @@ public class UserData {
public void setDeaths(int deaths) {
this.deaths = deaths;
}
public List<SessionData> getSessions() {
return sessions;
}
}

View File

@ -14,7 +14,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
* @author Rsl1122
*/
public class ActivityHandler {
private final Plan plugin;
private final DataCacheHandler handler;
@ -63,10 +63,12 @@ public class ActivityHandler {
* @param data UserData matching the Player
*/
public void handleLogin(PlayerJoinEvent event, UserData data) {
data.setLastPlayed(new Date().getTime());
Date now = new Date();
data.setLastPlayed(now.getTime());
Player player = event.getPlayer();
data.updateBanned(player);
data.setLoginTimes(data.getLoginTimes() + 1);
data.startSession(now.toInstant().getEpochSecond() * (long) 1000);
// handler.getLocationHandler().addLocation(player.getUniqueId(), player.getLocation());
}
@ -79,9 +81,11 @@ public class ActivityHandler {
* @param data UserData matching the Player
*/
public void handleLogOut(PlayerQuitEvent event, UserData data) {
long timeNow = new Date().getTime();
Date now = new Date();
long timeNow = now.getTime();
data.setPlayTime(data.getPlayTime() + (timeNow - data.getLastPlayed()));
data.setLastPlayed(timeNow);
data.endSession(now.toInstant().getEpochSecond() * (long) 1000);
}
/**

View File

@ -145,4 +145,5 @@ public abstract class Database {
public abstract void saveServerDataHashMap(HashMap<Long, ServerData> serverData);
public abstract void saveCommandUse(HashMap<String, Integer> data);
public abstract Set<UUID> getSavedUUIDs();
public abstract HashMap<String, Integer> getCommandUse();
}

View File

@ -482,7 +482,8 @@ public abstract class SQLDB extends Database {
}
}
private HashMap<String, Integer> getCommandUse() {
@Override
public HashMap<String, Integer> getCommandUse() {
HashMap<String, Integer> commandUse = new HashMap<>();
try {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + commanduseName);

View File

@ -17,12 +17,13 @@ import java.util.Set;
import java.util.UUID;
import main.java.com.djrapitops.plan.Settings;
import main.java.com.djrapitops.plan.data.PlanLiteAnalyzedData;
import main.java.com.djrapitops.plan.data.PlanLitePlayerData;
import main.java.com.djrapitops.plan.data.RawAnalysisData;
import main.java.com.djrapitops.plan.ui.Html;
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
import static org.bukkit.Bukkit.getOfflinePlayer;
import org.bukkit.GameMode;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import static org.bukkit.Bukkit.getOfflinePlayer;
/**
*
@ -68,7 +69,7 @@ public class Analysis {
return;
}
// Async task for Analysis
(new BukkitRunnable() {
BukkitTask asyncAnalysisTask = (new BukkitRunnable() {
@Override
public void run() {
uuids.stream().forEach((uuid) -> {
@ -86,125 +87,92 @@ public class Analysis {
}
});
}
// Create empty Dataset
final RawAnalysisData raw = new RawAnalysisData();
raw.setCommandUse(plugin.getDB().getCommandUse());
rawServerData = plugin.getDB().getServerDataHashMap();
log(Phrase.ANALYSIS_BEGIN_ANALYSIS + "");
AnalysisData data = new AnalysisData();
createPlayerActivityGraphs(data);
// Create empty Dataset
long gmZero = 0;
long gmOne = 0;
long gmTwo = 0;
long gmThree = 0;
long totalLoginTimes = 0;
long totalPlaytime = 0;
int totalBanned = 0;
int active = 0;
int joinleaver = 0;
int inactive = 0;
long totalKills = 0;
long totalMobKills = 0;
long totalDeaths = 0;
int ops = 0;
List<Integer> ages = new ArrayList<>();
// DEPRECATED - WILL BE REMOVED
boolean planLiteEnabled = isPlanLiteEnabled();
PlanLiteAnalyzedData plData = new PlanLiteAnalyzedData();
HashMap<String, Integer> townMap = new HashMap<>();
HashMap<String, Integer> factionMap = new HashMap<>();
int totalVotes = 0;
int totalMoney = 0;
HashMap<String, Long> latestLogins = new HashMap<>();
HashMap<String, Long> playtimes = new HashMap<>();
// Fill Dataset with userdata.
for (UserData uData : rawData) {
rawData.parallelStream().forEach((uData) -> {
try {
if (planLiteEnabled) {
PlanLitePlayerData litePlayerData = uData.getPlanLiteData();
String town = litePlayerData.getTown();
if (!townMap.containsKey(town)) {
townMap.put(town, 0);
}
townMap.replace(town, townMap.get(town) + 1);
String faction = litePlayerData.getFaction();
if (!factionMap.containsKey(faction)) {
factionMap.put(faction, 0);
}
factionMap.replace(faction, factionMap.get(faction) + 1);
totalVotes += litePlayerData.getVotes();
totalMoney += litePlayerData.getMoney();
}
HashMap<GameMode, Long> gmTimes = uData.getGmTimes();
gmZero += gmTimes.get(GameMode.SURVIVAL);
gmOne += gmTimes.get(GameMode.CREATIVE);
gmTwo += gmTimes.get(GameMode.ADVENTURE);
raw.addToGmZero(gmTimes.get(GameMode.SURVIVAL));
raw.addToGmOne(gmTimes.get(GameMode.CREATIVE));
raw.addToGmTwo(gmTimes.get(GameMode.ADVENTURE));
try {
Long gm = gmTimes.get(GameMode.SPECTATOR);
if (gm != null) {
gmThree += gm;
raw.addGmThree(gm);
}
} catch (NoSuchFieldError e) {
}
long playTime = uData.getPlayTime();
totalPlaytime += playTime;
raw.addTotalPlaytime(playTime);
String playerName = uData.getName();
String url = HtmlUtils.getInspectUrl(playerName);
String html = Html.BUTTON.parse(url, playerName);
latestLogins.put(html, uData.getLastPlayed());
totalLoginTimes += uData.getLoginTimes();
raw.getLatestLogins().put(html, uData.getLastPlayed());
raw.addTotalLoginTimes(uData.getLoginTimes());
int age = uData.getDemData().getAge();
if (age != -1) {
ages.add(age);
raw.getAges().add(age);
}
if (uData.isOp()) {
ops++;
raw.addOps(1);
}
if (uData.isBanned()) {
totalBanned++;
raw.addTotalBanned(1);
} else if (uData.getLoginTimes() == 1) {
joinleaver++;
raw.addJoinleaver(1);
} else if (AnalysisUtils.isActive(uData.getLastPlayed(), playTime, uData.getLoginTimes())) {
active++;
playtimes.put(html, playTime);
raw.addActive(1);
raw.getPlaytimes().put(html, playTime);
} else {
inactive++;
raw.addInactive(1);
}
totalKills += uData.getPlayerKills();
totalMobKills += uData.getMobKills();
totalDeaths += uData.getDeaths();
raw.addTotalKills(uData.getPlayerKills());
raw.addTotalMobKills(uData.getMobKills());
raw.addTotalDeaths(uData.getDeaths());
raw.getSessiondata().addAll(uData.getSessions());
} catch (NullPointerException e) {
plugin.logError(Phrase.DATA_CORRUPTION_WARN.parse(uData.getUuid() + ""));
}
}
});
// Save Dataset to AnalysisData
data.setTop20ActivePlayers(AnalysisUtils.createActivePlayersTable(playtimes, 20));
data.setRecentPlayers(AnalysisUtils.createListStringOutOfHashMapLong(latestLogins, 20));
// Analyze & Save RawAnalysisData to AnalysisData
data.setTop20ActivePlayers(AnalysisUtils.createActivePlayersTable(raw.getPlaytimes(), 20));
data.setRecentPlayers(AnalysisUtils.createListStringOutOfHashMapLong(raw.getLatestLogins(), 20));
addPlanLiteToData(planLiteEnabled, plData, factionMap, townMap, totalVotes, totalMoney, data);
long totalPlaytime = raw.getTotalPlaytime();
data.setTotalPlayTime(totalPlaytime);
data.setAveragePlayTime(totalPlaytime / rawData.size());
data.setTotalLoginTimes(totalLoginTimes);
data.setTotalLoginTimes(raw.getTotalLoginTimes());
createActivityVisalization(totalBanned, active, inactive, joinleaver, data);
createActivityVisalization(raw.getTotalBanned(), raw.getActive(), raw.getInactive(), raw.getJoinleaver(), data);
data.setOps(ops);
data.setOps(raw.getOps());
analyzeAverageAge(ages, data);
createGamemodeUsageVisualization(gmZero, gmOne, gmTwo, gmThree, data);
createCommandUseTable(data);
analyzeAverageAge(raw.getAges(), data);
createGamemodeUsageVisualization(raw.getGmZero(), raw.getGmOne(), raw.getGmTwo(), raw.getGmThree(), data);
createCommandUseTable(raw, data);
data.setTotaldeaths(totalDeaths);
data.setTotalkills(totalKills);
data.setTotalmobkills(totalMobKills);
data.setTotaldeaths(raw.getTotalDeaths());
data.setTotalkills(raw.getTotalKills());
data.setTotalmobkills(raw.getTotalMobKills());
data.setRefreshDate(new Date().getTime());
analysisCache.cache(data);
@ -212,6 +180,14 @@ public class Analysis {
this.cancel();
}
private void createCommandUseTable(final RawAnalysisData raw, AnalysisData data) {
if (!raw.getCommandUse().isEmpty()) {
data.setTop50CommandsListHtml(AnalysisUtils.createTableOutOfHashMap(raw.getCommandUse()));
} else {
data.setTop50CommandsListHtml(Html.ERROR_TABLE.parse());
}
}
private boolean isPlanLiteEnabled() {
boolean planLiteEnabled;
PlanLiteHook planLiteHook = plugin.getPlanLiteHook();
@ -260,21 +236,6 @@ public class Analysis {
data.setAverageAge(averageAge);
}
private void createCommandUseTable(AnalysisData data) {
if (rawServerData.keySet().size() > 0) {
ServerData sData = null;
for (long sDataKey : rawServerData.keySet()) {
sData = rawServerData.get(sDataKey);
break;
}
if (sData != null) {
data.setTop50CommandsListHtml(AnalysisUtils.createTableOutOfHashMap(sData.getCommandUsage()));
}
} else {
data.setTop50CommandsListHtml(Html.ERROR_TABLE.parse());
}
}
private void createGamemodeUsageVisualization(long gmZero, long gmOne, long gmTwo, long gmThree, AnalysisData data) {
long gmTotal = gmZero + gmOne + gmTwo + gmThree;
HashMap<GameMode, Long> totalGmTimes = new HashMap<>();