Skip to content

Commit 566201a

Browse files
committed
#2 Add an aggregated metric record counting all connected players
1 parent a61b460 commit 566201a

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

src/Stormancer.Plugins/Users/Stormancer.Server.Plugins.Users/Changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Added
2525
- Dimensions are saved in the analytics system on login
2626
- Added GetSelectedPlatformForPseudo to get the platform the profile system should use when computing the pseudonym.
2727
- Added platform as a default dimension for the connected users metric.
28+
- Added an aggregated `total connection users value` accross all dimensions metrics record to the list of metrics records generated by the sessions system. (Issue #2)
2829

2930
Fixed
3031
*****

src/Stormancer.Plugins/Users/Stormancer.Server.Plugins.Users/Stormancer.Server.Plugins.Users.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
66
<Company>Stormancer</Company>
77
<Authors>Stormancer</Authors>
8-
<Version>10.0.2.44</Version>
8+
<Version>10.0.2.45</Version>
99
<IncludeSymbols>true</IncludeSymbols>
1010
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/Stormancer.Plugins/Users/Stormancer.Server.Plugins.Users/UserSessions.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private IEnumerable<IIndexableField> MapSessionRecord(SessionRecord record)
175175
yield return new StringField("user.id", user.Id, Field.Store.NO);
176176
yield return new StringField("platformUserId.platform", record.platformId.Platform, Field.Store.NO);
177177
yield return new StringField("platformUserId.id", record.platformId.PlatformUserId, Field.Store.NO);
178-
yield return new StringField("platformUserId",record.platformId.ToString(), Field.Store.NO);
178+
yield return new StringField("platformUserId", record.platformId.ToString(), Field.Store.NO);
179179
foreach (var field in DefaultMapper.JsonMapper("user.auth", user.Auth))
180180
{
181181
yield return field;
@@ -512,7 +512,7 @@ public async Task CreateSessionForPeer(IScenePeerClient peer, User? user, Platfo
512512

513513
await _eventHandlers().RunEventHandler(h => h.OnLoggedIn(loginContext), ex => logger.Log(LogLevel.Error, "userSessions", "An error occurred while running LoggedIn event handlers", ex));
514514

515-
foreach (var (oldPeer,s) in currentSessions.Select(s =>( _scene.TryGetPeer(s.SessionId, out var peer) ? peer : null,s)))
515+
foreach (var (oldPeer, s) in currentSessions.Select(s => (_scene.TryGetPeer(s.SessionId, out var peer) ? peer : null, s)))
516516
{
517517
if (peer != oldPeer)
518518
{
@@ -1096,23 +1096,43 @@ public int AuthenticatedUsersCount
10961096

10971097
public ValueTask<IEnumerable<AuthenticatedUsersCount>> GetAuthenticatedUsersByDimensionsAsync()
10981098
{
1099-
var time = DateTime.UtcNow;
1100-
return ValueTask.FromResult(repository
1101-
.All
1102-
.Select(d => d.Source)
1103-
.WhereNotNull()
1104-
.GroupBy(s => s.Dimensions, _dimensionsComparer)
1105-
.Select(g => new AuthenticatedUsersCount
1099+
1100+
static IEnumerable<AuthenticatedUsersCount> GetAuthenticatedUsersByDimensions(SessionsRepository repository)
11061101
{
1107-
Timestamp = time,
1108-
Count = g.Count(),
1109-
Dimensions = g.Key
1110-
}));
1102+
var time = DateTime.UtcNow;
1103+
1104+
var total = 0;
1105+
foreach (var g in repository
1106+
.All
1107+
.Select(d => d.Source)
1108+
.WhereNotNull()
1109+
.GroupBy(s => s.Dimensions, _dimensionsComparer))
1110+
{
1111+
var count = g.Count();
1112+
total += count;
1113+
yield return new AuthenticatedUsersCount
1114+
{
1115+
Timestamp = time,
1116+
Count = count,
1117+
Dimensions = g.Key
1118+
}
1119+
;
1120+
}
1121+
1122+
yield return new AuthenticatedUsersCount
1123+
{
1124+
Timestamp = time,
1125+
Count = total,
1126+
Dimensions = FrozenDictionary<string, string>.Empty
1127+
};
1128+
}
1129+
1130+
return ValueTask.FromResult(GetAuthenticatedUsersByDimensions(repository));
11111131
}
11121132

11131133
public async Task UpdateUserOptionsAsync(string userId, string key, JObject value, CancellationToken cancellationToken)
11141134
{
1115-
var sessions = await GetSession(new PlatformId { Platform = Constants.PROVIDER_TYPE_STORMANCER, PlatformUserId = userId}, cancellationToken);
1135+
var sessions = await GetSession(new PlatformId { Platform = Constants.PROVIDER_TYPE_STORMANCER, PlatformUserId = userId }, cancellationToken);
11161136
bool first = true;
11171137
foreach (var session in sessions)
11181138
{

0 commit comments

Comments
 (0)