use serde_json::Value;
use super::client::{enc, Client};
use super::fmt::*;
use super::{Fail, Out, Parsed};
fn owner_name(v: &Value) -> String {
format!("{}/{}", st(v, "owner_handle"), st(v, "name"))
}
fn meta(v: &Value) -> &Value {
v.pointer("/latest_version/meta").unwrap_or(&Value::Null)
}
fn params(v: &Value) -> String {
match meta(v).get("params_b").and_then(Value::as_f64) {
Some(b) if b >= 1.0 => format!("{b:.1}B"),
Some(b) => format!("{:.0}M", b * 1000.0),
None => "-".into(),
}
}
pub fn model_ref(c: &Client, r: &str) -> Result<String, Fail> {
resolve_ref(c, "/api/models/resolve", "model_id", r)
}
fn resolve_ref(c: &Client, path: &str, id_key: &str, r: &str) -> Result<String, Fail> {
let named = r.starts_with("zc://") || r.contains('/');
if !named {
return Ok(r.to_string());
}
let full = match r.starts_with("zc://") {
true => r.to_string(),
false => format!("zc://{r}"),
};
let v = c.get_q(path, &[("ref", &full)])?;
v.get(id_key)
.and_then(Value::as_str)
.map(str::to_string)
.ok_or_else(|| Fail::Usage(format!("the hub resolved {r} to no {id_key}")))
}
pub fn model_matches(m: &Value, query: &str) -> bool {
let tags = meta(m)
.get("tags")
.and_then(Value::as_array)
.map(|t| {
t.iter()
.filter_map(Value::as_str)
.collect::<Vec<_>>()
.join(" ")
})
.unwrap_or_default();
let hay = format!(
"{} {} {} {} {} {tags}",
owner_name(m),
st(meta(m), "pipeline_tag"),
st(m, "license"),
st(meta(m), "library_name"),
st(meta(m), "architecture"),
)
.to_lowercase();
query
.split_whitespace()
.all(|w| hay.contains(&w.to_lowercase()))
}
pub fn models_table(v: &Value, query: &str, limit: usize) -> Doc {
let all: Vec<&Value> = items(v, "models")
.iter()
.filter(|m| query.is_empty() || model_matches(m, query))
.collect();
let mut t = Table::new(&["ID", "MODEL", "TASK", "PARAMS", "LICENSE", "VIS", "CREATED"]);
for m in all.iter().take(limit) {
let lic = match st(m, "license").as_str() {
"-" => st(meta(m), "license"),
l => l.to_string(),
};
t.rows.push(vec![
st(m, "id"),
clip(&owner_name(m), 48),
st(meta(m), "pipeline_tag"),
params(m),
lic,
st(m, "visibility"),
when(m, "created_at")[..10.min(when(m, "created_at").len())].to_string(),
]);
}
let doc = Doc::table(t);
match all.len() > limit {
true => doc.with_note(format!(
"{} more; narrow the search or pass --limit N",
all.len() - limit
)),
false => doc,
}
}
pub fn model_doc(m: &Value) -> Doc {
let mt = meta(m);
let mut d = Doc::kv(vec![
("id", st(m, "id")),
("model", owner_name(m)),
("visibility", st(m, "visibility")),
("license", st(m, "license")),
("task", st(mt, "pipeline_tag")),
("architecture", st(mt, "architecture")),
("library", st(mt, "library_name")),
("params", params(m)),
("downloads", st(mt, "downloads")),
("likes", st(mt, "likes")),
("source", st(mt, "hf_repo")),
("created", when(m, "created_at")),
(
"latest",
st(m.get("latest_version").unwrap_or(&Value::Null), "digest"),
),
]);
d = d.with_note(format!(
"{} version(s); zc model versions {}",
m.get("versions")
.and_then(Value::as_array)
.map_or(0, Vec::len),
st(m, "id")
));
d
}
pub fn versions_table(m: &Value) -> Doc {
let mut t = Table::new(&["DIGEST", "STATUS", "SIZE", "FILES", "SOURCE", "PUBLISHED"]);
for v in items(&m["versions"], "versions") {
t.rows.push(vec![
clip(&st(v, "digest"), 16),
st(v, "status"),
bytes_of(v, "size_bytes"),
st(v, "file_count"),
st(v, "source"),
when(v, "published_at"),
]);
}
Doc::table(t)
}
pub fn activity_doc(a: &Value) -> Doc {
let tot = &a["totals"];
let mut t = Table::new(&["DATE", "VIEWS", "DOWNLOADS", "CALLS"]);
for d in items(&a["series"], "series") {
let moved = ["views", "downloads", "calls"]
.iter()
.any(|k| d.get(*k).and_then(Value::as_u64).unwrap_or(0) > 0);
if moved {
t.rows.push(vec![
st(d, "date"),
st(d, "views"),
st(d, "downloads"),
st(d, "calls"),
]);
}
}
Doc::kv(vec![
("days", st(a, "days")),
("views", st(tot, "views")),
("downloads", st(tot, "downloads")),
("calls", st(tot, "calls")),
])
.with_table(t)
}
pub fn model(c: &Client, sub: &str, a: &Parsed) -> Result<Out, Fail> {
match sub {
"ls" | "list" | "search" => {
let query = a.pos.join(" ");
if sub == "search" && query.is_empty() {
return Err(Fail::Usage("usage: zc model search QUERY".into()));
}
let v = c.get("/api/models")?;
let limit = a.limit(50)?;
Ok((v.clone(), models_table(&v, &query, limit)))
}
"show" | "versions" | "activity" => {
let r = a
.pos
.first()
.ok_or_else(|| Fail::Usage(format!("usage: zc model {sub} MODEL")))?;
let id = model_ref(c, r)?;
match sub {
"show" => {
let v = c.get(&format!("/api/models/{}", enc(&id)))?;
Ok((v.clone(), model_doc(&v)))
}
"versions" => {
let v = c.get(&format!("/api/models/{}", enc(&id)))?;
Ok((v["versions"].clone(), versions_table(&v)))
}
_ => {
let days = a.opt("days").unwrap_or("30");
let v = c.get_q(
&format!("/api/models/{}/activity", enc(&id)),
&[("days", days)],
)?;
Ok((v.clone(), activity_doc(&v)))
}
}
}
_ => Err(Fail::Usage(
"usage: zc model ls|search|show|versions|activity".into(),
)),
}
}
pub fn datasets_table(v: &Value, query: &str) -> Doc {
let mut t = Table::new(&[
"ID", "DATASET", "FORMAT", "RECORDS", "SIZE", "VIS", "UPDATED",
]);
for d in items(v, "datasets") {
if !query.is_empty() && !owner_name(d).to_lowercase().contains(&query.to_lowercase()) {
continue;
}
let lv = d.get("latest_version").unwrap_or(&Value::Null);
let records = lv
.pointer("/meta/records")
.and_then(Value::as_u64)
.map(|n| n.to_string())
.unwrap_or_else(|| "-".into());
let format = match st(d, "format").as_str() {
"-" => lv
.pointer("/files/0/path")
.and_then(Value::as_str)
.and_then(|p| p.rsplit_once('.'))
.map(|(_, e)| e.to_string())
.unwrap_or_else(|| "-".into()),
f => f.to_string(),
};
t.rows.push(vec![
st(d, "id"),
clip(&owner_name(d), 48),
format,
records,
bytes_of(lv, "size_bytes"),
st(d, "visibility"),
when(lv, "published_at"),
]);
}
Doc::table(t)
}
pub fn dataset_doc(d: &Value) -> Doc {
let lv = d.get("latest_version").unwrap_or(&Value::Null);
let mut files = Table::new(&["FILE", "SIZE", "SHA256"]);
for f in items(&lv["files"], "files") {
files.rows.push(vec![
st(f, "path"),
bytes_of(f, "size_bytes"),
clip(&st(f, "sha256"), 16),
]);
}
let cols = lv
.pointer("/meta/columns")
.and_then(Value::as_array)
.map(|c| {
c.iter()
.filter_map(Value::as_str)
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
Doc::kv(vec![
("id", st(d, "id")),
("dataset", owner_name(d)),
("visibility", st(d, "visibility")),
("license", st(d, "license")),
(
"records",
lv.pointer("/meta/records")
.map_or("-".into(), |r| r.to_string()),
),
(
"columns",
if cols.is_empty() {
"-".into()
} else {
clip(&cols, 100)
},
),
("size", bytes_of(lv, "size_bytes")),
("version", clip(&st(lv, "digest"), 16)),
("published", when(lv, "published_at")),
])
.with_table(files)
}
pub fn preview_doc(p: &Value) -> Doc {
let cols: Vec<&str> = items(&p["columns"], "columns")
.iter()
.filter_map(Value::as_str)
.collect();
let mut t = Table::new(&cols);
for r in items(&p["rows"], "rows") {
t.rows.push(
r.as_array()
.map(|r| {
r.iter()
.map(|c| clip(c.as_str().unwrap_or("-"), 24))
.collect()
})
.unwrap_or_default(),
);
}
let mut d = Doc::kv(vec![("file", st(p, "path")), ("format", st(p, "format"))]);
if p["format"] == "unsupported" {
return d.with_note("the hub cannot preview this file");
}
d = d.with_table(t);
match p["truncated"].as_bool() {
Some(true) => d.with_note("first rows only"),
_ => d,
}
}
pub fn dataset(c: &Client, sub: &str, a: &Parsed) -> Result<Out, Fail> {
match sub {
"ls" | "list" => {
let v = c.get("/api/datasets")?;
Ok((v.clone(), datasets_table(&v, &a.pos.join(" "))))
}
"show" | "preview" => {
let r = a
.pos
.first()
.ok_or_else(|| Fail::Usage(format!("usage: zc dataset {sub} DATASET")))?;
let id = resolve_ref(c, "/api/datasets/resolve", "dataset_id", r)?;
match sub {
"show" => {
let v = c.get(&format!("/api/datasets/{}", enc(&id)))?;
Ok((v.clone(), dataset_doc(&v)))
}
_ => {
let v = c.get_q(
&format!("/api/datasets/{}/preview", enc(&id)),
&[
("path", a.opt("path").unwrap_or("")),
("limit", a.opt("rows").unwrap_or("")),
],
)?;
Ok((v.clone(), preview_doc(&v)))
}
}
}
_ => Err(Fail::Usage("usage: zc dataset ls|show|preview|get".into())),
}
}
pub fn catalog_table(v: &Value) -> Doc {
let mut t = Table::new(&["TEMPLATE", "NAME", "CATEGORY", "GPU", "HOSTS", "FROM"]);
for e in items(v, "catalog") {
let hosts = e["hosts"].as_array().cloned().unwrap_or_default();
let online = hosts
.iter()
.filter(|h| h["online"].as_bool() == Some(true))
.count();
let cheapest = hosts
.iter()
.filter_map(|h| h["price_per_hour"].as_f64())
.fold(None, |m: Option<f64>, p| Some(m.map_or(p, |m| m.min(p))));
t.rows.push(vec![
st(e, "template_id"),
st(e, "name"),
st(e, "category"),
if e["gpu_required"].as_bool() == Some(true) {
"yes"
} else {
"no"
}
.into(),
format!("{online}/{}", hosts.len()),
cheapest.map_or("-".into(), |p| format!("{}/h", yen(p))),
]);
}
Doc::table(t)
}
pub fn envs_table(v: &Value) -> Doc {
let mut t = Table::new(&[
"ID",
"TEMPLATE",
"NODE",
"STATUS",
"PRICE",
"CHARGED",
"RUNNING SINCE",
]);
for e in items(v, "envs") {
t.rows.push(vec![
st(e, "id"),
st(e, "template_name"),
clip(&st(e, "node_name"), 28),
st(e, "status"),
e["price_per_hour"]
.as_f64()
.map_or("-".into(), |p| format!("{}/h", yen(p))),
yen_of(e, "charged_total"),
when(e, "running_since"),
]);
}
Doc::table(t)
}
pub fn env_doc(e: &Value) -> Doc {
Doc::kv(vec![
("id", st(e, "id")),
("template", st(e, "template_name")),
("node", st(e, "node_name")),
("producer", st(e, "producer")),
("status", st(e, "status")),
("stop reason", st(e, "stop_reason")),
(
"price",
e["price_per_hour"]
.as_f64()
.map_or("-".into(), |p| format!("{}/h", yen(p))),
),
("charged", yen_of(e, "charged_total")),
("running since", when(e, "running_since")),
("open", st(e, "open_url")),
])
}
pub fn env(c: &Client, sub: &str, a: &Parsed) -> Result<Out, Fail> {
match sub {
"catalog" => {
let v = c.get_q(
"/api/envs/catalog",
&[("template_id", a.opt("template").unwrap_or(""))],
)?;
Ok((v.clone(), catalog_table(&v)))
}
"ls" | "list" => {
let v = c.get("/api/envs")?;
Ok((v.clone(), envs_table(&v)))
}
"show" | "stop" => {
let id = a
.pos
.first()
.ok_or_else(|| Fail::Usage(format!("usage: zc env {sub} ID")))?;
let path = format!("/api/envs/{}", enc(id));
match sub {
"show" => {
let v = c.get(&path)?;
Ok((v.clone(), env_doc(&v)))
}
_ => {
let v = c.delete(&path, None)?;
let status = st(&v, "status");
Ok((v, Doc::note(format!("environment {id} is {status}"))))
}
}
}
_ => Err(Fail::Usage("usage: zc env catalog|ls|show|stop".into())),
}
}
pub fn spaces_table(v: &Value) -> Doc {
let mut t = Table::new(&[
"ID",
"NAME",
"STATUS",
"DONE",
"FAILED",
"PER BATCH",
"SPENT",
"VIS",
"CREATED",
]);
for s in items(v, "items") {
let c = &s["counts"];
let n = |k: &str| c.get(k).and_then(Value::as_u64).unwrap_or(0);
let total = n("pending") + n("claimed") + n("succeeded") + n("failed");
t.rows.push(vec![
st(s, "id"),
clip(&st(s, "name"), 32),
st(s, "status"),
format!("{}/{total}", n("succeeded")),
n("failed").to_string(),
num(s, "price_per_batch", 1),
num(s, "spent_zkcr", 1),
st(s, "visibility"),
when(s, "created_at")[..10.min(when(s, "created_at").len())].to_string(),
]);
}
Doc::table(t)
}
pub fn space_doc(s: &Value) -> Doc {
let c = &s["counts"];
let req = &s["requirements"];
Doc::kv(vec![
("id", st(s, "id")),
("name", st(s, "name")),
("owner", st(s, "owner")),
("status", st(s, "status")),
("visibility", st(s, "visibility")),
("image", st(s, "image_ref")),
(
"batches",
format!(
"{} pending, {} claimed, {} succeeded, {} failed",
st(c, "pending"),
st(c, "claimed"),
st(c, "succeeded"),
st(c, "failed")
),
),
("per batch", num(s, "price_per_batch", 2)),
("held", num(s, "held_zkcr", 2)),
("spent", num(s, "spent_zkcr", 2)),
(
"needs",
format!(
"{} cpu, {} GB, {} gpu",
st(req, "cpus"),
st(req, "memory_gb"),
st(req, "gpus")
),
),
("created", when(s, "created_at")),
("finished", when(s, "finished_at")),
])
}
pub fn batches_table(v: &Value) -> Doc {
let mut t = Table::new(&[
"BATCH", "STATUS", "ATTEMPTS", "SECONDS", "NODE", "FINISHED", "ERROR",
]);
for b in items(v, "items") {
t.rows.push(vec![
st(b, "id"),
st(b, "status"),
st(b, "attempts"),
num(b, "duration_s", 1),
clip(&st(b, "claimed_by"), 10),
when(b, "finished_at"),
clip(&st(b, "last_error"), 40),
]);
}
let mut d = Doc::table(t);
if let Some(next) = v.get("next_cursor").and_then(Value::as_str) {
d = d.with_note(format!("more batches: --cursor {next}"));
}
d
}
pub fn space(c: &Client, sub: &str, a: &Parsed) -> Result<Out, Fail> {
match sub {
"ls" | "list" => {
let v = c.get("/api/spaces")?;
Ok((v.clone(), spaces_table(&v)))
}
"show" | "batches" | "cancel" | "pause" | "resume" => {
let id = a
.pos
.first()
.ok_or_else(|| Fail::Usage(format!("usage: zc space {sub} SPACE")))?;
let path = format!("/api/spaces/{}", enc(id));
match sub {
"show" => {
let v = c.get(&path)?;
Ok((v.clone(), space_doc(&v)))
}
"batches" => {
let v = c.get_q(
&format!("{path}/batches"),
&[
("status", a.opt("status").unwrap_or("")),
("limit", a.opt("limit").unwrap_or("")),
("cursor", a.opt("cursor").unwrap_or("")),
],
)?;
Ok((v.clone(), batches_table(&v)))
}
verb => {
let v = c.post(&format!("{path}/{verb}"), None)?;
let status = st(&v, "status");
Ok((
v,
Doc::note(format!("space {id}: {verb} done, now {status}")),
))
}
}
}
_ => Err(Fail::Usage(
"usage: zc space ls|show|batches|cancel|pause|resume".into(),
)),
}
}
pub fn jobs_table(v: &Value) -> Doc {
let mut t = Table::new(&["ID", "STATUS", "JOB", "STAGE", "COST", "CREATED"]);
for j in items(v, "jobs") {
let name = match st(j, "name").as_str() {
"-" => st(j, "model_ref"),
n => n.to_string(),
};
let stage = match (
j.pointer("/progress/stage").and_then(Value::as_str),
j.pointer("/progress/total").and_then(Value::as_u64),
) {
(Some(s), Some(n)) => format!(
"{s} ({}/{n})",
j.pointer("/progress/completed")
.and_then(Value::as_array)
.map_or(0, Vec::len)
),
(Some(s), None) => s.to_string(),
_ => "-".into(),
};
t.rows.push(vec![
st(j, "id"),
st(j, "status"),
clip(&name, 40),
stage,
yen_of(j, "total_zkcr"),
when(j, "created_at"),
]);
}
Doc::table(t)
}