aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Azure/Tango.AzureUtils.UI/ViewModels/EnvironmentLogStreamViewVM.cs
blob: 04fca66e103f8a524c3f4b4da12e2b4784cc4e59 (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
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.AzureUtils.Environment;
using Tango.AzureUtils.Logging;
using Tango.Core.Commands;
using Tango.SharedUI.Components;

namespace Tango.AzureUtils.UI.ViewModels
{
    public class EnvironmentLogStreamViewVM : AzureDashboardViewModel
    {
        private LogStreamManager _logStreamManager;

        private List<IWebAppBase> _deploymentSlots;
        public List<IWebAppBase> DeploymentSlots
        {
            get { return _deploymentSlots; }
            set { _deploymentSlots = value; RaisePropertyChangedAuto(); }
        }

        private IWebAppBase _selectedDeploymentSlot;
        public IWebAppBase SelectedDeploymentSlot
        {
            get { return _selectedDeploymentSlot; }
            set { _selectedDeploymentSlot = value; RaisePropertyChangedAuto(); }
        }

        private TextController _log;
        public TextController Log
        {
            get { return _log; }
            set { _log = value; RaisePropertyChangedAuto(); }
        }

        private bool _isStarted;
        public bool IsStarted
        {
            get { return _isStarted; }
            set { _isStarted = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        public RelayCommand StartCommand { get; set; }

        public RelayCommand StopCommand { get; set; }

        public RelayCommand ClearCommand { get; set; }

        public EnvironmentLogStreamViewVM()
        {
            Log = new TextController();

            StartCommand = new RelayCommand(Start,() => !IsStarted);
            StopCommand = new RelayCommand(Stop, () => IsStarted);
            ClearCommand = new RelayCommand(Clear);
        }

        public override void OnAuthenticated(IAzure azure, List<IWebAppBase> apps)
        {
            DeploymentSlots = apps.ToList();
            SelectedDeploymentSlot = DeploymentSlots.SingleOrDefault(x => x.Name.EndsWith("DEV"));

            _logStreamManager = new LogStreamManager(azure);
            _logStreamManager.ConfirmationRequired += ConfirmationHandler;
            _logStreamManager.Progress += ProgressHandler;
            _logStreamManager.LogAvailable += _logStreamManager_LogAvailable;
        }

        private void _logStreamManager_LogAvailable(object sender, string msg)
        {
            Log.WriteLine(msg);
        }

        private void Clear()
        {
            Log.Clear();
        }

        private void Stop()
        {
            IsStarted = false;
            _logStreamManager.StopLogStreaming();
        }

        private async void Start()
        {
            if (SelectedDeploymentSlot != null && _logStreamManager != null)
            {
                try
                {
                    IsStarted = true;
                    Log.Clear();
                    await _logStreamManager.StartLogStreamingAsync(SelectedDeploymentSlot);
                }
                catch
                {
                    IsStarted = false;
                }
            }
        }
    }
}