- Notifications
You must be signed in to change notification settings - Fork3
feat: include ping and network stats on status tooltip#181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
ethanndickson wants to merge4 commits intomainChoose a base branch fromethan/ping-tooltip
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletionsCoder-Desktop/Coder-Desktop/Coder_DesktopApp.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletionsCoder-Desktop/Coder-Desktop/Preview Content/PreviewVPN.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsCoder-Desktop/Coder-Desktop/Theme.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
170 changes: 163 additions & 7 deletionsCoder-Desktop/Coder-Desktop/VPN/MenuState.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import Foundation | ||
import SwiftProtobuf | ||
import SwiftUI | ||
import VPNLib | ||
@@ -9,6 +10,29 @@ struct Agent: Identifiable, Equatable, Comparable, Hashable { | ||
let hosts: [String] | ||
let wsName: String | ||
let wsID: UUID | ||
let lastPing: LastPing? | ||
let lastHandshake: Date? | ||
init(id: UUID, | ||
name: String, | ||
status: AgentStatus, | ||
hosts: [String], | ||
wsName: String, | ||
wsID: UUID, | ||
lastPing: LastPing? = nil, | ||
lastHandshake: Date? = nil, | ||
primaryHost: String) | ||
{ | ||
self.id = id | ||
self.name = name | ||
self.status = status | ||
self.hosts = hosts | ||
self.wsName = wsName | ||
self.wsID = wsID | ||
self.lastPing = lastPing | ||
self.lastHandshake = lastHandshake | ||
self.primaryHost = primaryHost | ||
} | ||
// Agents are sorted by status, and then by name | ||
static func < (lhs: Agent, rhs: Agent) -> Bool { | ||
@@ -18,21 +42,94 @@ struct Agent: Identifiable, Equatable, Comparable, Hashable { | ||
return lhs.wsName.localizedCompare(rhs.wsName) == .orderedAscending | ||
} | ||
var statusString: String { | ||
switch status { | ||
case .okay, .high_latency: | ||
break | ||
default: | ||
return status.description | ||
} | ||
guard let lastPing else { | ||
// Either: | ||
// - Old coder deployment | ||
// - We haven't received any pings yet | ||
return status.description | ||
} | ||
let highLatencyWarning = status == .high_latency ? "(High latency)" : "" | ||
var str: String | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if lastPing.didP2p { | ||
str = """ | ||
You're connected peer-to-peer. \(highLatencyWarning) | ||
You ↔ \(lastPing.latency.prettyPrintMs) ↔ \(wsName) | ||
""" | ||
} else { | ||
str = """ | ||
You're connected through a DERP relay. \(highLatencyWarning) | ||
We'll switch over to peer-to-peer when available. | ||
Total latency: \(lastPing.latency.prettyPrintMs) | ||
""" | ||
// We're not guranteed to have the preferred DERP latency | ||
if let preferredDerpLatency = lastPing.preferredDerpLatency { | ||
str += "\nYou ↔ \(lastPing.preferredDerp): \(preferredDerpLatency.prettyPrintMs)" | ||
let derpToWorkspaceEstLatency = lastPing.latency - preferredDerpLatency | ||
// We're not guaranteed the preferred derp latency is less than | ||
// the total, as they might have been recorded at slightly | ||
// different times, and we don't want to show a negative value. | ||
if derpToWorkspaceEstLatency > 0 { | ||
str += "\n\(lastPing.preferredDerp) ↔ \(wsName): \(derpToWorkspaceEstLatency.prettyPrintMs)" | ||
} | ||
} | ||
} | ||
str += "\n\nLast handshake: \(lastHandshake?.relativeTimeString ?? "Unknown")" | ||
return str | ||
} | ||
let primaryHost: String | ||
} | ||
extension TimeInterval { | ||
var prettyPrintMs: String { | ||
let milliseconds = self * 1000 | ||
return "\(milliseconds.formatted(.number.precision(.fractionLength(2)))) ms" | ||
} | ||
} | ||
struct LastPing: Equatable, Hashable { | ||
let latency: TimeInterval | ||
let didP2p: Bool | ||
let preferredDerp: String | ||
let preferredDerpLatency: TimeInterval? | ||
} | ||
enum AgentStatus: Int, Equatable, Comparable { | ||
case okay = 0 | ||
case connecting = 1 | ||
case high_latency = 2 | ||
case no_recent_handshake = 3 | ||
case off = 4 | ||
public var description: String { | ||
switch self { | ||
case .okay: "Connected" | ||
case .connecting: "Connecting..." | ||
case .high_latency: "Connected, but with high latency" // Message currently unused | ||
case .no_recent_handshake: "Could not establish a connection to the agent. Retrying..." | ||
case .off: "Offline" | ||
} | ||
} | ||
public var color: Color { | ||
switch self { | ||
case .okay: .green | ||
case .high_latency: .yellow | ||
case .no_recent_handshake: .red | ||
case .off: .secondary | ||
case .connecting: .yellow | ||
} | ||
} | ||
@@ -87,14 +184,27 @@ struct VPNMenuState { | ||
workspace.agents.insert(id) | ||
workspaces[wsID] = workspace | ||
var lastPing: LastPing? | ||
if agent.hasLastPing { | ||
lastPing = LastPing( | ||
latency: agent.lastPing.latency.timeInterval, | ||
didP2p: agent.lastPing.didP2P, | ||
preferredDerp: agent.lastPing.preferredDerp, | ||
preferredDerpLatency: | ||
agent.lastPing.hasPreferredDerpLatency | ||
? agent.lastPing.preferredDerpLatency.timeInterval | ||
: nil | ||
) | ||
} | ||
agents[id] = Agent( | ||
id: id, | ||
name: agent.name, | ||
status: agent.status, | ||
hosts: nonEmptyHosts, | ||
wsName: workspace.name, | ||
wsID: wsID, | ||
lastPing: lastPing, | ||
lastHandshake: agent.lastHandshake.maybeDate, | ||
// Hosts arrive sorted by length, the shortest looks best in the UI. | ||
primaryHost: nonEmptyHosts.first! | ||
) | ||
@@ -154,3 +264,49 @@ struct VPNMenuState { | ||
workspaces.removeAll() | ||
} | ||
} | ||
extension Date { | ||
var relativeTimeString: String { | ||
let formatter = RelativeDateTimeFormatter() | ||
formatter.unitsStyle = .full | ||
if Date.now.timeIntervalSince(self) < 1.0 { | ||
// Instead of showing "in 0 seconds" | ||
return "Just now" | ||
} | ||
return formatter.localizedString(for: self, relativeTo: Date.now) | ||
} | ||
} | ||
extension SwiftProtobuf.Google_Protobuf_Timestamp { | ||
var maybeDate: Date? { | ||
guard seconds > 0 else { return nil } | ||
return date | ||
} | ||
} | ||
extension Vpn_Agent { | ||
var healthyLastHandshakeMin: Date { | ||
Date.now.addingTimeInterval(-300) // 5 minutes ago | ||
} | ||
var healthyPingMax: TimeInterval { 0.15 } // 150ms | ||
var status: AgentStatus { | ||
// Initially the handshake is missing | ||
guard let lastHandshake = lastHandshake.maybeDate else { | ||
return .connecting | ||
} | ||
// If last handshake was not within the last five minutes, the agent | ||
// is potentially unhealthy. | ||
guard lastHandshake >= healthyLastHandshakeMin else { | ||
return .no_recent_handshake | ||
} | ||
// No ping data, but we have a recent handshake. | ||
// We show green for backwards compatibility with old Coder | ||
// deployments. | ||
guard hasLastPing else { | ||
return .okay | ||
} | ||
return lastPing.latency.timeInterval < healthyPingMax ? .okay : .high_latency | ||
} | ||
} |
8 changes: 8 additions & 0 deletionsCoder-Desktop/Coder-Desktop/Views/VPN/VPNMenuItem.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsCoder-Desktop/Coder-DesktopTests/AgentsTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.