[backend] Switch to TimeSpan extension method for easier code readability
This commit is contained in:
parent
de29780321
commit
c9b9a8e45b
5 changed files with 15 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
using Iceshrimp.Backend.Core.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Iceshrimp.Backend.Core.Database.Tables;
|
||||
|
@ -39,9 +40,9 @@ public class Job
|
|||
[Column("mutex")] public string? Mutex { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
public long Duration => (long)((FinishedAt ?? DateTime.UtcNow) - (StartedAt ?? QueuedAt)).TotalMilliseconds;
|
||||
public long Duration => ((FinishedAt ?? DateTime.UtcNow) - (StartedAt ?? QueuedAt)).GetTotalMilliseconds();
|
||||
|
||||
[NotMapped] public long QueueDuration => (long)((StartedAt ?? DateTime.UtcNow) - QueuedAt).TotalMilliseconds;
|
||||
[NotMapped] public long QueueDuration => ((StartedAt ?? DateTime.UtcNow) - QueuedAt).GetTotalMilliseconds();
|
||||
|
||||
[NotMapped] [Projectable] public DateTime LastUpdatedAt => FinishedAt ?? StartedAt ?? QueuedAt;
|
||||
}
|
6
Iceshrimp.Backend/Core/Extensions/TimeSpanExtensions.cs
Normal file
6
Iceshrimp.Backend/Core/Extensions/TimeSpanExtensions.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace Iceshrimp.Backend.Core.Extensions;
|
||||
|
||||
public static class TimeSpanExtensions
|
||||
{
|
||||
public static long GetTotalMilliseconds(this TimeSpan timeSpan) => Convert.ToInt64(timeSpan.TotalMilliseconds);
|
||||
}
|
|
@ -15,7 +15,7 @@ public static class IdHelpers
|
|||
|
||||
// We want to use a charset with a power-of-two amount of possible characters for optimal CSPRNG performance.
|
||||
var random = CryptographyHelpers.GenerateRandomString(8, CryptographyHelpers.Charset.CrockfordBase32Lower);
|
||||
var now = (long)createdAt.Value.Subtract(DateTime.UnixEpoch).TotalMilliseconds;
|
||||
var now = createdAt.Value.Subtract(DateTime.UnixEpoch).GetTotalMilliseconds();
|
||||
var time = Math.Max(now - Time2000, 0);
|
||||
var timestamp = time.ToBase36().PadLeft(8, '0');
|
||||
return timestamp + random;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using Iceshrimp.Backend.Core.Extensions;
|
||||
|
||||
namespace Iceshrimp.Backend.Core.Middleware;
|
||||
|
||||
|
@ -12,9 +12,8 @@ public class RequestDurationMiddleware : IMiddleware
|
|||
var pre = Stopwatch.GetTimestamp();
|
||||
ctx.Response.OnStarting(() =>
|
||||
{
|
||||
var duration = Math.Truncate(Stopwatch.GetElapsedTime(pre).TotalMilliseconds);
|
||||
ctx.Response.Headers.Append("X-Request-Duration",
|
||||
duration.ToString(CultureInfo.InvariantCulture) + " ms");
|
||||
var duration = Stopwatch.GetElapsedTime(pre).GetTotalMilliseconds();
|
||||
ctx.Response.Headers.Append("X-Request-Duration", $"{duration} ms");
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Diagnostics;
|
||||
using Iceshrimp.Backend.Core.Extensions;
|
||||
using Iceshrimp.Backend.Core.Helpers.LibMfm.Serialization;
|
||||
using Iceshrimp.Parsing;
|
||||
using Microsoft.FSharp.Collections;
|
||||
|
@ -379,7 +380,7 @@ public class MfmTests
|
|||
{
|
||||
var pre = Stopwatch.GetTimestamp();
|
||||
Mfm.parse(mfm);
|
||||
var ms = Stopwatch.GetElapsedTime(pre).TotalMilliseconds;
|
||||
var ms = Stopwatch.GetElapsedTime(pre).GetTotalMilliseconds();
|
||||
Console.WriteLine($@"Took {ms} ms");
|
||||
return ms;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue