-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathhttp.rs
More file actions
133 lines (119 loc) · 3.93 KB
/
http.rs
File metadata and controls
133 lines (119 loc) · 3.93 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
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use actix_cors::Cors;
use arrow_schema::Schema;
use itertools::Itertools;
use serde_json::Value;
use crate::option::CONFIG;
use self::{cluster::get_ingestor_info_storage, query::Query};
pub(crate) mod about;
mod cache;
pub mod cluster;
pub(crate) mod health_check;
pub(crate) mod ingest;
mod kinesis;
pub(crate) mod llm;
pub(crate) mod logstream;
pub(crate) mod middleware;
pub mod modal;
pub(crate) mod oidc;
mod otel;
pub(crate) mod query;
pub(crate) mod rbac;
pub(crate) mod role;
pub(crate) mod trino;
pub mod users;
pub const MAX_EVENT_PAYLOAD_SIZE: usize = 10485760;
pub const API_BASE_PATH: &str = "api";
pub const API_VERSION: &str = "v1";
pub(crate) fn base_path() -> String {
format!("/{API_BASE_PATH}/{API_VERSION}")
}
pub fn metrics_path() -> String {
format!("{}/metrics", base_path())
}
pub(crate) fn cross_origin_config() -> Cors {
if !CONFIG.parseable.cors || cfg!(feature = "debug") {
Cors::permissive().block_on_origin_mismatch(false)
} else {
Cors::default().block_on_origin_mismatch(false)
}
}
pub fn base_path_without_preceding_slash() -> String {
format!("{API_BASE_PATH}/{API_VERSION}")
}
/// Fetches the schema for the specified stream.
///
/// # Arguments
///
/// * `stream_name` - The name of the stream to fetch the schema for.
///
/// # Returns
///
/// An `anyhow::Result` containing the `arrow_schema::Schema` for the specified stream.
pub async fn fetch_schema(stream_name: &str) -> anyhow::Result<arrow_schema::Schema> {
let path_prefix =
relative_path::RelativePathBuf::from(format!("{}/{}", stream_name, ".stream"));
let store = CONFIG.storage().get_object_store();
let res: Vec<Schema> = store
.get_objects(
Some(&path_prefix),
Box::new(|file_name: String| file_name.contains(".schema")),
)
.await?
.iter()
// we should be able to unwrap as we know the data is valid schema
.map(|byte_obj| serde_json::from_slice(byte_obj).expect("data is valid json"))
.collect_vec();
let new_schema = Schema::try_merge(res)?;
Ok(new_schema)
}
/// unused for now, might need it later
#[allow(unused)]
pub async fn send_query_request_to_ingestor(query: &Query) -> anyhow::Result<Vec<Value>> {
// send the query request to the ingestor
let mut res = vec![];
let ima = get_ingestor_info_storage().await?;
for im in ima.iter() {
let uri = format!(
"{}{}/{}",
im.domain_name,
base_path_without_preceding_slash(),
"query"
);
let reqw = reqwest::Client::new()
.post(uri)
.json(query)
.header(http::header::AUTHORIZATION, im.token.clone())
.header(http::header::CONTENT_TYPE, "application/json")
.send()
.await;
if let Ok(reqw) = reqw {
// do i need to do a success check??
let v: Value = serde_json::from_slice(&reqw.bytes().await?)?;
// the value returned is an array of json objects
// so it needs to be flattened
if let Some(arr) = v.as_array() {
for val in arr {
res.push(val.to_owned())
}
}
}
}
Ok(res)
}