Server Preference pie to network page

This commit is contained in:
Rsl1122 2019-08-21 16:26:40 +03:00
parent de8f46f84c
commit a755973efe
6 changed files with 83 additions and 2 deletions

View File

@ -717,4 +717,31 @@ public class SessionQueries {
}
};
}
public static Query<Map<String, Long>> playtimePerServer(long after, long before) {
String sql = SELECT +
"SUM(" + SessionsTable.SESSION_END + '-' + SessionsTable.SESSION_START + ") as playtime," +
ServerTable.NAME +
FROM + SessionsTable.TABLE_NAME +
INNER_JOIN + ServerTable.TABLE_NAME + " s on s." + ServerTable.SERVER_UUID + '=' + SessionsTable.TABLE_NAME + '.' + SessionsTable.SERVER_UUID +
WHERE + SessionsTable.SESSION_END + ">=?" +
AND + SessionsTable.SESSION_START + "<=?" +
GROUP_BY + ServerTable.NAME;
return new QueryStatement<Map<String, Long>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, after);
statement.setLong(2, before);
}
@Override
public Map<String, Long> processResults(ResultSet set) throws SQLException {
Map<String, Long> playtimePerServer = new HashMap<>();
while (set.next()) {
playtimePerServer.put(set.getString(ServerTable.NAME), set.getLong("playtime"));
}
return playtimePerServer;
}
};
}
}

View File

@ -31,6 +31,8 @@ import com.djrapitops.plan.db.access.queries.objects.*;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.settings.theme.Theme;
import com.djrapitops.plan.system.settings.theme.ThemeVal;
import com.djrapitops.plan.utilities.html.graphs.Graphs;
import com.djrapitops.plan.utilities.html.graphs.bar.BarGraph;
import com.djrapitops.plan.utilities.html.graphs.line.LineGraphFactory;
@ -57,6 +59,7 @@ import java.util.stream.Collectors;
public class GraphJSONParser {
private final PlanConfig config;
private final Theme theme;
private final DBSystem dbSystem;
private final Graphs graphs;
private final TimeZone timeZone;
@ -64,10 +67,12 @@ public class GraphJSONParser {
@Inject
public GraphJSONParser(
PlanConfig config,
Theme theme,
DBSystem dbSystem,
Graphs graphs
) {
this.config = config;
this.theme = theme;
this.dbSystem = dbSystem;
this.graphs = graphs;
this.timeZone = config.getTimeZone();
@ -264,4 +269,18 @@ public class GraphJSONParser {
);
return Collections.singletonMap("punchCard", graphs.special().punchCard(sessions).getDots());
}
public Map<String, Object> serverPreferencePieJSONAsMap() {
long now = System.currentTimeMillis();
long monthAgo = now - TimeUnit.DAYS.toMillis(30L);
String[] pieColors = Arrays.stream(theme.getValue(ThemeVal.GRAPH_WORLD_PIE).split(","))
.map(color -> color.trim().replace("\"", ""))
.toArray(String[]::new);
Map<String, Object> data = new HashMap<>();
data.put("server_pie_colors", pieColors);
Map<String, Long> serverPlaytimes = dbSystem.getDatabase().query(SessionQueries.playtimePerServer(now, monthAgo));
data.put("server_pie_series_30d", graphs.pie().serverPreferencePie(serverPlaytimes).getSlices());
return data;
}
}

View File

@ -96,6 +96,8 @@ public class GraphsJSONHandler implements PageHandler {
return new JSONResponse(graphJSON.activityGraphsJSONAsMap());
case "uniqueAndNew":
return new JSONResponse(graphJSON.uniqueAndNewGraphJSON());
case "serverPie":
return new JSONResponse(graphJSON.serverPreferencePieJSONAsMap());
default:
throw new BadRequestException("unknown 'type' parameter: " + type);
}

View File

@ -65,11 +65,14 @@ public class PieGraphFactory {
return new ActivityPie(activityData, colors);
}
public Pie serverPreferencePie(Map<UUID, String> serverNames, Map<UUID, WorldTimes> serverWorldTimes) {
return new ServerPreferencePie(serverNames, serverWorldTimes);
}
public Pie serverPreferencePie(Map<String, Long> serverPlaytimes) {
return new ServerPreferencePie(serverPlaytimes);
}
public WorldPie worldPie(WorldTimes worldTimes) {
WorldAliasSettings worldAliasSettings = config.getWorldAliasSettings();
Map<String, Long> playtimePerAlias = worldAliasSettings.getPlaytimePerAlias(worldTimes);

View File

@ -29,6 +29,10 @@ public class ServerPreferencePie extends Pie {
super(turnToSlices(serverNames, serverWorldTimes));
}
ServerPreferencePie(Map<String, Long> serverPlaytimes) {
super(turnToSlices(serverPlaytimes));
}
private static List<PieSlice> turnToSlices(Map<UUID, String> serverNames, Map<UUID, WorldTimes> serverWorldTimes) {
List<PieSlice> slices = new ArrayList<>();
@ -44,4 +48,16 @@ public class ServerPreferencePie extends Pie {
return slices;
}
private static List<PieSlice> turnToSlices(Map<String, Long> serverPlaytimes) {
List<PieSlice> slices = new ArrayList<>();
for (Map.Entry<String, Long> server : serverPlaytimes.entrySet()) {
String serverName = server.getKey();
long playtime = server.getValue();
slices.add(new PieSlice(serverName, playtime));
}
return slices;
}
}

View File

@ -404,7 +404,7 @@
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold col-black"><i
class="fa fa-fw fa-network-wired col-teal"></i>
Server Playtime</h6>
Server Playtime for 30 Days</h6>
</div>
<div class="chart-pie" id="serverPie"></div>
</div>
@ -944,6 +944,20 @@
}
});
jsonRequest("../v1/graph?type=serverPie", function (json, error) {
if (data) {
serverPieSeries = {
name: 'Server Playtime',
colorByPoint: true,
colors: json.server_pie_colors,
data: json.server_pie_series_30d
};
serverPie('serverPie', serverPieSeries);
} else if (error) {
$('#serverPie').text("Failed to load graph data: " + error)
}
});
jsonRequest("../v1/graph?type=activity", function (json, error) {
if (json) {
activityPie('activityPie', {