aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.PPCConsole/ViewModels/MonitoringViewVM.cs
blob: 6c0c3f05f859a297cefaf46826615bac8ec2909e (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using LiveCharts;
using RealTimeGraphX.DataPoints;
using RealTimeGraphX.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Tango.Core.Commands;
using Tango.Core.Helpers;
using Tango.FSE.Common;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.Graphs;
using Tango.FSE.Common.Performance;
using Tango.PPC.Shared.Information;
using Tango.SystemInfo;
using static Tango.SharedUI.Controls.NavigationControl;

namespace Tango.FSE.PPCConsole.ViewModels
{
    /// <summary>
    /// Represents the PPC monitoring view model.
    /// </summary>
    /// <seealso cref="Tango.FSE.Common.FSEViewModel" />
    /// <seealso cref="Tango.SharedUI.Controls.NavigationControl.INavigationViewModel" />
    public class MonitoringViewVM : FSEViewModel, INavigationViewModel
    {
        #region Properties

        /// <summary>
        /// Gets or sets the CPU real-time graph controller.
        /// </summary>
        public WpfGraphController<DateTimeDataPoint, DoubleDataPoint> CPUController { get; set; }

        /// <summary>
        /// Gets or sets the RAM real-time controller.
        /// </summary>
        public WpfGraphController<DateTimeDataPoint, DoubleDataPoint> RAMController { get; set; }

        /// <summary>
        /// Gets or sets the disk space point label (Live Charts).
        /// </summary>
        public Func<ChartPoint, string> DiskSpacePointLabel { get; set; }

        private InformationPackage _systemInfo;
        /// <summary>
        /// Gets or sets the system information package.
        /// </summary>
        public InformationPackage SystemInfo
        {
            get { return _systemInfo; }
            set { _systemInfo = value; RaisePropertyChangedAuto(); }
        }

        private SystemObjectsCollection _selectedSystemObjectCollection;
        /// <summary>
        /// Gets or sets the selected system object collection.
        /// </summary>
        public SystemObjectsCollection SelectedSystemObjectCollection
        {
            get { return _selectedSystemObjectCollection; }
            set { _selectedSystemObjectCollection = value; RaisePropertyChangedAuto(); }
        }

        private bool _fetchingSystemInfo;
        /// <summary>
        /// Gets or sets a value indicating whether the view model is currently busy with fetching the remote system information.
        /// </summary>
        public bool FetchingSystemInfo
        {
            get { return _fetchingSystemInfo; }
            set { _fetchingSystemInfo = value; RaisePropertyChangedAuto(); }
        }

        private double _usedDiskSpace;
        /// <summary>
        /// Gets or sets the PPC used disk space.
        /// </summary>
        public double UsedDiskSpace
        {
            get { return _usedDiskSpace; }
            set
            {
                if (_usedDiskSpace != value)
                {
                    _usedDiskSpace = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        private double _availableDiskSpace;
        /// <summary>
        /// Gets or sets the PPC available disk space.
        /// </summary>
        public double AvailableDiskSpace
        {
            get { return _availableDiskSpace; }
            set
            {
                if (_availableDiskSpace != value)
                {
                    _availableDiskSpace = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Performs disk space optimization.
        /// </summary>
        public RelayCommand PerformDiskSpaceOptimizationCommand { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MonitoringViewVM"/> class.
        /// </summary>
        public MonitoringViewVM()
        {
            CPUController = CreateController(CreateSeries("Total", GraphHelper.GraphColor.White), CreateSeries("Application", GraphHelper.GraphColor.Red));
            RAMController = CreateController(CreateSeries("Total", GraphHelper.GraphColor.White), CreateSeries("Application", GraphHelper.GraphColor.Yellow));
            UsedDiskSpace = 1000 * 40;
            AvailableDiskSpace = 1000 * 60;
            DiskSpacePointLabel = (point) =>
            {
                return $"{(point.Y / 1000d).ToString("0.0")} GB";
            };

            PerformDiskSpaceOptimizationCommand = new RelayCommand(PerformDiskSpaceOptimization);
        }

        #endregion

        #region Override Methods

        public override void OnApplicationStarted()
        {
            base.OnApplicationStarted();
            PerformanceProvider.PerformancePackageAvailable += PerformanceProvider_PerformancePackageAvailable;
            MachineProvider.MachineDisconnected += MachineProvider_MachineDisconnected;
            MachineProvider.MachineConnected += MachineProvider_MachineConnected;
        }

        public override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            if (MachineProvider.IsConnected && MachineProvider.ConnectionType.IsRemote())
            {
                LoadSystemInformation();
            }
        }

        #endregion

        #region Event Handlers

        private void MachineProvider_MachineConnected(object sender, Common.Connection.MachineConnectedEventArgs e)
        {
            CPUController.Clear();
            RAMController.Clear();
            AvailableDiskSpace = 0;
            UsedDiskSpace = 0;
            LoadSystemInformation();
        }

        private void MachineProvider_MachineDisconnected(object sender, Common.Connection.MachineDisconnectedEventArgs e)
        {
            SystemInfo = null;
        }

        private void PerformanceProvider_PerformancePackageAvailable(object sender, PerformancePackageEventArgs e)
        {
            List<DateTimeDataPoint> xx = new List<DateTimeDataPoint>()
            {
                DateTime.Now,
                DateTime.Now
            };

            //CPU
            CPUController.PushData(xx, new List<DoubleDataPoint>()
            {
                e.Package.CPU,
                e.Package.ApplicationCPU
            });

            //App RAM
            RAMController.Range.MaximumY = e.Package.MaxRAM;
            RAMController.PushData(xx, new List<DoubleDataPoint>()
            {
                e.Package.RAM,
                e.Package.ApplicationRAM
            });

            //Disk Space
            var usedDiskSpace = e.Package.DiskCapacity - e.Package.AvailableDiskSpace;
            var availableDiskSpace = e.Package.AvailableDiskSpace;

            if (Math.Abs(UsedDiskSpace - usedDiskSpace) > 10)
            {
                UsedDiskSpace = usedDiskSpace;
            }

            if (Math.Abs(AvailableDiskSpace - availableDiskSpace) > 10)
            {
                AvailableDiskSpace = availableDiskSpace;
            }
        }

        #endregion

        #region Private Methods

        private WpfGraphController<DateTimeDataPoint, DoubleDataPoint> CreateController(params WpfGraphDataSeries[] seriesCollection)
        {
            var controller = new WpfGraphController<DateTimeDataPoint, DoubleDataPoint>();

            foreach (var series in seriesCollection)
            {
                controller.DataSeriesCollection.Add(series);
            }

            controller.Range.AutoY = false;
            controller.Range.MaximumY = 100;
            controller.Range.MinimumY = 0;
            controller.Range.MaximumX = new DateTime(0).AddMinutes(1);

            controller.RefreshRate = TimeSpan.FromMilliseconds(100);

            return controller;
        }

        private WpfGraphDataSeries CreateSeries(String name, GraphHelper.GraphColor fill)
        {
            WpfGraphDataSeries series = new WpfGraphDataSeries();
            series.Name = name;
            series.Fill = GraphHelper.GetGraphBrush(fill);
            series.StrokeThickness = 1;
            series.Stroke = GraphHelper.GetGraphStrokeColor();
            return series;
        }

        private async void LoadSystemInformation()
        {
            if (!MachineProvider.ConnectionType.IsRemote()) return;

            if (SystemInfo == null && !FetchingSystemInfo)
            {
                try
                {
                    FetchingSystemInfo = true;
                    SystemInfo = await SystemInfoProvider.GetSystemInformationPackage();
                    SelectedSystemObjectCollection = SystemInfo.System.FirstOrDefault();
                }
                catch (Exception ex)
                {
                    FetchingSystemInfo = false;
                    LogManager.Log(ex, "Error retrieving system information from remote machine.");
                    NotificationProvider.PushErrorReportingSnackbar(ex, "PPC Module Error", "Error retrieving the remote machine PPC system information.");
                }
                finally
                {
                    FetchingSystemInfo = false;
                }
            }
        }

        private async void PerformDiskSpaceOptimization()
        {
            if (await NotificationProvider.ShowQuestion("The following stage will try to optimize the disk space on the remote machine panel PC. Do you wish to continue?", "RUN OPTIMIZATION", "NO"))
            {
                try
                {
                    using (NotificationProvider.PushTaskItem("Performing disk space optimization, please wait..."))
                    {
                        var response = await FileSystemProvider.PerformDiskSpaceOptimization();
                        await NotificationProvider.ShowSuccess($"Disk space optimization completed successfully.\n{FileHelper.GetFriendlyFileSize(response.DeletedBytes)} cleared!");
                    }
                }
                catch (Exception ex)
                {
                    await NotificationProvider.ShowError($"Error occurred while trying disk space optimization.\n{ex.FlattenMessage()}");
                }
            }
        }

        #endregion
    }
}