- Notifications
You must be signed in to change notification settings - Fork3
fix: display offline workspaces#41
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
87ec698
fix: unquarantine dylib after download
ethanndickson024d7e3
capitalization
ethanndicksone64ea22
review
ethanndicksonce1883e
fix: display offline workspaces
ethanndickson972f269
review
ethanndickson63442fe
undo dumb idea
ethanndicksonce56128
Merge branch 'main' into ethan/offline-workspaces
ethanndicksonFile 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: 2 additions & 1 deletionCoder Desktop/Coder Desktop/About.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
26 changes: 13 additions & 13 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
140 changes: 140 additions & 0 deletionsCoder Desktop/Coder Desktop/VPNMenuState.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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import Foundation | ||
import SwiftUI | ||
import VPNLib | ||
struct Agent: Identifiable, Equatable, Comparable { | ||
let id: UUID | ||
let name: String | ||
let status: AgentStatus | ||
let hosts: [String] | ||
let wsName: String | ||
let wsID: UUID | ||
// Agents are sorted by status, and then by name | ||
static func < (lhs: Agent, rhs: Agent) -> Bool { | ||
if lhs.status != rhs.status { | ||
return lhs.status < rhs.status | ||
} | ||
return lhs.wsName.localizedCompare(rhs.wsName) == .orderedAscending | ||
} | ||
// Hosts arrive sorted by length, the shortest looks best in the UI. | ||
var primaryHost: String? { hosts.first } | ||
} | ||
enum AgentStatus: Int, Equatable, Comparable { | ||
case okay = 0 | ||
case warn = 1 | ||
case error = 2 | ||
case off = 3 | ||
public var color: Color { | ||
switch self { | ||
case .okay: .green | ||
case .warn: .yellow | ||
case .error: .red | ||
case .off: .gray | ||
} | ||
} | ||
static func < (lhs: AgentStatus, rhs: AgentStatus) -> Bool { | ||
lhs.rawValue < rhs.rawValue | ||
} | ||
} | ||
struct Workspace: Identifiable, Equatable, Comparable { | ||
let id: UUID | ||
let name: String | ||
var agents: Set<UUID> | ||
static func < (lhs: Workspace, rhs: Workspace) -> Bool { | ||
lhs.name.localizedCompare(rhs.name) == .orderedAscending | ||
} | ||
} | ||
struct VPNMenuState { | ||
var agents: [UUID: Agent] = [:] | ||
var workspaces: [UUID: Workspace] = [:] | ||
// Upserted agents that don't belong to any known workspace, have no FQDNs, | ||
// or have any invalid UUIDs. | ||
var invalidAgents: [Vpn_Agent] = [] | ||
mutating func upsertAgent(_ agent: Vpn_Agent) { | ||
guard | ||
let id = UUID(uuidData: agent.id), | ||
let wsID = UUID(uuidData: agent.workspaceID), | ||
var workspace = workspaces[wsID], | ||
!agent.fqdn.isEmpty | ||
else { | ||
invalidAgents.append(agent) | ||
return | ||
} | ||
// An existing agent with the same name, belonging to the same workspace | ||
// is from a previous workspace build, and should be removed. | ||
agents.filter { $0.value.name == agent.name && $0.value.wsID == wsID } | ||
.forEach { agents[$0.key] = nil } | ||
workspace.agents.insert(id) | ||
workspaces[wsID] = workspace | ||
agents[id] = Agent( | ||
id: id, | ||
name: agent.name, | ||
// If last handshake was not within last five minutes, the agent is unhealthy | ||
status: agent.lastHandshake.date > Date.now.addingTimeInterval(-300) ? .okay : .warn, | ||
// Remove trailing dot if present | ||
hosts: agent.fqdn.map { $0.hasSuffix(".") ? String($0.dropLast()) : $0 }, | ||
wsName: workspace.name, | ||
wsID: wsID | ||
) | ||
} | ||
mutating func deleteAgent(withId id: Data) { | ||
guard let agentUUID = UUID(uuidData: id) else { return } | ||
// Update Workspaces | ||
if let agent = agents[agentUUID], var ws = workspaces[agent.wsID] { | ||
ws.agents.remove(agentUUID) | ||
workspaces[agent.wsID] = ws | ||
} | ||
agents[agentUUID] = nil | ||
// Remove from invalid agents if present | ||
invalidAgents.removeAll { invalidAgent in | ||
invalidAgent.id == id | ||
} | ||
} | ||
mutating func upsertWorkspace(_ workspace: Vpn_Workspace) { | ||
guard let wsID = UUID(uuidData: workspace.id) else { return } | ||
workspaces[wsID] = Workspace(id: wsID, name: workspace.name, agents: []) | ||
// Check if we can associate any invalid agents with this workspace | ||
invalidAgents.filter { agent in | ||
agent.workspaceID == workspace.id | ||
}.forEach { agent in | ||
invalidAgents.removeAll { $0 == agent } | ||
upsertAgent(agent) | ||
} | ||
} | ||
mutating func deleteWorkspace(withId id: Data) { | ||
guard let wsID = UUID(uuidData: id) else { return } | ||
agents.filter { _, value in | ||
value.wsID == wsID | ||
}.forEach { key, _ in | ||
agents[key] = nil | ||
} | ||
workspaces[wsID] = nil | ||
} | ||
var sorted: [VPNMenuItem] { | ||
var items = agents.values.map { VPNMenuItem.agent($0) } | ||
// Workspaces with no agents are shown as offline | ||
items += workspaces.filter { _, value in | ||
value.agents.isEmpty | ||
}.map { VPNMenuItem.offlineWorkspace(Workspace(id: $0.key, name: $0.value.name, agents: $0.value.agents)) } | ||
return items.sorted() | ||
} | ||
mutating func clear() { | ||
agents.removeAll() | ||
workspaces.removeAll() | ||
} | ||
} |
64 changes: 8 additions & 56 deletionsCoder Desktop/Coder Desktop/VPNService.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.