1
0
mirror of https://github.com/akpaevj/onecmonitor.git synced 2026-06-17 22:34:48 +02:00
Files

200 lines
7.5 KiB
Plaintext

@page "/techlog/seances/{id:guid}"
@page "/techlog/seances/create"
@using FluentValidation
@using Microsoft.EntityFrameworkCore
@using OneSwiss.Common.Extensions
@using OneSwiss.Server.Attributes
@using OneSwiss.Server.Components.Shared
@using OneSwiss.Server.Helpers
@using OneSwiss.Server.Models
@using OneSwiss.Server.Services
@inject AgentsConnectionsManager AgentsConnectionsManager
@attribute [ExtAuthorize(Roles.WriteTechLogSeances)]
<EditDatabaseForm @ref="_editor" T="TechLogSeance" Id="Id"
NameSelector="item => item.Description"
Route="techlog/seances"
Title="Новый сеанс сбора технологического журнала"
OnInitialize="OnInitialize"
BeforeValidation="BeforeValidation"
AfterSave="AfterSave"
Validation="_validator.ValidateValue"
ModelSelector="ModelSelector">
<MudStack>
<MudTextField Class="mb-3" Label="Описание"
@bind-Value="@_editor.Model!.Description"
For="@(() => _editor.Model!.Description)"/>
</MudStack>
<MudStack Row Class="mb-3" AlignItems="AlignItems.Center">
<MudText>Режим запуска:</MudText>
<MudRadioGroup T="TechLogSeanceStartMode"
@bind-Value="_editor.Model!.StartMode"
@bind-Value:after="OnChangeStartMode">
@foreach (var item in Enum.GetValues<TechLogSeanceStartMode>())
{
<MudRadio Value="@item">@item.GetDisplay()</MudRadio>
}
</MudRadioGroup>
</MudStack>
<MudStack Row Class="mb-3" StretchItems="StretchItems.All" Breakpoint="Breakpoint.MdAndDown">
<MudDatePicker Label="Дата начала"
For="@(() => _startDate)"
@bind-Date="@_startDate"
@bind-Date:after="SetStartDateTime"
Disabled="_startDateTimeDisabled"/>
<MudTimePicker Label="Время начала"
For="@(() => _startTime)"
@bind-Time="@_startTime"
@bind-Time:after="SetStartDateTime"
Disabled="_startDateTimeDisabled"/>
<MudNumericField Label="Длительность (мин.)"
For="@(() => _editor.Model!.Duration)"
@bind-Value="_editor.Model!.Duration"
Disabled="_durationDisabled"/>
</MudStack>
<MudStack Row StretchItems="StretchItems.All" Breakpoint="Breakpoint.SmAndDown">
<ItemsSelector T="Agent"
@bind-SelectedValues="_editor.Model!.Agents"
NameSelector="item => item.InstanceName"
Items="@_editor.Context!.Agents.ToList()"
Title="Агенты"/>
<ItemsSelector T="LogTemplate"
@bind-SelectedValues="_editor.Model!.Templates"
NameSelector="item => item.Name"
Items="@_editor.Context!.LogTemplates.ToList()"
Title="Шаблоны сбора"/>
</MudStack>
</EditDatabaseForm>
@code
{
private readonly Validator _validator = new();
private EditDatabaseForm<TechLogSeance> _editor = null!;
private bool _startDateTimeDisabled;
private bool _durationDisabled;
private DateTime? _startDate;
private TimeSpan? _startTime;
[Parameter] public Guid Id { get; set; }
private void OnInitialize((TechLogSeance Model, AppDbContext Context) valueTuple)
{
if (valueTuple.Model.Id == Guid.Empty)
valueTuple.Model.StartDateTime = DateTime.Now;
_startDate = valueTuple.Model.StartDateTime;
_startTime = valueTuple.Model.StartDateTime.TimeOfDay;
SetAvailabilityInternal(valueTuple.Model);
}
protected override void OnAfterRender(bool firstRender)
{
if (!firstRender)
return;
_editor.AddAdditionalValidableProperty(nameof(TechLogSeance.Agents));
_editor.AddAdditionalValidableProperty(nameof(TechLogSeance.Templates));
}
private void BeforeValidation(TechLogSeance arg)
{
arg.StartDateTime = arg.StartMode switch
{
TechLogSeanceStartMode.Monitor => DateTime.MinValue,
TechLogSeanceStartMode.Immediately => DateTime.Now,
_ => _editor.Model!.StartDateTime
};
ModelHelper.UpdateModelItems(_editor.Model!.Agents, arg.Agents);
ModelHelper.UpdateModelItems(_editor.Model!.Templates, arg.Templates);
}
private void SetAvailabilityInternal(TechLogSeance obj)
{
_startDateTimeDisabled = obj.StartMode != TechLogSeanceStartMode.Scheduled;
_durationDisabled = obj.StartMode == TechLogSeanceStartMode.Monitor;
}
private void ChangeStartTime()
{
switch (_editor.Model!.StartMode)
{
case TechLogSeanceStartMode.Scheduled:
_startDate = DateTime.Now;
_startTime = DateTime.Now.AddMinutes(30).TimeOfDay;
break;
case TechLogSeanceStartMode.Immediately:
case TechLogSeanceStartMode.Monitor:
default:
_startDate = DateTime.MinValue;
_startTime = TimeSpan.Zero;
break;
}
}
private void OnChangeStartMode()
{
ChangeStartTime();
SetAvailabilityInternal(_editor.Model!);
}
private async Task AfterSave(TechLogSeance obj)
{
await RaiseUpdateSettings(CancellationToken.None);
}
private async Task RaiseUpdateSettings(CancellationToken cancellationToken)
{
var connections = await AgentsConnectionsManager.GetActiveAgentsConnections(cancellationToken);
foreach (var connection in connections)
await connection.SendSettingsRequest(cancellationToken);
}
private void SetStartDateTime()
{
_editor.Model!.StartDateTime = (_startDate ?? DateTime.MinValue).Date.Add(_startTime ?? TimeSpan.Zero);
}
private static IQueryable<TechLogSeance> ModelSelector(IQueryable<TechLogSeance> arg)
{
return arg.Include(c => c.Agents).Include(c => c.Templates);
}
private class Validator : ModelValidator<TechLogSeance>
{
public Validator()
{
RuleFor(c => c.Description)
.NotEmpty()
.WithMessage("Не заполнено описание");
RuleFor(c => c.Agents)
.NotEmpty()
.WithMessage("Не указаны подключаемые агенты");
RuleFor(c => c.Templates)
.NotEmpty()
.WithMessage("Не указаны подключаемые шаблоны");
When(c => c.StartMode != TechLogSeanceStartMode.Monitor, () =>
{
RuleFor(c => c.Duration)
.GreaterThan(1)
.WithMessage("Длительность не может быть меньше 1 минуты");
});
When(c => c.StartMode == TechLogSeanceStartMode.Scheduled, () =>
{
RuleFor(c => c.StartDateTime)
.GreaterThan(DateTime.Now)
.WithName("_startDate")
.WithMessage("Дата и время начала не могут быть меньше текущей даты");
});
}
}
}