server: added max users online in news panel with server stats;

This commit is contained in:
Oleg Agafonov 2024-06-17 00:15:08 +04:00
parent 161835fd76
commit ba243c6512
2 changed files with 29 additions and 16 deletions

View file

@ -4,6 +4,7 @@ import mage.server.managers.ManagerFactory;
import mage.server.managers.UserManager; import mage.server.managers.UserManager;
import mage.server.record.UserStats; import mage.server.record.UserStats;
import mage.server.record.UserStatsRepository; import mage.server.record.UserStatsRepository;
import mage.server.util.ServerMessagesUtil;
import mage.view.UserView; import mage.view.UserView;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -309,7 +310,9 @@ public class UserManagerImpl implements UserManager {
// max users online stats // max users online stats
if (currentOnlineCount > maxUsersOnline) { if (currentOnlineCount > maxUsersOnline) {
maxUsersOnline = currentOnlineCount; maxUsersOnline = currentOnlineCount;
// TODO: if server get too much logs after restart (on massive reconnect) then add logs timeout here
logger.info(String.format("New max users online: %d", maxUsersOnline)); logger.info(String.format("New max users online: %d", maxUsersOnline));
ServerMessagesUtil.instance.setMaxUsersOnline(maxUsersOnline); // update online stats for news panel
} }
} catch (Exception ex) { } catch (Exception ex) {
handleException(ex); handleException(ex);

View file

@ -4,8 +4,8 @@ import mage.utils.StreamUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
@ -20,7 +20,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
* Handles server messages (Messages of the Day). Reloads messages every 5 * Handles server messages (Messages of the Day). Reloads messages every 5
* minutes. * minutes.
* *
* @author nantuko * @author nantuko, JayDi85
*/ */
public enum ServerMessagesUtil { public enum ServerMessagesUtil {
instance; instance;
@ -29,11 +29,13 @@ public enum ServerMessagesUtil {
private static final String SERVER_MSG_TXT_FILE = "server.msg.txt"; private static final String SERVER_MSG_TXT_FILE = "server.msg.txt";
private static final int SERVER_MSG_REFRESH_RATE_SECS = 60; private static final int SERVER_MSG_REFRESH_RATE_SECS = 60;
private final List<String> messages = new ArrayList<>(); private final List<String> newsMessages = new ArrayList<>();
private String statsMessage = "";
private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final ReadWriteLock lock = new ReentrantReadWriteLock();
private static boolean ignore = false; private static boolean ignore = false;
private static long startDate; private static long startDate;
private static int maxUsersOnline = 0;
private static final AtomicInteger gamesStarted = new AtomicInteger(0); private static final AtomicInteger gamesStarted = new AtomicInteger(0);
private static final AtomicInteger gamesEnded = new AtomicInteger(0); private static final AtomicInteger gamesEnded = new AtomicInteger(0);
private static final AtomicInteger tournamentsStarted = new AtomicInteger(0); private static final AtomicInteger tournamentsStarted = new AtomicInteger(0);
@ -49,7 +51,9 @@ public enum ServerMessagesUtil {
public List<String> getMessages() { public List<String> getMessages() {
lock.readLock().lock(); lock.readLock().lock();
try { try {
return messages; List<String> res = new ArrayList<>(this.newsMessages);
res.add(this.statsMessage);
return res;
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }
@ -57,14 +61,14 @@ public enum ServerMessagesUtil {
private void reloadMessages() { private void reloadMessages() {
LOGGER.debug("Reading server messages..."); LOGGER.debug("Reading server messages...");
List<String> motdMessages = readFromFile(); List<String> updatedMessages = new ArrayList<>(readFromFile());
List<String> newMessages = new ArrayList<>(motdMessages); String updatedStats = getServerStatsMessage();
newMessages.add(getServerStatistics());
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
messages.clear(); this.newsMessages.clear();
messages.addAll(newMessages); this.newsMessages.addAll(updatedMessages);
this.statsMessage = updatedStats;
} finally { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
} }
@ -80,14 +84,15 @@ public enum ServerMessagesUtil {
if (!file.exists() || !file.canRead()) { if (!file.exists() || !file.canRead()) {
// warn user about miss messages file, except dev environment // warn user about miss messages file, except dev environment
if (!file.getAbsolutePath().contains("Mage.Server")) { if (!file.getAbsolutePath().contains("Mage.Server")) {
LOGGER.warn("Couldn't find server messages file using path: " + file.getAbsolutePath()); LOGGER.warn("Can't find server messages file: " + file.getAbsolutePath());
} }
} else { } else {
try { try {
is = new FileInputStream(file); is = Files.newInputStream(file.toPath());
ignore = false; ignore = false;
} catch (Exception f) { } catch (Exception e) {
LOGGER.error(f, f); // don't read file anymore on any error
LOGGER.error("Can't read server messages file: " + file.getAbsolutePath() + " - " + e.getMessage(), e);
ignore = true; ignore = true;
} }
} }
@ -106,19 +111,20 @@ public enum ServerMessagesUtil {
newMessages.add(message.trim()); newMessages.add(message.trim());
} }
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage(), e); LOGGER.error("Can't read message from server messages file: " + e.getMessage(), e);
} finally { } finally {
StreamUtils.closeQuietly(is); StreamUtils.closeQuietly(is);
} }
return newMessages; return newMessages;
} }
private String getServerStatistics() { private String getServerStatsMessage() {
long current = System.currentTimeMillis(); long current = System.currentTimeMillis();
long hours = ((current - startDate) / (1000 * 60 * 60)); long hours = ((current - startDate) / (1000 * 60 * 60));
String updated = new Date().toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime().format(DateTimeFormatter.ofPattern("HH:mm:ss")); String updated = new Date().toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
return String.format("Server uptime: %d hours; active games: %d of %d, tourneys: %d of %d; stats from %s", return String.format("Server uptime: %d hours; max online: %d; active games: %d of %d, tourneys: %d of %d; stats from %s",
hours, hours,
maxUsersOnline,
gamesStarted.get() - gamesEnded.get(), gamesStarted.get() - gamesEnded.get(),
gamesStarted.get(), gamesStarted.get(),
tournamentsStarted.get() - tournamentsEnded.get(), tournamentsStarted.get() - tournamentsEnded.get(),
@ -131,6 +137,10 @@ public enum ServerMessagesUtil {
startDate = milliseconds; startDate = milliseconds;
} }
public void setMaxUsersOnline(int newOnline) {
maxUsersOnline = newOnline;
}
public void incGamesStarted() { public void incGamesStarted() {
int value; int value;
do { do {