use crate::{
artifact::{RunManifest, StorageLocation},
storage::Store,
};
use serde::{Deserialize, Serialize};
use std::{io, path::Path};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RunRecord {
pub run_id: String,
pub kind: String,
pub outcome: crate::protocol::Outcome,
pub scientific_identity: String,
pub sealed: bool,
pub capture_complete: bool,
}
impl From<&RunManifest> for RunRecord {
fn from(m: &RunManifest) -> Self {
Self {
run_id: m.run_id.clone(),
kind: m.kind.clone(),
outcome: m.outcome,
scientific_identity: m.scientific_identity.clone(),
sealed: !m.removed.is_empty(),
capture_complete: m.capture_complete,
}
}
}
pub(crate) fn index_manifest(
store: &Store,
manifest: &RunManifest,
workspace: &Path,
) -> io::Result<()> {
let mut record = RunRecord::from(manifest);
record.sealed = store
.json::<crate::seal::SealReport>(&format!("{}/seal.json", manifest.run_id))
.is_ok();
store.put_json(
&format!("registry/{}.json", manifest.run_id),
&record,
workspace,
false,
)
}
pub fn query(location: &StorageLocation) -> io::Result<Vec<RunRecord>> {
let store = Store::open(location)?;
let mut out = Vec::new();
for key in store.backend.list("registry/")? {
out.push(store.json(&key)?);
}
out.sort_by(|a: &RunRecord, b| a.run_id.cmp(&b.run_id));
Ok(out)
}
pub fn rebuild(location: &StorageLocation, workspace: &Path) -> io::Result<Vec<RunRecord>> {
let store = Store::open(location)?;
let mut out = Vec::new();
for key in store.backend.list("")? {
if key.ends_with("/manifest.json") && key.split('/').count() == 2 {
let manifest: RunManifest = store.json(&key)?;
if manifest.format != "scientific-workflow-run.v1" {
continue;
}
index_manifest(&store, &manifest, workspace)?;
out.push(store.json(&format!("registry/{}.json", manifest.run_id))?);
}
}
Ok(out)
}