added reading of cards from data for template started migrating configs to all nix
This commit is contained in:
parent
25f70e849b
commit
60f28672d3
15 changed files with 646 additions and 46 deletions
31
src/cards.rs
31
src/cards.rs
|
|
@ -19,11 +19,18 @@ pub struct Card {
|
|||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct CardFromFile {
|
||||
name: String,
|
||||
count: usize,
|
||||
usd_value: Option<String>,
|
||||
pub name: String,
|
||||
pub count: i32,
|
||||
pub usd_value: Option<String>,
|
||||
}
|
||||
|
||||
pub struct CardForTemplate {
|
||||
pub name: String,
|
||||
pub count: i32,
|
||||
pub usd_value: String, // No longer an Option
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct CardFile {
|
||||
pub cards: Vec<CardFromFile>,
|
||||
|
|
@ -63,8 +70,8 @@ pub fn get_data_update_interval() -> Result<u64, Box<dyn std::error::Error>> {
|
|||
|
||||
pub fn process_export_data(cards: &mut Vec<CardFromFile>) {
|
||||
cards.sort_by(|a, b| b.usd_value.partial_cmp(&a.usd_value).unwrap());
|
||||
let top_10_cards = &cards[..10];
|
||||
for card in top_10_cards {
|
||||
let cards = &cards[..10];
|
||||
for card in cards {
|
||||
if let Some(price_str) = &card.usd_value {
|
||||
let value = price_str.parse::<f64>().unwrap();
|
||||
CARD_VALUES.with_label_values(&[&card.name]).set(value);
|
||||
|
|
@ -80,9 +87,7 @@ pub async fn process_cards(interval: &mut tokio::time::Interval) -> Result<(), B
|
|||
}
|
||||
|
||||
let file_path = &args[1];
|
||||
let file = File::open(file_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut cards_data: CardFile = serde_json::from_reader(reader)?;
|
||||
let mut cards_data = read_card_file(file_path).await?;
|
||||
let pb = util::setup_progress_bar(cards_data.cards.len() as u64);
|
||||
|
||||
for card_from_file in &mut cards_data.cards {
|
||||
|
|
@ -103,8 +108,16 @@ pub async fn process_cards(interval: &mut tokio::time::Interval) -> Result<(), B
|
|||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
pub fn setup_registry() -> Result<Arc<Registry>, Box<dyn std::error::Error>> {
|
||||
let registry = Arc::new(Registry::new());
|
||||
registry.register(Box::new(CARD_VALUES.clone())).unwrap();
|
||||
Ok(registry)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn read_card_file(file_path: &str) -> Result<CardFile, Box<dyn std::error::Error>> {
|
||||
let file = File::open(file_path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let cards_data: CardFile = serde_json::from_reader(reader)?;
|
||||
Ok(cards_data)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,32 @@
|
|||
use axum::response::IntoResponse;
|
||||
use hyper::StatusCode;
|
||||
|
||||
use crate::cards::{read_card_file, CardForTemplate};
|
||||
use crate::templates::RootTemplate;
|
||||
use crate::util::HtmlTemplate;
|
||||
|
||||
pub async fn root() -> impl IntoResponse {
|
||||
let template = RootTemplate {
|
||||
name: "Steven"
|
||||
};
|
||||
HtmlTemplate(template)
|
||||
pub async fn root() -> Result<HtmlTemplate<RootTemplate>, (StatusCode, String)> {
|
||||
let file_path = "test_data/test.json";
|
||||
match read_card_file(file_path).await {
|
||||
Ok(card_file) => {
|
||||
let cards_for_template: Vec<_> = card_file.cards
|
||||
.into_iter()
|
||||
.map(|card| CardForTemplate {
|
||||
name: card.name,
|
||||
count: card.count,
|
||||
usd_value: card.usd_value.unwrap_or_else(|| "N/A".into()),
|
||||
})
|
||||
.collect();
|
||||
let template = RootTemplate {
|
||||
name: "Steven".to_string(),
|
||||
cards: cards_for_template,
|
||||
};
|
||||
Ok(HtmlTemplate(template))
|
||||
},
|
||||
Err(err) => {
|
||||
Err((StatusCode::INTERNAL_SERVER_ERROR, format!("An error occurred: {}", err)))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn health() -> impl IntoResponse {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use cards::{setup_registry, get_data_update_interval};
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv::from_filename(".config/app.env").ok();
|
||||
dotenv::from_filename(".config/.env").ok();
|
||||
|
||||
let update_interval = get_data_update_interval()?;
|
||||
let registry = setup_registry()?;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,17 @@
|
|||
use askama::Template;
|
||||
|
||||
use crate::cards::CardForTemplate;
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "root.html")]
|
||||
pub struct RootTemplate<'a>{
|
||||
pub name: &'a str,
|
||||
pub struct RootTemplate {
|
||||
pub name: String,
|
||||
pub cards: Vec<CardForTemplate>,
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path="base.html")]
|
||||
pub struct BaseTemplate;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue