Skip to content

Commit

Permalink
change open into a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
lyang2821 committed Oct 11, 2024
1 parent 4f902ec commit 4c5d7cf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
10 changes: 7 additions & 3 deletions lapdev-conductor/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ impl Conductor {
}

fn format_repo_url(&self, repo: &str) -> String {
let repo = repo.trim();
let repo = repo.trim().to_lowercase();
let repo = if !repo.starts_with("http://")
&& !repo.starts_with("https://")
&& !repo.starts_with("ssh://")
Expand Down Expand Up @@ -1094,7 +1094,9 @@ impl Conductor {
let (id_rsa, public_key) = self.generate_key_pair()?;
let osuser = org.id.to_string().replace('-', "");

self.enterprise.check_organization_limit(org).await?;
self.enterprise
.check_organization_limit(org, user.id)
.await?;

let txn = self.db.conn.begin().await?;
if let Some(quota) = self
Expand Down Expand Up @@ -2275,7 +2277,9 @@ impl Conductor {
}

let org = self.db.get_organization(workspace.organization_id).await?;
self.enterprise.check_organization_limit(&org).await?;
self.enterprise
.check_organization_limit(&org, workspace.user_id)
.await?;

let txn = self.db.conn.begin().await?;
if let Some(quota) = self
Expand Down
63 changes: 21 additions & 42 deletions lapdev-dashboard/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,61 +199,30 @@ fn OpenWorkspaceView(
);
};

let open_workspace = {
dropdown_hidden.set(true);
let workspace_name = workspace_name.clone();
let workspace_hostname = workspace_hostname.clone();
move |_| {
let _ = window().open_with_url_and_target(
&format!("http://{workspace_name}.{workspace_hostname}/"),
"_blank",
);
}
};

let open_workspace_in_vscode = {
dropdown_hidden.set(true);
let workspace_name = workspace_name.clone();
let workspace_folder = workspace_folder.clone();
let cluster_info = expect_context::<Signal<Option<ClusterInfo>>>();
move |_| {
let port = cluster_info
.with_untracked(|i| i.as_ref().map(|i| i.ssh_proxy_port).unwrap_or(2222));
let _ = window()
.open_with_url_and_target(
&format!(
"vscode://vscode-remote/ssh-remote+{workspace_name}@{}{}/workspaces/{workspace_folder}",
workspace_hostname.split(':').next().unwrap_or(""),
if port == 22 {
"".to_string()
} else {
format!(":{port}")
}
),
"_blank"
);
}
};

let open_button_class = if workspace_status == WorkspaceStatus::Running {
"bg-green-700 dark:bg-green-600 hover:bg-green-800 dark:hover:bg-green-700"
} else {
"bg-green-200 dark:bg-green-700"
};
let open_button_class = format!("{open_button_class} py-2 text-sm font-medium text-white focus:ring-4 focus:ring-green-300 focus:outline-none dark:focus:ring-green-800");

let workspace_hostname = workspace_hostname.clone();
let cluster_info = expect_context::<Signal<Option<ClusterInfo>>>();
let port =
cluster_info.with_untracked(|i| i.as_ref().map(|i| i.ssh_proxy_port).unwrap_or(2222));
view! {
<div
class="pr-6 relative"
on:focusout=on_focusout
>
<button
class={format!("{open_button_class} px-4 rounded-l-lg")}
<a
class={format!("{open_button_class} px-4 rounded-l-lg inline-block")}
disabled=move || workspace_status != WorkspaceStatus::Running
on:click=open_workspace
target="_blank"
href={format!("http://{workspace_name}.{workspace_hostname}/")}
>
Open
</button>
</a>
<button
class={format!("{open_button_class} h-full absolute right-0 px-2 rounded-r-lg border-l-1")}
disabled=move || workspace_status != WorkspaceStatus::Running
Expand All @@ -271,9 +240,19 @@ fn OpenWorkspaceView(
<ul class="py-2 text-sm text-gray-700 dark:text-gray-200 bg-white rounded-lg border shadow w-64 dark:bg-gray-700">
<li>
<a
href="#"
href={
format!(
"vscode://vscode-remote/ssh-remote+{workspace_name}@{}{}/workspaces/{workspace_folder}",
workspace_hostname.split(':').next().unwrap_or(""),
if port == 22 {
"".to_string()
} else {
format!(":{port}")
}
)
}
class="block px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white"
on:click=open_workspace_in_vscode
target="_blank"
>
Open in VSCode Desktop
</a>
Expand Down
25 changes: 25 additions & 0 deletions lapdev-enterprise/src/enterprise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Enterprise {
pub async fn check_organization_limit(
&self,
organization: &entities::organization::Model,
user_id: Uuid,
) -> Result<(), ApiError> {
if !self.license.has_valid().await {
return Ok(());
Expand Down Expand Up @@ -83,6 +84,30 @@ impl Enterprise {
.unwrap_or_else(|_| "You have reached the usage limit".to_string()),
));
}

if organization.running_workspace_limit > 0 {
let all_orgs = self.db.get_user_organizations(user_id).await?;
if all_orgs.len() > 1 {
let mut personal_usage = 0;
for org in all_orgs {
let usage = self
.usage
.get_monthly_cost(
org.organization_id,
Some(user_id),
Utc::now().into(),
None,
)
.await?;
personal_usage += usage;
}
if personal_usage as i64 >= organization.usage_limit {
return Err(ApiError::InvalidRequest(
"You have reached your personal usage limit".to_string(),
));
}
}
}
}

Ok(())
Expand Down

0 comments on commit 4c5d7cf

Please sign in to comment.