From 38223580960eef468e102838475ce7fec0035343 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 20 Apr 2018 01:01:39 +0900 Subject: [PATCH] Show more info about bitcoin average quota --- .../Controllers/InvoiceController.UI.cs | 26 +------------------ BTCPayServer/Extensions.cs | 22 ++++++++++++++++ .../Services/Rates/CoinAverageRateProvider.cs | 16 ++++++++++++ BTCPayServer/Views/Server/Rates.cshtml | 10 ++++--- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 369165e01..4fb199d07 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -374,7 +374,7 @@ namespace BTCPayServer.Controllers model.Invoices.Add(new InvoiceModel() { Status = invoice.Status, - Date = Prettify(invoice.InvoiceTime), + Date = (DateTimeOffset.UtcNow - invoice.InvoiceTime).Prettify() + " ago", InvoiceId = invoice.Id, OrderId = invoice.OrderId ?? string.Empty, RedirectUrl = invoice.RedirectURL ?? string.Empty, @@ -387,30 +387,6 @@ namespace BTCPayServer.Controllers return View(model); } - private string Prettify(DateTimeOffset invoiceTime) - { - var ago = DateTime.UtcNow - invoiceTime; - - if (ago.TotalMinutes < 1) - { - return $"{(int)ago.TotalSeconds} second{Plural((int)ago.TotalSeconds)} ago"; - } - if (ago.TotalHours < 1) - { - return $"{(int)ago.TotalMinutes} minute{Plural((int)ago.TotalMinutes)} ago"; - } - if (ago.Days < 1) - { - return $"{(int)ago.TotalHours} hour{Plural((int)ago.TotalHours)} ago"; - } - return $"{(int)ago.TotalDays} day{Plural((int)ago.TotalDays)} ago"; - } - - private string Plural(int totalDays) - { - return totalDays > 1 ? "s" : string.Empty; - } - [HttpGet] [Route("invoices/create")] [Authorize(AuthenticationSchemes = "Identity.Application")] diff --git a/BTCPayServer/Extensions.cs b/BTCPayServer/Extensions.cs index 40fe847e5..1746562ea 100644 --- a/BTCPayServer/Extensions.cs +++ b/BTCPayServer/Extensions.cs @@ -34,6 +34,28 @@ namespace BTCPayServer { public static class Extensions { + public static string Prettify(this TimeSpan timeSpan) + { + if (timeSpan.TotalMinutes < 1) + { + return $"{(int)timeSpan.TotalSeconds} second{Plural((int)timeSpan.TotalSeconds)}"; + } + if (timeSpan.TotalHours < 1) + { + return $"{(int)timeSpan.TotalMinutes} minute{Plural((int)timeSpan.TotalMinutes)}"; + } + if (timeSpan.Days < 1) + { + return $"{(int)timeSpan.TotalHours} hour{Plural((int)timeSpan.TotalHours)}"; + } + return $"{(int)timeSpan.TotalDays} day{Plural((int)timeSpan.TotalDays)}"; + } + + private static string Plural(int totalDays) + { + return totalDays > 1 ? "s" : string.Empty; + } + public static string PrettyPrint(this TimeSpan expiration) { StringBuilder builder = new StringBuilder(); diff --git a/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs b/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs index ebaa5bde0..6ec20b634 100644 --- a/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs +++ b/BTCPayServer/Services/Rates/CoinAverageRateProvider.cs @@ -183,7 +183,21 @@ namespace BTCPayServer.Services.Rates var jobj = JObject.Parse(await resp.Content.ReadAsStringAsync()); var response = new GetRateLimitsResponse(); response.CounterReset = TimeSpan.FromSeconds(jobj["counter_reset"].Value()); + var totalPeriod = jobj["total_period"].Value(); + if (totalPeriod == "24h") + { + response.TotalPeriod = TimeSpan.FromHours(24); + } + else if (totalPeriod == "30d") + { + response.TotalPeriod = TimeSpan.FromDays(30); + } + else + { + response.TotalPeriod = TimeSpan.FromSeconds(jobj["total_period"].Value()); + } response.RequestsLeft = jobj["requests_left"].Value(); + response.RequestsPerPeriod = jobj["requests_per_period"].Value(); return response; } @@ -213,5 +227,7 @@ namespace BTCPayServer.Services.Rates { public TimeSpan CounterReset { get; set; } public int RequestsLeft { get; set; } + public int RequestsPerPeriod { get; set; } + public TimeSpan TotalPeriod { get; set; } } } diff --git a/BTCPayServer/Views/Server/Rates.cshtml b/BTCPayServer/Views/Server/Rates.cshtml index 89e145778..e6e3eff14 100644 --- a/BTCPayServer/Views/Server/Rates.cshtml +++ b/BTCPayServer/Views/Server/Rates.cshtml @@ -40,12 +40,16 @@
Current Bitcoin Average Quotas:
- - + + + + + + - +
Requests left@Model.RateLimits.RequestsLeftQuota period@Model.RateLimits.TotalPeriod.Prettify()
Requests quota@Model.RateLimits.RequestsLeft/@Model.RateLimits.RequestsPerPeriod
Quota reset in@Model.RateLimits.CounterReset@Model.RateLimits.CounterReset.Prettify()
}