Skip to content

Commit

Permalink
feat: expose network connectivity in the REST API
Browse files Browse the repository at this point in the history
- expoe  network connectivity details in the rest api

- allow rest clients to forcefully re-check the network connectivity

- add a last changed timestamp for when the last network availability
  change were detected.

- changed the scheduled job to use the interface instead of the
  implementation.
  • Loading branch information
revam committed Nov 12, 2023
1 parent 0064fee commit f3b44df
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ public class NetworkAvailabilityChangedEventArgs : EventArgs
/// </summary>
public NetworkAvailability NetworkAvailability { get; }

public NetworkAvailabilityChangedEventArgs(NetworkAvailability networkAvailability)
/// <summary>
/// When the last network change was detected.
/// </summary>
public DateTime LastCheckedAt { get; set; }

public NetworkAvailabilityChangedEventArgs(NetworkAvailability networkAvailability, DateTime lastChange)
{
NetworkAvailability = networkAvailability;
LastCheckedAt = lastChange;
}
}
}
5 changes: 5 additions & 0 deletions Shoko.Plugin.Abstractions/Services/IConnectivityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public interface IConnectivityService
/// </summary>
public NetworkAvailability NetworkAvailability { get; }

/// <summary>
/// When the last network change was detected.
/// </summary>
public DateTime LastChangedAt { get; }

/// <summary>
/// Is the AniDB UDP API currently reachable?
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/API/SignalR/Aggregate/NetworkEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ private async void OnNetworkAvailabilityChanged(object sender, NetworkAvailabili

public override object GetInitialMessage()
{
return new NetworkAvailabilitySignalRModel(EventHandler.NetworkAvailability);
return new NetworkAvailabilitySignalRModel(EventHandler.NetworkAvailability, EventHandler.LastChangedAt);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Shoko.Plugin.Abstractions;
Expand All @@ -13,13 +14,21 @@ public class NetworkAvailabilitySignalRModel
[JsonConverter(typeof(StringEnumConverter))]
public NetworkAvailability NetworkAvailability { get; }

public NetworkAvailabilitySignalRModel(NetworkAvailability networkAvailability)
/// <summary>
/// When the last network change was detected.
/// </summary>
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime LastChangedAt { get; }

public NetworkAvailabilitySignalRModel(NetworkAvailability networkAvailability, DateTime lastCheckedAt)
{
NetworkAvailability = networkAvailability;
LastChangedAt = lastCheckedAt.ToUniversalTime();
}

public NetworkAvailabilitySignalRModel(NetworkAvailabilityChangedEventArgs eventArgs)
{
NetworkAvailability = eventArgs.NetworkAvailability;
LastChangedAt = eventArgs.LastCheckedAt.ToUniversalTime();
}
}
30 changes: 29 additions & 1 deletion Shoko.Server/API/v3/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Shoko.Plugin.Abstractions.Services;
using Shoko.Server.API.Annotations;
using Shoko.Server.API.v3.Models.Common;
using Shoko.Server.Providers.AniDB.Interfaces;
Expand All @@ -17,6 +19,8 @@ namespace Shoko.Server.API.v3.Controllers;
[InitFriendly]
public class SettingsController : BaseController
{
private readonly IConnectivityService _connectivityService;

// As far as I can tell, only GET and PATCH should be supported, as we don't support unset settings.
// Some may be patched to "", though.

Expand Down Expand Up @@ -84,7 +88,31 @@ public ActionResult TestAniDB([FromBody] Credentials credentials)
return Ok();
}

public SettingsController(ISettingsProvider settingsProvider) : base(settingsProvider)
/// <summary>
/// Gets the current network connectivity details for the server.
/// </summary>
/// <returns></returns>
[HttpGet("Connectivity")]
public ActionResult<ConnectivityDetails> GetNetworkAvailability()
{
return new ConnectivityDetails(_connectivityService);
}

/// <summary>
/// Forcefully re-checks the current network connectivity, then returns the
/// updated details for the server.
/// </summary>
/// <returns></returns>
[HttpPost("Connectivity")]
public async Task<ActionResult<object>> CheckNetworkAvailability()
{
await _connectivityService.CheckAvailability();

return GetNetworkAvailability();
}

public SettingsController(ISettingsProvider settingsProvider, IConnectivityService connectivityService) : base(settingsProvider)
{
_connectivityService = connectivityService;
}
}
47 changes: 47 additions & 0 deletions Shoko.Server/API/v3/Models/Common/ConnectivityDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Shoko.Plugin.Abstractions.Enums;
using Shoko.Plugin.Abstractions.Services;

namespace Shoko.Server.API.v3.Models.Common;

public class ConnectivityDetails
{
/// <summary>
/// Current network availibility.
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public readonly NetworkAvailability NetworkAvailability;

/// <summary>
/// When the last network change was detected.
/// </summary>
[JsonConverter(typeof(IsoDateTimeConverter))]
public readonly DateTime LastChangedAt;

/// <summary>
/// Is the AniDB UDP API currently reachable?
/// </summary>
public readonly bool IsAniDBUdpReachable;

/// <summary>
/// Are we currently banned from using the AniDB HTTP API?
/// </summary>
public readonly bool IsAniDBHttpBanned;

/// <summary>
/// Are we currently banned from using the AniDB UDP API?
/// </summary>
public readonly bool IsAniDBUdpBanned;

public ConnectivityDetails(IConnectivityService service)
{
NetworkAvailability = service.NetworkAvailability;
LastChangedAt = service.LastChangedAt.ToUniversalTime();
IsAniDBUdpReachable = service.IsAniDBUdpReachable;
IsAniDBHttpBanned = service.IsAniDBHttpBanned;
IsAniDBUdpBanned = service.IsAniDBUdpBanned;
}
}
5 changes: 2 additions & 3 deletions Shoko.Server/Scheduling/Jobs/ConnectivityMonitorJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Quartz;
using QuartzJobFactory.Attributes;
using Shoko.Plugin.Abstractions.Services;
using Shoko.Server.Services.Connectivity;

namespace Shoko.Server.Scheduling.Jobs;

Expand All @@ -16,11 +15,11 @@ namespace Shoko.Server.Scheduling.Jobs;
[DisallowConcurrentExecution]
public class ConnectivityMonitorJob : IJob
{
private readonly ConnectivityService _connectivityService;
private readonly IConnectivityService _connectivityService;

public ConnectivityMonitorJob(IConnectivityService connectivityService)
{
_connectivityService = connectivityService as ConnectivityService;
_connectivityService = connectivityService;
}

protected ConnectivityMonitorJob() { }
Expand Down
13 changes: 11 additions & 2 deletions Shoko.Server/Services/Connectivity/ConnectivityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ConnectivityService : IConnectivityService

private NetworkAvailability _networkAvailability { get; set; } = NetworkAvailability.NoInterfaces;

private DateTime _lastChangedAt { get; set; } = DateTime.Now;

/// <inheritdoc/>
public event EventHandler<NetworkAvailabilityChangedEventArgs> NetworkAvailabilityChanged;

Expand All @@ -45,12 +47,19 @@ public NetworkAvailability NetworkAvailability
private set
{
var hasChanged = _networkAvailability != value;
_networkAvailability = value;
if (hasChanged)
Task.Run(() => NetworkAvailabilityChanged?.Invoke(null, new(value))).ConfigureAwait(false);
{
_lastChangedAt = DateTime.Now;
_networkAvailability = value;
Task.Run(() => NetworkAvailabilityChanged?.Invoke(null, new(value, _lastChangedAt))).ConfigureAwait(false);
}
}
}

/// <inheritdoc/>
public DateTime LastChangedAt =>
_lastChangedAt;

/// <inheritdoc/>
public bool IsAniDBUdpReachable =>
_anidbUdpHandler.IsNetworkAvailable;
Expand Down

0 comments on commit f3b44df

Please sign in to comment.