mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-30 17:40:00 -07:00
bindex: contained fjall code
This commit is contained in:
8
server/src/api/structs/chunk_metadata.rs
Normal file
8
server/src/api/structs/chunk_metadata.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ChunkMetadata {
|
||||
pub id: usize,
|
||||
pub previous: Option<String>,
|
||||
pub next: Option<String>,
|
||||
}
|
||||
34
server/src/api/structs/extension.rs
Normal file
34
server/src/api/structs/extension.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||
pub enum Extension {
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
CSV,
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
JSON,
|
||||
}
|
||||
|
||||
impl Extension {
|
||||
pub fn from(path: &Path) -> Option<Self> {
|
||||
if let Some(extension) = path.extension() {
|
||||
let extension = extension.to_str().unwrap();
|
||||
|
||||
if extension == Self::CSV.to_str() {
|
||||
Some(Self::CSV)
|
||||
} else if extension == Self::JSON.to_str() {
|
||||
Some(Self::JSON)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_str(&self) -> &str {
|
||||
match self {
|
||||
Extension::CSV => "csv",
|
||||
Extension::JSON => "json",
|
||||
}
|
||||
}
|
||||
}
|
||||
52
server/src/api/structs/kind.rs
Normal file
52
server/src/api/structs/kind.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use color_eyre::eyre::{eyre, ContextCompat};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::structs::{AnyMap, Date, Height, MapKey};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum Kind {
|
||||
Date,
|
||||
Height,
|
||||
Timestamp,
|
||||
Last,
|
||||
}
|
||||
|
||||
impl TryFrom<&String> for Kind {
|
||||
type Error = color_eyre::Report;
|
||||
|
||||
fn try_from(str: &String) -> Result<Self, Self::Error> {
|
||||
Ok(
|
||||
match str
|
||||
.to_lowercase()
|
||||
.chars()
|
||||
.next()
|
||||
.context("Expect kind to have first letter")?
|
||||
{
|
||||
'd' => Self::Date,
|
||||
'h' => Self::Height,
|
||||
't' => Self::Timestamp,
|
||||
'l' => Self::Last,
|
||||
_ => return Err(eyre!("Bad kind")),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&(dyn AnyMap + Send + Sync)> for BTreeSet<Kind> {
|
||||
fn from(map: &(dyn AnyMap + Send + Sync)) -> Self {
|
||||
let mut s = Self::new();
|
||||
if map.key_name() == Date::map_name() {
|
||||
s.insert(Kind::Date);
|
||||
}
|
||||
if map.key_name() == Height::map_name() {
|
||||
s.insert(Kind::Height);
|
||||
s.insert(Kind::Timestamp);
|
||||
}
|
||||
if map.last_value().is_some() {
|
||||
s.insert(Kind::Last);
|
||||
}
|
||||
s
|
||||
}
|
||||
}
|
||||
13
server/src/api/structs/mod.rs
Normal file
13
server/src/api/structs/mod.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
mod chunk_metadata;
|
||||
mod extension;
|
||||
mod kind;
|
||||
mod range;
|
||||
mod route;
|
||||
mod routes;
|
||||
|
||||
pub use chunk_metadata::*;
|
||||
pub use extension::*;
|
||||
pub use kind::*;
|
||||
pub use range::*;
|
||||
pub use route::*;
|
||||
pub use routes::*;
|
||||
32
server/src/api/structs/range.rs
Normal file
32
server/src/api/structs/range.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use axum::extract::Query;
|
||||
use color_eyre::eyre::eyre;
|
||||
|
||||
use crate::server::api::handlers::DatasetParams;
|
||||
|
||||
pub enum DatasetRange {
|
||||
All,
|
||||
Chunk(DatasetRangeChunk),
|
||||
}
|
||||
|
||||
impl TryFrom<&Query<DatasetParams>> for DatasetRange {
|
||||
type Error = color_eyre::Report;
|
||||
|
||||
fn try_from(query: &Query<DatasetParams>) -> Result<Self, Self::Error> {
|
||||
if let Some(chunk) = query.chunk {
|
||||
if query.all.is_some() {
|
||||
Err(eyre!("chunk and all are exclusive"))
|
||||
} else {
|
||||
Ok(Self::Chunk(DatasetRangeChunk::Chunk(chunk)))
|
||||
}
|
||||
} else if query.all.is_some() {
|
||||
Ok(Self::All)
|
||||
} else {
|
||||
Ok(Self::Chunk(DatasetRangeChunk::Last))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum DatasetRangeChunk {
|
||||
Chunk(usize),
|
||||
Last,
|
||||
}
|
||||
33
server/src/api/structs/route.rs
Normal file
33
server/src/api/structs/route.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use std::{collections::BTreeSet, path::PathBuf};
|
||||
|
||||
use crate::{io::Serialization, structs::AnyMap};
|
||||
|
||||
use super::Kind;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Route {
|
||||
pub type_name: String,
|
||||
pub list: BTreeSet<Kind>,
|
||||
pub path: PathBuf,
|
||||
pub serialization: Serialization,
|
||||
}
|
||||
|
||||
impl Route {
|
||||
pub fn update(&mut self, map: &(dyn AnyMap + Send + Sync)) {
|
||||
self.list.append(&mut BTreeSet::from(map));
|
||||
if self.serialization != map.serialization() {
|
||||
panic!("route.upate() different serialization")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&(dyn AnyMap + Send + Sync)> for Route {
|
||||
fn from(map: &(dyn AnyMap + Send + Sync)) -> Self {
|
||||
Self {
|
||||
list: BTreeSet::from(map),
|
||||
path: map.path_parent().to_owned(),
|
||||
type_name: map.type_name().split("::").last().unwrap().to_owned(),
|
||||
serialization: map.serialization(),
|
||||
}
|
||||
}
|
||||
}
|
||||
54
server/src/api/structs/routes.rs
Normal file
54
server/src/api/structs/routes.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use derive_deref::{Deref, DerefMut};
|
||||
|
||||
use crate::{
|
||||
parser::{AnyDatasets, Datasets},
|
||||
structs::Config,
|
||||
};
|
||||
|
||||
use super::Route;
|
||||
|
||||
#[derive(Debug, Clone, Default, Deref, DerefMut)]
|
||||
pub struct Routes(BTreeMap<String, Route>);
|
||||
|
||||
const WEBSITE_TYPES_PATH: &str = "../website/scripts/types";
|
||||
|
||||
impl Routes {
|
||||
pub fn build(datasets: &Datasets, config: &Config) -> Self {
|
||||
datasets
|
||||
.to_any_dataset_vec()
|
||||
.into_iter()
|
||||
.flat_map(|dataset| dataset.to_all_map_vec())
|
||||
.fold(Self::default(), |mut routes, map| {
|
||||
routes
|
||||
.entry(map.id(config))
|
||||
.or_insert_with(|| Route::from(map))
|
||||
.update(map);
|
||||
routes
|
||||
})
|
||||
}
|
||||
|
||||
pub fn generate_dts_file(&self) {
|
||||
// let map_to_type = |name: &str, map: &HashMap<String, Route>| -> String {
|
||||
// let paths = map
|
||||
// .values()
|
||||
// .map(|route| format!("\"{}\"", route.url_path))
|
||||
// .join(" | ");
|
||||
|
||||
// format!("export type {}Path = {};\n", name, paths)
|
||||
// };
|
||||
|
||||
// let date_type = map_to_type("Date", &self.date);
|
||||
|
||||
// let height_type = map_to_type("Height", &self.height);
|
||||
|
||||
// let last_type = map_to_type("Last", &self.last);
|
||||
|
||||
// fs::write(
|
||||
// format!("{WEBSITE_TYPES_PATH}/paths.d.ts"),
|
||||
// format!("// This file is auto generated by the server\n// Manual changes are forbidden\n\n{date_type}\n{height_type}\n{last_type}"),
|
||||
// )
|
||||
// .unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user