2024-05-12 12:58:54 +02:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
use hyperlog_core::log::GraphItem;
|
|
|
|
use hyperlog_protos::hyperlog::{graph_client::GraphClient, graph_item::Contents, GetRequest};
|
|
|
|
use itertools::Itertools;
|
|
|
|
use tonic::transport::Channel;
|
|
|
|
|
2024-05-12 14:38:03 +02:00
|
|
|
#[allow(dead_code)]
|
2024-05-12 15:54:03 +02:00
|
|
|
#[derive(Clone)]
|
2024-05-12 12:58:54 +02:00
|
|
|
pub struct Querier {
|
|
|
|
channel: Channel,
|
|
|
|
}
|
|
|
|
|
2024-05-12 14:38:03 +02:00
|
|
|
#[allow(dead_code, unused_variables)]
|
2024-05-12 12:58:54 +02:00
|
|
|
impl Querier {
|
|
|
|
pub async fn new() -> anyhow::Result<Self> {
|
|
|
|
let channel = Channel::from_static("http://localhost:4000")
|
|
|
|
.connect()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(Self { channel })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_available_roots(&self) -> Option<Vec<String>> {
|
|
|
|
//self.engine.get_roots()
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get(
|
|
|
|
&self,
|
|
|
|
root: &str,
|
|
|
|
path: impl IntoIterator<Item = impl Into<String>>,
|
|
|
|
) -> anyhow::Result<Option<GraphItem>> {
|
|
|
|
let paths = path.into_iter().map(|i| i.into()).collect_vec();
|
|
|
|
|
|
|
|
tracing::debug!(
|
|
|
|
"quering: root:({}), path:({}), len: ({}))",
|
|
|
|
root,
|
|
|
|
paths.join("."),
|
|
|
|
paths.len()
|
|
|
|
);
|
|
|
|
|
|
|
|
let channel = self.channel.clone();
|
|
|
|
|
|
|
|
let mut client = GraphClient::new(channel);
|
|
|
|
|
|
|
|
let request = tonic::Request::new(GetRequest {
|
|
|
|
root: root.into(),
|
|
|
|
paths,
|
|
|
|
});
|
|
|
|
|
|
|
|
let response = client.get(request).await?;
|
|
|
|
|
|
|
|
let graph_item = response.into_inner();
|
|
|
|
|
|
|
|
if let Some(item) = graph_item.item {
|
|
|
|
Ok(transform_proto_to_local(&item))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transform_proto_to_local(input: &hyperlog_protos::hyperlog::GraphItem) -> Option<GraphItem> {
|
|
|
|
match &input.contents {
|
|
|
|
Some(item) => match item {
|
|
|
|
Contents::User(user) => {
|
|
|
|
let mut items = BTreeMap::new();
|
|
|
|
|
|
|
|
for (key, value) in &user.items {
|
|
|
|
if let Some(item) = transform_proto_to_local(value) {
|
|
|
|
items.insert(key.clone(), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(GraphItem::User(items))
|
|
|
|
}
|
|
|
|
Contents::Section(section) => {
|
|
|
|
let mut items = BTreeMap::new();
|
|
|
|
|
|
|
|
for (key, value) in §ion.items {
|
|
|
|
if let Some(item) = transform_proto_to_local(value) {
|
|
|
|
items.insert(key.clone(), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(GraphItem::Section(items))
|
|
|
|
}
|
|
|
|
Contents::Item(item) => Some(GraphItem::Item {
|
|
|
|
title: item.title.clone(),
|
|
|
|
description: item.description.clone(),
|
|
|
|
state: match &item.item_state {
|
|
|
|
Some(state) => match state {
|
|
|
|
hyperlog_protos::hyperlog::item_graph_item::ItemState::NotDone(_) => {
|
|
|
|
hyperlog_core::log::ItemState::NotDone
|
|
|
|
}
|
|
|
|
hyperlog_protos::hyperlog::item_graph_item::ItemState::Done(_) => {
|
|
|
|
hyperlog_core::log::ItemState::Done
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => hyperlog_core::log::ItemState::NotDone,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|