2021-11-17 08:36:23 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2022-05-09 04:21:10 -04:00
using System ;
2021-11-17 08:36:23 -05:00
using System.Collections.Generic ;
using System.Linq ;
using System.Reflection ;
2023-04-21 05:50:36 -04:00
using System.Text.Json.Serialization ;
2022-10-12 06:36:30 -04:00
using Jupiter.Implementation ;
2021-11-17 08:36:23 -05:00
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.Extensions.Options ;
2022-10-12 06:36:30 -04:00
namespace Jupiter.Controllers
2021-11-17 08:36:23 -05:00
{
2023-07-27 11:20:47 -04:00
[ApiController]
[Route("api/v1/status")]
[Authorize]
public class StatusController : Controller
{
private readonly VersionFile _versionFile ;
private readonly IOptionsMonitor < JupiterSettings > _jupiterSettings ;
private readonly IOptionsMonitor < ClusterSettings > _clusterSettings ;
private readonly IPeerStatusService _statusService ;
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
public StatusController ( VersionFile versionFile , IOptionsMonitor < JupiterSettings > jupiterSettings , IOptionsMonitor < ClusterSettings > clusterSettings , IPeerStatusService statusService )
{
_versionFile = versionFile ;
_jupiterSettings = jupiterSettings ;
_clusterSettings = clusterSettings ;
_statusService = statusService ;
}
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
/// <summary>
/// Fetch information about Jupiter
/// </summary>
/// <remarks>
/// General information about the service, which version it is running and more.
/// </remarks>
/// <returns></returns>
[HttpGet("")]
[ProducesResponseType(type: typeof(StatusResponse), 200)]
public IActionResult Status ( )
{
IEnumerable < AssemblyMetadataAttribute > attrs = typeof ( StatusController ) . Assembly . GetCustomAttributes < AssemblyMetadataAttribute > ( ) ;
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
string srcControlIdentifier = "Unknown" ;
AssemblyMetadataAttribute ? gitHashAttribute = attrs . FirstOrDefault ( attr = > attr . Key = = "GitHash" ) ;
if ( gitHashAttribute ? . Value ! = null & & ! string . IsNullOrEmpty ( gitHashAttribute . Value ) )
{
srcControlIdentifier = gitHashAttribute . Value ;
}
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
AssemblyMetadataAttribute ? p4ChangeAttribute = attrs . FirstOrDefault ( attr = > attr . Key = = "PerforceChangelist" ) ;
if ( p4ChangeAttribute ? . Value ! = null & & ! string . IsNullOrEmpty ( p4ChangeAttribute . Value ) )
{
srcControlIdentifier = p4ChangeAttribute . Value ;
}
2022-05-09 04:21:10 -04:00
2023-07-27 11:20:47 -04:00
return Ok ( new StatusResponse ( _versionFile . VersionString ? ? "Unknown" , srcControlIdentifier , GetCapabilities ( ) , _jupiterSettings . CurrentValue . CurrentSite ) ) ;
}
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
private static string [ ] GetCapabilities ( )
{
return new string [ ]
{
"transactionlog" ,
"ddc"
} ;
}
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
/// <summary>
/// Fetch information about other deployments
/// </summary>
/// <remarks>
/// General information about the Jupiter service, which version it is running and more.
/// </remarks>
/// <returns></returns>
[HttpGet("peers")]
[ProducesResponseType(type: typeof(PeersResponse), 200)]
public IActionResult Peers ( [ FromQuery ] bool includeInternalEndpoints = false )
{
return Ok ( new PeersResponse ( _jupiterSettings , _clusterSettings , includeInternalEndpoints , _statusService ) ) ;
}
}
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
public class PeersResponse
{
[JsonConstructor]
public PeersResponse ( string currentSite , List < KnownPeer > peers )
{
CurrentSite = currentSite ;
Peers = peers ;
}
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
public PeersResponse ( IOptionsMonitor < JupiterSettings > jupiterSettings , IOptionsMonitor < ClusterSettings > clusterSettings , bool includeInternalEndpoints , IPeerStatusService peerStatusService )
{
CurrentSite = jupiterSettings . CurrentValue . CurrentSite ;
Peers = clusterSettings . CurrentValue . Peers . Select ( settings = > new KnownPeer ( settings , includeInternalEndpoints , peerStatusService ) ) . ToList ( ) ;
}
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
public string CurrentSite { get ; set ; } = null ! ;
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Used by serialization")]
public List < KnownPeer > Peers { get ; set ; } = new List < KnownPeer > ( ) ;
}
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
public class KnownPeer
{
[JsonConstructor]
public KnownPeer ( string site , string fullName , List < Uri > endpoints , int latency )
{
Site = site ;
FullName = fullName ;
Endpoints = endpoints ;
Latency = latency ;
}
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
public KnownPeer ( PeerSettings peerSettings , bool includeInternalEndpoints , IPeerStatusService statusService )
{
Site = peerSettings . Name ;
FullName = peerSettings . FullName ;
IEnumerable < PeerEndpoints > endpoints = peerSettings . Endpoints ;
if ( ! includeInternalEndpoints )
{
endpoints = endpoints . Where ( s = > ! s . IsInternal ) ;
}
2022-05-09 04:21:10 -04:00
2023-07-27 11:20:47 -04:00
Endpoints = endpoints . Select ( e = > e . Url ) . ToList ( ) ;
2022-03-01 08:57:36 -05:00
2023-07-27 11:20:47 -04:00
PeerStatus ? peerStatus = statusService . GetPeerStatus ( peerSettings . Name ) ;
if ( peerStatus ! = null )
{
Latency = peerStatus . Latency ;
}
}
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
public string Site { get ; set ; }
public string FullName { get ; set ; }
2022-02-09 12:26:25 -05:00
2023-07-27 11:20:47 -04:00
public int Latency { get ; set ; }
2022-03-01 06:55:00 -05:00
2023-07-27 11:20:47 -04:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Used by serialization")]
public List < Uri > Endpoints { get ; set ; }
}
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
public class StatusResponse
{
public StatusResponse ( string version , string gitHash , string [ ] capabilities , string siteIdentifier )
{
Version = version ;
GitHash = gitHash ;
Capabilities = capabilities ;
SiteIdentifier = siteIdentifier ;
}
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
public string Version { get ; set ; }
public string GitHash { get ; set ; }
public string [ ] Capabilities { get ; set ; }
public string SiteIdentifier { get ; set ; }
}
2021-11-17 08:36:23 -05:00
}