| 1 |
using System.Globalization; |
| 2 |
using Blog.Models; |
| 3 |
using Blog.Services; |
| 4 |
using Microsoft.AspNetCore.Components; |
| 5 |
using Microsoft.Extensions.Options; |
| 6 |
|
| 7 |
namespace Blog.Components.Pages; |
| 8 |
|
| 9 |
public partial class Rvrb : ComponentBase |
| 10 |
{ |
| 11 |
[Inject] |
| 12 |
public required RvrbService Service { get; set; } |
| 13 |
|
| 14 |
[Inject] |
| 15 |
public required IOptions<RvrbOptions> Settings { get; set; } |
| 16 |
|
| 17 |
private RvrbStatus Status { get; set; } = RvrbStatus.Unreachable("not read yet"); |
| 18 |
|
| 19 |
private RvrbSnapshot? Snapshot => Status.Snapshot; |
| 20 |
|
| 21 |
private RvrbOptions Options => Settings.Value; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private TimeSpan Age => DateTimeOffset.UtcNow - Status.FetchedAt; |
| 29 |
|
| 30 |
private string StatusLabel => Status switch |
| 31 |
{ |
| 32 |
{ Snapshot: null } => "offline", |
| 33 |
{ Snapshot.Live: null } => "up, not in the room", |
| 34 |
_ => "up, in the room" |
| 35 |
}; |
| 36 |
|
| 37 |
private string StatusClass => Status switch |
| 38 |
{ |
| 39 |
{ Snapshot: null } => "status-down", |
| 40 |
{ Snapshot.Live: null } => "status-idle", |
| 41 |
_ => "status-up" |
| 42 |
}; |
| 43 |
|
| 44 |
protected override async Task OnInitializedAsync() |
| 45 |
{ |
| 46 |
Status = await Service.GetStatusAsync().ConfigureAwait(true); |
| 47 |
await base.OnInitializedAsync().ConfigureAwait(true); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
private static string BarTitle(RvrbDay day) => $"{day.Date:yyyy-MM-dd}: {day.Plays} plays"; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private static string DayLabel(RvrbDay day) => day.Date.ToString("ddd", CultureInfo.InvariantCulture); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private int BarHeight(int plays) |
| 65 |
{ |
| 66 |
if (plays == 0) return 0; |
| 67 |
|
| 68 |
var busiest = Snapshot?.PlaysPerDay.Max(day => day.Plays) ?? 0; |
| 69 |
return busiest == 0 ? 0 : Math.Max(4, (int)Math.Round(plays * 100.0 / busiest)); |
| 70 |
} |
| 71 |
} |