Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitce3fd30

Browse files
committed
cleanup guards and fairings
1 parent25162cc commitce3fd30

File tree

9 files changed

+103
-56
lines changed

9 files changed

+103
-56
lines changed

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

Lines 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
usecrate::{
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-
asyncfndoc_handler<'a>(path:PathBuf,cluster:Cluster) ->Result<ResponseOk,Status>{
28+
asyncfndoc_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-
asyncfnblog_handler<'a>(path:PathBuf,cluster:Cluster) ->Result<ResponseOk,Status>{
90+
asyncfnblog_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
asyncfnrender<'a>(
124141
cluster:Cluster,
142+
current_user:CurrentUserState,
125143
path:&'aPathBuf,
126144
mutnav_links:Vec<NavLink>,
127145
nav_title:&'astr,
@@ -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
letmut 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‎

Lines 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
usecrate::models::User;
9-
usecrate::{Clusters,Context};
11+
usecrate::{Clusters,Context,CurrentUser};
1012

1113
pubfndefault_database_url() ->String{
1214
matchvar("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
_ =>returnOutcome::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+
pubstructCurrentUserState{
69+
pubuser:User,
70+
pubvisible_clusters:HashMap<String,String>,
71+
}
72+
73+
#[rocket::async_trait]
74+
impl<'r>FromRequest<'r>forCurrentUserState{
75+
typeError =();
76+
77+
asyncfnfrom_request(request:&'rRequest<'_>) ->Outcome<CurrentUserState,()>{
78+
let user_data =match request.guard::<&State<CurrentUser>>().await{
79+
Outcome::Success(user) => user,
80+
_ =>returnOutcome::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‎

Lines 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
usecrate::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
pubstructContext{
44-
pubuser: models::User,
4544
pubcluster: models::Cluster,
46-
pubvisible_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+
pubstructCurrentUser{
122+
pubuser:Arc<Mutex<models::User>>,
123+
pubvisible_clusters:Arc<Mutex<HashMap<String,String>>>,
124+
}
125+
126+
implCurrentUser{
127+
pubfnuser(&self,user: models::User){
128+
*self.user.lock() = user;
129+
}
130+
131+
pubfnget_user(&self) -> models::User{
132+
self.user.lock().clone()
133+
}
134+
135+
pubfnvisible_clusters(&self,visible_clusters:HashMap<String,String>){
136+
*self.visible_clusters.lock() = visible_clusters;
137+
}
138+
139+
pubfnget_visible_clusters(&self) ->HashMap<String,String>{
140+
self.visible_clusters.lock().clone()
141+
}
142+
143+
pubfnnew() ->CurrentUser{
144+
CurrentUser::default()
145+
}
146+
}
147+
122148
#[get("/projects")]
123149
pubasyncfnproject_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
pubasyncfndashboard(
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
letmut 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‎

Lines 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‎

Lines 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
pubstructNavbar{
80+
pubcurrent_cluster:Option<models::Cluster>,
8081
pubcurrent_user:Option<models::User>,
8182
pubstandalone_dashboard:bool,
8283
}
8384

8485
implNavbar{
85-
pubfnrender(user:Option<models::User>) ->String{
86+
pubfnrender(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‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct Layout {
3030
pubhead:Head,
3131
pubcontent:Option<String>,
3232
pubuser:Option<models::User>,
33+
pubcluster:Option<models::Cluster>,
3334
pubnav_title:Option<String>,
3435
pubnav_links:Vec<docs::NavLink>,
3536
pubtoc_links:Vec<docs::TocLink>,
@@ -43,6 +44,11 @@ impl Layout {
4344
}
4445
}
4546

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

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

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

1919
<divclass="container mb-4 mb-xxl-5">

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

Lines 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
<aclass="nav-link text-white"href="https://discord.gg/DmyJP3qJ7U">Discord</a>
1616
</nav>
1717
</div>
18+
<% if !crate::utils::config::standalone_dashboard() {; %>
1819
<divclass="col-12 col-lg-6 col-xl-4">
1920
<navclass="nav d-flex flex-column">
2021
<aclass="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
<aclass="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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</button>
1313
<divclass="collapse navbar-collapse"id="navbarSupportedContent">
1414
<ulclass="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
<liclass="nav-item d-flex align-items-center">
1717
<aclass="nav-link"href="/dashboard">Dashboard</a>
1818
</li>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp