Improve efficiency by passing StatsObject to the StreamUtils helper functions instead of requesting it every time
This commit is contained in:
parent
5a7248c101
commit
5e55c7fb19
|
@ -30,12 +30,11 @@
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="row">@user.Username</th>
|
<th scope="row">@user.Username</th>
|
||||||
<td>
|
<td>
|
||||||
@if (StreamUtils.IsLive(user.Username)) {
|
@if (StreamUtils.IsLive(user.Username, stats)) {
|
||||||
var uptimestr = stats.Server.Applications.First(p => p.Name == "ingress").MethodLive.Streams.First(p => p.Name == user.Username).Time;
|
var uptime = TimeSpan.FromMilliseconds(StreamUtils.GetClientTime(user.Username, stats)).StripMilliseconds();
|
||||||
var uptime = TimeSpan.FromMilliseconds(long.Parse(uptimestr)).StripMilliseconds();
|
|
||||||
|
|
||||||
if (user.AllowRestream && !string.IsNullOrWhiteSpace(user.RestreamTargets)) {
|
if (user.AllowRestream && !string.IsNullOrWhiteSpace(user.RestreamTargets)) {
|
||||||
var restreams = StreamUtils.CountLiveRestreams(user.Username);
|
var restreams = StreamUtils.CountLiveRestreams(user.Username, stats);
|
||||||
if (restreams > 0) {
|
if (restreams > 0) {
|
||||||
<button class="btn btn-dark" role="button" style="width:18ch" disabled>Restreaming (@restreams)</button>
|
<button class="btn btn-dark" role="button" style="width:18ch" disabled>Restreaming (@restreams)</button>
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
else {
|
else {
|
||||||
var db = new AppDb.DbConn();
|
var db = new AppDb.DbConn();
|
||||||
var user = db.Users.First(p => p.Username == HttpContext.Session.GetString("authenticatedUser"));
|
var user = db.Users.First(p => p.Username == HttpContext.Session.GetString("authenticatedUser"));
|
||||||
|
var stats = StreamUtils.GetStatsObject();
|
||||||
<div class="alert alert-success" role="alert">
|
<div class="alert alert-success" role="alert">
|
||||||
<h4 class="alert-heading">Welcome back!</h4>
|
<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>
|
<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>
|
</div>
|
||||||
<input type="text" class="form-control" id="input-streamurl" value="@Program.IngressDomain/ingress/@user.StreamKey" disabled>
|
<input type="text" class="form-control" id="input-streamurl" value="@Program.IngressDomain/ingress/@user.StreamKey" disabled>
|
||||||
<div class="input-group-append">
|
<div class="input-group-append">
|
||||||
@if (StreamUtils.IsLive(user.Username)) {
|
@if (StreamUtils.IsLive(user.Username, stats)) {
|
||||||
var stats = StreamUtils.GetStatsObject();
|
var uptime = TimeSpan.FromMilliseconds(StreamUtils.GetClientTime(user.Username, stats)).StripMilliseconds();
|
||||||
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 (user.AllowRestream && !string.IsNullOrWhiteSpace(user.RestreamTargets)) {
|
if (user.AllowRestream && !string.IsNullOrWhiteSpace(user.RestreamTargets)) {
|
||||||
if (StreamUtils.GetClientTime(user.Username) > 5000) {
|
if (StreamUtils.GetClientTime(user.Username, stats) > 5000) {
|
||||||
var restreams = StreamUtils.CountLiveRestreams(user.Username);
|
var restreams = StreamUtils.CountLiveRestreams(user.Username, stats);
|
||||||
if (restreams > 0) {
|
if (restreams > 0) {
|
||||||
<button class="btn btn-success" role="button" style="width:20ch" disabled>Live & restreaming</button>
|
<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="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">
|
<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">
|
<div class="input-group-append">
|
||||||
@if (!string.IsNullOrWhiteSpace(user.RestreamTargets) && StreamUtils.IsLive(user.Username) && StreamUtils.GetClientTime(user.Username) > 5000) {
|
@if (!string.IsNullOrWhiteSpace(user.RestreamTargets) && StreamUtils.IsLive(user.Username, stats) && StreamUtils.GetClientTime(user.Username, stats) > 5000) {
|
||||||
var restreams = StreamUtils.CountLiveRestreams(user.Username);
|
var restreams = StreamUtils.CountLiveRestreams(user.Username, stats);
|
||||||
if (restreams == 1) {
|
if (restreams == 1) {
|
||||||
<button class="btn btn-dark" role="button" style="width:19ch" disabled>@restreams Restream active</button>
|
<button class="btn btn-dark" role="button" style="width:19ch" disabled>@restreams Restream active</button>
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,11 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var user = db.Users.First(p => p.Username == Model.User);
|
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;
|
Stream stream = null;
|
||||||
if (live) {
|
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
|
var pronounAdditional = user.PronounSubject == "they" ? "are" : "is"; // TODO make this configurable too
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,27 +8,23 @@ namespace RTMPDash {
|
||||||
public static class StreamUtils {
|
public static class StreamUtils {
|
||||||
private static readonly XmlSerializer Serializer = new(typeof(StatsObject));
|
private static readonly XmlSerializer Serializer = new(typeof(StatsObject));
|
||||||
|
|
||||||
public static bool IsLive(string user) => GetStatsObject()
|
public static bool IsLive(string user, StatsObject stats) => stats.Server.Applications
|
||||||
.Server.Applications.First(p => p.Name == "ingress")
|
.First(p => p.Name == "ingress")
|
||||||
.MethodLive.Streams.Any(p => p.Name == user);
|
.MethodLive.Streams.Any(p => p.Name == user);
|
||||||
|
|
||||||
public static bool IsLive(string user, string target) => GetStatsObject()
|
public static bool IsLive(string user, string target, StatsObject stats) => stats.Server.Applications
|
||||||
.Server.Applications.First(p => p.Name == "ingress")
|
.First(p => p.Name == "ingress")
|
||||||
.MethodLive.Streams
|
.MethodLive.Streams
|
||||||
.Any(p => p.Name == user
|
.Any(p => p.Name == user && p.Clients.Any(c => c.Address == target.Replace("rtmp://", "")));
|
||||||
&& p.Clients.Any(c => c.Address
|
|
||||||
== target
|
|
||||||
.Replace("rtmp://", "")));
|
|
||||||
|
|
||||||
public static long GetClientTime(string user) => long.Parse(GetStatsObject()
|
public static long GetClientTime(string user, StatsObject stats) =>
|
||||||
.Server.Applications.First(p => p.Name == "ingress")
|
long.Parse(stats.Server.Applications.First(p => p.Name == "ingress")
|
||||||
.MethodLive.Streams.First(p => p.Name == user)
|
.MethodLive.Streams.First(p => p.Name == user)
|
||||||
.Time);
|
.Time);
|
||||||
|
|
||||||
public static int CountLiveRestreams(string user) {
|
public static int CountLiveRestreams(string user, StatsObject stats) {
|
||||||
var db = new AppDb.DbConn();
|
var db = new AppDb.DbConn();
|
||||||
var dbUser = db.Users.First(p => p.Username == user);
|
var dbUser = db.Users.First(p => p.Username == user);
|
||||||
var stats = GetStatsObject();
|
|
||||||
return dbUser.RestreamTargets.Split(",")
|
return dbUser.RestreamTargets.Split(",")
|
||||||
.Count(target => stats.Server.Applications.First(p => p.Name == "ingress")
|
.Count(target => stats.Server.Applications.First(p => p.Name == "ingress")
|
||||||
.MethodLive.Streams
|
.MethodLive.Streams
|
||||||
|
|
Loading…
Reference in a new issue