alter primary keys to uuids and adjust the membership system
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"acc-server-manager/local/model"
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -28,7 +29,7 @@ func (r *StateHistoryRepository) Insert(ctx context.Context, model *model.StateH
|
||||
}
|
||||
|
||||
// GetLastSessionID gets the last session ID for a server
|
||||
func (r *StateHistoryRepository) GetLastSessionID(ctx context.Context, serverID uint) (uint, error) {
|
||||
func (r *StateHistoryRepository) GetLastSessionID(ctx context.Context, serverID uuid.UUID) (uuid.UUID, error) {
|
||||
var lastSession model.StateHistory
|
||||
result := r.BaseRepository.db.WithContext(ctx).
|
||||
Where("server_id = ?", serverID).
|
||||
@@ -37,9 +38,9 @@ func (r *StateHistoryRepository) GetLastSessionID(ctx context.Context, serverID
|
||||
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return 0, nil // Return 0 if no sessions found
|
||||
return uuid.Nil, nil // Return nil UUID if no sessions found
|
||||
}
|
||||
return 0, result.Error
|
||||
return uuid.Nil, result.Error
|
||||
}
|
||||
|
||||
return lastSession.SessionID, nil
|
||||
@@ -48,13 +49,19 @@ func (r *StateHistoryRepository) GetLastSessionID(ctx context.Context, serverID
|
||||
// GetSummaryStats calculates peak players, total sessions, and average players.
|
||||
func (r *StateHistoryRepository) GetSummaryStats(ctx context.Context, filter *model.StateHistoryFilter) (model.StateHistoryStats, error) {
|
||||
var stats model.StateHistoryStats
|
||||
// Parse ServerID to UUID for query
|
||||
serverUUID, err := uuid.Parse(filter.ServerID)
|
||||
if err != nil {
|
||||
return model.StateHistoryStats{}, err
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.StateHistory{}).
|
||||
Select(`
|
||||
COALESCE(MAX(player_count), 0) as peak_players,
|
||||
COUNT(DISTINCT session_id) as total_sessions,
|
||||
COALESCE(AVG(player_count), 0) as average_players
|
||||
`).
|
||||
Where("server_id = ?", filter.ServerID)
|
||||
Where("server_id = ?", serverUUID)
|
||||
|
||||
if !filter.StartDate.IsZero() && !filter.EndDate.IsZero() {
|
||||
query = query.Where("date_created BETWEEN ? AND ?", filter.StartDate, filter.EndDate)
|
||||
@@ -71,6 +78,12 @@ func (r *StateHistoryRepository) GetTotalPlaytime(ctx context.Context, filter *m
|
||||
var totalPlaytime struct {
|
||||
TotalMinutes float64
|
||||
}
|
||||
// Parse ServerID to UUID for query
|
||||
serverUUID, err := uuid.Parse(filter.ServerID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
rawQuery := `
|
||||
SELECT SUM(duration_minutes) as total_minutes FROM (
|
||||
SELECT (strftime('%s', MAX(date_created)) - strftime('%s', MIN(date_created))) / 60.0 as duration_minutes
|
||||
@@ -80,7 +93,7 @@ func (r *StateHistoryRepository) GetTotalPlaytime(ctx context.Context, filter *m
|
||||
HAVING COUNT(*) > 1 AND MAX(player_count) > 0
|
||||
)
|
||||
`
|
||||
err := r.db.WithContext(ctx).Raw(rawQuery, filter.ServerID, filter.StartDate, filter.EndDate).Scan(&totalPlaytime).Error
|
||||
err = r.db.WithContext(ctx).Raw(rawQuery, serverUUID, filter.StartDate, filter.EndDate).Scan(&totalPlaytime).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -90,6 +103,12 @@ func (r *StateHistoryRepository) GetTotalPlaytime(ctx context.Context, filter *m
|
||||
// GetPlayerCountOverTime gets downsampled player count data.
|
||||
func (r *StateHistoryRepository) GetPlayerCountOverTime(ctx context.Context, filter *model.StateHistoryFilter) ([]model.PlayerCountPoint, error) {
|
||||
var points []model.PlayerCountPoint
|
||||
// Parse ServerID to UUID for query
|
||||
serverUUID, err := uuid.Parse(filter.ServerID)
|
||||
if err != nil {
|
||||
return points, err
|
||||
}
|
||||
|
||||
rawQuery := `
|
||||
SELECT
|
||||
DATETIME(MIN(date_created)) as timestamp,
|
||||
@@ -99,13 +118,19 @@ func (r *StateHistoryRepository) GetPlayerCountOverTime(ctx context.Context, fil
|
||||
GROUP BY strftime('%Y-%m-%d %H', date_created)
|
||||
ORDER BY timestamp
|
||||
`
|
||||
err := r.db.WithContext(ctx).Raw(rawQuery, filter.ServerID, filter.StartDate, filter.EndDate).Scan(&points).Error
|
||||
err = r.db.WithContext(ctx).Raw(rawQuery, serverUUID, filter.StartDate, filter.EndDate).Scan(&points).Error
|
||||
return points, err
|
||||
}
|
||||
|
||||
// GetSessionTypes counts sessions by type.
|
||||
func (r *StateHistoryRepository) GetSessionTypes(ctx context.Context, filter *model.StateHistoryFilter) ([]model.SessionCount, error) {
|
||||
var sessionTypes []model.SessionCount
|
||||
// Parse ServerID to UUID for query
|
||||
serverUUID, err := uuid.Parse(filter.ServerID)
|
||||
if err != nil {
|
||||
return sessionTypes, err
|
||||
}
|
||||
|
||||
rawQuery := `
|
||||
SELECT session as name, COUNT(*) as count FROM (
|
||||
SELECT session
|
||||
@@ -116,13 +141,19 @@ func (r *StateHistoryRepository) GetSessionTypes(ctx context.Context, filter *mo
|
||||
GROUP BY session
|
||||
ORDER BY count DESC
|
||||
`
|
||||
err := r.db.WithContext(ctx).Raw(rawQuery, filter.ServerID, filter.StartDate, filter.EndDate).Scan(&sessionTypes).Error
|
||||
err = r.db.WithContext(ctx).Raw(rawQuery, serverUUID, filter.StartDate, filter.EndDate).Scan(&sessionTypes).Error
|
||||
return sessionTypes, err
|
||||
}
|
||||
|
||||
// GetDailyActivity counts sessions per day.
|
||||
func (r *StateHistoryRepository) GetDailyActivity(ctx context.Context, filter *model.StateHistoryFilter) ([]model.DailyActivity, error) {
|
||||
var dailyActivity []model.DailyActivity
|
||||
// Parse ServerID to UUID for query
|
||||
serverUUID, err := uuid.Parse(filter.ServerID)
|
||||
if err != nil {
|
||||
return dailyActivity, err
|
||||
}
|
||||
|
||||
rawQuery := `
|
||||
SELECT
|
||||
strftime('%Y-%m-%d', date_created) as date,
|
||||
@@ -132,13 +163,19 @@ func (r *StateHistoryRepository) GetDailyActivity(ctx context.Context, filter *m
|
||||
GROUP BY 1
|
||||
ORDER BY 1
|
||||
`
|
||||
err := r.db.WithContext(ctx).Raw(rawQuery, filter.ServerID, filter.StartDate, filter.EndDate).Scan(&dailyActivity).Error
|
||||
err = r.db.WithContext(ctx).Raw(rawQuery, serverUUID, filter.StartDate, filter.EndDate).Scan(&dailyActivity).Error
|
||||
return dailyActivity, err
|
||||
}
|
||||
|
||||
// GetRecentSessions retrieves the 10 most recent sessions.
|
||||
func (r *StateHistoryRepository) GetRecentSessions(ctx context.Context, filter *model.StateHistoryFilter) ([]model.RecentSession, error) {
|
||||
var recentSessions []model.RecentSession
|
||||
// Parse ServerID to UUID for query
|
||||
serverUUID, err := uuid.Parse(filter.ServerID)
|
||||
if err != nil {
|
||||
return recentSessions, err
|
||||
}
|
||||
|
||||
rawQuery := `
|
||||
SELECT
|
||||
session_id as id,
|
||||
@@ -154,6 +191,6 @@ func (r *StateHistoryRepository) GetRecentSessions(ctx context.Context, filter *
|
||||
ORDER BY date DESC
|
||||
LIMIT 10
|
||||
`
|
||||
err := r.db.WithContext(ctx).Raw(rawQuery, filter.ServerID, filter.StartDate, filter.EndDate).Scan(&recentSessions).Error
|
||||
err = r.db.WithContext(ctx).Raw(rawQuery, serverUUID, filter.StartDate, filter.EndDate).Scan(&recentSessions).Error
|
||||
return recentSessions, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user