use crate::{graph_loader::fetch_file, io::csv_loader::CsvLoader, prelude::*};
use serde::Deserialize;
use std::{collections::HashMap, path::PathBuf};
use tracing::error;
#[derive(Deserialize, std::fmt::Debug)]
pub struct Lotr {
pub src_id: String,
pub dst_id: String,
pub time: i64,
}
pub fn lotr_file() -> Result<PathBuf, Box<dyn std::error::Error>> {
fetch_file(
"lotr.csv",
true,
"https://raw.githubusercontent.com/Raphtory/Data/main/lotr.csv",
600,
)
}
#[derive(Deserialize, std::fmt::Debug)]
struct Character {
pub name: String,
pub race: String,
pub gender: String,
}
fn lotr_properties_file() -> Result<PathBuf, Box<dyn std::error::Error>> {
fetch_file(
"lotr_properties.csv",
true,
"https://raw.githubusercontent.com/Raphtory/Data/main/lotr_properties.csv",
600,
)
}
pub fn lotr_graph() -> Graph {
let g = Graph::new();
CsvLoader::new(lotr_file().unwrap())
.load_into_graph(&g, |lotr: Lotr, g: &Graph| {
let src_id = lotr.src_id;
let dst_id = lotr.dst_id;
let time = lotr.time;
g.add_node(time, src_id.clone(), NO_PROPS, None)
.map_err(|err| error!("{:?}", err))
.ok();
g.add_node(time, dst_id.clone(), NO_PROPS, None)
.map_err(|err| error!("{:?}", err))
.ok();
g.add_edge(time, src_id.clone(), dst_id.clone(), NO_PROPS, None)
.expect("Error: Unable to add edge");
})
.expect("Failed to load graph from CSV data files");
g
}
pub fn lotr_graph_with_props() -> Graph {
let g = lotr_graph();
CsvLoader::new(lotr_properties_file().unwrap())
.load_into_graph(&g, |char: Character, g: &Graph| {
if let Some(node) = g.node(char.name) {
let _ = node.add_metadata(HashMap::from([
("race", char.race),
("gender", char.gender),
]));
}
})
.expect("Failed to load graph from CSV data files");
g
}