From 1ef7f6568e98d0c20457e869f37be9481b02c106 Mon Sep 17 00:00:00 2001 From: Bryan Van de Ven Date: Fri, 27 Oct 2023 13:19:32 -0700 Subject: [PATCH] Plumbing for source locator (#36) --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/app.rs | 19 ++++++++++++++++++- src/data.rs | 10 ++++++++++ src/deferred_data.rs | 16 +++++++++++++++- src/file_data.rs | 8 +++++++- src/http/client.rs | 10 +++++++++- src/main.rs | 11 ++++++++--- src/merge_data.rs | 14 ++++++++++++-- src/parallel_data.rs | 7 ++++++- 10 files changed, 96 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dbadf75..d6834d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1725,6 +1725,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.6" @@ -1802,6 +1811,7 @@ dependencies = [ "egui_extras", "env_logger", "getrandom", + "itertools", "log", "percentage", "rand", diff --git a/Cargo.toml b/Cargo.toml index e9984ca..3795f0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ rand = { version = "0.8" } # transitive depedency, required for rand to support wasm getrandom = { version = "0.2", features = ["js"] } +itertools = "0.11.0" percentage = "0.1.0" regex = "1.10.0" diff --git a/src/app.rs b/src/app.rs index 8895805..ab68c3c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -8,6 +8,8 @@ use egui::{ Align2, Color32, NumExt, Pos2, Rect, RichText, ScrollArea, Slider, Stroke, TextStyle, Vec2, }; use egui_extras::{Column, TableBuilder}; +#[cfg(not(target_arch = "wasm32"))] +use itertools::Itertools; use percentage::{Percentage, PercentageInteger}; use regex::{escape, Regex}; use serde::{Deserialize, Serialize}; @@ -2542,9 +2544,24 @@ impl UiExtra for egui::Ui { pub fn start(data_sources: Vec>) { env_logger::try_init().unwrap_or(()); // Log to stderr (if you run with `RUST_LOG=debug`). + let all_locators = data_sources + .iter() + .flat_map(|x| x.fetch_description().source_locator) + .collect::>(); + + let unique_locators = all_locators.into_iter().unique().collect_vec(); + + let locator = match &unique_locators[..] { + [] => "No data source".to_string(), + [x] => x.to_string(), + [x, ..] => format!("{} and {} other sources", x, unique_locators.len() - 1), + }; + + let app_name = format!("{locator} - Legion Prof"); + let native_options = eframe::NativeOptions::default(); eframe::run_native( - "Legion Prof", + &app_name, native_options, Box::new(|cc| Box::new(ProfApp::new(cc, data_sources))), ) diff --git a/src/data.rs b/src/data.rs index 81ea80d..9b2fbce 100644 --- a/src/data.rs +++ b/src/data.rs @@ -206,7 +206,13 @@ pub struct SlotMetaTile { pub data: SlotMetaTileData, } +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct DataSourceDescription { + pub source_locator: Vec, +} + pub trait DataSource { + fn fetch_description(&self) -> DataSourceDescription; fn fetch_info(&self) -> DataSourceInfo; fn fetch_summary_tile(&self, entry_id: &EntryID, tile_id: TileID, full: bool) -> SummaryTile; fn fetch_slot_tile(&self, entry_id: &EntryID, tile_id: TileID, full: bool) -> SlotTile; @@ -215,6 +221,7 @@ pub trait DataSource { } pub trait DataSourceMut { + fn fetch_description(&self) -> DataSourceDescription; fn fetch_info(&mut self) -> DataSourceInfo; fn fetch_summary_tile( &mut self, @@ -232,6 +239,9 @@ pub trait DataSourceMut { } impl DataSourceMut for T { + fn fetch_description(&self) -> DataSourceDescription { + DataSource::fetch_description(self) + } fn fetch_info(&mut self) -> DataSourceInfo { DataSource::fetch_info(self) } diff --git a/src/deferred_data.rs b/src/deferred_data.rs index 361e578..d2fa083 100644 --- a/src/deferred_data.rs +++ b/src/deferred_data.rs @@ -1,8 +1,10 @@ use crate::data::{ - DataSourceInfo, DataSourceMut, EntryID, SlotMetaTile, SlotTile, SummaryTile, TileID, + DataSourceDescription, DataSourceInfo, DataSourceMut, EntryID, SlotMetaTile, SlotTile, + SummaryTile, TileID, }; pub trait DeferredDataSource { + fn fetch_description(&self) -> DataSourceDescription; fn fetch_info(&mut self); fn get_infos(&mut self) -> Vec; fn fetch_summary_tile(&mut self, entry_id: &EntryID, tile_id: TileID, full: bool); @@ -34,6 +36,10 @@ impl DeferredDataSourceWrapper { } impl DeferredDataSource for DeferredDataSourceWrapper { + fn fetch_description(&self) -> DataSourceDescription { + self.data_source.fetch_description() + } + fn fetch_info(&mut self) { self.infos.push(self.data_source.fetch_info()); } @@ -102,6 +108,10 @@ impl CountingDeferredDataSource { } impl DeferredDataSource for CountingDeferredDataSource { + fn fetch_description(&self) -> DataSourceDescription { + self.data_source.fetch_description() + } + fn fetch_info(&mut self) { self.start_request(); self.data_source.fetch_info() @@ -145,6 +155,10 @@ impl DeferredDataSource for CountingDeferredDataSource } impl DeferredDataSource for Box { + fn fetch_description(&self) -> DataSourceDescription { + self.as_ref().fetch_description() + } + fn fetch_info(&mut self) { self.as_mut().fetch_info() } diff --git a/src/file_data.rs b/src/file_data.rs index af680b2..2df2e4c 100644 --- a/src/file_data.rs +++ b/src/file_data.rs @@ -4,7 +4,8 @@ use std::path::{Path, PathBuf}; use serde::Deserialize; use crate::data::{ - DataSource, DataSourceInfo, EntryID, SlotMetaTile, SlotTile, SummaryTile, TileID, + DataSource, DataSourceDescription, DataSourceInfo, EntryID, SlotMetaTile, SlotTile, + SummaryTile, TileID, }; use crate::http::schema::TileRequestRef; @@ -30,6 +31,11 @@ impl FileDataSource { } impl DataSource for FileDataSource { + fn fetch_description(&self) -> DataSourceDescription { + DataSourceDescription { + source_locator: vec![String::from(self.basedir.to_string_lossy())], + } + } fn fetch_info(&self) -> DataSourceInfo { let path = self.basedir.join("info"); self.read_file::(&path) diff --git a/src/http/client.rs b/src/http/client.rs index 5754284..7f6c59f 100644 --- a/src/http/client.rs +++ b/src/http/client.rs @@ -13,7 +13,9 @@ use serde::Deserialize; use url::Url; -use crate::data::{DataSourceInfo, EntryID, SlotMetaTile, SlotTile, SummaryTile, TileID}; +use crate::data::{ + DataSourceDescription, DataSourceInfo, EntryID, SlotMetaTile, SlotTile, SummaryTile, TileID, +}; use crate::deferred_data::DeferredDataSource; use crate::http::fetch::{fetch, DataSourceResponse}; use crate::http::schema::TileRequestRef; @@ -62,6 +64,12 @@ impl HTTPClientDataSource { } impl DeferredDataSource for HTTPClientDataSource { + fn fetch_description(&self) -> DataSourceDescription { + DataSourceDescription { + source_locator: vec![self.baseurl.to_string()], + } + } + fn fetch_info(&mut self) { let url = self.baseurl.join("info").expect("invalid baseurl"); self.request::(url, self.infos.clone()); diff --git a/src/main.rs b/src/main.rs index 690a47a..80adac1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,9 @@ use rand::Rng; use std::collections::BTreeMap; use legion_prof_viewer::data::{ - DataSourceInfo, DataSourceMut, EntryID, EntryInfo, Field, FieldID, FieldSchema, Item, ItemMeta, - ItemUID, SlotMetaTile, SlotMetaTileData, SlotTile, SlotTileData, SummaryTile, SummaryTileData, - TileID, TileSet, UtilPoint, + DataSourceDescription, DataSourceInfo, DataSourceMut, EntryID, EntryInfo, Field, FieldID, + FieldSchema, Item, ItemMeta, ItemUID, SlotMetaTile, SlotMetaTileData, SlotTile, SlotTileData, + SummaryTile, SummaryTileData, TileID, TileSet, UtilPoint, }; #[cfg(not(target_arch = "wasm32"))] @@ -268,6 +268,11 @@ impl RandomDataSource { } impl DataSourceMut for RandomDataSource { + fn fetch_description(&self) -> DataSourceDescription { + DataSourceDescription { + source_locator: vec!["Random Data Source".to_string()], + } + } fn fetch_info(&mut self) -> DataSourceInfo { self.info.clone() } diff --git a/src/merge_data.rs b/src/merge_data.rs index ded23bc..bc02ceb 100644 --- a/src/merge_data.rs +++ b/src/merge_data.rs @@ -1,8 +1,8 @@ use std::collections::VecDeque; use crate::data::{ - DataSourceInfo, EntryID, EntryIndex, EntryInfo, Field, ItemLink, ItemUID, SlotMetaTile, - SlotTile, SummaryTile, TileID, + DataSourceDescription, DataSourceInfo, EntryID, EntryIndex, EntryInfo, Field, ItemLink, + ItemUID, SlotMetaTile, SlotTile, SummaryTile, TileID, }; use crate::deferred_data::DeferredDataSource; use crate::timestamp::Interval; @@ -192,6 +192,16 @@ impl MergeDeferredDataSource { } impl DeferredDataSource for MergeDeferredDataSource { + fn fetch_description(&self) -> DataSourceDescription { + DataSourceDescription { + source_locator: self + .data_sources + .iter() + .flat_map(|x| x.fetch_description().source_locator) + .collect::>(), + } + } + fn fetch_info(&mut self) { for data_source in &mut self.data_sources { data_source.fetch_info(); diff --git a/src/parallel_data.rs b/src/parallel_data.rs index 248f6ad..870bce9 100644 --- a/src/parallel_data.rs +++ b/src/parallel_data.rs @@ -1,7 +1,8 @@ use std::sync::{Arc, Mutex}; use crate::data::{ - DataSource, DataSourceInfo, EntryID, SlotMetaTile, SlotTile, SummaryTile, TileID, + DataSource, DataSourceDescription, DataSourceInfo, EntryID, SlotMetaTile, SlotTile, + SummaryTile, TileID, }; use crate::deferred_data::DeferredDataSource; @@ -26,6 +27,10 @@ impl ParallelDeferredDataSource { } impl DeferredDataSource for ParallelDeferredDataSource { + fn fetch_description(&self) -> DataSourceDescription { + self.data_source.fetch_description() + } + fn fetch_info(&mut self) { let data_source = self.data_source.clone(); let infos = self.infos.clone();