-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add request statistics tracking functionality
- Loading branch information
Showing
10 changed files
with
155 additions
and
5 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
45 changes: 45 additions & 0 deletions
45
Refresh.GameServer/Database/GameDatabaseContext.Statistics.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,45 @@ | ||
using Refresh.GameServer.Types; | ||
|
||
namespace Refresh.GameServer.Database; | ||
|
||
public partial class GameDatabaseContext // Statistics | ||
{ | ||
public RequestStatistics GetRequestStatistics() | ||
{ | ||
RequestStatistics? statistics = this._realm.All<RequestStatistics>().FirstOrDefault(); | ||
if (statistics != null) return statistics; | ||
|
||
statistics = new RequestStatistics(); | ||
this._realm.Write(() => | ||
{ | ||
this._realm.Add(statistics); | ||
}); | ||
|
||
return statistics; | ||
} | ||
|
||
public void IncrementApiRequests() | ||
{ | ||
RequestStatistics statistics = this.GetRequestStatistics(); | ||
this._realm.Write(() => { | ||
statistics.TotalRequests++; | ||
statistics.ApiRequests++; | ||
}); | ||
} | ||
public void IncrementLegacyApiRequests() | ||
{ | ||
RequestStatistics statistics = this.GetRequestStatistics(); | ||
this._realm.Write(() => { | ||
statistics.TotalRequests++; | ||
statistics.LegacyApiRequests++; | ||
}); | ||
} | ||
public void IncrementGameRequests() | ||
{ | ||
RequestStatistics statistics = this.GetRequestStatistics(); | ||
this._realm.Write(() => { | ||
statistics.TotalRequests++; | ||
statistics.GameRequests++; | ||
}); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
Refresh.GameServer/Endpoints/ApiV3/DataTypes/Response/ApiRequestStatisticsResponse.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,27 @@ | ||
using Refresh.GameServer.Types; | ||
|
||
namespace Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response; | ||
|
||
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] | ||
public class ApiRequestStatisticsResponse : IApiResponse, IDataConvertableFrom<ApiRequestStatisticsResponse, RequestStatistics> | ||
{ | ||
public required long TotalRequests { get; set; } | ||
public required long ApiRequests { get; set; } | ||
public required long LegacyApiRequests { get; set; } | ||
public required long GameRequests { get; set; } | ||
|
||
public static ApiRequestStatisticsResponse? FromOld(RequestStatistics? old) | ||
{ | ||
if (old == null) return null; | ||
|
||
return new ApiRequestStatisticsResponse | ||
{ | ||
TotalRequests = old.TotalRequests, | ||
ApiRequests = old.ApiRequests, | ||
LegacyApiRequests = old.LegacyApiRequests, | ||
GameRequests = old.GameRequests, | ||
}; | ||
} | ||
|
||
public static IEnumerable<ApiRequestStatisticsResponse> FromOldList(IEnumerable<RequestStatistics> oldList) => oldList.Select(FromOld)!; | ||
} |
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
37 changes: 37 additions & 0 deletions
37
Refresh.GameServer/Services/RequestStatisticTrackingService.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,37 @@ | ||
using System.Reflection; | ||
using Bunkum.CustomHttpListener.Request; | ||
using Bunkum.HttpServer; | ||
using Bunkum.HttpServer.Database; | ||
using Bunkum.HttpServer.Responses; | ||
using Bunkum.HttpServer.Services; | ||
using NotEnoughLogs; | ||
using Refresh.GameServer.Database; | ||
using Refresh.GameServer.Endpoints; | ||
|
||
namespace Refresh.GameServer.Services; | ||
|
||
public class RequestStatisticTrackingService : Service | ||
{ | ||
internal RequestStatisticTrackingService(LoggerContainer<BunkumContext> logger) : base(logger) | ||
{} | ||
|
||
public override Response? OnRequestHandled(ListenerContext context, MethodInfo method, Lazy<IDatabaseContext> database) | ||
{ | ||
GameDatabaseContext gameDatabase = (GameDatabaseContext)database.Value; | ||
|
||
if (context.Uri.AbsolutePath.StartsWith(GameEndpointAttribute.BaseRoute)) | ||
{ | ||
gameDatabase.IncrementGameRequests(); | ||
} | ||
else if(context.Uri.AbsolutePath.StartsWith(LegacyApiEndpointAttribute.BaseRoute)) | ||
{ | ||
gameDatabase.IncrementLegacyApiRequests(); | ||
} | ||
else | ||
{ | ||
gameDatabase.IncrementApiRequests(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,11 @@ | ||
using Realms; | ||
|
||
namespace Refresh.GameServer.Types; | ||
|
||
public partial class RequestStatistics : IRealmObject | ||
{ | ||
public long TotalRequests { get; set; } | ||
public long ApiRequests { get; set; } | ||
public long LegacyApiRequests { get; set; } | ||
public long GameRequests { get; set; } | ||
} |