chnaged file to memory

This commit is contained in:
rdavidek 2025-12-28 20:28:17 +01:00
parent 7868580c00
commit 9c59bd0d9d
2 changed files with 40 additions and 17 deletions

View File

@ -16,7 +16,7 @@ import java.util.stream.Collectors;
public class App {
private static final Gson GSON = new Gson();
private static final String DATA_FILE = "stats.json";
private static final List<DartsGame> GAMES_CACHE = Collections.synchronizedList(new ArrayList<>());
private static final long MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5MB
public static void main(String[] args) throws IOException {
@ -26,6 +26,7 @@ public class App {
server.createContext("/", new StaticHandler());
server.createContext("/api/stats", new StatsHandler());
server.createContext("/api/upload", new UploadHandler());
server.createContext("/api/clear", new ClearHandler());
server.setExecutor(null);
System.out.println("Server started on port " + port);
@ -103,7 +104,8 @@ public class App {
List<RawGame> rawGames = GSON.fromJson(body, new TypeToken<List<RawGame>>(){}.getType());
List<DartsGame> convertedGames = convertRawToGames(rawGames);
Files.writeString(Paths.get(DATA_FILE), GSON.toJson(convertedGames));
GAMES_CACHE.clear();
GAMES_CACHE.addAll(convertedGames);
String response = "Upload successful";
byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
@ -181,8 +183,7 @@ public class App {
return;
}
List<DartsGame> games = loadGames();
StatsResponse response = aggregateStats(games);
StatsResponse response = aggregateStats(new ArrayList<>(GAMES_CACHE));
String json = GSON.toJson(response);
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
@ -194,19 +195,6 @@ public class App {
os.close();
}
private List<DartsGame> loadGames() {
try {
if (!Files.exists(Paths.get(DATA_FILE))) {
return Collections.emptyList();
}
String content = Files.readString(Paths.get(DATA_FILE));
return GSON.fromJson(content, new TypeToken<List<DartsGame>>(){}.getType());
} catch (IOException e) {
e.printStackTrace();
return Collections.emptyList();
}
}
private StatsResponse aggregateStats(List<DartsGame> games) {
Map<String, List<DartsGame>> byPlayer = games.stream()
.collect(Collectors.groupingBy(DartsGame::getPlayer));
@ -240,4 +228,22 @@ public class App {
.build();
}
}
static class ClearHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
if (!"POST".equals(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(405, -1);
return;
}
GAMES_CACHE.clear();
String response = "Cache cleared";
byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, responseBytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(responseBytes);
}
}
}
}

View File

@ -166,6 +166,7 @@
<h2 style="margin: 0 0 8px 0; font-size: 1.1rem">Upload Stats JSON</h2>
<input type="file" id="fileInput" accept=".json">
<button onclick="uploadFile()">Upload</button>
<button onclick="clearCache()" style="background: #475569; margin-left: 10px;">Clear Cache</button>
</div>
<span id="uploadStatus" style="font-weight: 600"></span>
</div>
@ -230,6 +231,22 @@
reader.readAsText(file);
}
async function clearCache() {
if (!confirm('Are you sure you want to clear all data?')) return;
const status = document.getElementById('uploadStatus');
try {
const response = await fetch('/api/clear', { method: 'POST' });
if (response.ok) {
status.innerText = 'Cache cleared!';
loadStats();
} else {
status.innerText = 'Clear failed: ' + await response.text();
}
} catch (err) {
status.innerText = 'Error: ' + err.message;
}
}
async function loadStats() {
const response = await fetch('/api/stats');
const data = await response.json();