Blog/Components/Pages/Rvrb.razor.cs 2.1 K · 63 lines · raw · history

1 using Blog.Models;
2 using Blog.Services;
3 using Microsoft.AspNetCore.Components;
4 using Microsoft.Extensions.Options;
5
6 namespace Blog.Components.Pages;
7
8 public partial class Rvrb : ComponentBase
9 {
10 [Inject]
11 public required RvrbService Service { get; set; }
12
13 [Inject]
14 public required IOptions<RvrbOptions> Settings { get; set; }
15
16 private RvrbStatus Status { get; set; } = RvrbStatus.Unreachable("not read yet");
17
18 private RvrbSnapshot? Snapshot => Status.Snapshot;
19
20 private RvrbOptions Options => Settings.Value;
21
22 /// <summary>
23 /// How stale the snapshot is by the time it reaches the browser. Snapshots are cached and
24 /// shared, so the track that was 40 seconds in when it was taken is further along by now — this
25 /// is what lets the page pick the counter up where the bot left it.
26 /// </summary>
27 private TimeSpan Age => DateTimeOffset.UtcNow - Status.FetchedAt;
28
29 private string StatusLabel => Status switch
30 {
31 { Snapshot: null } => "offline",
32 { Snapshot.Live: null } => "up, not in the room",
33 _ => "up, in the room"
34 };
35
36 private string StatusClass => Status switch
37 {
38 { Snapshot: null } => "status-down",
39 { Snapshot.Live: null } => "status-idle",
40 _ => "status-up"
41 };
42
43 protected override async Task OnInitializedAsync()
44 {
45 Status = await Service.GetStatusAsync().ConfigureAwait(true);
46 await base.OnInitializedAsync().ConfigureAwait(true);
47 }
48
49 /// <summary>The hover label on a bar: which day it is, and what it counts.</summary>
50 private static string BarTitle(RvrbDay day) => $"{day.Date:yyyy-MM-dd}: {day.Plays} plays";
51
52 /// <summary>
53 /// A day's bar height as a percentage of the busiest day in the window, floored at something
54 /// visible so a day with one play doesn't render as nothing at all.
55 /// </summary>
56 private int BarHeight(int plays)
57 {
58 if (plays == 0) return 0;
59
60 var busiest = Snapshot?.PlaysPerDay.Max(day => day.Plays) ?? 0;
61 return busiest == 0 ? 0 : Math.Max(4, (int)Math.Round(plays * 100.0 / busiest));
62 }
63 }