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 }; public async Task> GetJobsAsync(int page = 1, int pageSize = 50, List? statuses = null, JobType? jobType = null, Guid? layerId = null) { var start = (page - 1) * pageSize; var query = $"Jobs?start={start}&limit={pageSize}"; if (statuses != null && statuses.Count > 0) { foreach (var status in statuses) { query += $"&statuses={(int)status}"; } } 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>(json, _jsonOptions); return result ?? new PagedResult(); } public async Task GetJobByIdAsync(Guid id) { var response = await _httpClient.GetAsync($"Jobs/{id}"); if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadFromJsonAsync(); } public async Task RetryJobAsync(Guid id) { var response = await _httpClient.PostAsync($"Jobs/{id}/retry", null); return response.IsSuccessStatusCode; } public async Task CancelJobAsync(Guid id) { var response = await _httpClient.DeleteAsync($"Jobs/{id}"); return response.IsSuccessStatusCode; } public async Task GetStatsAsync() { var response = await _httpClient.GetAsync("Jobs/stats"); if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadFromJsonAsync(); } public async Task CreateJobForLayerAsync(Guid layerId) { var response = await _httpClient.PostAsync($"Jobs/create-for-layer/{layerId}", null); if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadFromJsonAsync(); } } 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; } }