mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-27 08:09:58 -07:00
general: snapshot
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::{collections::BTreeMap, path::PathBuf};
|
||||
|
||||
use axum::{
|
||||
extract::{Path, Query, State},
|
||||
http::HeaderMap,
|
||||
@@ -7,11 +9,11 @@ use color_eyre::{eyre::eyre, owo_colors::OwoColorize};
|
||||
use reqwest::{header::HOST, StatusCode};
|
||||
use serde::Deserialize;
|
||||
|
||||
use parser::{log, DateMap, HeightMap, WNaiveDate, HEIGHT_MAP_CHUNK_SIZE, OHLC};
|
||||
use parser::{log, Date, DateMap, Height, HeightMap, MapChunkId, HEIGHT_MAP_CHUNK_SIZE, OHLC};
|
||||
|
||||
use crate::{
|
||||
chunk::Chunk, headers::add_cors_to_headers, kind::Kind, response::typed_value_to_response,
|
||||
AppState,
|
||||
routes::Route, AppState,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -63,19 +65,22 @@ fn _file_handler(
|
||||
let (kind, route) = if path.starts_with(date_prefix) {
|
||||
(
|
||||
Kind::Date,
|
||||
routes
|
||||
.date
|
||||
.get(&path.strip_prefix(date_prefix).unwrap().replace('-', "_")),
|
||||
routes.date.get(&replace_dash_by_underscore(
|
||||
path.strip_prefix(date_prefix).unwrap(),
|
||||
)),
|
||||
)
|
||||
} else if path.starts_with(height_prefix) {
|
||||
(
|
||||
Kind::Height,
|
||||
routes
|
||||
.height
|
||||
.get(&path.strip_prefix(height_prefix).unwrap().replace('-', "_")),
|
||||
routes.height.get(&replace_dash_by_underscore(
|
||||
path.strip_prefix(height_prefix).unwrap(),
|
||||
)),
|
||||
)
|
||||
} else {
|
||||
(Kind::Last, routes.last.get(&path.replace('-', "_")))
|
||||
(
|
||||
Kind::Last,
|
||||
routes.last.get(&replace_dash_by_underscore(&path)),
|
||||
)
|
||||
};
|
||||
|
||||
if route.is_none() {
|
||||
@@ -87,48 +92,18 @@ fn _file_handler(
|
||||
let mut chunk = None;
|
||||
|
||||
if kind != Kind::Last {
|
||||
let datasets = match kind {
|
||||
Kind::Date => DateMap::<usize>::_read_dir(&route.file_path, &route.serialization),
|
||||
Kind::Height => HeightMap::<usize>::_read_dir(&route.file_path, &route.serialization),
|
||||
match kind {
|
||||
Kind::Date => {
|
||||
let datasets = DateMap::<usize>::_read_dir(&route.file_path, &route.serialization);
|
||||
process_datasets(headers, kind, &mut chunk, &mut route, query, datasets)?;
|
||||
}
|
||||
Kind::Height => {
|
||||
let datasets =
|
||||
HeightMap::<usize>::_read_dir(&route.file_path, &route.serialization);
|
||||
process_datasets(headers, kind, &mut chunk, &mut route, query, datasets)?;
|
||||
}
|
||||
_ => panic!(),
|
||||
};
|
||||
|
||||
let (last_chunk_id, _) = datasets.last_key_value().unwrap();
|
||||
|
||||
let chunk_id = query.chunk.unwrap_or(*last_chunk_id);
|
||||
|
||||
let path = datasets.get(&chunk_id);
|
||||
|
||||
if path.is_none() {
|
||||
return Err(eyre!("Couldn't find chunk"));
|
||||
}
|
||||
|
||||
route.file_path = path.unwrap().to_str().unwrap().to_string();
|
||||
|
||||
let offset = match kind {
|
||||
Kind::Date => 1,
|
||||
Kind::Height => HEIGHT_MAP_CHUNK_SIZE,
|
||||
_ => panic!(),
|
||||
};
|
||||
|
||||
let offsetted_to_url = |offseted| {
|
||||
datasets.get(&offseted).map(|_| {
|
||||
let host = headers[HOST].to_str().unwrap();
|
||||
let scheme = if host.contains("0.0.0.0") || host.contains("localhost") {
|
||||
"http"
|
||||
} else {
|
||||
"https"
|
||||
};
|
||||
|
||||
format!("{scheme}://{host}{}?chunk={offseted}", route.url_path)
|
||||
})
|
||||
};
|
||||
|
||||
chunk = Some(Chunk {
|
||||
id: chunk_id,
|
||||
next: chunk_id.checked_add(offset).and_then(offsetted_to_url),
|
||||
previous: chunk_id.checked_sub(offset).and_then(offsetted_to_url),
|
||||
})
|
||||
}
|
||||
|
||||
let type_name = route.values_type.split("::").last().unwrap();
|
||||
@@ -142,9 +117,73 @@ fn _file_handler(
|
||||
"f32" => typed_value_to_response::<f32>(kind, &route.file_path, chunk)?,
|
||||
"f64" => typed_value_to_response::<f64>(kind, &route.file_path, chunk)?,
|
||||
"OHLC" => typed_value_to_response::<OHLC>(kind, &route.file_path, chunk)?,
|
||||
"WNaiveDate" => typed_value_to_response::<WNaiveDate>(kind, &route.file_path, chunk)?,
|
||||
"Date" => typed_value_to_response::<Date>(kind, &route.file_path, chunk)?,
|
||||
"Height" => typed_value_to_response::<Height>(kind, &route.file_path, chunk)?,
|
||||
_ => panic!("Incompatible type: {type_name}"),
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn replace_dash_by_underscore(s: &str) -> String {
|
||||
s.replace('-', "_")
|
||||
}
|
||||
|
||||
fn process_datasets<ChunkId>(
|
||||
headers: HeaderMap,
|
||||
kind: Kind,
|
||||
chunk: &mut Option<Chunk>,
|
||||
route: &mut Route,
|
||||
query: Query<Params>,
|
||||
datasets: BTreeMap<ChunkId, PathBuf>,
|
||||
) -> color_eyre::Result<()>
|
||||
where
|
||||
ChunkId: MapChunkId,
|
||||
{
|
||||
let (last_chunk_id, _) = datasets.last_key_value().unwrap_or_else(|| {
|
||||
dbg!(&datasets, &route);
|
||||
panic!()
|
||||
});
|
||||
|
||||
let chunk_id = query
|
||||
.chunk
|
||||
.map(|id| ChunkId::from_usize(id))
|
||||
.unwrap_or(*last_chunk_id);
|
||||
|
||||
let path = datasets.get(&chunk_id);
|
||||
|
||||
if path.is_none() {
|
||||
return Err(eyre!("Couldn't find chunk"));
|
||||
}
|
||||
|
||||
route.file_path = path.unwrap().to_str().unwrap().to_string();
|
||||
|
||||
let offset = match kind {
|
||||
Kind::Date => 1,
|
||||
Kind::Height => HEIGHT_MAP_CHUNK_SIZE as usize,
|
||||
_ => panic!(),
|
||||
};
|
||||
|
||||
let offsetted_to_url = |offseted| {
|
||||
datasets.get(&ChunkId::from_usize(offseted)).map(|_| {
|
||||
let host = headers[HOST].to_str().unwrap();
|
||||
let scheme = if host.contains("0.0.0.0") || host.contains("localhost") {
|
||||
"http"
|
||||
} else {
|
||||
"https"
|
||||
};
|
||||
|
||||
format!("{scheme}://{host}{}?chunk={offseted}", route.url_path)
|
||||
})
|
||||
};
|
||||
|
||||
let chunk_id = chunk_id.to_usize();
|
||||
|
||||
chunk.replace(Chunk {
|
||||
id: chunk_id,
|
||||
next: chunk_id.checked_add(offset).and_then(offsetted_to_url),
|
||||
previous: chunk_id.checked_sub(offset).and_then(offsetted_to_url),
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use bincode::Decode;
|
||||
use parser::{Serialization, SerializedDateMap, SerializedHeightMap};
|
||||
use parser::{Date, Serialization, SerializedBTreeMap, SerializedVec};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
pub fn import_map<T>(relative_path: &str) -> color_eyre::Result<SerializedDateMap<T>>
|
||||
pub fn import_map<T>(relative_path: &str) -> color_eyre::Result<SerializedBTreeMap<Date, T>>
|
||||
where
|
||||
T: Serialize + Debug + DeserializeOwned + Decode,
|
||||
{
|
||||
Serialization::from_extension(relative_path.split('.').last().unwrap()).import(relative_path)
|
||||
}
|
||||
|
||||
pub fn import_vec<T>(relative_path: &str) -> color_eyre::Result<SerializedHeightMap<T>>
|
||||
pub fn import_vec<T>(relative_path: &str) -> color_eyre::Result<SerializedVec<T>>
|
||||
where
|
||||
T: Serialize + Debug + DeserializeOwned + Decode,
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#[derive(PartialEq, Eq)]
|
||||
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||
pub enum Kind {
|
||||
Date,
|
||||
Height,
|
||||
|
||||
@@ -56,7 +56,7 @@ async fn main() -> color_eyre::Result<()> {
|
||||
.with_state(state)
|
||||
.layer(compression_layer);
|
||||
|
||||
let port = 3110;
|
||||
let port = 3111;
|
||||
|
||||
log(&format!("Starting server on port {port}..."));
|
||||
|
||||
|
||||
@@ -33,10 +33,16 @@ impl Routes {
|
||||
let mut split_key = key.split('/').collect_vec();
|
||||
|
||||
let mut split_last = split_key.pop().unwrap().split('.').rev().collect_vec();
|
||||
|
||||
let last = split_last.pop().unwrap().to_owned();
|
||||
|
||||
let mut skip = 2;
|
||||
|
||||
let serialization = split_last.pop().map_or_else(
|
||||
|| {
|
||||
if *split_key.get(1).unwrap() == "price" {
|
||||
skip = 1;
|
||||
|
||||
Serialization::Json
|
||||
} else {
|
||||
Serialization::Binary
|
||||
@@ -44,8 +50,11 @@ impl Routes {
|
||||
},
|
||||
Serialization::from_extension,
|
||||
);
|
||||
let split_key = split_key.iter().skip(2).collect_vec();
|
||||
|
||||
let split_key = split_key.iter().skip(skip).collect_vec();
|
||||
|
||||
let map_key = split_key.iter().join("_");
|
||||
|
||||
let url_path = split_key.iter().join("-");
|
||||
|
||||
let file_path = key.to_owned();
|
||||
|
||||
Reference in New Issue
Block a user