Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce3fd30

Browse filesBrowse files
committed
cleanup guards and fairings
1 parent 25162cc commit ce3fd30
Copy full SHA for ce3fd30

File tree

9 files changed

+103
-56
lines changed
Filter options

9 files changed

+103
-56
lines changed

‎pgml-dashboard/src/api/docs.rs

Copy file name to clipboardExpand all lines: pgml-dashboard/src/api/docs.rs
+25-13Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rocket::{http::Status, route::Route, State};
55
use yaml_rust::YamlLoader;
66

77
use crate::{
8-
guards::Cluster,
8+
guards::{Cluster, CurrentUserState},
99
responses::{ResponseOk, Template},
1010
templates::docs::*,
1111
utils::{config, markdown},
@@ -25,7 +25,11 @@ async fn search(query: &str, index: &State<markdown::SearchIndex>) -> ResponseOk
2525
}
2626

2727
#[get("/docs/<path..>", rank = 10)]
28-
async fn doc_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk, Status> {
28+
async fn doc_handler<'a>(
29+
path: PathBuf,
30+
cluster: Cluster,
31+
current_user: CurrentUserState,
32+
) -> Result<ResponseOk, Status> {
2933
let guides = vec![
3034
NavLink::new("Setup").children(vec![
3135
NavLink::new("Installation").children(vec![
@@ -71,13 +75,26 @@ async fn doc_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk,
7175
]),
7276
];
7377

74-
render(cluster, &path, guides, "Guides", &Path::new("docs")).await
78+
render(
79+
cluster,
80+
current_user,
81+
&path,
82+
guides,
83+
"Guides",
84+
&Path::new("docs"),
85+
)
86+
.await
7587
}
7688

7789
#[get("/blog/<path..>", rank = 10)]
78-
async fn blog_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk, Status> {
90+
async fn blog_handler<'a>(
91+
path: PathBuf,
92+
cluster: Cluster,
93+
current_user: CurrentUserState,
94+
) -> Result<ResponseOk, Status> {
7995
render(
8096
cluster,
97+
current_user,
8198
&path,
8299
vec![
83100
NavLink::new("Introducing PostgresML Python SDK: Build End-to-End Vector Search Applications without OpenAI and Pinecone")
@@ -122,6 +139,7 @@ async fn blog_handler<'a>(path: PathBuf, cluster: Cluster) -> Result<ResponseOk,
122139

123140
async fn render<'a>(
124141
cluster: Cluster,
142+
current_user: CurrentUserState,
125143
path: &'a PathBuf,
126144
mut nav_links: Vec<NavLink>,
127145
nav_title: &'a str,
@@ -196,23 +214,17 @@ async fn render<'a>(
196214
nav_link.should_open(&url.to_str().unwrap().to_string());
197215
}
198216

199-
let user = if cluster.context.user.is_anonymous() {
200-
None
201-
} else {
202-
Some(cluster.context.user)
203-
};
204-
205217
let mut layout = crate::templates::Layout::new(&title);
206218
if image.is_some() {
207219
layout.image(&image.unwrap());
208220
}
209221
if description.is_some() {
210222
layout.description(&description.unwrap());
211223
}
212-
if user.is_some() {
213-
layout.user(&user.unwrap());
214-
}
224+
215225
let layout = layout
226+
.user(&current_user.user)
227+
.cluster(&cluster.context.cluster)
216228
.nav_title(nav_title)
217229
.nav_links(&nav_links)
218230
.toc_links(&toc_links);

‎pgml-dashboard/src/guards.rs

Copy file name to clipboardExpand all lines: pgml-dashboard/src/guards.rs
+31-20Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use rocket::request::{FromRequest, Outcome, Request};
55
use rocket::State;
66
use sqlx::PgPool;
77

8+
use std::collections::HashMap;
9+
810
use crate::models::User;
9-
use crate::{Clusters, Context};
11+
use crate::{Clusters, Context, CurrentUser};
1012

1113
pub fn default_database_url() -> String {
1214
match var("DATABASE_URL") {
@@ -47,33 +49,42 @@ impl<'r> FromRequest<'r> for Cluster {
4749
None => -1,
4850
};
4951

50-
let user_id: i64 = match cookies.get_private("user_id") {
51-
Some(user_id) => match user_id.value().parse::<i64>() {
52-
Ok(user_id) => user_id,
53-
Err(_) => -1,
54-
},
55-
56-
None => -1,
57-
};
58-
59-
let clusters_shared_state = match request.guard::<&State<Clusters>>().await {
52+
let clusters = match request.guard::<&State<Clusters>>().await {
6053
Outcome::Success(pool) => pool,
6154
_ => return Outcome::Forward(()),
6255
};
6356

64-
let pool = clusters_shared_state.get(cluster_id);
57+
let pool = clusters.get(cluster_id);
6558

6659
let context = Context {
67-
user: User {
68-
id: user_id,
69-
email: "".to_string(),
70-
},
71-
cluster: clusters_shared_state.get_context(cluster_id).cluster,
72-
visible_clusters: clusters_shared_state
73-
.get_context(cluster_id)
74-
.visible_clusters,
60+
cluster: clusters.get_context(cluster_id).cluster,
7561
};
7662

7763
Outcome::Success(Cluster { pool, context })
7864
}
7965
}
66+
67+
#[derive(Debug)]
68+
pub struct CurrentUserState {
69+
pub user: User,
70+
pub visible_clusters: HashMap<String, String>,
71+
}
72+
73+
#[rocket::async_trait]
74+
impl<'r> FromRequest<'r> for CurrentUserState {
75+
type Error = ();
76+
77+
async fn from_request(request: &'r Request<'_>) -> Outcome<CurrentUserState, ()> {
78+
let user_data = match request.guard::<&State<CurrentUser>>().await {
79+
Outcome::Success(user) => user,
80+
_ => return Outcome::Forward(()),
81+
};
82+
83+
let current_user_state = CurrentUserState {
84+
user: user_data.get_user(),
85+
visible_clusters: user_data.get_visible_clusters(),
86+
};
87+
88+
Outcome::Success(current_user_state)
89+
}
90+
}

‎pgml-dashboard/src/lib.rs

Copy file name to clipboardExpand all lines: pgml-dashboard/src/lib.rs
+33-13Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::templates::{
2424
DeploymentsTab, Layout, ModelsTab, NotebooksTab, ProjectsTab, SnapshotsTab, UploaderTab,
2525
};
2626
use crate::utils::tabs;
27-
use guards::Cluster;
27+
use guards::{Cluster, CurrentUserState};
2828
use responses::{BadRequest, Error, ResponseOk};
2929
use sqlx::Executor;
3030

@@ -41,9 +41,7 @@ pub struct ClustersSettings {
4141
/// This gives it a bit of shared state that allows the dashboard to display cluster-specific information.
4242
#[derive(Debug, Default, Clone)]
4343
pub struct Context {
44-
pub user: models::User,
4544
pub cluster: models::Cluster,
46-
pub visible_clusters: HashMap<String, String>,
4745
}
4846

4947
/// Globally shared state, saved in memory.
@@ -119,6 +117,34 @@ impl Clusters {
119117
}
120118
}
121119

120+
#[derive(Debug, Default, Clone)]
121+
pub struct CurrentUser {
122+
pub user: Arc<Mutex<models::User>>,
123+
pub visible_clusters: Arc<Mutex<HashMap<String, String>>>,
124+
}
125+
126+
impl CurrentUser {
127+
pub fn user(&self, user: models::User) {
128+
*self.user.lock() = user;
129+
}
130+
131+
pub fn get_user(&self) -> models::User {
132+
self.user.lock().clone()
133+
}
134+
135+
pub fn visible_clusters(&self, visible_clusters: HashMap<String, String>) {
136+
*self.visible_clusters.lock() = visible_clusters;
137+
}
138+
139+
pub fn get_visible_clusters(&self) -> HashMap<String, String> {
140+
self.visible_clusters.lock().clone()
141+
}
142+
143+
pub fn new() -> CurrentUser {
144+
CurrentUser::default()
145+
}
146+
}
147+
122148
#[get("/projects")]
123149
pub async fn project_index(cluster: Cluster) -> Result<ResponseOk, Error> {
124150
Ok(ResponseOk(
@@ -541,6 +567,7 @@ pub async fn uploaded_index(cluster: Cluster, table_name: &str) -> ResponseOk {
541567
#[get("/?<tab>&<notebook_id>&<model_id>&<project_id>&<snapshot_id>&<deployment_id>&<table_name>")]
542568
pub async fn dashboard(
543569
cluster: Cluster,
570+
current_user: CurrentUserState,
544571
tab: Option<&str>,
545572
notebook_id: Option<i64>,
546573
model_id: Option<i64>,
@@ -549,17 +576,10 @@ pub async fn dashboard(
549576
deployment_id: Option<i64>,
550577
table_name: Option<String>,
551578
) -> Result<ResponseOk, Error> {
552-
let user = if cluster.context.user.is_anonymous() {
553-
None
554-
} else {
555-
Some(cluster.context.user.clone())
556-
};
557-
558579
let mut layout = crate::templates::Layout::new("Dashboard");
559-
560-
if user.is_some() {
561-
layout.user(&user.clone().unwrap());
562-
}
580+
layout
581+
.user(&current_user.user)
582+
.cluster(&cluster.context.cluster);
563583

564584
let all_tabs = vec![
565585
tabs::Tab {

‎pgml-dashboard/src/main.rs

Copy file name to clipboardExpand all lines: pgml-dashboard/src/main.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ async fn main() {
138138

139139
let _ = rocket::build()
140140
.manage(clusters)
141+
.manage(pgml_dashboard::CurrentUser::new())
141142
.manage(markdown::SearchIndex::open().unwrap())
142143
.mount("/", rocket::routes![index, error])
143144
.mount("/dashboard/static", FileServer::from(&config::static_dir()))

‎pgml-dashboard/src/templates/components.rs

Copy file name to clipboardExpand all lines: pgml-dashboard/src/templates/components.rs
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ pub struct Boxes<'a> {
7777
#[derive(TemplateOnce)]
7878
#[template(path = "layout/nav/top.html")]
7979
pub struct Navbar {
80+
pub current_cluster: Option<models::Cluster>,
8081
pub current_user: Option<models::User>,
8182
pub standalone_dashboard: bool,
8283
}
8384

8485
impl Navbar {
85-
pub fn render(user: Option<models::User>) -> String {
86+
pub fn render(user: Option<models::User>, cluster: Option<models::Cluster>) -> String {
8687
Navbar {
88+
current_cluster: cluster,
8789
current_user: user,
8890
standalone_dashboard: config::standalone_dashboard(),
8991
}

‎pgml-dashboard/src/templates/mod.rs

Copy file name to clipboardExpand all lines: pgml-dashboard/src/templates/mod.rs
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct Layout {
3030
pub head: Head,
3131
pub content: Option<String>,
3232
pub user: Option<models::User>,
33+
pub cluster: Option<models::Cluster>,
3334
pub nav_title: Option<String>,
3435
pub nav_links: Vec<docs::NavLink>,
3536
pub toc_links: Vec<docs::TocLink>,
@@ -43,6 +44,11 @@ impl Layout {
4344
}
4445
}
4546

47+
pub fn cluster(&mut self, cluster: &models::Cluster) -> &mut Self {
48+
self.cluster = Some(cluster.to_owned());
49+
self
50+
}
51+
4652
pub fn description(&mut self, description: &str) -> &mut Self {
4753
self.head.description = Some(description.to_owned());
4854
self
@@ -377,13 +383,6 @@ pub struct Uploaded {
377383
pub table_name: String,
378384
}
379385

380-
#[derive(TemplateOnce)]
381-
#[template(path = "layout/nav/top.html")]
382-
pub struct Navbar {
383-
pub current_user: Option<models::User>,
384-
pub standalone_dashboard: bool,
385-
}
386-
387386
#[derive(TemplateOnce)]
388387
#[template(path = "content/dashboard/dashboard.html")]
389388
pub struct Dashboard<'a> {

‎pgml-dashboard/templates/layout/base.html

Copy file name to clipboardExpand all lines: pgml-dashboard/templates/layout/base.html
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<div class="container mb-4 mb-xxl-5">
1515

16-
<%- Navbar::render( user ) %>
16+
<%- Navbar::render( user, cluster ) %>
1717
</div>
1818

1919
<div class="container mb-4 mb-xxl-5">

‎pgml-dashboard/templates/layout/footer.html

Copy file name to clipboardExpand all lines: pgml-dashboard/templates/layout/footer.html
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ <h5 class="h5 d-flex align-items-center gap-2 mb-5">
1515
<a class="nav-link text-white" href="https://discord.gg/DmyJP3qJ7U">Discord</a>
1616
</nav>
1717
</div>
18+
<% if !crate::utils::config::standalone_dashboard() {; %>
1819
<div class="col-12 col-lg-6 col-xl-4">
1920
<nav class="nav d-flex flex-column">
2021
<a class="nav-link text-white" href="<%= crate::utils::config::signup_url() %>">Create an Account</a>
@@ -23,6 +24,7 @@ <h5 class="h5 d-flex align-items-center gap-2 mb-5">
2324
<a class="nav-link text-white" href="/privacy" data-turbo="false">Privacy Policy</a>
2425
</nav>
2526
</div>
27+
<% } %>
2628
</div>
2729
</div>
2830

‎pgml-dashboard/templates/layout/nav/top.html

Copy file name to clipboardExpand all lines: pgml-dashboard/templates/layout/nav/top.html
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</button>
1313
<div class="collapse navbar-collapse" id="navbarSupportedContent">
1414
<ul class="navbar-nav flex-grow-1 justify-content-center me-auto mb-2 mb-lg-0">
15-
<% if standalone_dashboard || (current_user.as_ref().is_some() && current_user.as_ref().unwrap().id != -1 ) { %>
15+
<% if standalone_dashboard || (current_cluster.is_some() && current_cluster.unwrap().id != -1) { %>
1616
<li class="nav-item d-flex align-items-center">
1717
<a class="nav-link" href="/dashboard">Dashboard</a>
1818
</li>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.