From 4f0108d997e2bd56bb48d1ddec47630038ff110b Mon Sep 17 00:00:00 2001 From: gsd Date: Sat, 27 Apr 2024 00:38:02 +0300 Subject: [PATCH] online stats features v1 --- src/main/java/app/updates/OnlineUpdater.java | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/main/java/app/updates/OnlineUpdater.java b/src/main/java/app/updates/OnlineUpdater.java index 7377a0e..492d184 100644 --- a/src/main/java/app/updates/OnlineUpdater.java +++ b/src/main/java/app/updates/OnlineUpdater.java @@ -9,7 +9,12 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; import java.time.Instant; +import java.time.LocalDate; +import java.util.List; @Component public class OnlineUpdater extends BaseUpdater { @@ -56,6 +61,19 @@ public class OnlineUpdater extends BaseUpdater { return Math.abs(rw - ro); } + //todo abstract on all + public List getStatsOnPerDay(String server_id) { + return jdbcTemplate_ro.query("SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY FROM_UNIXTIME(`utime`) div ? ORDER BY `ts` ASC", + new Object[]{server_id, PerDayStats.period}, + (rs, rowNum) -> new PerDayStats(rs)); + } + + public List getStatsOnPerFiveMinutes(String server_id) { + return jdbcTemplate_ro.query("SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY FROM_UNIXTIME(`utime`) div ? ORDER BY `ts` ASC", + new Object[]{server_id, PerFiveMinutesStats.period}, + (rs, rowNum) -> new PerFiveMinutesStats(rs)); + } + //CREATE TABLE `tf2.facti13`.`servers_online` (`id` INT NOT NULL AUTO_INCREMENT , `utime` INT NOT NULL , `player_count` INT NOT NULL , `max_players` INT NOT NULL , `server_id` VARCHAR(32) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB; public class ServerOnline { private int id; @@ -64,4 +82,48 @@ public class OnlineUpdater extends BaseUpdater { private int max_players; private String server_id; } + + //stats per 5 = '500' minutes + //SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `server_id` like 'srv5' and FROM_UNIXTIME(`utime`) > CAST(DATE_FORMAT(NOW() ,'%Y-%m-26') as DATE) GROUP BY FROM_UNIXTIME(`utime`) div 500 ORDER BY `ts` ASC; + public class PerFiveMinutesStats extends PerPeriodStats { + private static final int period = 500; + PerFiveMinutesStats(ResultSet rs) throws SQLException { + super(rs, period); + } + } + + + //SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `player_count` != 0 and `server_id` like 'srv5' GROUP BY FROM_UNIXTIME(`utime`) div 800000 ORDER BY `ts` ASC; + public class PerDayStats extends PerPeriodStats { + private static final int period = 800000; + PerDayStats(ResultSet rs) throws SQLException { + super(rs, period); + } + } + + public abstract class PerPeriodStats { + private final double player_count; + private final Timestamp timestamp; + //private long utime; + private final int period; + + PerPeriodStats(ResultSet rs, int period) throws SQLException { + this.player_count = rs.getDouble("player_count"); + this.timestamp = rs.getTimestamp("fulldate"); + //this.utime = rs.getLong("ts"); + this.period = period / 100 * 60; + } + + public long getPlayer_count() { + return Math.round(this.player_count); + } + + public LocalDate getDate() { + return this.timestamp.toLocalDateTime().toLocalDate(); + } + + public int getPeriod() { + return period; + } + } }