using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.DI; using Tango.FSE.Common; using Tango.FSE.Common.Connection; using Tango.FSE.Common.Statistics; using Tango.PPC.Shared.Statistics; using Tango.Transport; namespace Tango.FSE.UI.Statistics { public class DefaultStatisticsProvider : FSEExtendedObject, IStatisticsProvider { public event EventHandler JobRunReceived; private IMachineProvider MachineProvider { get; set; } public DefaultStatisticsProvider(IMachineProvider machineProvider) { MachineProvider = machineProvider; MachineProvider.MachineConnected += MachineProvider_MachineConnected; } private void MachineProvider_MachineConnected(object sender, MachineConnectedEventArgs e) { if (MachineProvider.IsPPCAvailable) { InitStreaming(); } } private async void InitStreaming() { await Task.Delay(5000); LogManager.Log("Initializing statistics streaming..."); MachineProvider.MachineOperator.SendGenericContinuousRequest(new StartJobRunsStreamingRequest()) .Subscribe(response => { try { if (Settings.StatisticsStreamingConfig.Enable) { if (response.JobRunComposition != null) { JobRunReceived?.Invoke(this, new JobRunReceivedEventArgs() { JobRunComposition = response.JobRunComposition }); } } } catch (Exception ex) { LogManager.Log(ex, "Error on statistics streaming JobRun received event."); } },(ex) => { LogManager.Log(ex, "Error initializing statistics streaming."); },() => { }); } public async Task GetRequiredFiltersData() { try { LogManager.Log("Retrieving remote machine statistics required filters data..."); var response = await MachineProvider.MachineOperator.SendGenericRequest(new GetStatisticsRequiredFiltersRequest()); return response.FiltersData; } catch (Exception ex) { throw LogManager.Log(ex, "Error retrieving remote machine statistics required filters data."); } } public async Task GetStatistics(Filters filters) { try { LogManager.Log("Retrieving remote machine statistics data..."); DateTime startUtc = new DateTime(filters.StartDateUTC.Year, filters.StartDateUTC.Month, filters.StartDateUTC.Day, 0, 0, 0).ToUniversalTime(); TimeSpan offsetTime = (filters.EndDateUTC.Date == DateTime.Now.Date) ? DateTime.Now.TimeOfDay : new TimeSpan(23, 59, 59); DateTime endUtc = filters.EndDateUTC.ToUniversalTime() + offsetTime; filters.StartDateUTC = startUtc; filters.EndDateUTC = endUtc; var response = await MachineProvider.MachineOperator.SendGenericRequest( new GetStatisticsRequest() { Filters = filters }, new TransportRequestConfig() { Timeout = TimeSpan.FromSeconds(60) }); StatisticsModel model = new StatisticsModel(); model.StatisticsResult = response.Result; return model; } catch (Exception ex) { throw LogManager.Log(ex, "Error retrieving remote machine statistics data."); } } public async Task GetJobRunExtendedInfo(int jobRunID) { try { LogManager.Log($"Retrieving extended job run ({jobRunID}) information..."); var response = await MachineProvider.MachineOperator.SendGenericRequest(new GetJobRunExtendedInfoRequest() { JobRunID = jobRunID }); return response.ExtendedInfo; } catch (Exception ex) { throw LogManager.Log(ex, $"Error getting job run extended info for job run '{jobRunID}'."); } } } }