use std::collections::{BTreeMap, BTreeSet};
use crate::naming::models::Stream;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoadPath {
Streaming,
StageInsertFiles,
StageCopy,
S3AutoIngest,
S3InsertFiles,
S3Copy,
Zerobus,
VolumeCopy,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ObjectKind {
Table,
Stage,
Pipe,
Volume,
}
impl std::fmt::Display for ObjectKind {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Table => "table",
Self::Stage => "stage",
Self::Pipe => "pipe",
Self::Volume => "volume",
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ObjectState {
Exists,
Missing,
Incompatible(Vec<String>),
}
impl ObjectState {
pub fn marker(&self) -> &'static str {
match self {
Self::Exists => "=",
Self::Missing => "+",
Self::Incompatible(_) => "!",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlannedObject {
pub kind: ObjectKind,
pub name: String,
pub state: ObjectState,
pub create_sql: String,
}
impl PlannedObject {
pub fn problems(&self) -> Vec<String> {
let ObjectState::Incompatible(reasons) = &self.state else {
return Vec::new();
};
let prefix = format!("{} {}", self.kind, self.name);
reasons
.iter()
.map(|reason| format!("{prefix}: {reason}"))
.collect()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamPlan {
pub stream: Stream,
pub load_path: LoadPath,
pub objects: Vec<PlannedObject>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Plan {
pub streams: Vec<StreamPlan>,
}
impl Plan {
pub fn problems(&self) -> Vec<String> {
self.objects().flat_map(PlannedObject::problems).collect()
}
pub fn missing(&self) -> Vec<&PlannedObject> {
self.objects()
.filter(|object| object.state == ObjectState::Missing)
.collect()
}
fn objects(&self) -> impl Iterator<Item = &PlannedObject> {
self.streams.iter().flat_map(|stream| stream.objects.iter())
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ExistingObjects {
pub tables: BTreeMap<String, BTreeMap<String, String>>,
pub stages: BTreeSet<String>,
pub pipes: BTreeMap<String, Option<String>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StagePipe<'a> {
pub pipe: &'a str,
pub table: &'a str,
pub source: &'a str,
pub auto_ingest: bool,
}