Skip to content

Commit 2dfd932

Browse files
Adding check for chat timestamp
Currently having an issue where chat is disconnecting and not reconnecting. This will at least tell us when we have a problem so I don't have to check manually.
1 parent 733187b commit 2dfd932

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

src/SiteMonitor/ViewModels/MainWindowViewModel.cs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Net;
34
using System.Net.Http;
45
using System.Net.NetworkInformation;
@@ -25,6 +26,7 @@ public class MainWindowViewModel : ViewModelBase {
2526
private bool _serverUp;
2627
private bool _websiteUp;
2728
private WindowState _windowState;
29+
private string? _chatTimestamp;
2830

2931
/// <summary>
3032
/// Initializes a new instance of the <see cref="MainWindowViewModel" /> class.
@@ -51,6 +53,14 @@ public string? ServerAddress {
5153
}
5254
}
5355

56+
/// <summary>
57+
/// The timestamp of the last chat message that was received.
58+
/// </summary>
59+
public string? ChatTimestamp {
60+
get => _chatTimestamp;
61+
set => this.RaiseAndSetIfChanged(ref _chatTimestamp, value);
62+
}
63+
5464
/// <summary>
5565
/// True if the server is online.
5666
/// </summary>
@@ -110,7 +120,20 @@ private async Task PingSite() {
110120
WebsiteUp = await SendHeadRequest("https://nullinside.com");
111121
ApiUp = await SendHeadRequest("https://nullinside.com/api/v1/featureToggle");
112122
NullUp = await SendHeadRequest("https://nullinside.com/null/v1/database/migration");
113-
if ((!WebsiteUp || !ApiUp || !NullUp) && IsMinimized) {
123+
ChatTimestamp = await SendGetRequest("https://nullinside.com/twitch-bot/v1/bot/chat/timestamp");
124+
var chatNotUpdating = false;
125+
if (null != ChatTimestamp) {
126+
var parsed = ChatTimestamp.Trim('"');
127+
if (DateTime.TryParse(parsed, out DateTime time)) {
128+
var timestamp = time.ToLocalTime().ToString(CultureInfo.InvariantCulture);
129+
var diff = DateTime.Now - time.ToLocalTime();
130+
timestamp = $"{timestamp} ({diff.Hours}h {diff.Minutes}m {diff.Seconds}s ago)";
131+
ChatTimestamp = timestamp;
132+
chatNotUpdating = diff > TimeSpan.FromMinutes(5);
133+
}
134+
}
135+
136+
if ((!WebsiteUp || !ApiUp || !NullUp || chatNotUpdating) && IsMinimized) {
114137
WindowState = WindowState.Normal;
115138
}
116139

@@ -121,8 +144,8 @@ private async Task PingSite() {
121144
/// <summary>
122145
/// Sends a head request to ensure a URL is online.
123146
/// </summary>
124-
/// <param name="address"></param>
125-
/// <returns></returns>
147+
/// <param name="address">The address to send the request to.</param>
148+
/// <returns>True if successful, false otherwise.</returns>
126149
private async Task<bool> SendHeadRequest(string address) {
127150
try {
128151
var handler = new HttpClientHandler();
@@ -137,6 +160,26 @@ private async Task<bool> SendHeadRequest(string address) {
137160
return false;
138161
}
139162
}
163+
164+
/// <summary>
165+
/// Sends a head request to ensure a URL is online.
166+
/// </summary>
167+
/// <param name="address">The address to send the request to.</param>
168+
/// <returns>The content of the response.</returns>
169+
private async Task<string?> SendGetRequest(string address) {
170+
try {
171+
var handler = new HttpClientHandler();
172+
handler.AutomaticDecompression = ~DecompressionMethods.None;
173+
using var httpClient = new HttpClient(handler);
174+
using var request = new HttpRequestMessage(HttpMethod.Get, address);
175+
request.Headers.TryAddWithoutValidation("user-agent", Constants.FAKE_USER_AGENT);
176+
HttpResponseMessage response = await httpClient.SendAsync(request);
177+
return await response.Content.ReadAsStringAsync();
178+
}
179+
catch {
180+
return null;
181+
}
182+
}
140183

141184
private async Task PingServer() {
142185
while (true) {

src/SiteMonitor/Views/MainWindow.axaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@
2626

2727
<Grid Margin="20">
2828
<Grid.RowDefinitions>
29+
<RowDefinition Height="Auto" />
2930
<RowDefinition Height="Auto" />
3031
<RowDefinition />
3132
</Grid.RowDefinitions>
3233
<StackPanel Orientation="Horizontal" Grid.Row="0">
3334
<Label VerticalAlignment="Center">Server Address:</Label>
3435
<TextBox Text="{Binding ServerAddress, Mode=TwoWay}" Width="300" />
3536
</StackPanel>
36-
<UniformGrid Grid.Row="1" Margin="0 25 0 0">
37+
<StackPanel Orientation="Horizontal" Grid.Row="1" Margin="0, 5, 0, 0">
38+
<Label VerticalAlignment="Center">Last Chat Received:</Label>
39+
<TextBox IsReadOnly="True" Text="{Binding ChatTimestamp}" Width="300" />
40+
</StackPanel>
41+
<UniformGrid Grid.Row="2" Margin="0 25 0 0">
3742
<StackPanel>
3843
<Label FontSize="26" HorizontalAlignment="Center">Server Online</Label>
3944
<Image Source="/Assets/good.png" Width="48" Stretch="UniformToFill" IsVisible="{Binding ServerUp}" />

src/nullinside-api

0 commit comments

Comments
 (0)