-
-
Notifications
You must be signed in to change notification settings - Fork 140
feat: modify Dashboard and Filters type to HashMap #1172
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
* | ||
*/ | ||
|
||
use std::sync::RwLock; | ||
use std::{collections::HashMap, sync::RwLock}; | ||
|
||
use once_cell::sync::Lazy; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -108,7 +108,7 @@ pub struct Dashboard { | |
} | ||
|
||
#[derive(Default, Debug)] | ||
pub struct Dashboards(RwLock<Vec<Dashboard>>); | ||
pub struct Dashboards(RwLock<HashMap<String, Dashboard>>); | ||
|
||
impl Dashboards { | ||
pub async fn load(&self) -> anyhow::Result<()> { | ||
|
@@ -173,20 +173,29 @@ impl Dashboards { | |
} | ||
|
||
let mut s = self.0.write().expect(LOCK_EXPECT); | ||
s.append(&mut this); | ||
s.extend( | ||
this.into_iter() | ||
.map(|d| (d.dashboard_id.clone().unwrap_or_else(|| d.name.clone()), d)), | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn update(&self, dashboard: &Dashboard) { | ||
let mut s = self.0.write().expect(LOCK_EXPECT); | ||
s.retain(|d| d.dashboard_id != dashboard.dashboard_id); | ||
s.push(dashboard.clone()); | ||
s.retain(|_, d| d.dashboard_id != dashboard.dashboard_id); | ||
s.insert( | ||
dashboard | ||
.dashboard_id | ||
.clone() | ||
.unwrap_or(dashboard.name.clone()), | ||
dashboard.clone(), | ||
); | ||
Comment on lines
+186
to
+193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a simple insert will work? Insert is equivalent to an update in the case of existing keys. |
||
} | ||
|
||
pub fn delete_dashboard(&self, dashboard_id: &str) { | ||
let mut s = self.0.write().expect(LOCK_EXPECT); | ||
s.retain(|d| d.dashboard_id != Some(dashboard_id.to_string())); | ||
s.remove(dashboard_id); | ||
} | ||
|
||
pub fn get_dashboard(&self, dashboard_id: &str, user_id: &str) -> Option<Dashboard> { | ||
|
@@ -195,19 +204,19 @@ impl Dashboards { | |
.expect(LOCK_EXPECT) | ||
.iter() | ||
.find(|d| { | ||
d.dashboard_id == Some(dashboard_id.to_string()) | ||
&& d.user_id == Some(user_id.to_string()) | ||
d.1.dashboard_id == Some(dashboard_id.to_string()) | ||
&& d.1.user_id == Some(user_id.to_string()) | ||
Comment on lines
205
to
+208
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we do this with a simple |
||
}) | ||
.cloned() | ||
.map(|(_, d)| d.clone()) | ||
} | ||
|
||
pub fn list_dashboards_by_user(&self, user_id: &str) -> Vec<Dashboard> { | ||
pub fn list_dashboards_by_user(&self, user_id: &str) -> HashMap<String, Dashboard> { | ||
self.0 | ||
.read() | ||
.expect(LOCK_EXPECT) | ||
.iter() | ||
.filter(|d| d.user_id == Some(user_id.to_string())) | ||
.cloned() | ||
.filter(|(_, d)| d.user_id == Some(user_id.to_string())) | ||
.map(|(k, d)| (k.clone(), d.clone())) | ||
.collect() | ||
} | ||
} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar suggestions here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we do this using
HashMap::insert
s, withdashboard_id
as always the key!