aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Insights/InsightsHelper.cs
blob: 29f761af6193142373af9bd567a1afb6057727e4 (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
using Google.Protobuf.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.Diagnostics;
using Tango.PMR.Insights;

namespace Tango.Insights
{
    public static class InsightsHelper
    {
        private static List<PropertyInfo> _diagnosticsProperties;
        private static List<PropertyInfo> _insightsProperties;

        static InsightsHelper()
        {
            _diagnosticsProperties = new List<PropertyInfo>();
            _insightsProperties = new List<PropertyInfo>();

            foreach (var prop in typeof(DiagnosticsMonitors).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                _diagnosticsProperties.Add(prop);
            }

            foreach (var prop in typeof(InsightsMonitors).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
            {
                _insightsProperties.Add(prop);
            }
        }

        public static InsightsMonitors MapMonitors(DiagnosticsMonitors diagnosticsMonitors)
        {
            InsightsMonitors insightsMonitors = new InsightsMonitors();

            for (int i = 0; i < _diagnosticsProperties.Count; i++)
            {
                if (_diagnosticsProperties[i].PropertyType == typeof(RepeatedField<double>))
                {
                    var arr = _diagnosticsProperties[i].GetValue(diagnosticsMonitors) as RepeatedField<double>;

                    if (arr.Count > 0)
                    {
                        _insightsProperties[i].SetValue(insightsMonitors, arr.Average());
                    }
                }
            }

            return insightsMonitors;
        }

        public static InsightsMonitors AverageMonitors(List<InsightsMonitors> insightsMonitorsCollection)
        {
            InsightsMonitors monitors = new InsightsMonitors();

            foreach (var prop in _insightsProperties.Where(x => !typeof(IEnumerable).IsAssignableFrom(x.PropertyType)).ToList())
            {
                prop.SetValue(monitors, insightsMonitorsCollection.Average(x => (double)prop.GetValue(x)));
            }

            return monitors;
        }

        public static InsightsMonitors AverageMonitors(List<DiagnosticsMonitors> diagnosticsMonitorsCollection)
        {
            List<InsightsMonitors> insightsMonitorsCollection = diagnosticsMonitorsCollection.ToList().Select(x => MapMonitors(x)).ToList();
            return AverageMonitors(insightsMonitorsCollection);
        }

        public static InsightsMonitors CreateEmptyGap()
        {
            InsightsMonitors monitors = new InsightsMonitors();

            foreach (var prop in _insightsProperties.Where(x => !typeof(IEnumerable).IsAssignableFrom(x.PropertyType)).ToList())
            {
                prop.SetValue(monitors, double.NaN);
            }

            return monitors;
        }
    }
}