-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexes.rs
More file actions
173 lines (157 loc) · 4.81 KB
/
indexes.rs
File metadata and controls
173 lines (157 loc) · 4.81 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
use crate::config;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct Index {
index_name: String,
index_type: String,
columns: Vec<String>,
metric: Option<String>,
status: String,
created_at: String,
updated_at: String,
}
#[derive(Deserialize)]
struct ListResponse {
indexes: Vec<Index>,
}
pub fn list(
workspace_id: &str,
connection_id: &str,
schema: &str,
table: &str,
format: &str,
) {
let profile_config = match config::load("default") {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
let api_key = match &profile_config.api_key {
Some(key) if key != "PLACEHOLDER" => key.clone(),
_ => {
eprintln!("error: not authenticated. Run 'hotdata auth' to log in.");
std::process::exit(1);
}
};
let url = format!(
"{}/connections/{}/tables/{}/{}/indexes",
profile_config.api_url, connection_id, schema, table
);
let client = reqwest::blocking::Client::new();
let resp = match client
.get(&url)
.header("Authorization", format!("Bearer {api_key}"))
.header("X-Workspace-Id", workspace_id)
.send()
{
Ok(r) => r,
Err(e) => {
eprintln!("error connecting to API: {e}");
std::process::exit(1);
}
};
if !resp.status().is_success() {
use crossterm::style::Stylize;
eprintln!("{}", crate::util::api_error(resp.text().unwrap_or_default()).red());
std::process::exit(1);
}
let body: ListResponse = match resp.json() {
Ok(v) => v,
Err(e) => {
eprintln!("error parsing response: {e}");
std::process::exit(1);
}
};
match format {
"json" => println!("{}", serde_json::to_string_pretty(&body.indexes).unwrap()),
"yaml" => print!("{}", serde_yaml::to_string(&body.indexes).unwrap()),
"table" => {
if body.indexes.is_empty() {
use crossterm::style::Stylize;
eprintln!("{}", "No indexes found.".dark_grey());
} else {
let rows: Vec<Vec<String>> = body.indexes.iter().map(|i| vec![
i.index_name.clone(),
i.index_type.clone(),
i.columns.join(", "),
i.metric.clone().unwrap_or_default(),
i.status.clone(),
crate::util::format_date(&i.created_at),
]).collect();
crate::table::print(&["NAME", "TYPE", "COLUMNS", "METRIC", "STATUS", "CREATED"], &rows);
}
}
_ => unreachable!(),
}
}
pub fn create(
workspace_id: &str,
connection_id: &str,
schema: &str,
table: &str,
name: &str,
columns: &str,
index_type: &str,
metric: Option<&str>,
async_mode: bool,
) {
let profile_config = match config::load("default") {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
let api_key = match &profile_config.api_key {
Some(key) if key != "PLACEHOLDER" => key.clone(),
_ => {
eprintln!("error: not authenticated. Run 'hotdata auth' to log in.");
std::process::exit(1);
}
};
let cols: Vec<&str> = columns.split(',').map(str::trim).collect();
let mut body = serde_json::json!({
"index_name": name,
"columns": cols,
"index_type": index_type,
"async": async_mode,
});
if let Some(m) = metric {
body["metric"] = serde_json::json!(m);
}
let url = format!(
"{}/connections/{}/tables/{}/{}/indexes",
profile_config.api_url, connection_id, schema, table
);
let client = reqwest::blocking::Client::new();
let resp = match client
.post(&url)
.header("Authorization", format!("Bearer {api_key}"))
.header("X-Workspace-Id", workspace_id)
.json(&body)
.send()
{
Ok(r) => r,
Err(e) => {
eprintln!("error connecting to API: {e}");
std::process::exit(1);
}
};
if !resp.status().is_success() {
use crossterm::style::Stylize;
eprintln!("{}", crate::util::api_error(resp.text().unwrap_or_default()).red());
std::process::exit(1);
}
use crossterm::style::Stylize;
if async_mode {
let body: serde_json::Value = resp.json().unwrap_or_default();
let job_id = body["job_id"].as_str().unwrap_or("unknown");
println!("{}", "Index creation submitted.".green());
println!("job_id: {}", job_id);
println!("{}", "Use 'hotdata jobs <job_id>' to check status.".dark_grey());
} else {
println!("{}", "Index created.".green());
}
}