using Microsoft.Extensions.Logging; using ReportingServices.Shared.HelperClasses; using ReportingServices.Shared.Models.ProductionReport; using ReportingServices.Shared.ViewModels.ProductionReport; using System.Text.Json; using System.Web; namespace ReportingServices.Shared.Repositories { public class FabTimeReportingRepository : IFabTimeReportingRepository { private readonly string _toolFilter = "~R76%2C%20~R78%2C%20~R25%2C%20~R67%2C%20~R69%2C%20~R71%2C%20~R47%2C%20~R51%2C%20~R28"; private readonly ILogger _logger; public FabTimeReportingRepository(ILogger logger) { _logger = logger; } public async Task> GetMovesTrendData(string startDate = "", string endDate = "") { string url = APIHelperFunctions.GenerateURLWithParameters(startDate: startDate, endDate: endDate, chart: "MOVESLOTLIST", areasLike: "CLEANROOM", operationsLike: "1UNLOAD"); _logger.LogInformation("FabTime URL: {url}", url); try { return await GetJsonData>(url); } catch (Exception ex) { _logger.LogCritical(ex, "Error in API call."); } return null; } public async Task> GetToolStateTrendData(string toolType) { string url = APIHelperFunctions.GenerateURLWithParameters(chart: "TOOLSTATE", periodLen: "24", capacityTypesLike: toolType, toolsLike: _toolFilter); _logger.LogInformation("FabTime URL: {url}", url); try { return await GetJsonData>(url); } catch (Exception ex) { _logger.LogCritical(ex, "Error in API call."); } return null; } public async Task> GetToolStateData(string toolType) { string capacityFilter = toolType == "ASM" ? toolType + "%2CASM%2B" : toolType; string startDate = HttpUtility.UrlEncode(APIHelperFunctions.GetDateWithOffsetAsAPIString(DateTime.Now.ToString(), -12.5f)); string url = APIHelperFunctions.GenerateURLWithParameters(chart: "ToolStateGantt", periodLen: "24", capacityTypesLike: capacityFilter, toolsLike: _toolFilter, startDate: startDate); _logger.LogInformation("FabTime URL: {url}", url); try { return await GetJsonData>(url); } catch (Exception ex) { _logger.LogCritical(ex, "Error in API call."); } return null; } public async Task GetJsonData(string url) { T deserializedJson = default(T); using (var client = new HttpClient()) { using (HttpResponseMessage response = await client.GetAsync(url)) { string apiResponse = await response.Content.ReadAsStringAsync(); _logger.LogInformation("API Response: {response}" + apiResponse); try { deserializedJson = JsonSerializer.Deserialize(apiResponse); } catch (Exception ex) { _logger.LogCritical(ex, "Failed to deserialize Json object."); } } } return deserializedJson; } } }