Add progress to campaign entity
This commit is contained in:
@ -20,8 +20,10 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
|
||||
private RegistryService? registryService;
|
||||
|
||||
protected RegistryService RegistryService => registryService ??= scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||
protected override int StepCount => Enum.GetValues(typeof(Step)).Length;
|
||||
|
||||
protected RegistryService RegistryService => registryService ??= scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||
|
||||
public override async Task ProcessAsync()
|
||||
{
|
||||
switch ((Step)campaign.Step)
|
||||
@ -53,8 +55,7 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id);
|
||||
|
||||
campaign.Step = (int)Step.ExecutionWait;
|
||||
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
||||
await ProgressStepAsync(context, (int)Step.ExecutionWait);
|
||||
|
||||
var registryCount = await RegistryService.GetRegistryCountAsync(context, true);
|
||||
operations = await HeadquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.NsiCommon_ExportNsiItem_15_7_0_1);
|
||||
@ -85,8 +86,7 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
if (operationExecutionState.HasCampaignOperation(campaign.Id)) return;
|
||||
|
||||
campaign.Step = (int)Step.ResultWait;
|
||||
await HeadquartersService.UpdateCampaignStepAsync(campaign);
|
||||
await ProgressStepAsync((int)Step.ResultWait);
|
||||
|
||||
var operations = await HeadquartersService.GetOperationsAsync(campaign.Id);
|
||||
foreach (var operation in operations)
|
||||
@ -109,8 +109,7 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
using var transaction = await context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
campaign.Step = (int)Step.End;
|
||||
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
||||
await ProgressStepAsync(context, (int)Step.End);
|
||||
|
||||
await HeadquartersService.SetCampaignEndedAsync(context, campaign.Id);
|
||||
|
||||
|
||||
@ -20,6 +20,8 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
|
||||
private RegistryService? registryService;
|
||||
|
||||
protected override int StepCount => Enum.GetValues(typeof(Step)).Length;
|
||||
|
||||
protected RegistryService RegistryService => registryService ??= scope.ServiceProvider.GetRequiredService<RegistryService>();
|
||||
|
||||
public override async Task ProcessAsync()
|
||||
@ -53,8 +55,7 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
await HeadquartersService.SetCampaignStartedAsync(context, campaign.Id);
|
||||
|
||||
campaign.Step = (int)Step.ExecutionWait;
|
||||
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
||||
await ProgressStepAsync(context, (int)Step.ExecutionWait);
|
||||
|
||||
var registryCount = await RegistryService.GetRegistryCountAsync(context, false);
|
||||
operations = await HeadquartersService.InitiateOperationsAsync(context, registryCount, campaign.Id, Operation.OperationType.Nsi_ExportNsiItem_15_7_0_1);
|
||||
@ -85,8 +86,7 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
{
|
||||
if (operationExecutionState.HasCampaignOperation(campaign.Id)) return;
|
||||
|
||||
campaign.Step = (int)Step.ResultWait;
|
||||
await HeadquartersService.UpdateCampaignStepAsync(campaign);
|
||||
await ProgressStepAsync((int)Step.ResultWait);
|
||||
|
||||
var operations = await HeadquartersService.GetOperationsAsync(campaign.Id);
|
||||
foreach (var operation in operations)
|
||||
@ -109,8 +109,7 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
using var transaction = await context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
campaign.Step = (int)Step.End;
|
||||
await HeadquartersService.UpdateCampaignStepAsync(context, campaign);
|
||||
await ProgressStepAsync(context, (int)Step.End);
|
||||
|
||||
await HeadquartersService.SetCampaignEndedAsync(context, campaign.Id);
|
||||
|
||||
|
||||
@ -19,6 +19,8 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
|
||||
protected HeadquartersService HeadquartersService => headquartersService ??= scope.ServiceProvider.GetRequiredService<HeadquartersService>();
|
||||
|
||||
protected abstract int StepCount { get; }
|
||||
|
||||
public Campaign Campaign => campaign;
|
||||
|
||||
public IManager.ManagerState State { get; protected set; } = IManager.ManagerState.Created;
|
||||
@ -31,5 +33,18 @@ namespace Hcs.WebApp.BackgroundServices.CampaignManagers
|
||||
|
||||
State = IManager.ManagerState.Ended;
|
||||
}
|
||||
|
||||
protected async Task ProgressStepAsync(int step)
|
||||
{
|
||||
using var context = HeadquartersService.GetNewContext();
|
||||
await ProgressStepAsync(context, step);
|
||||
}
|
||||
|
||||
protected async Task ProgressStepAsync(HcsDbContext context, int step)
|
||||
{
|
||||
campaign.Step = step;
|
||||
campaign.Progress = (step + 1) / StepCount;
|
||||
await HeadquartersService.UpdateCampaignStepAndProgressAsync(context, campaign);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,6 +41,11 @@
|
||||
<Columns>
|
||||
<RadzenDataGridColumn TItem="Campaign" Property="Id" Title="ID" SortOrder="SortOrder.Descending" Resizable="false" Width="100px" MaxWidth="100px" />
|
||||
<RadzenDataGridColumn TItem="Campaign" Property="Type" Title="Тип кампании" />
|
||||
<RadzenDataGridColumn Title="Состояние" Filterable="false" Sortable="false" Resizable="false" Width="70px" MaxWidth="70px">
|
||||
<Template Context="campaign">
|
||||
<RadzenProgressBar ProgressBarStyle="@(campaign.EndedAt.HasValue ? (string.IsNullOrEmpty(campaign.FailureReason) ? ProgressBarStyle.Success : ProgressBarStyle.Danger) : ProgressBarStyle.Primary)" Value="@campaign.Progress" ShowValue="false" />
|
||||
</Template>
|
||||
</RadzenDataGridColumn>
|
||||
<RadzenDataGridColumn TItem="Campaign" Property="CreatedAt" Title="Дата создания" Resizable="false" Width="150px" MaxWidth="150px" />
|
||||
<RadzenDataGridColumn TItem="Campaign" Property="StartedAt" Title="Дата начала" Resizable="false" Width="150px" MaxWidth="150px" />
|
||||
<RadzenDataGridColumn TItem="Campaign" Property="StartedAt" Title="Дата окончания" Resizable="false" Width="150px" MaxWidth="150px" />
|
||||
|
||||
@ -28,6 +28,8 @@ namespace Hcs.WebApp.Data.Hcs
|
||||
|
||||
public int Step { get; set; } = 0;
|
||||
|
||||
public int Progress { get; set; } = 0;
|
||||
|
||||
public string? FailureReason { get; set; }
|
||||
|
||||
public virtual ICollection<Operation>? Operations { get; set; } = null;
|
||||
|
||||
@ -169,18 +169,19 @@ namespace Hcs.WebApp.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateCampaignStepAsync(Campaign campaign)
|
||||
public async Task UpdateCampaignStepAndProgressAsync(Campaign campaign)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
await UpdateCampaignStepAsync(context, campaign);
|
||||
await UpdateCampaignStepAndProgressAsync(context, campaign);
|
||||
}
|
||||
|
||||
public async Task UpdateCampaignStepAsync(HcsDbContext context, Campaign campaign)
|
||||
public async Task UpdateCampaignStepAndProgressAsync(HcsDbContext context, Campaign campaign)
|
||||
{
|
||||
var targetCampaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaign.Id);
|
||||
if (targetCampaign != null)
|
||||
{
|
||||
targetCampaign.Step = campaign.Step;
|
||||
targetCampaign.Progress = campaign.Progress;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user