mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-26 07:39:59 -07:00
general: add /api/last route
This commit is contained in:
@@ -28,13 +28,14 @@ pub use constant::*;
|
||||
pub use date_metadata::*;
|
||||
pub use mining::*;
|
||||
pub use price::*;
|
||||
use serde_json::Value;
|
||||
pub use subs::*;
|
||||
pub use transaction::*;
|
||||
pub use utxo::*;
|
||||
|
||||
use crate::{
|
||||
databases::Databases,
|
||||
io::Json,
|
||||
io::{Json, JSON_EXTENSION},
|
||||
states::{
|
||||
AddressCohortsInputStates,
|
||||
AddressCohortsOneShotStates,
|
||||
@@ -98,9 +99,11 @@ pub struct AllDatasets {
|
||||
pub utxo: UTXODatasets,
|
||||
}
|
||||
|
||||
const DATASETS_PATH: &str = "../datasets";
|
||||
|
||||
impl AllDatasets {
|
||||
pub fn import() -> color_eyre::Result<Self> {
|
||||
let path = "../datasets";
|
||||
let path = DATASETS_PATH;
|
||||
|
||||
let price = PriceDatasets::import(path)?;
|
||||
|
||||
@@ -264,17 +267,15 @@ impl AllDatasets {
|
||||
}
|
||||
|
||||
pub fn export_meta_files(&self) -> color_eyre::Result<()> {
|
||||
let path_to_type: BTreeMap<&Path, &str> = self
|
||||
let mut path_to_type: BTreeMap<&Path, &str> = self
|
||||
.to_any_dataset_vec()
|
||||
.into_iter()
|
||||
.flat_map(|dataset| {
|
||||
dataset
|
||||
.to_all_map_vec()
|
||||
.into_iter()
|
||||
.flat_map(|map| map.exported_path_with_t_name())
|
||||
})
|
||||
.flat_map(|dataset| dataset.to_all_map_vec())
|
||||
.flat_map(|map| map.exported_path_with_t_name())
|
||||
.collect();
|
||||
|
||||
path_to_type.insert(Path::new("../datasets/last"), "Value");
|
||||
|
||||
let datasets_len = path_to_type.len();
|
||||
|
||||
let server_inputs_path = "../server/in";
|
||||
@@ -310,9 +311,40 @@ impl AllDatasets {
|
||||
.into_par_iter()
|
||||
.try_for_each(|dataset| -> color_eyre::Result<()> { dataset.export() })?;
|
||||
|
||||
let mut path_to_last: BTreeMap<String, Value> = BTreeMap::default();
|
||||
|
||||
self.to_mut_any_dataset_vec()
|
||||
.into_iter()
|
||||
.for_each(|dataset| dataset.post_export());
|
||||
.for_each(|dataset| {
|
||||
dataset.post_export();
|
||||
|
||||
dataset.to_all_map_vec().iter().for_each(|map| {
|
||||
if let Some(last_path) = map.path_last() {
|
||||
if let Some(last_value) = map.last_value() {
|
||||
let mut last_path = last_path.clone();
|
||||
last_path.pop();
|
||||
|
||||
let last_path = last_path.to_str().unwrap();
|
||||
|
||||
let skip = if last_path.starts_with(DATASETS_PATH) {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
path_to_last.insert(
|
||||
last_path.split('/').skip(skip).join("-").to_string(),
|
||||
last_value,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Json::export(
|
||||
Path::new(&format!("{DATASETS_PATH}/last.{JSON_EXTENSION}")),
|
||||
&path_to_last,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ mod utils;
|
||||
pub use crate::{
|
||||
actions::iter_blocks,
|
||||
datasets::OHLC,
|
||||
io::{Binary, Json, Serialization},
|
||||
io::{Binary, Json, Serialization, COMPRESSED_BIN_EXTENSION, JSON_EXTENSION},
|
||||
structs::{
|
||||
Config, Date, DateMap, Height, HeightMap, MapChunkId, SerializedBTreeMap, SerializedVec,
|
||||
HEIGHT_MAP_CHUNK_SIZE,
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
pub trait AnyMap {
|
||||
fn path(&self) -> &Path;
|
||||
fn path_last(&self) -> &Option<PathBuf>;
|
||||
|
||||
fn last_value(&self) -> Option<Value>;
|
||||
|
||||
fn t_name(&self) -> &str;
|
||||
|
||||
fn exported_path_with_t_name(&self) -> Vec<(&Path, &str)> {
|
||||
@@ -16,8 +20,6 @@ pub trait AnyMap {
|
||||
}
|
||||
}
|
||||
|
||||
// fn reset(&mut self) -> color_eyre::Result<()>;
|
||||
|
||||
fn pre_export(&mut self);
|
||||
fn export(&self) -> color_eyre::Result<()>;
|
||||
fn post_export(&mut self);
|
||||
|
||||
@@ -286,6 +286,13 @@ where
|
||||
&self.path_last
|
||||
}
|
||||
|
||||
fn last_value(&self) -> Option<serde_json::Value> {
|
||||
self.imported
|
||||
.last_key_value()
|
||||
.and_then(|(_, serialized)| serialized.last())
|
||||
.and_then(|v| serde_json::to_value(v).ok())
|
||||
}
|
||||
|
||||
fn t_name(&self) -> &str {
|
||||
std::any::type_name::<Value>()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user