Skip to content

Commit 7a4e0b8

Browse files
feat: allow multiple tags in dashboards list_by_tag API (#1374)
1 parent 2bd8f2f commit 7a4e0b8

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

src/handlers/http/modal/server.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,6 @@ impl Server {
307307
.authorize(Action::ListDashboard),
308308
),
309309
)
310-
.service(
311-
web::resource("/list_by_tag/{tag}").route(
312-
web::get()
313-
.to(dashboards::list_dashboards_by_tag)
314-
.authorize(Action::ListDashboard),
315-
),
316-
)
317310
.service(
318311
web::scope("/{dashboard_id}")
319312
.service(

src/handlers/http/users/dashboards.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ pub async fn list_dashboards(req: HttpRequest) -> Result<impl Responder, Dashboa
4444
return Err(DashboardError::Metadata("Invalid limit value"));
4545
}
4646
}
47+
48+
if let Some(tags) = query_map.get("tags") {
49+
let tags: Vec<String> = tags
50+
.split(',')
51+
.map(|s| s.trim().to_string())
52+
.filter(|s| !s.is_empty())
53+
.collect();
54+
if tags.is_empty() {
55+
return Err(DashboardError::Metadata("Tags cannot be empty"));
56+
}
57+
let dashboards = DASHBOARDS.list_dashboards_by_tags(tags).await;
58+
let dashboard_summaries = dashboards
59+
.iter()
60+
.map(|dashboard| dashboard.to_summary())
61+
.collect::<Vec<_>>();
62+
return Ok((web::Json(dashboard_summaries), StatusCode::OK));
63+
}
4764
}
4865
let dashboards = DASHBOARDS.list_dashboards(dashboard_limit).await;
4966
let dashboard_summaries = dashboards
@@ -215,21 +232,6 @@ pub async fn list_tags() -> Result<impl Responder, DashboardError> {
215232
Ok((web::Json(tags), StatusCode::OK))
216233
}
217234

218-
pub async fn list_dashboards_by_tag(tag: Path<String>) -> Result<impl Responder, DashboardError> {
219-
let tag = tag.into_inner();
220-
if tag.is_empty() {
221-
return Err(DashboardError::Metadata("Tag cannot be empty"));
222-
}
223-
224-
let dashboards = DASHBOARDS.list_dashboards_by_tag(&tag).await;
225-
let dashboard_summaries = dashboards
226-
.iter()
227-
.map(|dashboard| dashboard.to_summary())
228-
.collect::<Vec<_>>();
229-
230-
Ok((web::Json(dashboard_summaries), StatusCode::OK))
231-
}
232-
233235
#[derive(Debug, thiserror::Error)]
234236
pub enum DashboardError {
235237
#[error("Failed to connect to storage: {0}")]

src/users/dashboards.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,20 @@ impl Dashboards {
375375
}
376376

377377
/// List dashboards by tag
378-
/// This function returns a list of dashboards that have the specified tag
379-
pub async fn list_dashboards_by_tag(&self, tag: &str) -> Vec<Dashboard> {
378+
/// This function returns a list of dashboards that match any of the provided tags
379+
/// If no tags are provided, it returns an empty list
380+
pub async fn list_dashboards_by_tags(&self, tags: Vec<String>) -> Vec<Dashboard> {
380381
let dashboards = self.0.read().await;
381382
dashboards
382383
.iter()
383384
.filter(|d| {
384-
d.tags
385-
.as_ref()
386-
.is_some_and(|tags| tags.contains(&tag.to_string()))
385+
if let Some(dashboard_tags) = &d.tags {
386+
dashboard_tags
387+
.iter()
388+
.any(|dashboard_tag| tags.contains(dashboard_tag))
389+
} else {
390+
false
391+
}
387392
})
388393
.cloned()
389394
.collect()

0 commit comments

Comments
 (0)