aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/Statistics/DefaultStatisticsProvider.cs
blob: aff348548fd09bd76fbce0555502a6e52e8645a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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<JobRunReceivedEventArgs> 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<StartJobRunsStreamingRequest, StartJobRunsStreamingResponse>(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<RequiredFiltersData> GetRequiredFiltersData()
        {
            try
            {
                LogManager.Log("Retrieving remote machine statistics required filters data...");
                var response = await MachineProvider.MachineOperator.SendGenericRequest<GetStatisticsRequiredFiltersRequest, GetStatisticsRequiredFiltersResponse>(new GetStatisticsRequiredFiltersRequest());
                return response.FiltersData;
            }
            catch (Exception ex)
            {
                throw LogManager.Log(ex, "Error retrieving remote machine statistics required filters data.");
            }
        }

        public async Task<StatisticsModel> 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<GetStatisticsRequest, GetStatisticsResponse>(
                    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<JobRunExtendedInfo> GetJobRunExtendedInfo(int jobRunID)
        {
            try
            {
                LogManager.Log($"Retrieving extended job run ({jobRunID}) information...");
                var response = await MachineProvider.MachineOperator.SendGenericRequest<GetJobRunExtendedInfoRequest, GetJobRunExtendedInfoResponse>(new GetJobRunExtendedInfoRequest() { JobRunID = jobRunID });
                return response.ExtendedInfo;
            }
            catch (Exception ex)
            {
                throw LogManager.Log(ex, $"Error getting job run extended info for job run '{jobRunID}'.");
            }
        }
    }
}