general: snapshot

This commit is contained in:
k
2024-07-20 23:13:41 +02:00
parent d8a5b4a2e6
commit a145b35ad1
100 changed files with 5402 additions and 2967 deletions

View File

@@ -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(())
}