-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnections.rs
More file actions
485 lines (442 loc) · 15.8 KB
/
Copy pathconnections.rs
File metadata and controls
485 lines (442 loc) · 15.8 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::api::ApiClient;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct HealthResponse {
#[allow(dead_code)]
connection_id: String,
healthy: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
latency_ms: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
error: Option<String>,
}
/// Result of a best-effort health check. Either the endpoint responded with a
/// parseable body, or it did not — in which case we record why and keep going.
enum HealthStatus {
Available(HealthResponse),
Unavailable(String),
}
impl HealthStatus {
fn is_confirmed_unhealthy(&self) -> bool {
matches!(self, HealthStatus::Available(h) if !h.healthy)
}
}
impl Serialize for HealthStatus {
fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
match self {
HealthStatus::Available(h) => h.serialize(ser),
HealthStatus::Unavailable(_) => ser.serialize_none(),
}
}
}
fn fetch_health(api: &ApiClient, connection_id: &str, show_spinner: bool) -> HealthStatus {
let spinner = show_spinner.then(|| crate::util::spinner("Checking connection health..."));
let (status, body) = api.get_raw(&format!("/connections/{connection_id}/health"));
if let Some(s) = spinner {
s.finish_and_clear();
}
if !status.is_success() {
return HealthStatus::Unavailable(crate::util::api_error(body));
}
match serde_json::from_str::<HealthResponse>(&body) {
Ok(h) => HealthStatus::Available(h),
Err(e) => HealthStatus::Unavailable(format!("parse error: {e}")),
}
}
fn format_health(health: &HealthStatus) -> String {
use crossterm::style::Stylize;
match health {
HealthStatus::Available(h) if h.healthy => match h.latency_ms {
Some(ms) => format!("{} {}", "healthy".green(), format!("({ms}ms)").dark_grey()),
None => "healthy".green().to_string(),
},
HealthStatus::Available(h) => {
let err = h.error.as_deref().unwrap_or("unknown error");
format!("{} — {}", "unhealthy".red(), err)
}
HealthStatus::Unavailable(err) => {
format!("{} — {}", "unavailable".yellow(), err)
}
}
}
#[derive(Deserialize, Serialize)]
struct ConnectionType {
name: String,
label: String,
}
#[derive(Deserialize)]
struct ListConnectionTypesResponse {
connection_types: Vec<ConnectionType>,
}
#[derive(Deserialize, Serialize)]
struct ConnectionTypeDetail {
name: String,
label: String,
config_schema: Option<serde_json::Value>,
auth: Option<serde_json::Value>,
}
pub fn types_list(workspace_id: &str, format: &str) {
let api = ApiClient::new(Some(workspace_id));
let body: ListConnectionTypesResponse = api.get("/connection-types");
match format {
"json" => println!(
"{}",
serde_json::to_string_pretty(&body.connection_types).unwrap()
),
"yaml" => print!("{}", serde_yaml::to_string(&body.connection_types).unwrap()),
"table" => {
if body.connection_types.is_empty() {
use crossterm::style::Stylize;
eprintln!("{}", "No connection types found.".dark_grey());
} else {
let rows: Vec<Vec<String>> = body
.connection_types
.iter()
.map(|ct| vec![ct.name.clone(), ct.label.clone()])
.collect();
crate::table::print(&["NAME", "LABEL"], &rows);
}
}
_ => unreachable!(),
}
}
pub fn types_get(workspace_id: &str, name: &str, format: &str) {
let api = ApiClient::new(Some(workspace_id));
let detail: ConnectionTypeDetail = api.get(&format!("/connection-types/{name}"));
match format {
"json" => println!("{}", serde_json::to_string_pretty(&detail).unwrap()),
"yaml" => print!("{}", serde_yaml::to_string(&detail).unwrap()),
"table" => {
println!("name: {}", detail.name);
println!("label: {}", detail.label);
if let Some(schema) = &detail.config_schema {
println!("config: {}", serde_json::to_string_pretty(schema).unwrap());
}
if let Some(auth) = &detail.auth {
println!("auth: {}", serde_json::to_string_pretty(auth).unwrap());
}
}
_ => unreachable!(),
}
}
#[derive(Deserialize, Serialize)]
struct Connection {
id: String,
name: String,
source_type: String,
}
#[derive(Deserialize, Serialize)]
struct ConnectionDetail {
id: String,
name: String,
source_type: String,
#[serde(default)]
table_count: u64,
#[serde(default)]
synced_table_count: u64,
}
#[derive(Deserialize)]
struct ListResponse {
connections: Vec<Connection>,
}
/// Resolve a connection name or ID to a connection ID, exiting on failure.
pub fn resolve_connection_id(api: &ApiClient, name_or_id: &str) -> String {
let body: ListResponse = api.get("/connections");
match body
.connections
.iter()
.find(|c| c.id == name_or_id || c.name == name_or_id)
{
Some(conn) => conn.id.clone(),
None => {
use crossterm::style::Stylize;
eprintln!(
"{}",
format!("error: no connection named or with id '{name_or_id}'").red()
);
std::process::exit(1);
}
}
}
pub fn get(workspace_id: &str, connection_id: &str, format: &str) {
let api = ApiClient::new(Some(workspace_id));
let is_table = format == "table";
let spinner = is_table.then(|| crate::util::spinner("Fetching connection..."));
let detail: ConnectionDetail = api.get(&format!("/connections/{connection_id}"));
if let Some(s) = spinner {
s.finish_and_clear();
}
let health = fetch_health(&api, connection_id, is_table);
match format {
"json" => {
let combined = serde_json::json!({
"id": detail.id,
"name": detail.name,
"source_type": detail.source_type,
"table_count": detail.table_count,
"synced_table_count": detail.synced_table_count,
"health": &health,
});
println!("{}", serde_json::to_string_pretty(&combined).unwrap());
}
"yaml" => {
let combined = serde_json::json!({
"id": detail.id,
"name": detail.name,
"source_type": detail.source_type,
"table_count": detail.table_count,
"synced_table_count": detail.synced_table_count,
"health": &health,
});
print!("{}", serde_yaml::to_string(&combined).unwrap());
}
"table" => {
use crossterm::style::Stylize;
let label = |l: &str| format!("{:<16}", l).dark_grey().to_string();
println!("{}{}", label("id:"), detail.id.dark_cyan());
println!("{}{}", label("name:"), detail.name.white());
println!("{}{}", label("source_type:"), detail.source_type.green());
println!(
"{}{} synced / {} total",
label("tables:"),
detail.synced_table_count.to_string().cyan(),
detail.table_count.to_string().cyan(),
);
println!("{}{}", label("health:"), format_health(&health));
}
_ => unreachable!(),
}
}
#[derive(Deserialize, Serialize)]
struct CreateResponse {
id: String,
name: String,
source_type: String,
tables_discovered: u64,
discovery_status: String,
discovery_error: Option<String>,
}
pub fn create(workspace_id: &str, name: &str, source_type: &str, config: &str, format: &str) {
let config_value: serde_json::Value = match serde_json::from_str(config) {
Ok(v) => v,
Err(e) => {
eprintln!("error: --config must be a valid JSON object: {e}");
std::process::exit(1);
}
};
let body = serde_json::json!({
"name": name,
"source_type": source_type,
"config": config_value,
});
let api = ApiClient::new(Some(workspace_id));
let is_table = format == "table";
let spinner = is_table.then(|| crate::util::spinner("Creating connection..."));
let (status, resp_body) = api.post_raw("/connections", &body);
if let Some(s) = &spinner {
s.finish_and_clear();
}
if !status.is_success() {
use crossterm::style::Stylize;
eprintln!("{}", crate::util::api_error(resp_body).red());
std::process::exit(1);
}
let result: CreateResponse = match serde_json::from_str(&resp_body) {
Ok(v) => v,
Err(e) => {
eprintln!("error parsing response: {e}");
std::process::exit(1);
}
};
let health = fetch_health(&api, &result.id, is_table);
match format {
"json" => {
let combined = serde_json::json!({
"id": result.id,
"name": result.name,
"source_type": result.source_type,
"tables_discovered": result.tables_discovered,
"discovery_status": result.discovery_status,
"discovery_error": result.discovery_error,
"health": &health,
});
println!("{}", serde_json::to_string_pretty(&combined).unwrap());
}
"yaml" => {
let combined = serde_json::json!({
"id": result.id,
"name": result.name,
"source_type": result.source_type,
"tables_discovered": result.tables_discovered,
"discovery_status": result.discovery_status,
"discovery_error": result.discovery_error,
"health": &health,
});
print!("{}", serde_yaml::to_string(&combined).unwrap());
}
"table" => {
use crossterm::style::Stylize;
println!("{}", "Connection created".green());
println!("id: {}", result.id);
println!("name: {}", result.name);
println!("source_type: {}", result.source_type);
println!("tables_discovered: {}", result.tables_discovered);
let status_colored = match result.discovery_status.as_str() {
"success" => result.discovery_status.green().to_string(),
"failed" => result
.discovery_error
.as_deref()
.unwrap_or("failed")
.red()
.to_string(),
_ => result.discovery_status.yellow().to_string(),
};
println!("discovery_status: {status_colored}");
println!("health: {}", format_health(&health));
}
_ => unreachable!(),
}
if health.is_confirmed_unhealthy() {
std::process::exit(1);
}
}
pub fn list(workspace_id: &str, format: &str) {
let api = ApiClient::new(Some(workspace_id));
let body: ListResponse = api.get("/connections");
match format {
"json" => {
println!(
"{}",
serde_json::to_string_pretty(&body.connections).unwrap()
);
}
"yaml" => {
print!("{}", serde_yaml::to_string(&body.connections).unwrap());
}
"table" => {
if body.connections.is_empty() {
use crossterm::style::Stylize;
eprintln!("{}", "No connections found.".dark_grey());
} else {
let rows: Vec<Vec<String>> = body
.connections
.iter()
.map(|c| vec![c.id.clone(), c.name.clone(), c.source_type.clone()])
.collect();
crate::table::print(&["ID", "NAME", "SOURCE TYPE"], &rows);
}
}
_ => unreachable!(),
}
}
pub fn refresh(
workspace_id: &str,
connection_id: &str,
data: bool,
schema: Option<&str>,
table: Option<&str>,
async_mode: bool,
include_uncached: bool,
) {
use crossterm::style::Stylize;
if async_mode && !data {
eprintln!(
"{}",
"--async only valid with --data (schema refresh is always synchronous)".red()
);
std::process::exit(1);
}
if include_uncached && !data {
eprintln!("{}", "--include-uncached only valid with --data".red());
std::process::exit(1);
}
if include_uncached && table.is_some() {
eprintln!(
"{}",
"--include-uncached cannot be combined with --table (it only applies to connection-wide refresh)".red()
);
std::process::exit(1);
}
if table.is_some() && schema.is_none() {
eprintln!("{}", "--table requires --schema".red());
std::process::exit(1);
}
if data && schema.is_some() && table.is_none() {
eprintln!(
"{}",
"--schema requires --table for data refresh (no schema-scoped data refresh)".red()
);
std::process::exit(1);
}
let mut body = serde_json::json!({
"connection_id": connection_id,
"data": data,
});
if let Some(s) = schema {
body["schema_name"] = serde_json::Value::String(s.to_string());
}
if let Some(t) = table {
body["table_name"] = serde_json::Value::String(t.to_string());
}
if async_mode {
body["async"] = serde_json::Value::Bool(true);
}
if include_uncached {
body["include_uncached"] = serde_json::Value::Bool(true);
}
let api = ApiClient::new(Some(workspace_id));
let (status, resp_body) = api.post_raw("/refresh", &body);
if !status.is_success() {
eprintln!("{}", crate::util::api_error(resp_body).red());
std::process::exit(1);
}
let parsed: serde_json::Value = serde_json::from_str(&resp_body).unwrap_or_default();
if async_mode {
let job_id = parsed["id"].as_str().unwrap_or("unknown");
println!("{}", "Data refresh submitted.".green());
println!("job_id: {}", job_id);
println!(
"{}",
format!("Use 'hotdata jobs {}' to check status.", job_id).dark_grey()
);
return;
}
if !data {
let discovered = parsed["tables_discovered"].as_u64().unwrap_or(0);
let added = parsed["tables_added"].as_u64().unwrap_or(0);
let modified = parsed["tables_modified"].as_u64().unwrap_or(0);
println!("{}", "Schema refresh completed.".green());
println!(
"{}",
format!(" tables: {discovered} discovered, {added} added, {modified} modified")
.dark_grey()
);
return;
}
if let Some(rows) = parsed["rows_synced"].as_u64() {
let dur = parsed["duration_ms"].as_u64().unwrap_or(0);
println!("{}", "Data refresh completed.".green());
println!("{}", format!(" {rows} rows synced ({dur} ms)").dark_grey());
} else {
let refreshed = parsed["tables_refreshed"].as_u64().unwrap_or(0);
let failed = parsed["tables_failed"].as_u64().unwrap_or(0);
let total = parsed["total_rows"].as_u64().unwrap_or(0);
let dur = parsed["duration_ms"].as_u64().unwrap_or(0);
println!("{}", "Data refresh completed.".green());
println!(
"{}",
format!(
" {refreshed} tables refreshed, {failed} failed, {total} total rows ({dur} ms)"
)
.dark_grey()
);
if let Some(errors) = parsed["errors"].as_array() {
if !errors.is_empty() {
eprintln!("{}", format!(" {} error(s):", errors.len()).yellow());
for err in errors {
eprintln!(" {}", err);
}
}
}
}
}