@ -5,6 +5,8 @@ import app.entities.other.SteamID;
import app.entities.server.Server ;
import app.entities.Stats ;
import app.utils.SteamIDConverter ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.jdbc.core.JdbcTemplate ;
@ -27,6 +29,7 @@ public class UsertimeService {
//Лучше это всегда держать в true
private boolean last_map = true ;
private Stats stats ;
private static final Logger logger = LoggerFactory . getLogger ( UsertimeService . class ) ;
@Autowired
public UsertimeService ( Stats stats ) {
@ -88,28 +91,42 @@ public class UsertimeService {
}
public Timestamp getLastplay ( String server_id , SteamID steamID , String map ) {
return jdbcTemplate . query ( "select timestamp from user_connections WHERE srv_id like ? AND map LIKE ? and connection_type LIKE ? AND account_id = ? ORDER BY user_connections.id DESC LIMIT 1" ,
new Object [ ] { server_id , map , "disconnect" , steamID . account_id } ,
if ( server_id = = null | | server_id . isEmpty ( ) | | steamID = = null | | map = = null ) {
logger . warn ( "Cannot fetch lastplay, req values is null" ) ;
return null ;
}
return jdbcTemplate . query ( "select timestamp from user_connections WHERE account_id = ? AND srv_id = ? AND map = ? and connection_type = ? ORDER BY user_connections.id DESC LIMIT 1" ,
new Object [ ] { steamID . account_id , server_id , map , "disconnect" } ,
( rs , n ) - > rs . getTimestamp ( "timestamp" ) )
. stream ( ) . findFirst ( ) . orElse ( null ) ;
}
public BigDecimal getTotalPlaytime ( String server_id , SteamID steamID , String map ) {
List < BigDecimal > l = jdbcTemplate . query ( "select sum(connect_duration) as total from user_connections WHERE srv_id like ? AND map LIKE ? and connection_type LIKE ? AND account_id = ?" ,
new Object [ ] { server_id , map , "disconnect" , steamID . account_id } ,
if ( server_id = = null | | server_id . isEmpty ( ) | | steamID = = null | | map = = null ) {
logger . warn ( "Cannot fetch totalplaytime, req values is null" ) ;
return null ;
}
List < BigDecimal > l = jdbcTemplate . query ( "select sum(connect_duration) as total from user_connections WHERE account_id = ? AND srv_id = ? AND map = ? and connection_type = ?" ,
new Object [ ] { steamID . account_id , server_id , map , "disconnect" } ,
( rs , n ) - > rs . getBigDecimal ( "total" ) ) ;
//optional if has error
return l . size ( ) > 0 ? l . get ( 0 ) : null ;
}
public List < String > getMap ( String server_id , int limit ) {
return jdbcTemplate . query ( "select map from user_connections WHERE srv_id like ? group by map, id ORDER BY user_connections.id DESC LIMIT ?" ,
if ( server_id = = null | | server_id . isEmpty ( ) | | server_id . contains ( "%" ) ) {
logger . warn ( "Get map use wildcard or null srv_id" ) ;
}
return jdbcTemplate . query ( "select map from user_connections WHERE srv_id = ? group by map, id ORDER BY user_connections.id DESC LIMIT ?" ,
new Object [ ] { server_id , limit } ,
( rs , n ) - > rs . getString ( "map" ) ) ;
}
public List < String > getMaps ( String server_id ) {
return jdbcTemplate . query ( "select map from user_connections where srv_id like ? group by map" ,
return jdbcTemplate . query ( "select map from user_connections where srv_id = ? group by map" ,
new Object [ ] { server_id } ,
( rs , n ) - > rs . getString ( "map" ) ) ;
}
@ -122,15 +139,70 @@ public class UsertimeService {
}
public List < Gametime > getGametimeOnServer ( SteamID steamID , String server , Integer limit , Integer offset ) {
return jdbcTemplate . query ( "SELECT * FROM user_connections WHERE srv_id like ? and account_id = ? AND connection_type LIKE ? ORDER BY user_connections.id DESC LIMIT ? OFFSET ?" ,
new Object [ ] { server , steamID . account_id , "disconnect" , limit , offset } ,
int any_server = 0 ;
if ( server = = null | | server . isEmpty ( ) | | server . contains ( "%" ) )
any_server = 1 ;
return jdbcTemplate . query ( "SELECT * FROM user_connections WHERE account_id = ? and (? = 1 or srv_id = ?) AND connection_type = ? ORDER BY user_connections.id DESC LIMIT ? OFFSET ?" ,
new Object [ ] { steamID . account_id , any_server , server , "disconnect" , limit , offset } ,
( rs , n ) - > new Gametime ( rs ) ) ;
}
public Long getTotalGametimeOnServer ( SteamID steamID , String server ) {
return jdbcTemplate . query ( "SELECT count(id) FROM user_connections WHERE srv_id like ? and account_id = ? AND connection_type LIKE ?" ,
new Object [ ] { server , steamID . account_id , "disconnect" } ,
int any_server = 0 ;
if ( server = = null | | server . isEmpty ( ) | | server . contains ( "%" ) )
any_server = 1 ;
return jdbcTemplate . query ( "SELECT count(id) FROM user_connections WHERE account_id = ? AND (? = 1 or srv_id = ?) and connection_type = ?" ,
new Object [ ] { steamID . account_id , any_server , server , "disconnect" } ,
( rs , n ) - > rs . getLong ( 1 ) )
. stream ( ) . findFirst ( ) . orElse ( 0L ) ;
}
/ *
WITH ordered AS (
SELECT
id ,
account_id ,
timestamp ,
connection_type ,
srv_id ,
connect_duration ,
ROW_NUMBER ( ) OVER ( PARTITION BY account_id ORDER BY timestamp , id ) AS rn
FROM tf2_facti13 . user_connections
WHERE account_id = 127332962
) ,
paired AS (
SELECT
c . account_id ,
c . timestamp AS session_start ,
d . timestamp AS session_end ,
c . id AS connect_id ,
d . id AS disconnect_id ,
c . srv_id AS server_id ,
EXTRACT ( EPOCH FROM ( d . timestamp - c . timestamp ) ) AS delta_in_seconds ,
d . connect_duration as cd ,
- - Нумеруем сессии для каждого пользователя по времени начала
ROW_NUMBER ( ) OVER ( PARTITION BY c . account_id ORDER BY c . timestamp ) AS session_number
FROM ordered c
JOIN ordered d
ON c . account_id = d . account_id
AND c . rn + 1 = d . rn
WHERE c . connection_type = ' connect '
AND d . connection_type = ' disconnect '
AND c . srv_id = d . srv_id - - сессия на одном сервере
)
SELECT
session_number ,
account_id ,
session_start ,
session_end ,
delta_in_seconds ,
cd ,
connect_id ,
disconnect_id ,
server_id
FROM paired
ORDER BY session_number desc ;
* /
}