Skip to content

Commit

Permalink
feat(kubegraph): add basic fake connector support
Browse files Browse the repository at this point in the history
  • Loading branch information
HoKim98 committed May 23, 2024
1 parent 9bd0b2d commit 4c4ca69
Show file tree
Hide file tree
Showing 19 changed files with 606 additions and 57 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ members = [
"crates/kiss/operator",
"crates/kubegraph/analyzer/llm",
"crates/kubegraph/api",
"crates/kubegraph/connector/fake",
"crates/kubegraph/connector/local",
"crates/kubegraph/connector/prometheus",
"crates/kubegraph/connector/simulation",
"crates/kubegraph/function/dummy",
"crates/kubegraph/gateway",
"crates/kubegraph/graph/local",
Expand Down
7 changes: 4 additions & 3 deletions crates/kubegraph/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ workspace = true

[features]
default = ["minimal"]
minimal = ["connector-simulation", "df-polars", "function-dummy"]
minimal = ["connector-fake", "connector-local", "df-polars", "function-dummy"]
full = ["analyzer-full", "connector-full", "df-full", "function-full"]

# Analyzers
analyzer-full = ["analyzer-llm"]
analyzer-llm = []

# Connectors
connector-full = ["connector-prometheus", "connector-simulation"]
connector-full = ["connector-fake", "connector-local", "connector-prometheus"]
connector-fake = []
connector-local = []
connector-prometheus = []
connector-simulation = []

# DataFrame
df-full = ["df-polars"]
Expand Down
175 changes: 175 additions & 0 deletions crates/kubegraph/api/src/connector/fake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use std::collections::BTreeMap;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnectorFakeSpec {
#[serde(default)]
pub edges: Option<NetworkConnectorFakeData>,
#[serde(default)]
pub nodes: Option<NetworkConnectorFakeData>,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnectorFakeData {
#[serde(default = "NetworkConnectorFakeData::default_count")]
#[validate(range(min = 1))]
pub count: usize,
#[serde(default)]
pub frame: NetworkConnectorFakeDataFrame,
}

impl NetworkConnectorFakeData {
const fn default_count() -> usize {
1
}
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(transparent, rename_all = "camelCase")]
pub struct NetworkConnectorFakeDataFrame {
pub map: BTreeMap<String, NetworkConnectorFakeDataModel>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum NetworkConnectorFakeDataModel {
Constant(#[serde(default)] self::model::ConstantModel),
Name(#[serde(default)] self::model::NameModel),
Normal(#[serde(default)] self::model::NormalModel),
}

impl NetworkConnectorFakeDataModel {
const fn default_mean() -> f64 {
0.0
}

const fn default_prefix() -> Option<String> {
None
}

const fn default_seed() -> Option<i64> {
None
}

const fn default_std() -> f64 {
1.0
}

const fn default_value() -> f64 {
0.0
}
}

mod impl_json_schema_for_fake_data_model {
use std::borrow::Cow;

use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};

#[allow(dead_code)]
#[derive(JsonSchema)]
enum NetworkConnectorFakeDataModelType {
Constant,
Name,
Normal,
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[serde(rename_all = "camelCase")]
struct NetworkConnectorFakeDataModel {
#[serde(default = "super::NetworkConnectorFakeDataModel::default_mean")]
mean: f64,
#[serde(default = "super::NetworkConnectorFakeDataModel::default_prefix")]
#[validate(length(min = 1, max = 32))]
prefix: Option<String>,
r#type: NetworkConnectorFakeDataModelType,
#[serde(default = "super::NetworkConnectorFakeDataModel::default_seed")]
seed: Option<i64>,
#[serde(default = "super::NetworkConnectorFakeDataModel::default_std")]
#[validate(range(min = 0.0))]
std: f64,
#[serde(default = "super::NetworkConnectorFakeDataModel::default_value")]
value: f64,
#[serde(default)]
value_type: super::NetworkConnectorFakeDataValueType,
}

impl JsonSchema for super::NetworkConnectorFakeDataModel {
#[inline]
fn is_referenceable() -> bool {
<NetworkConnectorFakeDataModel as JsonSchema>::is_referenceable()
}

#[inline]
fn schema_name() -> String {
<NetworkConnectorFakeDataModel as JsonSchema>::schema_name()
}

#[inline]
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
<NetworkConnectorFakeDataModel as JsonSchema>::json_schema(gen)
}

#[inline]
fn schema_id() -> Cow<'static, str> {
<NetworkConnectorFakeDataModel as JsonSchema>::schema_id()
}
}
}

pub mod model {
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ConstantModel {
#[serde(default = "super::NetworkConnectorFakeDataModel::default_value")]
pub value: f64,
#[serde(default)]
pub value_type: super::NetworkConnectorFakeDataValueType,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NameModel {
#[serde(default = "super::NetworkConnectorFakeDataModel::default_prefix")]
#[validate(length(min = 1, max = 32))]
pub prefix: Option<String>,
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct NormalModel {
#[serde(default = "super::NetworkConnectorFakeDataModel::default_mean")]
pub mean: f64,
#[serde(default = "super::NetworkConnectorFakeDataModel::default_seed")]
pub seed: Option<i64>,
#[serde(default = "super::NetworkConnectorFakeDataModel::default_std")]
#[validate(range(min = 0.0))]
pub std: f64,
#[serde(default)]
pub value_type: super::NetworkConnectorFakeDataValueType,
}
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum NetworkConnectorFakeDataValueType {
#[default]
F64,
I64,
}

#[cfg(feature = "df-polars")]
impl From<NetworkConnectorFakeDataValueType> for ::pl::datatypes::DataType {
fn from(value: NetworkConnectorFakeDataValueType) -> Self {
match value {
NetworkConnectorFakeDataValueType::F64 => Self::Float64,
NetworkConnectorFakeDataValueType::I64 => Self::Int64,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use serde::{Deserialize, Serialize};
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct NetworkConnectorSimulationSpec {
pub struct NetworkConnectorLocalSpec {
pub path: PathBuf,

#[serde(default = "NetworkConnectorSimulationSpec::default_key_edges")]
#[serde(default = "NetworkConnectorLocalSpec::default_key_edges")]
pub key_edges: String,
#[serde(default = "NetworkConnectorSimulationSpec::default_key_nodes")]
#[serde(default = "NetworkConnectorLocalSpec::default_key_nodes")]
pub key_nodes: String,
}

impl NetworkConnectorSimulationSpec {
impl NetworkConnectorLocalSpec {
fn default_key_edges() -> String {
"edges.csv".into()
}
Expand Down
36 changes: 24 additions & 12 deletions crates/kubegraph/api/src/connector/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#[cfg(feature = "connector-fake")]
pub mod fake;
#[cfg(feature = "connector-local")]
pub mod local;
#[cfg(feature = "connector-prometheus")]
pub mod prometheus;
#[cfg(feature = "connector-simulation")]
pub mod simulation;

use anyhow::Result;
use async_trait::async_trait;
Expand Down Expand Up @@ -131,32 +133,38 @@ impl<M> PartialEq<NetworkConnectorType> for NetworkConnectorSpec<M> {
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub enum NetworkConnectorKind {
#[cfg(feature = "connector-fake")]
Fake(self::fake::NetworkConnectorFakeSpec),
#[cfg(feature = "connector-local")]
Local(self::local::NetworkConnectorLocalSpec),
#[cfg(feature = "connector-prometheus")]
Prometheus(self::prometheus::NetworkConnectorPrometheusSpec),
#[cfg(feature = "connector-simulation")]
Simulation(self::simulation::NetworkConnectorSimulationSpec),
}

impl NetworkConnectorKind {
fn name(&self) -> String {
match self {
#[cfg(feature = "connector-fake")]
Self::Fake(_) => NetworkConnectorType::Fake.name().into(),
#[cfg(feature = "connector-local")]
Self::Local(_) => NetworkConnectorType::Local.name().into(),
#[cfg(feature = "connector-prometheus")]
Self::Prometheus(spec) => format!(
"{type}/{spec}",
type = NetworkConnectorType::Prometheus.name(),
spec = spec.name(),
),
#[cfg(feature = "connector-simulation")]
Self::Simulation(_) => NetworkConnectorType::Simulation.name().into(),
}
}

const fn to_ref(&self) -> NetworkConnectorType {
match self {
#[cfg(feature = "connector-fake")]
Self::Fake(_) => NetworkConnectorType::Fake,
#[cfg(feature = "connector-local")]
Self::Local(_) => NetworkConnectorType::Local,
#[cfg(feature = "connector-prometheus")]
Self::Prometheus(_) => NetworkConnectorType::Prometheus,
#[cfg(feature = "connector-simulation")]
Self::Simulation(_) => NetworkConnectorType::Simulation,
}
}
}
Expand All @@ -173,19 +181,23 @@ impl PartialEq<NetworkConnectorType> for NetworkConnectorKind {
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum NetworkConnectorType {
#[cfg(feature = "connector-fake")]
Fake,
#[cfg(feature = "connector-local")]
Local,
#[cfg(feature = "connector-prometheus")]
Prometheus,
#[cfg(feature = "connector-simulation")]
Simulation,
}

impl NetworkConnectorType {
pub const fn name(&self) -> &'static str {
match self {
#[cfg(feature = "connector-fake")]
Self::Fake => "fake",
#[cfg(feature = "connector-local")]
Self::Local => "local",
#[cfg(feature = "connector-prometheus")]
Self::Prometheus => "prometheus",
#[cfg(feature = "connector-simulation")]
Self::Simulation => "simulation",
}
}
}
13 changes: 10 additions & 3 deletions crates/kubegraph/api/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,6 @@ impl GraphMetadataExt for GraphMetadata {
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphMetadataRaw {
#[serde(default, flatten)]
pub extras: BTreeMap<String, String>,
Expand Down Expand Up @@ -476,7 +475,7 @@ mod impl_json_schema_for_graph_metadata_raw {
#[allow(dead_code)]
#[derive(JsonSchema)]
#[serde(transparent)]
struct GraphMetadataRaw(BTreeMap<String, String>);
struct GraphMetadataRaw(#[validate(inner(length(min = 1)))] BTreeMap<String, String>);

impl JsonSchema for super::GraphMetadataRaw {
#[inline]
Expand Down Expand Up @@ -572,25 +571,33 @@ where
#[derive(
Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub struct GraphMetadataPinned {
#[serde(default = "GraphMetadataPinned::default_capacity")]
#[validate(length(min = 1))]
pub capacity: String,
#[serde(default = "GraphMetadataPinned::default_flow")]
#[validate(length(min = 1))]
pub flow: String,
#[serde(default = "GraphMetadataPinned::default_function")]
#[validate(length(min = 1))]
pub function: String,
#[serde(default = "GraphMetadataPinned::default_interval_ms", rename = "le")]
#[validate(length(min = 1))]
pub interval_ms: String,
#[serde(default = "GraphMetadataPinned::default_name")]
#[validate(length(min = 1))]
pub name: String,
#[serde(default = "GraphMetadataPinned::default_sink")]
#[validate(length(min = 1))]
pub sink: String,
#[serde(default = "GraphMetadataPinned::default_src")]
#[validate(length(min = 1))]
pub src: String,
#[serde(default = "GraphMetadataPinned::default_supply")]
#[validate(length(min = 1))]
pub supply: String,
#[serde(default = "GraphMetadataPinned::default_unit_cost")]
#[validate(length(min = 1))]
pub unit_cost: String,
}

Expand Down
Loading

0 comments on commit 4c4ca69

Please sign in to comment.