databricks-tui 0.17.0

Terminal dashboard for Databricks: compute, jobs, pipelines, warehouses, Unity Catalog, SQL console, costs and lineage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use crate::cli::DatabricksCli;
use crate::shape::{fmt_duration_ms, relative_time, DetailData, Status};
use serde_json::Value;

/// Fetches the full detail view for one resource: key facts, recent
/// activity, and the raw JSON. Never fails — errors land in `raw`.
pub async fn fetch(cli: &DatabricksCli, group: &str, id: &str) -> DetailData {
    // The activity call is independent of `get` — run them concurrently.
    let verb = if group == "volumes" { "read" } else { "get" };
    let get_args = [group, verb, id];
    let get = cli.run(&get_args);
    let (main, mut activity) = match group {
        "clusters" => tokio::join!(get, cluster_events(cli, id)),
        "jobs" => tokio::join!(get, job_runs(cli, id)),
        "warehouses" => tokio::join!(get, warehouse_queries(cli, id)),
        _ => (get.await, Vec::new()),
    };
    let json = match main {
        Ok(v) => v,
        Err(e) => {
            return DetailData {
                summary: Vec::new(),
                activity: Vec::new(),
                raw: format!("{e:#}"),
            }
        }
    };
    let raw = serde_json::to_string_pretty(&json).unwrap_or_else(|_| json.to_string());

    let summary = match group {
        "clusters" => cluster_summary(&json),
        "jobs" => job_summary(&json),
        "pipelines" => {
            activity = pipeline_updates(&json);
            pipeline_summary(&json)
        }
        "lakeview" => {
            activity = dashboard_contents(&json);
            dashboard_summary(&json)
        }
        "tables" => {
            activity = table_columns(&json);
            table_summary(&json)
        }
        "volumes" => volume_summary(&json),
        _ => warehouse_summary(&json),
    };

    DetailData {
        summary,
        activity,
        raw,
    }
}

fn push(out: &mut Vec<(String, String)>, key: &str, value: Option<String>) {
    if let Some(v) = value {
        if !v.is_empty() {
            out.push((key.to_string(), v));
        }
    }
}

fn str_of(v: &Value) -> Option<String> {
    v.as_str().map(str::to_string)
}

fn cluster_summary(j: &Value) -> Vec<(String, String)> {
    let mut s = Vec::new();
    push(&mut s, "State", str_of(&j["state"]));
    push(&mut s, "Spark", str_of(&j["spark_version"]));
    push(&mut s, "Node type", str_of(&j["node_type_id"]));
    let workers = j["num_workers"]
        .as_u64()
        .map(|n| n.to_string())
        .or_else(|| {
            let (min, max) = (
                j["autoscale"]["min_workers"].as_u64()?,
                j["autoscale"]["max_workers"].as_u64()?,
            );
            Some(format!("{}{} (autoscale)", min, max))
        });
    push(&mut s, "Workers", workers);
    push(&mut s, "Creator", str_of(&j["creator_user_name"]));
    push(
        &mut s,
        "Started",
        j["start_time"].as_u64().map(relative_time),
    );
    push(
        &mut s,
        "Auto-terminate",
        j["autotermination_minutes"]
            .as_u64()
            .map(|m| format!("{m} min")),
    );
    s
}

async fn cluster_events(cli: &DatabricksCli, id: &str) -> Vec<(Status, String)> {
    let Ok(json) = cli.run(&["clusters", "events", id]).await else {
        return Vec::new();
    };
    json["events"]
        .as_array()
        .map(|events| {
            events
                .iter()
                .take(8)
                .map(|e| {
                    let kind = e["type"].as_str().unwrap_or("EVENT");
                    let when = e["timestamp"]
                        .as_u64()
                        .map(relative_time)
                        .unwrap_or_default();
                    (kind.parse().unwrap(), format!("{kind} · {when}"))
                })
                .collect()
        })
        .unwrap_or_default()
}

fn job_summary(j: &Value) -> Vec<(String, String)> {
    let mut s = Vec::new();
    let settings = &j["settings"];
    push(&mut s, "Creator", str_of(&j["creator_user_name"]));
    let schedule = settings["schedule"]["quartz_cron_expression"]
        .as_str()
        .map(|cron| {
            let tz = settings["schedule"]["timezone_id"]
                .as_str()
                .unwrap_or("UTC");
            let paused = settings["schedule"]["pause_status"].as_str().unwrap_or("");
            let suffix = if paused == "PAUSED" { " (paused)" } else { "" };
            format!("{cron} {tz}{suffix}")
        });
    push(&mut s, "Schedule", schedule);
    push(
        &mut s,
        "Tasks",
        settings["tasks"].as_array().map(|t| t.len().to_string()),
    );
    push(
        &mut s,
        "Max concurrent",
        settings["max_concurrent_runs"]
            .as_u64()
            .map(|n| n.to_string()),
    );
    s
}

async fn job_runs(cli: &DatabricksCli, id: &str) -> Vec<(Status, String)> {
    let Ok(json) = cli
        .run(&["jobs", "list-runs", "--job-id", id, "--limit", "10"])
        .await
    else {
        return Vec::new();
    };
    json.as_array()
        .map(|runs| {
            runs.iter()
                .take(10)
                .map(|r| {
                    let status: Status = r["state"]["result_state"]
                        .as_str()
                        .or_else(|| r["state"]["life_cycle_state"].as_str())
                        .unwrap_or("")
                        .parse()
                        .unwrap();
                    let when = r["start_time"]
                        .as_u64()
                        .map(relative_time)
                        .unwrap_or_default();
                    let dur = r["run_duration"]
                        .as_u64()
                        .or_else(|| r["execution_duration"].as_u64())
                        .map(fmt_duration_ms)
                        .unwrap_or_default();
                    let label = status.label().to_string();
                    let text = [label, when, dur]
                        .into_iter()
                        .filter(|p| !p.is_empty())
                        .collect::<Vec<_>>()
                        .join(" · ");
                    (status, text)
                })
                .collect()
        })
        .unwrap_or_default()
}

fn pipeline_summary(j: &Value) -> Vec<(String, String)> {
    let mut s = Vec::new();
    push(&mut s, "State", str_of(&j["state"]));
    push(
        &mut s,
        "Mode",
        j["spec"]["continuous"]
            .as_bool()
            .map(|c| if c { "continuous" } else { "triggered" }.to_string()),
    );
    push(&mut s, "Channel", str_of(&j["spec"]["channel"]));
    push(&mut s, "Edition", str_of(&j["spec"]["edition"]));
    push(&mut s, "Catalog", str_of(&j["spec"]["catalog"]));
    push(&mut s, "Target", str_of(&j["spec"]["target"]));
    push(&mut s, "Creator", str_of(&j["creator_user_name"]));
    s
}

/// `pipelines get` already includes recent updates — no extra call needed.
fn pipeline_updates(j: &Value) -> Vec<(Status, String)> {
    j["latest_updates"]
        .as_array()
        .map(|updates| {
            updates
                .iter()
                .take(8)
                .map(|u| {
                    let state = u["state"].as_str().unwrap_or("UNKNOWN");
                    let when = u["creation_time"]
                        .as_str()
                        .map(str::to_string)
                        .unwrap_or_default();
                    (state.parse().unwrap(), format!("{state} · {when}"))
                })
                .collect()
        })
        .unwrap_or_default()
}

fn dashboard_summary(j: &Value) -> Vec<(String, String)> {
    let mut s = Vec::new();
    push(&mut s, "State", str_of(&j["lifecycle_state"]));
    push(&mut s, "Path", str_of(&j["path"]));
    push(&mut s, "Warehouse", str_of(&j["warehouse_id"]));
    push(&mut s, "Updated", str_of(&j["update_time"]));
    if let Some(def) = parse_serialized(j) {
        push(
            &mut s,
            "Pages",
            def["pages"].as_array().map(|p| p.len().to_string()),
        );
        push(
            &mut s,
            "Datasets",
            def["datasets"].as_array().map(|d| d.len().to_string()),
        );
    }
    s
}

/// The dashboard definition is embedded as a JSON string inside the response.
fn parse_serialized(j: &Value) -> Option<Value> {
    j["serialized_dashboard"]
        .as_str()
        .and_then(|s| serde_json::from_str(s).ok())
}

/// Pages and widget titles, best effort — Lakeview layouts nest deeply.
fn dashboard_contents(j: &Value) -> Vec<(Status, String)> {
    let Some(def) = parse_serialized(j) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for page in def["pages"].as_array().into_iter().flatten() {
        let page_name = page["displayName"]
            .as_str()
            .or_else(|| page["name"].as_str())
            .unwrap_or("page");
        out.push((Status::Running, format!("{page_name}")));
        for item in page["layout"].as_array().into_iter().flatten() {
            let widget = &item["widget"];
            let title = widget["spec"]["frame"]["title"]
                .as_str()
                .filter(|t| !t.is_empty())
                .or_else(|| widget["name"].as_str())
                .unwrap_or("widget");
            let kind = widget["spec"]["widgetType"].as_str().unwrap_or("");
            let line = if kind.is_empty() {
                format!("  · {title}")
            } else {
                format!("  · {title} ({kind})")
            };
            out.push((Status::Unknown(String::new()), line));
        }
    }
    out.truncate(30);
    out
}

fn table_summary(j: &Value) -> Vec<(String, String)> {
    let mut s = Vec::new();
    push(&mut s, "Type", str_of(&j["table_type"]));
    push(&mut s, "Format", str_of(&j["data_source_format"]));
    push(&mut s, "Owner", str_of(&j["owner"]));
    push(&mut s, "Location", str_of(&j["storage_location"]));
    push(&mut s, "Comment", str_of(&j["comment"]));
    push(
        &mut s,
        "Updated",
        j["updated_at"].as_u64().map(relative_time),
    );
    s
}

/// Column list rendered as the activity section of a table detail.
fn table_columns(j: &Value) -> Vec<(Status, String)> {
    j["columns"]
        .as_array()
        .map(|cols| {
            cols.iter()
                .map(|c| {
                    let name = c["name"].as_str().unwrap_or("?");
                    let ty = c["type_text"]
                        .as_str()
                        .or_else(|| c["type_name"].as_str())
                        .unwrap_or("");
                    (Status::Unknown(String::new()), format!("{name}  ·  {ty}"))
                })
                .collect()
        })
        .unwrap_or_default()
}

fn volume_summary(j: &Value) -> Vec<(String, String)> {
    let mut s = Vec::new();
    push(&mut s, "Type", str_of(&j["volume_type"]));
    push(&mut s, "Owner", str_of(&j["owner"]));
    push(&mut s, "Location", str_of(&j["storage_location"]));
    push(&mut s, "Comment", str_of(&j["comment"]));
    push(
        &mut s,
        "Updated",
        j["updated_at"].as_u64().map(relative_time),
    );
    s
}

/// Recent queries on a warehouse, via the query history REST API —
/// no warehouse wake-up needed.
async fn warehouse_queries(cli: &DatabricksCli, id: &str) -> Vec<(Status, String)> {
    let path = format!("/api/2.0/sql/history/queries?filter_by.warehouse_ids={id}&max_results=12");
    let Ok(json) = cli.run(&["api", "get", &path]).await else {
        return Vec::new();
    };
    json["res"]
        .as_array()
        .map(|queries| {
            queries
                .iter()
                .map(|q| {
                    let status: Status = q["status"].as_str().unwrap_or("").parse().unwrap();
                    let user = q["user_display_name"]
                        .as_str()
                        .or_else(|| q["user_name"].as_str())
                        .unwrap_or("?");
                    let when = q["query_start_time_ms"]
                        .as_u64()
                        .map(relative_time)
                        .unwrap_or_default();
                    let dur = q["duration"]
                        .as_u64()
                        .map(fmt_duration_ms)
                        .unwrap_or_default();
                    let text: String = q["query_text"]
                        .as_str()
                        .unwrap_or("")
                        .split_whitespace()
                        .collect::<Vec<_>>()
                        .join(" ")
                        .chars()
                        .take(60)
                        .collect();
                    let parts: Vec<String> = [user.to_string(), dur, when, text]
                        .into_iter()
                        .filter(|p| !p.is_empty())
                        .collect();
                    (status, parts.join(" · "))
                })
                .collect()
        })
        .unwrap_or_default()
}

fn warehouse_summary(j: &Value) -> Vec<(String, String)> {
    let mut s = Vec::new();
    push(&mut s, "State", str_of(&j["state"]));
    push(&mut s, "Size", str_of(&j["cluster_size"]));
    push(
        &mut s,
        "Clusters",
        j["num_clusters"].as_u64().map(|n| n.to_string()),
    );
    push(
        &mut s,
        "Scaling",
        match (
            j["min_num_clusters"].as_u64(),
            j["max_num_clusters"].as_u64(),
        ) {
            (Some(min), Some(max)) => Some(format!("{min}{max}")),
            _ => None,
        },
    );
    push(
        &mut s,
        "Auto-stop",
        j["auto_stop_mins"].as_u64().map(|m| format!("{m} min")),
    );
    push(
        &mut s,
        "Serverless",
        j["enable_serverless_compute"]
            .as_bool()
            .map(|b| b.to_string()),
    );
    push(&mut s, "Creator", str_of(&j["creator_name"]));
    s
}