use crate::run::Run;
use builder_pattern::Builder;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
pub struct ListArtifacts {
#[into]
pub run_id: String,
#[default(None)]
pub path: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListedArtifacts {
pub root_uri: String,
#[serde(default)]
pub files: Vec<FileInfo>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FileInfo {
pub path: String,
pub is_dir: bool,
#[serde(default)]
pub file_size: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
pub struct Artifact {
#[into]
pub experiment_id: String,
#[into]
pub run_id: String,
#[into]
pub path: PathBuf,
}
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
pub struct UploadArtifact {
pub local: PathBuf,
pub remote: Artifact,
}
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
pub struct DownloadRunArtifacts {
#[into]
pub experiment_id: String,
#[into]
pub run_id: String,
#[into]
pub files: Vec<ArtifactFile>,
#[into]
pub destination: PathBuf,
}
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
pub struct ArtifactFile {
#[into]
pub path: String,
#[into]
pub size: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
pub struct DownloadRunArtifact {
#[into]
pub experiment_id: String,
#[into]
pub run_id: String,
#[into]
pub file: String,
#[into]
pub destination: PathBuf,
#[into]
pub expected_size: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
pub struct RunArtifacts {
#[into]
pub experiment_id: String,
#[into]
pub run_id: String,
#[into]
pub paths: Vec<PathBuf>,
#[into]
pub root: PathBuf,
}
impl DownloadRunArtifact {
pub fn path(&self) -> PathBuf {
if !self.destination.ends_with(&self.file) {
self.destination.join(&self.file)
} else {
self.destination.clone()
}
}
}
impl RunArtifacts {
pub fn get_file(&self, name: impl AsRef<str>) -> Option<PathBuf> {
for path in &self.paths {
if path.ends_with(name.as_ref()) {
return Some(path.clone());
}
}
None
}
}
impl DownloadRunArtifacts {
pub fn as_single_downloads(self) -> Vec<DownloadRunArtifact> {
let mut downloads = Vec::with_capacity(self.files.len());
for file in self.files {
let download = DownloadRunArtifact {
experiment_id: self.experiment_id.clone(),
run_id: self.run_id.clone(),
file: file.path,
destination: self.destination.clone(),
expected_size: file.size,
};
downloads.push(download);
}
downloads
}
pub fn new_from_run(destination: impl AsRef<Path>, run: Run, list: ListedArtifacts) -> Self {
Self {
destination: destination.as_ref().to_owned(),
run_id: run.info.run_id,
experiment_id: run.info.experiment_id,
files: list
.files
.into_iter()
.map(|file| ArtifactFile {
path: file.path,
size: file.file_size,
})
.collect(),
}
}
}