Browse Source

Improve efficiency by passing StatsObject to the StreamUtils helper functions instead of requesting it every time

master
Laura Hausmann 9 months ago
parent
commit
5e55c7fb19
Signed by: zotan GPG Key ID: 5EC1D38FFC321311
  1. 7
      Pages/Admin.cshtml
  2. 15
      Pages/Dashboard.cshtml
  3. 5
      Pages/profile.cshtml
  4. 34
      StreamUtils.cs

7
Pages/Admin.cshtml

@ -30,12 +30,11 @@
<tr>
<th scope="row">@user.Username</th>
<td>
@if (StreamUtils.IsLive(user.Username)) {
var uptimestr = stats.Server.Applications.First(p => p.Name == "ingress").MethodLive.Streams.First(p => p.Name == user.Username).Time;
var uptime = TimeSpan.FromMilliseconds(long.Parse(uptimestr)).StripMilliseconds();
@if (StreamUtils.IsLive(user.Username, stats)) {
var uptime = TimeSpan.FromMilliseconds(StreamUtils.GetClientTime(user.Username, stats)).StripMilliseconds();
if (user.AllowRestream && !string.IsNullOrWhiteSpace(user.RestreamTargets)) {
var restreams = StreamUtils.CountLiveRestreams(user.Username);
var restreams = StreamUtils.CountLiveRestreams(user.Username, stats);
if (restreams > 0) {
<button class="btn btn-dark" role="button" style="width:18ch" disabled>Restreaming (@restreams)</button>
}

15
Pages/Dashboard.cshtml

@ -18,6 +18,7 @@
else {
var db = new AppDb.DbConn();
var user = db.Users.First(p => p.Username == HttpContext.Session.GetString("authenticatedUser"));
var stats = StreamUtils.GetStatsObject();
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Welcome back!</h4>
<p>Thanks for using @Program.SiteName. If you have any issues, please contact me on <a href="https://t.me/zotan">Telegram</a>, <a href="https://threema.id/S59S9U8J">Threema</a>, or via <a href="mailto:zotan@zotan.pw">email</a>.</p>
@ -60,13 +61,11 @@ else {
</div>
<input type="text" class="form-control" id="input-streamurl" value="@Program.IngressDomain/ingress/@user.StreamKey" disabled>
<div class="input-group-append">
@if (StreamUtils.IsLive(user.Username)) {
var stats = StreamUtils.GetStatsObject();
var uptimestr = stats.Server.Applications.First(p => p.Name == "ingress").MethodLive.Streams.First(p => p.Name == user.Username).Time;
var uptime = TimeSpan.FromMilliseconds(long.Parse(uptimestr)).StripMilliseconds();
@if (StreamUtils.IsLive(user.Username, stats)) {
var uptime = TimeSpan.FromMilliseconds(StreamUtils.GetClientTime(user.Username, stats)).StripMilliseconds();
if (user.AllowRestream && !string.IsNullOrWhiteSpace(user.RestreamTargets)) {
if (StreamUtils.GetClientTime(user.Username) > 5000) {
var restreams = StreamUtils.CountLiveRestreams(user.Username);
if (StreamUtils.GetClientTime(user.Username, stats) > 5000) {
var restreams = StreamUtils.CountLiveRestreams(user.Username, stats);
if (restreams > 0) {
<button class="btn btn-success" role="button" style="width:20ch" disabled>Live & restreaming</button>
}
@ -125,8 +124,8 @@ else {
<input type="hidden" value="restream_targets_set" name="action">
<input type="text" class="form-control" name="value" placeholder="rtmp://live-ber.twitch.tv/app/streamkey,rtmp://a.rtmp.youtube.com/live2/streamkey" value="@user.RestreamTargets">
<div class="input-group-append">
@if (!string.IsNullOrWhiteSpace(user.RestreamTargets) && StreamUtils.IsLive(user.Username) && StreamUtils.GetClientTime(user.Username) > 5000) {
var restreams = StreamUtils.CountLiveRestreams(user.Username);
@if (!string.IsNullOrWhiteSpace(user.RestreamTargets) && StreamUtils.IsLive(user.Username, stats) && StreamUtils.GetClientTime(user.Username, stats) > 5000) {
var restreams = StreamUtils.CountLiveRestreams(user.Username, stats);
if (restreams == 1) {
<button class="btn btn-dark" role="button" style="width:19ch" disabled>@restreams Restream active</button>
}

5
Pages/profile.cshtml

@ -9,10 +9,11 @@
return;
}
var user = db.Users.First(p => p.Username == Model.User);
var live = StreamUtils.IsLive(user.Username);
var stats = StreamUtils.GetStatsObject();
var live = StreamUtils.IsLive(user.Username, stats);
Stream stream = null;
if (live) {
stream = StreamUtils.GetStatsObject().Server.Applications.First(p => p.Name == "ingress").MethodLive.Streams.FirstOrDefault(p => p.Name == user.Username);
stream = stats.Server.Applications.First(p => p.Name == "ingress").MethodLive.Streams.FirstOrDefault(p => p.Name == user.Username);
}
var pronounAdditional = user.PronounSubject == "they" ? "are" : "is"; // TODO make this configurable too
}

34
StreamUtils.cs

@ -8,27 +8,23 @@ namespace RTMPDash {
public static class StreamUtils {
private static readonly XmlSerializer Serializer = new(typeof(StatsObject));
public static bool IsLive(string user) => GetStatsObject()
.Server.Applications.First(p => p.Name == "ingress")
.MethodLive.Streams.Any(p => p.Name == user);
public static bool IsLive(string user, string target) => GetStatsObject()
.Server.Applications.First(p => p.Name == "ingress")
.MethodLive.Streams
.Any(p => p.Name == user
&& p.Clients.Any(c => c.Address
== target
.Replace("rtmp://", "")));
public static long GetClientTime(string user) => long.Parse(GetStatsObject()
.Server.Applications.First(p => p.Name == "ingress")
.MethodLive.Streams.First(p => p.Name == user)
.Time);
public static int CountLiveRestreams(string user) {
public static bool IsLive(string user, StatsObject stats) => stats.Server.Applications
.First(p => p.Name == "ingress")
.MethodLive.Streams.Any(p => p.Name == user);
public static bool IsLive(string user, string target, StatsObject stats) => stats.Server.Applications
.First(p => p.Name == "ingress")
.MethodLive.Streams
.Any(p => p.Name == user && p.Clients.Any(c => c.Address == target.Replace("rtmp://", "")));
public static long GetClientTime(string user, StatsObject stats) =>
long.Parse(stats.Server.Applications.First(p => p.Name == "ingress")
.MethodLive.Streams.First(p => p.Name == user)
.Time);
public static int CountLiveRestreams(string user, StatsObject stats) {
var db = new AppDb.DbConn();
var dbUser = db.Users.First(p => p.Username == user);
var stats = GetStatsObject();
return dbUser.RestreamTargets.Split(",")
.Count(target => stats.Server.Applications.First(p => p.Name == "ingress")
.MethodLive.Streams

Loading…
Cancel
Save