aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/MainViewVM.cs
blob: 69543cb5a42a4b546c61f2e7d2760eb77b3525e2 (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
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Tango.AzureUtils.Deployment;
using Tango.AzureUtils.UI.Managers;
using Tango.Core.Commands;
using Tango.Core.Cryptography;
using Tango.Core.DI;
using Tango.SharedUI;

namespace Tango.AzureUtils.UI.ViewModels
{
    public class MainViewVM : AzureDashboardViewModel
    {
        private IAzure _azure;

        #region Properties

        private bool _isInitialized;
        public bool IsInitialized
        {
            get { return _isInitialized; }
            set { _isInitialized = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private AzureUtilsProgressEventArgs _progress;
        public AzureUtilsProgressEventArgs Progress
        {
            get { return _progress; }
            set { _progress = value; RaisePropertyChangedAuto(); }
        }

        private String _email;
        public String Email
        {
            get { return _email; }
            set { _email = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private String _password;
        public String Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }


        #endregion

        #region Commands

        public RelayCommand InitCommand { get; set; }

        #endregion

        public MainViewVM()
        {
            InitCommand = new RelayCommand(Init, () => !IsInitialized && !String.IsNullOrWhiteSpace(Email) && !String.IsNullOrWhiteSpace(Password));

            try
            {
                Email = Settings.Email;
                Password = Settings.Password;
            }
            catch { }
        }

        public override void OnApplicationReady()
        {
            StatusManager.StatusUpdated += StatusManager_StatusUpdated;
            StatusManager.UpdateStatus(AzureUtilsStage.Ready, "Waiting for authentication...", false);
        }

        private void StatusManager_StatusUpdated(object sender, AzureUtilsProgressEventArgs e)
        {
            Progress = e;
        }

        public async void Init()
        {
            try
            {
                IsFree = false;

                StatusManager.UpdateStatus(AzureUtilsStage.Initializing, "Authenticating...", true);
                _azure = await AzureUtilsAuthenticationFactory.AuthenticateOrGetAsync(ConfigurationManager.AppSettings.Get("GatewayUrl"), Email, Password);

                Settings.Email = Email;
                Settings.Password = Password;
                Settings.Save();


                List<IWebAppBase> allApps = new List<IWebAppBase>();

                StatusManager.UpdateStatus(AzureUtilsStage.Initializing, "Retrieving machine service deployment slots...", true);

                var apps = await _azure.WebApps.ListAsync();

                foreach (var app in apps)
                {
                    if (app.Name.Contains("Machine"))
                    {
                        allApps.Add(app);
                    }

                    var slots = await app.DeploymentSlots.ListAsync();

                    foreach (var slot in slots)
                    {
                        if (slot.Name.Contains("Machine"))
                        {
                            allApps.Add(slot);
                        }
                    }
                }

                IsInitialized = true;

                StatusManager.UpdateStatus(AzureUtilsStage.Ready, "Authenticated successfully.", false);

                TangoIOC.Default.GetAllInstancesByBase<AzureDashboardViewModel>().Where(x => x != this).ToList().ForEach(x => x.OnAuthenticated(_azure, allApps.ToList()));
            }
            catch (Exception ex)
            {
                StatusManager.UpdateStatus(ex);
            }
            finally
            {
                IsFree = true;
            }
        }
    }
}
ss="w"> { _countOfSet = value; RaisePropertyChangedAuto(); } } private string _message; /// <summary> /// Gets the message of selected log item to display in right panel. /// </summary> public string Message { get { return _message; } set { _message = value; RaisePropertyChangedAuto(); } } private bool _isEmbeddedLog; public bool IsEmbeddedLog { get { return _isEmbeddedLog; } set { _isEmbeddedLog = value; RaisePropertyChangedAuto(); } } private bool _isSet; /// <summary> /// Gets or sets a value indicating whether set of files. /// </summary> public bool IsSet { get { return _isSet; } set { _isSet = value; RaisePropertyChangedAuto(); } } private bool _loading; public bool Loading { get { return _loading; } set { _loading = value; RaisePropertyChangedAuto(); } } public CultureInfo Culture { get; set; } #endregion public RelayCommand OpeFileLogCommand { get; set; } #region Constructors public MainViewVM() { Culture = new CultureInfo("he-IL"); SelectedLogCategories = new SelectedObjectCollection<LogCategory>(new ObservableCollection<LogCategory>() { LogCategory.Info, LogCategory.Warning, LogCategory.Error, LogCategory.Critical, LogCategory.Debug, }, new ObservableCollection<LogCategory>() { LogCategory.Info, LogCategory.Warning, LogCategory.Error, LogCategory.Critical, LogCategory.Debug, }); _logViewerManager = new LogViewerManager(); IsSet = false; IsEmbeddedLog = false; Loading = false; Clear(); OpeFileLogCommand = new RelayCommand(OpenLogFile); SelectedLogCategories.SynchedSource.CollectionChanged += (_, __) => { ApplyLogsFilter(); }; } #endregion #region Loading /// <summary> /// Clears the all filters. Set filter properties to init state. /// </summary> private void Clear() { FileName = ""; StartSelectedTime = null; EndSelectedTime = null; Filter = ""; SelectedLog = null; SelectedLogCategories.SynchedSource = SelectedLogCategories.Source; CountOfSet = 0; IsSet = false; if (Logs != null) { Logs.Clear(); RaisePropertyChanged("Logs"); } } /// <summary> /// Opens the log file from menu. /// </summary> private void OpenLogFile() { var dialog = new CommonOpenFileDialog() { Multiselect = false, EnsureFileExists = true, }; dialog.Filters.Add(new CommonFileDialogFilter("Log files", "*.log")); CommonFileDialogResult result = dialog.ShowDialog(); if (result == CommonFileDialogResult.Ok) { LoadLogFile(dialog.FileName); } } /// <summary> /// Loads the log file from menu or command line. /// </summary> public async void LoadLogFile(String fileName) { try { Clear(); Loading = true; Mouse.OverrideCursor = Cursors.Wait; _logViewerManager.InitLogFile(fileName); List<LogItemBase> logs = new List<LogItemBase>(); await Task.Factory.StartNew(() => { logs.AddRange(_logViewerManager.Parse()); }); CountOfSet = _logViewerManager.CountOfSet; IsSet = CountOfSet > 0 ? true : false; IsEmbeddedLog = _logViewerManager.IsEmbeddedLog; FileName = _logViewerManager.FileName; Logs = new ObservableCollection<LogItemBase>(logs); LogsViewSource = CollectionViewSource.GetDefaultView(Logs); StartSelectedTime = Logs.Min(x => x.TimeStamp).TimeOfDay; EndSelectedTime = Logs.Max(x => x.TimeStamp).TimeOfDay; ApplyLogsFilter(); } catch (Exception ex) { Mouse.OverrideCursor = null; Loading = false; MessageBox.Show(ex.FlattenMessage()); } finally { Mouse.OverrideCursor = null; Loading = false; } } #endregion #region Filtering /// <summary> /// Applies the all filters( time, categories,filter massage) to view. /// </summary> private void ApplyLogsFilter() { if (LogsViewSource != null) { LogsViewSource.Filter = (x) => { LogItemBase log = x as LogItemBase; return (SelectedLogCategories.SynchedSource.Contains(log.Category) && (String.IsNullOrWhiteSpace(Filter) || log.Message.ToLower().Contains(Filter.ToLower())) && (StartSelectedTime == null || StartSelectedTime == TimeSpan.Zero || log.TimeStamp.TimeOfDay >= StartSelectedTime) && (EndSelectedTime == null || EndSelectedTime == TimeSpan.Zero || log.TimeStamp.TimeOfDay <= EndSelectedTime)); }; } } #endregion } }