Skip to content

Commit 8e9e789

Browse files
author
Devdutt Shenoi
committed
refactor: DRY get_all_dashboards
1 parent d08cd82 commit 8e9e789

File tree

4 files changed

+23
-103
lines changed

4 files changed

+23
-103
lines changed

src/storage/azure_blob.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -668,42 +668,6 @@ impl ObjectStorage for BlobStore {
668668
.collect::<Vec<_>>())
669669
}
670670

671-
async fn get_all_dashboards(
672-
&self,
673-
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError> {
674-
let mut dashboards: HashMap<RelativePathBuf, Vec<Bytes>> = HashMap::new();
675-
let users_root_path = object_store::path::Path::from(USERS_ROOT_DIR);
676-
let resp = self
677-
.client
678-
.list_with_delimiter(Some(&users_root_path))
679-
.await?;
680-
681-
let users = resp
682-
.common_prefixes
683-
.iter()
684-
.flat_map(|path| path.parts())
685-
.filter(|name| name.as_ref() != USERS_ROOT_DIR)
686-
.map(|name| name.as_ref().to_string())
687-
.collect::<Vec<_>>();
688-
for user in users {
689-
let user_dashboard_path =
690-
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/dashboards"));
691-
let dashboards_path = RelativePathBuf::from(&user_dashboard_path);
692-
let dashboard_bytes = self
693-
.get_objects(
694-
Some(&dashboards_path),
695-
Box::new(|file_name| file_name.ends_with(".json")),
696-
)
697-
.await?;
698-
699-
dashboards
700-
.entry(dashboards_path)
701-
.or_default()
702-
.extend(dashboard_bytes);
703-
}
704-
Ok(dashboards)
705-
}
706-
707671
///fetch all correlations uploaded in object store
708672
/// return the correlation file path and all correlation json bytes for each file path
709673
async fn get_all_correlations(

src/storage/localfs.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -374,36 +374,6 @@ impl ObjectStorage for LocalFS {
374374
Ok(dirs)
375375
}
376376

377-
async fn get_all_dashboards(
378-
&self,
379-
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError> {
380-
let mut dashboards: HashMap<RelativePathBuf, Vec<Bytes>> = HashMap::new();
381-
let users_root_path = self.root.join(USERS_ROOT_DIR);
382-
let directories = ReadDirStream::new(fs::read_dir(&users_root_path).await?);
383-
let users: Vec<DirEntry> = directories.try_collect().await?;
384-
for user in users {
385-
if !user.path().is_dir() {
386-
continue;
387-
}
388-
let dashboards_path = users_root_path.join(user.path()).join("dashboards");
389-
let directories = ReadDirStream::new(fs::read_dir(&dashboards_path).await?);
390-
let dashboards_files: Vec<DirEntry> = directories.try_collect().await?;
391-
for dashboard in dashboards_files {
392-
let dashboard_absolute_path = dashboard.path();
393-
let file = fs::read(dashboard_absolute_path.clone()).await?;
394-
let dashboard_relative_path = dashboard_absolute_path
395-
.strip_prefix(self.root.as_path())
396-
.unwrap();
397-
398-
dashboards
399-
.entry(RelativePathBuf::from_path(dashboard_relative_path).unwrap())
400-
.or_default()
401-
.push(file.into());
402-
}
403-
}
404-
Ok(dashboards)
405-
}
406-
407377
///fetch all correlations stored in disk
408378
/// return the correlation file path and all correlation json bytes for each file path
409379
async fn get_all_correlations(

src/storage/object_storage.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,29 @@ pub trait ObjectStorage: Debug + Send + Sync + 'static {
126126

127127
async fn get_all_dashboards(
128128
&self,
129-
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError>;
129+
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError> {
130+
let mut dashboards: HashMap<RelativePathBuf, Vec<Bytes>> = HashMap::new();
131+
132+
let users_dir = RelativePathBuf::from_iter([USERS_ROOT_DIR]);
133+
for user in self.list_dirs_relative(&users_dir).await? {
134+
let user_dashboard_path =
135+
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/dashboards"));
136+
let dashboards_path = RelativePathBuf::from(&user_dashboard_path);
137+
let dashboard_bytes = self
138+
.get_objects(
139+
Some(&dashboards_path),
140+
Box::new(|file_name| file_name.ends_with(".json")),
141+
)
142+
.await?;
143+
144+
dashboards
145+
.entry(dashboards_path)
146+
.or_default()
147+
.extend(dashboard_bytes);
148+
}
149+
Ok(dashboards)
150+
}
151+
130152
async fn get_all_correlations(
131153
&self,
132154
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError>;

src/storage/s3.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -801,42 +801,6 @@ impl ObjectStorage for S3 {
801801
.collect::<Vec<_>>())
802802
}
803803

804-
async fn get_all_dashboards(
805-
&self,
806-
) -> Result<HashMap<RelativePathBuf, Vec<Bytes>>, ObjectStorageError> {
807-
let mut dashboards: HashMap<RelativePathBuf, Vec<Bytes>> = HashMap::new();
808-
let users_root_path = object_store::path::Path::from(USERS_ROOT_DIR);
809-
let resp = self
810-
.client
811-
.list_with_delimiter(Some(&users_root_path))
812-
.await?;
813-
814-
let users = resp
815-
.common_prefixes
816-
.iter()
817-
.flat_map(|path| path.parts())
818-
.filter(|name| name.as_ref() != USERS_ROOT_DIR)
819-
.map(|name| name.as_ref().to_string())
820-
.collect::<Vec<_>>();
821-
for user in users {
822-
let user_dashboard_path =
823-
object_store::path::Path::from(format!("{USERS_ROOT_DIR}/{user}/dashboards"));
824-
let dashboards_path = RelativePathBuf::from(&user_dashboard_path);
825-
let dashboard_bytes = self
826-
.get_objects(
827-
Some(&dashboards_path),
828-
Box::new(|file_name| file_name.ends_with(".json")),
829-
)
830-
.await?;
831-
832-
dashboards
833-
.entry(dashboards_path)
834-
.or_default()
835-
.extend(dashboard_bytes);
836-
}
837-
Ok(dashboards)
838-
}
839-
840804
///fetch all correlations stored in object store
841805
/// return the correlation file path and all correlation json bytes for each file path
842806
async fn get_all_correlations(

0 commit comments

Comments
 (0)