-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAgentViewModel.cs
More file actions
563 lines (487 loc) · 20.9 KB
/
Copy pathAgentViewModel.cs
File metadata and controls
563 lines (487 loc) · 20.9 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
using Coder.Desktop.App.Services;
using Coder.Desktop.App.Utils;
using Coder.Desktop.CoderSdk;
using Coder.Desktop.CoderSdk.Coder;
using Coder.Desktop.Vpn.Proto;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.ApplicationModel.DataTransfer;
namespace Coder.Desktop.App.ViewModels;
public interface IAgentViewModelFactory
{
public AgentViewModel Create(IAgentExpanderHost expanderHost, Uuid id, string fullyQualifiedDomainName,
string hostnameSuffix, AgentConnectionStatus connectionStatus, Uri dashboardBaseUrl,
string? workspaceName, bool? didP2p, string? preferredDerp, TimeSpan? latency, TimeSpan? preferredDerpLatency, DateTime? lastHandshake);
public AgentViewModel CreateDummy(IAgentExpanderHost expanderHost, Uuid id,
string hostnameSuffix,
AgentConnectionStatus connectionStatus, Uri dashboardBaseUrl, string workspaceName);
}
public class AgentViewModelFactory(
ILogger<AgentViewModel> childLogger,
ICoderApiClientFactory coderApiClientFactory,
ICredentialManager credentialManager,
IAgentAppViewModelFactory agentAppViewModelFactory) : IAgentViewModelFactory
{
public AgentViewModel Create(IAgentExpanderHost expanderHost, Uuid id, string fullyQualifiedDomainName,
string hostnameSuffix,
AgentConnectionStatus connectionStatus, Uri dashboardBaseUrl,
string? workspaceName, bool? didP2p, string? preferredDerp, TimeSpan? latency, TimeSpan? preferredDerpLatency,
DateTime? lastHandshake)
{
return new AgentViewModel(childLogger, coderApiClientFactory, credentialManager, agentAppViewModelFactory,
expanderHost, id)
{
ConfiguredFqdn = fullyQualifiedDomainName,
ConfiguredHostname = string.Empty,
ConfiguredHostnameSuffix = hostnameSuffix,
ConnectionStatus = connectionStatus,
DashboardBaseUrl = dashboardBaseUrl,
WorkspaceName = workspaceName,
DidP2p = didP2p,
PreferredDerp = preferredDerp,
Latency = latency,
PreferredDerpLatency = preferredDerpLatency,
LastHandshake = lastHandshake,
};
}
public AgentViewModel CreateDummy(IAgentExpanderHost expanderHost, Uuid id,
string hostnameSuffix,
AgentConnectionStatus connectionStatus, Uri dashboardBaseUrl, string workspaceName)
{
return new AgentViewModel(childLogger, coderApiClientFactory, credentialManager, agentAppViewModelFactory,
expanderHost, id)
{
ConfiguredFqdn = string.Empty,
ConfiguredHostname = workspaceName,
ConfiguredHostnameSuffix = hostnameSuffix,
ConnectionStatus = connectionStatus,
DashboardBaseUrl = dashboardBaseUrl,
WorkspaceName = workspaceName,
};
}
}
public enum AgentConnectionStatus
{
Healthy,
Connecting,
Unhealthy,
NoRecentHandshake,
Offline
}
public static class AgentConnectionStatusExtensions
{
public static string ToDisplayString(this AgentConnectionStatus status) =>
status switch
{
AgentConnectionStatus.Healthy => "Healthy",
AgentConnectionStatus.Connecting => "Connecting",
AgentConnectionStatus.Unhealthy => "High latency",
AgentConnectionStatus.NoRecentHandshake => "No recent handshake",
AgentConnectionStatus.Offline => "Offline",
_ => status.ToString()
};
}
public partial class AgentViewModel : ObservableObject, IModelUpdateable<AgentViewModel>
{
private const string DefaultDashboardUrl = "https://coder.com";
private const int MaxAppsPerRow = 6;
// These are fake UUIDs, for UI purposes only. Display apps don't exist on
// the backend as real app resources and therefore don't have an ID.
private static readonly Uuid VscodeAppUuid = new("819828b1-5213-4c3d-855e-1b74db6ddd19");
private static readonly Uuid VscodeInsidersAppUuid = new("becf1e10-5101-4940-a853-59af86468069");
private readonly ILogger<AgentViewModel> _logger;
private readonly ICoderApiClientFactory _coderApiClientFactory;
private readonly ICredentialManager _credentialManager;
private readonly IAgentAppViewModelFactory _agentAppViewModelFactory;
// The AgentViewModel only gets created on the UI thread.
private readonly DispatcherQueue _dispatcherQueue =
DispatcherQueue.GetForCurrentThread();
private readonly IAgentExpanderHost _expanderHost;
// This isn't an ObservableProperty because the property itself never
// changes. We add an event listener for the collection changing in the
// constructor.
public readonly ObservableCollection<AgentAppViewModel> Apps = [];
public readonly Uuid Id;
// This is set only for "dummy" agents that represent unstarted workspaces. If set, then ConfiguredFqdn
// should be empty, otherwise it will override this.
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ViewableHostname))]
[NotifyPropertyChangedFor(nameof(ViewableHostnameSuffix))]
[NotifyPropertyChangedFor(nameof(FullyQualifiedDomainName))]
public required partial string ConfiguredHostname { get; set; }
// This should be set if we have an FQDN from the VPN service, and overrides ConfiguredHostname if set.
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ViewableHostname))]
[NotifyPropertyChangedFor(nameof(ViewableHostnameSuffix))]
[NotifyPropertyChangedFor(nameof(FullyQualifiedDomainName))]
public required partial string ConfiguredFqdn { get; set; }
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ViewableHostname))]
[NotifyPropertyChangedFor(nameof(ViewableHostnameSuffix))]
[NotifyPropertyChangedFor(nameof(FullyQualifiedDomainName))]
public required partial string ConfiguredHostnameSuffix { get; set; } // including leading dot
public string FullyQualifiedDomainName
{
get
{
if (!string.IsNullOrEmpty(ConfiguredFqdn)) return ConfiguredFqdn;
return ConfiguredHostname + ConfiguredHostnameSuffix;
}
}
/// <summary>
/// ViewableHostname is the hostname portion of the fully qualified domain name (FQDN) specifically for
/// views that render it differently than the suffix. If the ConfiguredHostnameSuffix doesn't actually
/// match the FQDN, then this will be the entire FQDN, and ViewableHostnameSuffix will be empty.
/// </summary>
public string ViewableHostname => !FullyQualifiedDomainName.EndsWith(ConfiguredHostnameSuffix)
? FullyQualifiedDomainName
: FullyQualifiedDomainName[0..^ConfiguredHostnameSuffix.Length];
/// <summary>
/// ViewableHostnameSuffix is the domain suffix portion (including leading dot) of the fully qualified
/// domain name (FQDN) specifically for views that render it differently from the rest of the FQDN. If
/// the ConfiguredHostnameSuffix doesn't actually match the FQDN, then this will be empty and the
/// ViewableHostname will contain the entire FQDN.
/// </summary>
public string ViewableHostnameSuffix => FullyQualifiedDomainName.EndsWith(ConfiguredHostnameSuffix)
? ConfiguredHostnameSuffix
: string.Empty;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowExpandAppsMessage))]
[NotifyPropertyChangedFor(nameof(ExpandAppsMessage))]
[NotifyPropertyChangedFor(nameof(ConnectionTooltip))]
public required partial AgentConnectionStatus ConnectionStatus { get; set; }
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(DashboardUrl))]
public required partial Uri DashboardBaseUrl { get; set; }
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(DashboardUrl))]
public required partial string? WorkspaceName { get; set; }
[ObservableProperty] public partial bool IsExpanded { get; set; } = false;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowExpandAppsMessage))]
[NotifyPropertyChangedFor(nameof(ExpandAppsMessage))]
public partial bool FetchingApps { get; set; } = false;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowExpandAppsMessage))]
[NotifyPropertyChangedFor(nameof(ExpandAppsMessage))]
public partial bool AppFetchErrored { get; set; } = false;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ConnectionTooltip))]
public partial bool? DidP2p { get; set; } = false;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ConnectionTooltip))]
public partial string? PreferredDerp { get; set; } = null;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ConnectionTooltip))]
public partial TimeSpan? Latency { get; set; } = null;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ConnectionTooltip))]
public partial TimeSpan? PreferredDerpLatency { get; set; } = null;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ConnectionTooltip))]
public partial DateTime? LastHandshake { get; set; } = null;
public string ConnectionTooltip
{
get
{
var description = new StringBuilder();
var highLatencyWarning = ConnectionStatus == AgentConnectionStatus.Unhealthy ? $"({AgentConnectionStatus.Unhealthy.ToDisplayString()})" : "";
if (DidP2p != null && DidP2p.Value && Latency != null)
{
description.Append($"""
You're connected peer-to-peer. {highLatencyWarning}
You ↔ {Latency.Value.Milliseconds} ms ↔ {WorkspaceName}
"""
);
}
else if (Latency != null)
{
description.Append($"""
You're connected through a DERP relay. {highLatencyWarning}
We'll switch over to peer-to-peer when available.
Total latency: {Latency.Value.Milliseconds} ms
"""
);
if (PreferredDerpLatency != null)
{
description.Append($"\nYou ↔ {PreferredDerp}: {PreferredDerpLatency.Value.Milliseconds} ms");
var derpToWorkspaceEstimatedLatency = Latency - PreferredDerpLatency;
// Guard against negative values if the two readings were taken at different times
if (derpToWorkspaceEstimatedLatency > TimeSpan.Zero)
{
description.Append($"\n{PreferredDerp} ms ↔ {WorkspaceName}: {derpToWorkspaceEstimatedLatency.Value.Milliseconds} ms");
}
}
}
else
{
description.Append(ConnectionStatus.ToDisplayString());
}
if (LastHandshake != null)
description.Append($"\n\nLast handshake: {LastHandshake?.ToString()}");
return description.ToString().TrimEnd('\n', ' '); ;
}
}
// We only show 6 apps max, which fills the entire width of the tray
// window.
public IEnumerable<AgentAppViewModel> VisibleApps => Apps.Count > MaxAppsPerRow ? Apps.Take(MaxAppsPerRow) : Apps;
public bool ShowExpandAppsMessage => ExpandAppsMessage != null;
public string? ExpandAppsMessage
{
get
{
if (ConnectionStatus == AgentConnectionStatus.Offline)
return "Your workspace is offline.";
if (FetchingApps && Apps.Count == 0)
// Don't show this message if we have any apps already. When
// they finish loading, we'll just update the screen with any
// changes.
return "Fetching workspace apps...";
if (AppFetchErrored && Apps.Count == 0)
// There's very limited screen real estate here so we don't
// show the actual error message.
return "Could not fetch workspace apps.";
if (Apps.Count == 0)
return "No apps to show.";
return null;
}
}
public string DashboardUrl
{
get
{
if (string.IsNullOrWhiteSpace(WorkspaceName)) return DashboardBaseUrl.ToString();
try
{
return new Uri(DashboardBaseUrl, $"/@me/{WorkspaceName}").ToString();
}
catch
{
return DefaultDashboardUrl;
}
}
}
public AgentViewModel(ILogger<AgentViewModel> logger, ICoderApiClientFactory coderApiClientFactory,
ICredentialManager credentialManager, IAgentAppViewModelFactory agentAppViewModelFactory,
IAgentExpanderHost expanderHost, Uuid id)
{
_logger = logger;
_coderApiClientFactory = coderApiClientFactory;
_credentialManager = credentialManager;
_agentAppViewModelFactory = agentAppViewModelFactory;
_expanderHost = expanderHost;
Id = id;
PropertyChanging += (x, args) =>
{
if (args.PropertyName == nameof(IsExpanded))
{
var value = !IsExpanded;
if (value)
_expanderHost.HandleAgentExpanded(Id, value);
}
};
PropertyChanged += (x, args) =>
{
if (args.PropertyName == nameof(IsExpanded))
{
// Every time the drawer is expanded, re-fetch all apps.
if (IsExpanded && !FetchingApps)
FetchApps();
}
};
// Since the property value itself never changes, we add event
// listeners for the underlying collection changing instead.
Apps.CollectionChanged += (_, _) =>
{
OnPropertyChanged(new PropertyChangedEventArgs(nameof(VisibleApps)));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(ShowExpandAppsMessage)));
OnPropertyChanged(new PropertyChangedEventArgs(nameof(ExpandAppsMessage)));
};
}
public bool TryApplyChanges(AgentViewModel model)
{
if (Id != model.Id) return false;
// To avoid spurious UI updates which cause flashing, don't actually
// write to values unless they've changed.
if (ConfiguredFqdn != model.ConfiguredFqdn)
ConfiguredFqdn = model.ConfiguredFqdn;
if (ConfiguredHostname != model.ConfiguredHostname)
ConfiguredHostname = model.ConfiguredHostname;
if (ConfiguredHostnameSuffix != model.ConfiguredHostnameSuffix)
ConfiguredHostnameSuffix = model.ConfiguredHostnameSuffix;
if (ConnectionStatus != model.ConnectionStatus)
ConnectionStatus = model.ConnectionStatus;
if (DashboardBaseUrl != model.DashboardBaseUrl)
DashboardBaseUrl = model.DashboardBaseUrl;
if (WorkspaceName != model.WorkspaceName)
WorkspaceName = model.WorkspaceName;
if (DidP2p != model.DidP2p)
DidP2p = model.DidP2p;
if (PreferredDerp != model.PreferredDerp)
PreferredDerp = model.PreferredDerp;
if (Latency != model.Latency)
Latency = model.Latency;
if (PreferredDerpLatency != model.PreferredDerpLatency)
PreferredDerpLatency = model.PreferredDerpLatency;
if (LastHandshake != model.LastHandshake)
LastHandshake = model.LastHandshake;
// Apps are not set externally.
return true;
}
[RelayCommand]
private void ToggleExpanded()
{
SetExpanded(!IsExpanded);
}
public void SetExpanded(bool expanded)
{
if (IsExpanded == expanded) return;
// This will bubble up to the TrayWindowViewModel because of the
// PropertyChanged handler.
IsExpanded = expanded;
}
partial void OnConnectionStatusChanged(AgentConnectionStatus oldValue, AgentConnectionStatus newValue)
{
if (IsExpanded && newValue is not AgentConnectionStatus.Offline) FetchApps();
}
private void FetchApps()
{
if (FetchingApps) return;
FetchingApps = true;
// If the workspace is off, then there's no agent and there's no apps.
if (ConnectionStatus == AgentConnectionStatus.Offline)
{
FetchingApps = false;
Apps.Clear();
return;
}
// API client creation could fail, which would leave FetchingApps true.
ICoderApiClient client;
try
{
client = _coderApiClientFactory.Create(_credentialManager);
}
catch
{
FetchingApps = false;
throw;
}
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
client.GetWorkspaceAgent(Id.ToString(), cts.Token).ContinueWith(t =>
{
cts.Dispose();
ContinueFetchApps(t);
}, CancellationToken.None);
}
private void ContinueFetchApps(Task<WorkspaceAgent> task)
{
// Ensure we're on the UI thread.
if (!_dispatcherQueue.HasThreadAccess)
{
_dispatcherQueue.TryEnqueue(() => ContinueFetchApps(task));
return;
}
FetchingApps = false;
AppFetchErrored = !task.IsCompletedSuccessfully;
if (!task.IsCompletedSuccessfully)
{
_logger.LogWarning(task.Exception, "Could not fetch workspace agent");
return;
}
var workspaceAgent = task.Result;
var apps = new List<AgentAppViewModel>();
foreach (var app in workspaceAgent.Apps)
{
if (!app.External || !string.IsNullOrEmpty(app.Command)) continue;
if (!Uri.TryCreate(app.Url, UriKind.Absolute, out var appUri))
{
_logger.LogWarning("Could not parse app URI '{Url}' for '{DisplayName}', app will not appear in list",
app.Url,
app.DisplayName);
continue;
}
// HTTP or HTTPS external apps are usually things like
// wikis/documentation, which clutters up the app.
if (appUri.Scheme is "http" or "https")
continue;
// Icon parse failures are not fatal, we will just use the fallback
// icon.
_ = Uri.TryCreate(DashboardBaseUrl, app.Icon, out var iconUrl);
apps.Add(_agentAppViewModelFactory.Create(app.Id, app.DisplayName, appUri, iconUrl));
}
foreach (var displayApp in workspaceAgent.DisplayApps)
{
if (displayApp is not WorkspaceAgent.DisplayAppVscode and not WorkspaceAgent.DisplayAppVscodeInsiders)
continue;
var id = VscodeAppUuid;
var displayName = "VS Code";
var icon = "/icon/code.svg";
var scheme = "vscode";
if (displayApp is WorkspaceAgent.DisplayAppVscodeInsiders)
{
id = VscodeInsidersAppUuid;
displayName = "VS Code Insiders";
icon = "/icon/code-insiders.svg";
scheme = "vscode-insiders";
}
Uri appUri;
try
{
appUri = new UriBuilder
{
Scheme = scheme,
Host = "vscode-remote",
Path = $"/ssh-remote+{FullyQualifiedDomainName}/{workspaceAgent.ExpandedDirectory}",
}.Uri;
}
catch (Exception e)
{
_logger.LogWarning(e,
"Could not craft app URI for display app {displayApp}, app will not appear in list",
displayApp);
continue;
}
// Icon parse failures are not fatal, we will just use the fallback
// icon.
_ = Uri.TryCreate(DashboardBaseUrl, icon, out var iconUrl);
apps.Add(_agentAppViewModelFactory.Create(id, displayName, appUri, iconUrl));
}
// Sort by name.
ModelUpdate.ApplyLists(Apps, apps, (a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal));
}
[RelayCommand]
private void CopyHostname(object parameter)
{
var dataPackage = new DataPackage
{
RequestedOperation = DataPackageOperation.Copy,
};
dataPackage.SetText(FullyQualifiedDomainName);
Clipboard.SetContent(dataPackage);
if (parameter is not FrameworkElement frameworkElement) return;
var flyout = new Flyout
{
Content = new TextBlock
{
Text = "DNS Copied",
Margin = new Thickness(4),
},
};
FlyoutBase.SetAttachedFlyout(frameworkElement, flyout);
FlyoutBase.ShowAttachedFlyout(frameworkElement);
}
}