|
| 1 | +use crate::config; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +#[derive(Deserialize, Serialize)] |
| 5 | +struct Index { |
| 6 | + index_name: String, |
| 7 | + index_type: String, |
| 8 | + columns: Vec<String>, |
| 9 | + metric: Option<String>, |
| 10 | + status: String, |
| 11 | + created_at: String, |
| 12 | + updated_at: String, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Deserialize)] |
| 16 | +struct ListResponse { |
| 17 | + indexes: Vec<Index>, |
| 18 | +} |
| 19 | + |
| 20 | +pub fn list( |
| 21 | + workspace_id: &str, |
| 22 | + connection_id: &str, |
| 23 | + schema: &str, |
| 24 | + table: &str, |
| 25 | + format: &str, |
| 26 | +) { |
| 27 | + let profile_config = match config::load("default") { |
| 28 | + Ok(c) => c, |
| 29 | + Err(e) => { |
| 30 | + eprintln!("{e}"); |
| 31 | + std::process::exit(1); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + let api_key = match &profile_config.api_key { |
| 36 | + Some(key) if key != "PLACEHOLDER" => key.clone(), |
| 37 | + _ => { |
| 38 | + eprintln!("error: not authenticated. Run 'hotdata auth' to log in."); |
| 39 | + std::process::exit(1); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + let url = format!( |
| 44 | + "{}/connections/{}/tables/{}/{}/indexes", |
| 45 | + profile_config.api_url, connection_id, schema, table |
| 46 | + ); |
| 47 | + let client = reqwest::blocking::Client::new(); |
| 48 | + |
| 49 | + let resp = match client |
| 50 | + .get(&url) |
| 51 | + .header("Authorization", format!("Bearer {api_key}")) |
| 52 | + .header("X-Workspace-Id", workspace_id) |
| 53 | + .send() |
| 54 | + { |
| 55 | + Ok(r) => r, |
| 56 | + Err(e) => { |
| 57 | + eprintln!("error connecting to API: {e}"); |
| 58 | + std::process::exit(1); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + if !resp.status().is_success() { |
| 63 | + use crossterm::style::Stylize; |
| 64 | + eprintln!("{}", crate::util::api_error(resp.text().unwrap_or_default()).red()); |
| 65 | + std::process::exit(1); |
| 66 | + } |
| 67 | + |
| 68 | + let body: ListResponse = match resp.json() { |
| 69 | + Ok(v) => v, |
| 70 | + Err(e) => { |
| 71 | + eprintln!("error parsing response: {e}"); |
| 72 | + std::process::exit(1); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + match format { |
| 77 | + "json" => println!("{}", serde_json::to_string_pretty(&body.indexes).unwrap()), |
| 78 | + "yaml" => print!("{}", serde_yaml::to_string(&body.indexes).unwrap()), |
| 79 | + "table" => { |
| 80 | + if body.indexes.is_empty() { |
| 81 | + use crossterm::style::Stylize; |
| 82 | + eprintln!("{}", "No indexes found.".dark_grey()); |
| 83 | + } else { |
| 84 | + let rows: Vec<Vec<String>> = body.indexes.iter().map(|i| vec![ |
| 85 | + i.index_name.clone(), |
| 86 | + i.index_type.clone(), |
| 87 | + i.columns.join(", "), |
| 88 | + i.metric.clone().unwrap_or_default(), |
| 89 | + i.status.clone(), |
| 90 | + crate::util::format_date(&i.created_at), |
| 91 | + ]).collect(); |
| 92 | + crate::table::print(&["NAME", "TYPE", "COLUMNS", "METRIC", "STATUS", "CREATED"], &rows); |
| 93 | + } |
| 94 | + } |
| 95 | + _ => unreachable!(), |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +pub fn create( |
| 100 | + workspace_id: &str, |
| 101 | + connection_id: &str, |
| 102 | + schema: &str, |
| 103 | + table: &str, |
| 104 | + name: &str, |
| 105 | + columns: &str, |
| 106 | + index_type: &str, |
| 107 | + metric: Option<&str>, |
| 108 | + async_mode: bool, |
| 109 | +) { |
| 110 | + let profile_config = match config::load("default") { |
| 111 | + Ok(c) => c, |
| 112 | + Err(e) => { |
| 113 | + eprintln!("{e}"); |
| 114 | + std::process::exit(1); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + let api_key = match &profile_config.api_key { |
| 119 | + Some(key) if key != "PLACEHOLDER" => key.clone(), |
| 120 | + _ => { |
| 121 | + eprintln!("error: not authenticated. Run 'hotdata auth' to log in."); |
| 122 | + std::process::exit(1); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + let cols: Vec<&str> = columns.split(',').map(str::trim).collect(); |
| 127 | + let mut body = serde_json::json!({ |
| 128 | + "index_name": name, |
| 129 | + "columns": cols, |
| 130 | + "index_type": index_type, |
| 131 | + "async": async_mode, |
| 132 | + }); |
| 133 | + if let Some(m) = metric { |
| 134 | + body["metric"] = serde_json::json!(m); |
| 135 | + } |
| 136 | + |
| 137 | + let url = format!( |
| 138 | + "{}/connections/{}/tables/{}/{}/indexes", |
| 139 | + profile_config.api_url, connection_id, schema, table |
| 140 | + ); |
| 141 | + let client = reqwest::blocking::Client::new(); |
| 142 | + |
| 143 | + let resp = match client |
| 144 | + .post(&url) |
| 145 | + .header("Authorization", format!("Bearer {api_key}")) |
| 146 | + .header("X-Workspace-Id", workspace_id) |
| 147 | + .json(&body) |
| 148 | + .send() |
| 149 | + { |
| 150 | + Ok(r) => r, |
| 151 | + Err(e) => { |
| 152 | + eprintln!("error connecting to API: {e}"); |
| 153 | + std::process::exit(1); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + if !resp.status().is_success() { |
| 158 | + use crossterm::style::Stylize; |
| 159 | + eprintln!("{}", crate::util::api_error(resp.text().unwrap_or_default()).red()); |
| 160 | + std::process::exit(1); |
| 161 | + } |
| 162 | + |
| 163 | + use crossterm::style::Stylize; |
| 164 | + if async_mode { |
| 165 | + let body: serde_json::Value = resp.json().unwrap_or_default(); |
| 166 | + let job_id = body["job_id"].as_str().unwrap_or("unknown"); |
| 167 | + println!("{}", "Index creation submitted.".green()); |
| 168 | + println!("job_id: {}", job_id); |
| 169 | + println!("{}", "Use 'hotdata jobs <job_id>' to check status.".dark_grey()); |
| 170 | + } else { |
| 171 | + println!("{}", "Index created.".green()); |
| 172 | + } |
| 173 | +} |
0 commit comments