feat: added some more routes to explore db contents

This commit is contained in:
əlemi 2023-10-19 23:43:31 +02:00
parent 4e401ed173
commit 86dbf15f01
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -1,10 +1,11 @@
use std::{net::SocketAddr, sync::Arc};
use axum::{Router, routing::get, extract::{Query, State}, Json};
use sea_orm::DatabaseConnection;
use serde::Deserialize;
use reqwest::StatusCode;
use sea_orm::{DatabaseConnection, EntityTrait, PaginatorTrait};
use serde::{Deserialize, Serialize};
use crate::serve::bubble::BubbleChart;
use crate::{serve::bubble::BubbleChart, nodeinfo::model::NodeInfoOwned, entities::{domain, node_info}};
use super::bubble::{ChartVertex, ChartArc};
@ -12,6 +13,8 @@ pub async fn serve_api_routes(addr: SocketAddr, db: DatabaseConnection) {
let app = Router::new()
.route("/crawl", get(route_crawl_domain))
.route("/instance", get(route_get_instance))
.route("/stats", get(route_get_stats))
.with_state(Arc::new(db));
tracing::info!("listening on {}", addr);
@ -41,3 +44,38 @@ async fn route_crawl_domain(
}
}
}
async fn route_get_instance(
State(db): State<Arc<DatabaseConnection>>,
Query(params): Query<Params>
) -> Result<Json<NodeInfoOwned>, StatusCode> {
Ok(Json(
serde_json::from_str(
&domain::Entity::find_by_id(&params.domain)
.find_also_related(node_info::Entity)
.one(db.as_ref())
.await
.map_err(|_e| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::NOT_FOUND)?
.1
.ok_or(StatusCode::NOT_FOUND)?
.data
).expect("could not deserialize nodeinfo on db")
))
}
#[derive(Serialize)]
struct StatsReport {
total_instances: u64,
}
async fn route_get_stats(
State(db): State<Arc<DatabaseConnection>>,
) -> Json<StatsReport> {
Json(StatsReport {
total_instances: node_info::Entity::find()
.count(db.as_ref())
.await
.expect("could not count number of instances")
})
}