-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from theImmortalCoders/dev
Deploy
- Loading branch information
Showing
25 changed files
with
316 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
rag-2-backend/Infrastructure/Module/Stats/Dto/OverallStatsResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace rag_2_backend.Infrastructure.Module.Stats.Dto; | ||
|
||
public class OverallStatsResponse | ||
{ | ||
public required int PlayersAmount { get; init; } | ||
public required double TotalMemoryMb { get; init; } | ||
public required int GamesAmount { get; init; } | ||
public required int GameRecordsAmount { get; init; } | ||
public DateTime StatsUpdatedDate { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#region | ||
|
||
using Newtonsoft.Json; | ||
using rag_2_backend.Infrastructure.Dao; | ||
using rag_2_backend.Infrastructure.Database.Entity; | ||
using rag_2_backend.Infrastructure.Module.Stats.Dto; | ||
using StackExchange.Redis; | ||
|
||
#endregion | ||
|
||
namespace rag_2_backend.Infrastructure.Util; | ||
|
||
public class StatsUtil( | ||
IConfiguration configuration, | ||
IConnectionMultiplexer redisConnection, | ||
UserDao userDao, | ||
GameDao gameDao, | ||
GameRecordDao gameRecordDao | ||
) | ||
{ | ||
private readonly IDatabase _redisDatabase = redisConnection.GetDatabase(); | ||
|
||
public GameStatsResponse UpdateCachedGameStats(Game game) | ||
{ | ||
var records = gameRecordDao.GetGameRecordsByGameWithUser(game.Id); | ||
|
||
var gameStatsResponse = new GameStatsResponse | ||
{ | ||
FirstPlayed = records.Count > 0 ? records[0].Started : null, | ||
LastPlayed = records.Count > 0 ? records.Last().Ended : null, | ||
Plays = records.Count, | ||
TotalStorageMb = gameRecordDao.GetGameRecordsByGameWithUser(game.Id) | ||
.Select(r => r.SizeMb) | ||
.ToList() | ||
.Sum(), | ||
TotalPlayers = records.Select(r => r.User.Id).Distinct().Count(), | ||
StatsUpdatedDate = DateTime.Now | ||
}; | ||
|
||
var cacheKey = $"{configuration.GetValue<string>("Redis:Stats:Prefix")}{game.Id}"; | ||
var serializedStats = JsonConvert.SerializeObject(gameStatsResponse); | ||
|
||
_redisDatabase.StringSet(cacheKey, serializedStats, TimeSpan.FromDays(1)); | ||
|
||
return gameStatsResponse; | ||
} | ||
|
||
public OverallStatsResponse UpdateCachedStats() | ||
{ | ||
var overallStatsResponse = new OverallStatsResponse | ||
{ | ||
PlayersAmount = userDao.CountUsers(), | ||
TotalMemoryMb = gameRecordDao.CountTotalStorageMb(), | ||
GamesAmount = gameDao.GetAllGames().Count, | ||
GameRecordsAmount = gameRecordDao.CountAllGameRecords(), | ||
StatsUpdatedDate = DateTime.Now | ||
}; | ||
|
||
var serializedStats = JsonConvert.SerializeObject(overallStatsResponse); | ||
_redisDatabase.StringSet( | ||
configuration.GetValue<string>("Redis:Stats:Prefix") + | ||
configuration.GetValue<string>("Redis:Stats:OverallStatsKey"), | ||
serializedStats, TimeSpan.FromDays(1)); | ||
|
||
return overallStatsResponse; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.