Related records fix

This commit is contained in:
dingo
2026-04-08 08:31:51 -04:00
parent 3bf4ec90d9
commit e8abd54301
3 changed files with 221 additions and 12 deletions
@@ -131,6 +131,17 @@ public sealed class RelationshipRegistry : IRelationshipRegistry
SourceFilterValue = "Device",
});
Register(new RelationshipRule
{
SourceContentType = ContentTypeName.Vms,
SourcePropertyName = "Id",
TargetContentType = ContentTypeName.Ips,
TargetPropertyName = "HostId",
DisplayLabel = "IP Addresses",
TargetFilterPropertyName = "HostType",
TargetFilterValue = "Vm",
});
Register(new RelationshipRule
{
SourceContentType = ContentTypeName.Ips,
@@ -153,6 +164,17 @@ public sealed class RelationshipRegistry : IRelationshipRegistry
SourceFilterValue = "Device",
});
Register(new RelationshipRule
{
SourceContentType = ContentTypeName.Ips,
SourcePropertyName = "HostId",
TargetContentType = ContentTypeName.Vms,
TargetPropertyName = "Id",
DisplayLabel = "VM",
SourceFilterPropertyName = "HostType",
SourceFilterValue = "Vm",
});
Register(new RelationshipRule
{
SourceContentType = ContentTypeName.Ips,
@@ -214,4 +236,4 @@ public sealed class RelationshipRegistry : IRelationshipRegistry
{
return _allRules.AsReadOnly();
}
}
}
@@ -46,6 +46,7 @@ else
ContentType="@_definition.ContentType"
IsVisible="@_showRelationships"
AllData="@_dataCache"
OnEnsureContentTypeLoaded="EnsureCachedAsync"
CanEdit="@_canEdit"
OnEditRequested="OpenEditFromRelatedAsync"
OnClose="CloseRelationships" />
@@ -155,21 +155,27 @@
[Parameter] public bool IsVisible { get; set; }
[Parameter] public EventCallback OnClose { get; set; }
[Parameter] public Dictionary<ContentTypeName, IReadOnlyList<IInventoryEntity>> AllData { get; set; } = new();
[Parameter] public EventCallback<ContentTypeName> OnEnsureContentTypeLoaded { get; set; }
[Parameter] public bool CanEdit { get; set; }
[Parameter] public EventCallback<RelatedEditRequest> OnEditRequested { get; set; }
private readonly List<InspectContext> _stack = new();
private readonly Dictionary<string, string> _groupFilters = new();
private readonly Dictionary<string, GroupSortState> _groupSorts = new();
private string _cacheFingerprint = string.Empty;
protected override void OnParametersSet()
protected override async Task OnParametersSetAsync()
{
if (!IsVisible || Entity is null)
{
_stack.Clear();
_cacheFingerprint = string.Empty;
return;
}
await EnsureDependenciesLoadedAsync(Entity, ContentType);
var latestFingerprint = ComputeCacheFingerprint();
if (_stack.Count == 0 ||
_stack[0].ContentType != ContentType ||
_stack[0].Entity.Id != Entity.Id)
@@ -177,6 +183,12 @@
_stack.Clear();
_stack.Add(BuildContext(Entity, ContentType));
}
else if (!string.Equals(_cacheFingerprint, latestFingerprint, StringComparison.Ordinal))
{
await RebuildStackAsync();
}
_cacheFingerprint = ComputeCacheFingerprint();
}
private InspectContext BuildContext(IInventoryEntity entity, ContentTypeName contentType)
@@ -201,7 +213,7 @@
foreach (var field in definition.Fields.OrderBy(f => f.SortOrder))
{
var value = GetDisplayValue(entity, field);
var value = GetDetailValue(entity, contentType: definition.ContentType, field);
rows.Add(new DetailRow
{
Label = field.DisplayName,
@@ -242,7 +254,7 @@
if (!string.IsNullOrEmpty(rule.SourceFilterPropertyName))
{
var filterValue = GetPropertyValue(entity, rule.SourceFilterPropertyName);
if (!string.Equals(filterValue, rule.SourceFilterValue, StringComparison.OrdinalIgnoreCase))
if (!ValuesMatch(rule.SourceFilterPropertyName, filterValue, rule.SourceFilterValue))
continue;
}
@@ -340,6 +352,7 @@
{
ContentTypeName.Systems => "System",
ContentTypeName.Devices => "Device",
ContentTypeName.Vms => "Vm",
_ => null,
};
@@ -349,7 +362,7 @@
var relatedIps = ips
.OfType<IpRecord>()
.Where(ip =>
string.Equals(GetPropertyValue(ip, "HostType"), hostType, StringComparison.OrdinalIgnoreCase) &&
HostTypesMatch(GetPropertyValue(ip, "HostType"), hostType) &&
string.Equals(GetPropertyValue(ip, "HostId"), entity.Id.ToString(), StringComparison.OrdinalIgnoreCase))
.ToList();
@@ -381,12 +394,12 @@
if (!string.IsNullOrEmpty(rule.TargetFilterPropertyName))
{
var targetFilterValue = GetPropertyValue(target, rule.TargetFilterPropertyName);
if (!string.Equals(targetFilterValue, rule.TargetFilterValue, StringComparison.OrdinalIgnoreCase))
if (!ValuesMatch(rule.TargetFilterPropertyName, targetFilterValue, rule.TargetFilterValue))
return false;
}
var targetValue = GetPropertyValue(target, rule.TargetPropertyName);
return string.Equals(sourceValue, targetValue, StringComparison.OrdinalIgnoreCase);
return ValuesMatch(rule.TargetPropertyName, sourceValue, targetValue);
}
private string GetDisplayValue(IInventoryEntity entity, FieldDefinition field)
@@ -412,6 +425,48 @@
return rawValue;
}
private string GetDetailValue(IInventoryEntity entity, ContentTypeName contentType, FieldDefinition field)
{
if (!string.Equals(field.PropertyName, "HostName", StringComparison.OrdinalIgnoreCase))
{
return GetDisplayValue(entity, field);
}
var hostType = GetPropertyValue(entity, "HostType");
var hostId = GetPropertyValue(entity, "HostId");
var hostName = EntityValueResolver.GetValue(entity, field.PropertyName, field.JsonPropertyName);
if (string.IsNullOrWhiteSpace(hostType) || string.IsNullOrWhiteSpace(hostId))
{
return hostName;
}
var hostContentType = TryMapHostTypeToContentType(hostType);
if (hostContentType is null)
{
return string.IsNullOrWhiteSpace(hostName)
? $"{hostType} #{hostId}"
: $"{hostType} #{hostId} ({hostName})";
}
if (AllData.TryGetValue(hostContentType.Value, out var relatedEntities))
{
var hostEntity = relatedEntities.FirstOrDefault(item =>
string.Equals(item.Id.ToString(), hostId, StringComparison.OrdinalIgnoreCase));
if (hostEntity is not null)
{
var hostDefinition = ContentTypeRegistry.GetDefinition(hostContentType.Value);
var displayName = GetEntityDisplayName(hostEntity, hostDefinition);
return $"{hostDefinition.DisplayName}: {displayName}";
}
}
return string.IsNullOrWhiteSpace(hostName)
? $"{hostType} #{hostId}"
: $"{hostType} #{hostId} ({hostName})";
}
private List<IInventoryEntity> SortRelatedItems(List<IInventoryEntity> items, ContentTypeDefinition definition)
{
var statusField = definition.Fields.FirstOrDefault(f => string.Equals(f.PropertyName, "Status", StringComparison.OrdinalIgnoreCase));
@@ -446,14 +501,19 @@
private string GetEntityTitle(InspectContext context)
{
var displayField = context.Definition.Fields.FirstOrDefault(f => f.PropertyName == context.Definition.DisplayPropertyName);
var title = EntityValueResolver.GetValue(context.Entity, context.Definition.DisplayPropertyName, displayField?.JsonPropertyName);
return string.IsNullOrWhiteSpace(title) ? $"ID {context.Entity.Id}" : title;
return GetEntityDisplayName(context.Entity, context.Definition);
}
private void OpenEntity(IInventoryEntity entity, ContentTypeName contentType)
private string GetEntityDisplayName(IInventoryEntity entity, ContentTypeDefinition definition)
{
_stack.Add(BuildContext(entity, contentType));
var displayField = definition.Fields.FirstOrDefault(f => f.PropertyName == definition.DisplayPropertyName);
var title = EntityValueResolver.GetValue(entity, definition.DisplayPropertyName, displayField?.JsonPropertyName);
return string.IsNullOrWhiteSpace(title) ? $"ID {entity.Id}" : title;
}
private Task OpenEntity(IInventoryEntity entity, ContentTypeName contentType)
{
return OpenEntityAsync(entity, contentType);
}
private async Task RequestEdit(ContentTypeName contentType, int entityId)
@@ -622,6 +682,132 @@
return (networkBytes[fullBytes] & mask) == (targetBytes[fullBytes] & mask);
}
private async Task OpenEntityAsync(IInventoryEntity entity, ContentTypeName contentType)
{
await EnsureDependenciesLoadedAsync(entity, contentType);
_stack.Add(BuildContext(entity, contentType));
_cacheFingerprint = ComputeCacheFingerprint();
await InvokeAsync(StateHasChanged);
}
private async Task RebuildStackAsync()
{
if (_stack.Count == 0)
{
return;
}
var contexts = _stack
.Select(context => (context.Entity, context.ContentType))
.ToList();
_stack.Clear();
foreach (var (entity, contentType) in contexts)
{
await EnsureDependenciesLoadedAsync(entity, contentType);
_stack.Add(BuildContext(entity, contentType));
}
}
private async Task EnsureDependenciesLoadedAsync(IInventoryEntity entity, ContentTypeName contentType)
{
if (!OnEnsureContentTypeLoaded.HasDelegate)
{
return;
}
var requiredTypes = RelationshipRegistry
.GetRulesForContentType(contentType)
.Select(rule => rule.TargetContentType)
.ToHashSet();
switch (contentType)
{
case ContentTypeName.Systems:
case ContentTypeName.Devices:
case ContentTypeName.Vms:
requiredTypes.Add(ContentTypeName.Ips);
requiredTypes.Add(ContentTypeName.Subnets);
break;
case ContentTypeName.Ips:
requiredTypes.Add(ContentTypeName.Subnets);
break;
case ContentTypeName.Subnets:
requiredTypes.Add(ContentTypeName.Ips);
break;
}
foreach (var targetContentType in requiredTypes)
{
if (!AllData.ContainsKey(targetContentType))
{
await OnEnsureContentTypeLoaded.InvokeAsync(targetContentType);
}
}
}
private string ComputeCacheFingerprint()
{
return string.Join("|", AllData
.OrderBy(pair => pair.Key)
.Select(pair => $"{pair.Key}:{pair.Value.Count}"));
}
private static bool ValuesMatch(string propertyName, string? left, string? right)
{
if (string.Equals(propertyName, "HostType", StringComparison.OrdinalIgnoreCase))
{
return HostTypesMatch(left, right);
}
return string.Equals(left, right, StringComparison.OrdinalIgnoreCase);
}
private static bool HostTypesMatch(string? left, string? right)
{
var leftType = NormalizeHostType(left);
var rightType = NormalizeHostType(right);
if (leftType is not null && rightType is not null)
{
return leftType == rightType;
}
return string.Equals(left, right, StringComparison.OrdinalIgnoreCase);
}
private static ContentTypeName? NormalizeHostType(string? hostType)
{
if (string.IsNullOrWhiteSpace(hostType))
{
return null;
}
return TryMapHostTypeToContentType(hostType);
}
private static ContentTypeName? TryMapHostTypeToContentType(string hostType)
{
if (string.Equals(hostType, "System", StringComparison.OrdinalIgnoreCase))
{
return ContentTypeName.Systems;
}
if (string.Equals(hostType, "Device", StringComparison.OrdinalIgnoreCase))
{
return ContentTypeName.Devices;
}
if (string.Equals(hostType, "Vm", StringComparison.OrdinalIgnoreCase) ||
string.Equals(hostType, "VM", StringComparison.OrdinalIgnoreCase))
{
return ContentTypeName.Vms;
}
return null;
}
private sealed class InspectContext
{
public required IInventoryEntity Entity { get; init; }