1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-10-17 12:42:51 +02:00
Minecraft-Phantom-Panel/Web/Phantom.Web/Pages/Events.razor

92 lines
3.1 KiB
Plaintext

@page "/events"
@attribute [Authorize(Permission.ViewEventsPolicy)]
@using System.Collections.Immutable
@using Phantom.Common.Data.Web.EventLog
@using Phantom.Common.Data.Web.Users
@using Phantom.Web.Services.Agents
@using Phantom.Web.Services.Events
@using Phantom.Web.Services.Instances
@inherits Phantom.Web.Components.PhantomComponent
@inject AgentManager AgentManager
@inject EventLogManager EventLogManager
@inject InstanceManager InstanceManager
<h1>Event Log</h1>
@if (loadError is {} error) {
<p role="alert">@error</p>
return;
}
<Table TItem="EventLogItem" Items="logItems">
<HeaderRow>
<Column Class="text-end" MinWidth="200px">Time</Column>
<Column>Agent</Column>
<Column>Event Type</Column>
<Column>Subject</Column>
<Column Width="100%">Data</Column>
</HeaderRow>
<ItemRow Context="logItem">
<Cell class="text-end">
<TimeWithOffset Time="logItem.UtcTime.ToLocalTime()" />
</Cell>
<Cell>
@if (logItem.AgentGuid is {} agentGuid) {
<p class="fw-semibold">@(GetAgentName(agentGuid))</p>
<small class="font-monospace text-uppercase">@agentGuid.ToString()</small>
}
else {
<text>-</text>
}
</Cell>
<Cell>@logItem.EventType.ToNiceString()</Cell>
<Cell>
<p class="fw-semibold">@(GetSubjectName(logItem.SubjectType, logItem.SubjectId) ?? "-")</p>
<small class="font-monospace text-uppercase">@(logItem.SubjectId)</small>
</Cell>
<Cell>
<code>@logItem.JsonData</code>
</Cell>
</ItemRow>
<NoItemsRow>
No event log entries found.
</NoItemsRow>
</Table>
@code {
private ImmutableArray<EventLogItem>? logItems;
private string? loadError;
private ImmutableDictionary<Guid, string> agentNamesByGuid = ImmutableDictionary<Guid, string>.Empty;
private ImmutableDictionary<Guid, string> instanceNamesByGuid = ImmutableDictionary<Guid, string>.Empty;
protected override async Task OnInitializedAsync() {
var result = await EventLogManager.GetMostRecentItems(await GetAuthenticatedUser(), 50, CancellationToken);
if (result) {
logItems = result.Value;
agentNamesByGuid = AgentManager.GetAll().ToImmutableDictionary(static kvp => kvp.AgentGuid, static kvp => kvp.Configuration.AgentName);
instanceNamesByGuid = InstanceManager.GetAll().Values.ToImmutableDictionary(static instance => instance.InstanceGuid, static instance => instance.Configuration.InstanceName);
}
else {
logItems = ImmutableArray<EventLogItem>.Empty;
loadError = result.Error switch {
UserActionFailure.NotAuthorized => "You do not have permission to view the event log.",
_ => "Unknown error."
};
}
}
private string GetAgentName(Guid agentGuid) {
return agentNamesByGuid.TryGetValue(agentGuid, out var name) ? name : "?";
}
private string? GetSubjectName(EventLogSubjectType type, string id) {
return type switch {
EventLogSubjectType.Instance => instanceNamesByGuid.TryGetValue(Guid.Parse(id), out var name) ? name : null,
_ => null
};
}
}