Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make api's for graphs we use instead of templating json #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions glados-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ pub async fn run_glados_web(config: Arc<State>) -> Result<()> {
"/api/hourly-success-rate/",
get(routes::hourly_success_rate),
)
.route(
"/api/latest-census-node-diversiity-data",
get(routes::get_radius_diversity_data),
)
.route(
"/api/latest-census-node-radius-data",
get(routes::generate_radius_graph_data),
)
.route(
"/api/is-content-in-deadzone/:content_key",
get(routes::is_content_in_deadzone),
Expand Down
29 changes: 17 additions & 12 deletions glados-web/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ impl Display for ClientDiversityResult {
}
}

async fn generate_radius_graph_data(state: &Arc<State>) -> Vec<CalculatedRadiusChartData> {
pub async fn generate_radius_graph_data(
Extension(state): Extension<Arc<State>>,
) -> Result<Json<Vec<CalculatedRadiusChartData>>, StatusCode> {
let builder = state.database_connection.get_database_backend();
let mut radius_density = Query::select();
radius_density
Expand Down Expand Up @@ -206,7 +208,20 @@ async fn generate_radius_graph_data(state: &Arc<State>) -> Vec<CalculatedRadiusC
});
}

radius_percentages
Ok(Json(radius_percentages))
}

pub async fn get_radius_diversity_data(
Extension(state): Extension<Arc<State>>,
) -> Result<Json<Vec<ClientDiversityResult>>, StatusCode> {
let client_diversity_data = match get_max_census_id(&state).await {
None => vec![],
Some(max_census_id) => generate_client_diversity_data(&state, None, max_census_id)
.await
.unwrap(),
};

Ok(Json(client_diversity_data))
}

fn xor_distance_to_fraction(radius_high_bytes: [u8; 4]) -> f64 {
Expand Down Expand Up @@ -384,14 +399,6 @@ async fn generate_enr_list_from_census_id(
}

pub async fn root(Extension(state): Extension<Arc<State>>) -> impl IntoResponse {
let client_diversity_data = match get_max_census_id(&state).await {
None => vec![],
Some(max_census_id) => generate_client_diversity_data(&state, None, max_census_id)
.await
.unwrap(),
};

let radius_percentages = generate_radius_graph_data(&state).await;
// Run queries for content dashboard data concurrently
let (hour_stats, day_stats, week_stats) = tokio::join!(
get_audit_stats(
Expand Down Expand Up @@ -429,8 +436,6 @@ pub async fn root(Extension(state): Extension<Arc<State>>) -> impl IntoResponse
let week_stats = week_stats.unwrap();

let template = IndexTemplate {
client_diversity_data,
average_radius_chart: radius_percentages,
stats: [hour_stats, day_stats, week_stats],
};
HtmlTemplate(template)
Expand Down
6 changes: 1 addition & 5 deletions glados-web/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ use axum::{
};
use entity::{client_info, content, content_audit, execution_metadata, key_value, node, record};

use crate::routes::{
CalculatedRadiusChartData, ClientDiversityResult, PaginatedCensusListResult, RawEnr,
};
use crate::routes::{ClientDiversityResult, PaginatedCensusListResult, RawEnr};
use glados_core::stats::AuditStats;

#[derive(Template)]
#[template(path = "index.html")]
pub struct IndexTemplate {
pub client_diversity_data: Vec<ClientDiversityResult>,
pub average_radius_chart: Vec<CalculatedRadiusChartData>,
pub stats: [AuditStats; 3],
}

Expand Down
15 changes: 11 additions & 4 deletions glados-web/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ <h3 style="text-align: center">Client Count</h3>

</div>
<script>
pie_chart_count({{ client_diversity_data| json | safe }});
const census_data = {{ average_radius_chart| json | safe }};
radius_node_id_scatter_chart(census_data);
radius_stacked_chart(census_data);
fetch('/api/latest-census-node-diversiity-data')
.then(response => response.json())
.then(client_diversity_data => {
pie_chart_count(client_diversity_data);
});
fetch('/api/latest-census-node-radius-data')
.then(response => response.json())
.then(census_data => {
radius_node_id_scatter_chart(census_data);
radius_stacked_chart(census_data);
});
statsHistoryChart();

</script>
Expand Down