Skip to content

Commit

Permalink
Move summarize function to runtime config crate
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Sep 4, 2024
1 parent 7149cf0 commit 786e803
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 119 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/runtime-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rust-version.workspace = true

[dependencies]
anyhow = { workspace = true }
spin-common = { path = "../common" }
spin-factor-key-value = { path = "../factor-key-value" }
spin-key-value-azure = { path = "../key-value-azure" }
spin-key-value-redis = { path = "../key-value-redis" }
Expand Down
40 changes: 37 additions & 3 deletions crates/runtime-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::{Path, PathBuf};

use anyhow::Context as _;
use spin_common::ui::quoted_path;
use spin_factor_key_value::runtime_config::spin::{self as key_value};
use spin_factor_key_value::{DefaultLabelResolver as _, KeyValueFactor};
use spin_factor_llm::{spin as llm, LlmFactor};
Expand All @@ -19,9 +20,9 @@ use spin_factors::{
runtime_config::toml::TomlKeyTracker, FactorRuntimeConfigSource, RuntimeConfigSourceFinalizer,
};
use spin_key_value_spin::{SpinKeyValueRuntimeConfig, SpinKeyValueStore};
use spin_runtime_factors::TriggerFactorsRuntimeConfig;
use spin_sqlite as sqlite;
use spin_trigger::cli::UserProvidedPath;
use toml::Value;

/// The default state directory for the trigger.
pub const DEFAULT_STATE_DIR: &str = ".spin";
Expand All @@ -48,6 +49,41 @@ pub struct ResolvedRuntimeConfig<T> {
pub toml: toml::Table,
}

impl<T> ResolvedRuntimeConfig<T> {
pub fn summarize(&self, runtime_config_path: Option<&Path>) {
let summarize_labeled_typed_tables = |key| {
let mut summaries = vec![];
if let Some(tables) = self.toml.get(key).and_then(Value::as_table) {
for (label, config) in tables {
if let Some(ty) = config.get("type").and_then(Value::as_str) {
summaries.push(format!("[{key}.{label}: {ty}]"))
}
}
}
summaries
};

let mut summaries = vec![];
// [key_value_store.<label>: <type>]
summaries.extend(summarize_labeled_typed_tables("key_value_store"));
// [sqlite_database.<label>: <type>]
summaries.extend(summarize_labeled_typed_tables("sqlite_database"));
// [llm_compute: <type>]
if let Some(table) = self.toml.get("llm_compute").and_then(Value::as_table) {
if let Some(ty) = table.get("type").and_then(Value::as_str) {
summaries.push(format!("[llm_compute: {ty}"));
}
}
if !summaries.is_empty() {
let summaries = summaries.join(", ");
let from_path = runtime_config_path
.map(|path| format!("from {}", quoted_path(path)))
.unwrap_or_default();
println!("Using runtime config {summaries} {from_path}");
}
}
}

impl<T> ResolvedRuntimeConfig<T>
where
T: for<'a, 'b> TryFrom<TomlRuntimeConfigSource<'a, 'b>>,
Expand Down Expand Up @@ -95,8 +131,6 @@ where
let runtime_config_dir = runtime_config_path
.and_then(Path::parent)
.map(ToOwned::to_owned);
let toml_resolver =
TomlResolver::new(&toml, local_app_dir, provided_state_dir, provided_log_dir);
let tls_resolver = runtime_config_dir.clone().map(SpinTlsRuntimeConfig::new);
let key_value_config_resolver =
key_value_config_resolver(runtime_config_dir, toml_resolver.state_dir()?);
Expand Down
3 changes: 1 addition & 2 deletions crates/runtime-factors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ spin-factor-wasi = { path = "../factor-wasi" }
spin-factors = { path = "../factors" }
spin-factors-executor = { path = "../factors-executor" }
spin-runtime-config = { path = "../runtime-config" }
spin-trigger = { path = "../trigger" }
spin-telemetry = { path = "../telemetry" }
spin-trigger = { path = "../trigger" }
terminal = { path = "../terminal" }
toml = "0.8"
tracing = { workspace = true }

[lints]
Expand Down
43 changes: 2 additions & 41 deletions crates/runtime-factors/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use super::{TriggerAppArgs, TriggerFactors, TriggerFactorsRuntimeConfig};

use anyhow::Context as _;
use spin_common::ui::quoted_path;
use spin_factors_executor::FactorsExecutor;
use spin_runtime_config::ResolvedRuntimeConfig;
use spin_trigger::cli::{
FactorsConfig, InitialKvSetterHook, KeyValueDefaultStoreSummaryHook, RuntimeFactorsBuilder,
SqlStatementExecutorHook, SqliteDefaultStoreSummaryHook, StdioLoggingExecutorHooks,
};
use toml::Value;

/// A [`RuntimeFactorsBuilder`] for [`TriggerFactors`].
pub struct FactorsBuilder;
Expand All @@ -34,7 +32,7 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
use_gpu,
)?;

summarize_runtime_config(&runtime_config, config.runtime_config_file.as_deref());
runtime_config.summarize(config.runtime_config_file.as_deref());

let factors = TriggerFactors::new(
runtime_config.state_dir(),
Expand Down Expand Up @@ -67,40 +65,3 @@ impl RuntimeFactorsBuilder for FactorsBuilder {
Ok(())
}
}

fn summarize_runtime_config<T>(
runtime_config: &ResolvedRuntimeConfig<T>,
runtime_config_path: Option<&Path>,
) {
let toml = &runtime_config.toml;
let summarize_labeled_typed_tables = |key| {
let mut summaries = vec![];
if let Some(tables) = toml.get(key).and_then(Value::as_table) {
for (label, config) in tables {
if let Some(ty) = config.get("type").and_then(Value::as_str) {
summaries.push(format!("[{key}.{label}: {ty}]"))
}
}
}
summaries
};

let mut summaries = vec![];
// [key_value_store.<label>: <type>]
summaries.extend(summarize_labeled_typed_tables("key_value_store"));
// [sqlite_database.<label>: <type>]
summaries.extend(summarize_labeled_typed_tables("sqlite_database"));
// [llm_compute: <type>]
if let Some(table) = toml.get("llm_compute").and_then(Value::as_table) {
if let Some(ty) = table.get("type").and_then(Value::as_str) {
summaries.push(format!("[llm_compute: {ty}"));
}
}
if !summaries.is_empty() {
let summaries = summaries.join(", ");
let from_path = runtime_config_path
.map(|path| format!("from {}", quoted_path(path)))
.unwrap_or_default();
println!("Using runtime config {summaries} {from_path}");
}
}
2 changes: 1 addition & 1 deletion crates/trigger-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
spin-app = { path = "../app" }
spin-core = { path = "../core" }
spin-factors = { path = "../factors" }
spin-factor-outbound-http = { path = "../factor-outbound-http" }
spin-factor-wasi = { path = "../factor-wasi" }
spin-factors = { path = "../factors" }
spin-http = { path = "../http" }
spin-factor-outbound-networking = { path = "../factor-outbound-networking" }
spin-telemetry = { path = "../telemetry" }
Expand Down
2 changes: 1 addition & 1 deletion crates/trigger-redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ async-trait = "0.1"
futures = "0.3"
redis = { version = "0.26.1", features = ["tokio-comp"] }
serde = "1.0.188"
spin-factors = { path = "../factors" }
spin-factor-variables = { path = "../factor-variables" }
spin-factors = { path = "../factors" }
spin-telemetry = { path = "../telemetry" }
spin-trigger = { path = "../trigger" }
spin-world = { path = "../world" }
Expand Down
70 changes: 1 addition & 69 deletions examples/spin-timer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/spin-timer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ anyhow = "1.0.68"
clap = { version = "3.1.15", features = ["derive", "env"] }
futures = "0.3.25"
serde = "1.0.188"
spin-factors = { path = "../../crates/factors" }
spin-runtime-factors = { path = "../../crates/runtime-factors" }
spin-trigger = { path = "../../crates/trigger" }
spin-factors = { path = "../../crates/factors" }
tokio = { version = "1.11", features = ["full"] }
tokio-scoped = "0.2.0"
wasmtime = "22.0.0"
Expand Down

0 comments on commit 786e803

Please sign in to comment.