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.record.UserStats;
import mage.server.record.UserStatsRepository;
import mage.server.util.ServerMessagesUtil;
import mage.view.UserView;
import org.apache.log4j.Logger;
@ -309,7 +310,9 @@ public class UserManagerImpl implements UserManager {
// max users online stats
if (currentOnlineCount > maxUsersOnline) {
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));
ServerMessagesUtil.instance.setMaxUsersOnline(maxUsersOnline); // update online stats for news panel
}
} catch (Exception ex) {
handleException(ex);

View file

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