Files
DiunaBI/DiunaBI.UI.Shared/Services/JobService.cs

110 lines
3.0 KiB
C#
Raw Normal View History

2025-12-03 13:33:38 +01:00
using System.Net.Http.Json;
using System.Text.Json;
using DiunaBI.Application.DTOModels.Common;
using DiunaBI.Domain.Entities;
namespace DiunaBI.UI.Shared.Services;
public class JobService
{
private readonly HttpClient _httpClient;
public JobService(HttpClient httpClient)
{
_httpClient = httpClient;
}
private readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNameCaseInsensitive = true
};
2025-12-08 21:28:24 +01:00
public async Task<PagedResult<QueueJob>> GetJobsAsync(int page = 1, int pageSize = 50, List<JobStatus>? statuses = null, JobType? jobType = null, Guid? layerId = null)
2025-12-03 13:33:38 +01:00
{
var start = (page - 1) * pageSize;
var query = $"Jobs?start={start}&limit={pageSize}";
2025-12-08 21:28:24 +01:00
if (statuses != null && statuses.Count > 0)
{
foreach (var status in statuses)
{
query += $"&statuses={(int)status}";
}
}
2025-12-03 13:33:38 +01:00
if (jobType.HasValue)
query += $"&jobType={(int)jobType.Value}";
if (layerId.HasValue)
query += $"&layerId={layerId.Value}";
var response = await _httpClient.GetAsync(query);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<PagedResult<QueueJob>>(json, _jsonOptions);
return result ?? new PagedResult<QueueJob>();
}
public async Task<QueueJob?> GetJobByIdAsync(Guid id)
{
var response = await _httpClient.GetAsync($"Jobs/{id}");
if (!response.IsSuccessStatusCode)
return null;
return await response.Content.ReadFromJsonAsync<QueueJob>();
}
public async Task<bool> RetryJobAsync(Guid id)
{
var response = await _httpClient.PostAsync($"Jobs/{id}/retry", null);
return response.IsSuccessStatusCode;
}
public async Task<bool> CancelJobAsync(Guid id)
{
var response = await _httpClient.DeleteAsync($"Jobs/{id}");
return response.IsSuccessStatusCode;
}
public async Task<JobStats?> GetStatsAsync()
{
var response = await _httpClient.GetAsync("Jobs/stats");
if (!response.IsSuccessStatusCode)
return null;
return await response.Content.ReadFromJsonAsync<JobStats>();
}
public async Task<CreateJobResult?> CreateJobForLayerAsync(Guid layerId)
{
var response = await _httpClient.PostAsync($"Jobs/create-for-layer/{layerId}", null);
if (!response.IsSuccessStatusCode)
return null;
return await response.Content.ReadFromJsonAsync<CreateJobResult>();
}
}
public class JobStats
{
public int Pending { get; set; }
public int Running { get; set; }
public int Completed { get; set; }
public int Failed { get; set; }
public int Retrying { get; set; }
public int Total { get; set; }
}
public class CreateJobResult
{
public bool Success { get; set; }
public Guid JobId { get; set; }
public string? Message { get; set; }
public bool Existing { get; set; }
}