forked from lighttube-org/LightTube
-
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.
Implement a /health endpoint (lighttube-org#170)
* Implement health managing * Consider cache hits
- Loading branch information
Showing
4 changed files
with
69 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using InnerTube.Models; | ||
|
||
namespace LightTube.ApiModels; | ||
|
||
public class HealthResponse | ||
{ | ||
public int VideoHealth { get; set; } | ||
public double AveragePlayerResponseTime { get; set; } | ||
public CacheStats CacheStats { get; set; } | ||
} |
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,32 @@ | ||
namespace LightTube.Health; | ||
|
||
public static class HealthManager | ||
{ | ||
private static List<KeyValuePair<string, bool>> videoStatuses = []; | ||
private static List<long> playerResponseTimes = []; | ||
|
||
public static void PushVideoResponse(string videoId, bool isSuccess, long playerResponseTime) | ||
{ | ||
// don't include cache hits | ||
if (playerResponseTime < 50) return; | ||
|
||
// if entry with the same videoId exists, remove it | ||
videoStatuses.RemoveAll(x => x.Key == videoId); | ||
|
||
// only keep last 100 requests | ||
if (videoStatuses.Count >= 100) videoStatuses.RemoveAt(0); | ||
if (playerResponseTimes.Count >= 100) playerResponseTimes.RemoveAt(0); | ||
|
||
playerResponseTimes.Add(playerResponseTime); | ||
videoStatuses.Add(new KeyValuePair<string, bool>(videoId, isSuccess)); | ||
} | ||
|
||
public static float GetHealthPercentage() => | ||
Math.Clamp(MathF.Round((float)videoStatuses.Count(x => x.Value) / Math.Max(videoStatuses.Count, 1) * 100), 0, 100); | ||
|
||
public static double GetAveragePlayerResponseTime() | ||
{ | ||
if (playerResponseTimes.Count == 0) return 0; | ||
return playerResponseTimes.Average(); | ||
} | ||
} |