Something went wrong. Try again.
Highly ambitious ATProtocol AppView service and sdks forked from slices.network/slices
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148//! GraphQL schema extensions for slice statistics
use async_graphql::dynamic::{Field, FieldFuture, FieldValue, Object, TypeRef};use crate::models::CollectionStats;
/// Container for CollectionStats#[derive(Clone)]pub struct CollectionStatsContainer { pub stats: CollectionStats,}
/// Container for SliceStats#[derive(Clone)]pub struct SliceStatsContainer { pub collections: Vec<String>, pub collection_stats: Vec<CollectionStats>, pub total_lexicons: i64, pub total_records: i64, pub total_actors: i64,}
/// Creates the CollectionStats GraphQL typepub fn create_collection_stats_type() -> Object { let mut collection_stats = Object::new("CollectionStats");
collection_stats = collection_stats.field(Field::new("collection", TypeRef::named_nn(TypeRef::STRING), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<CollectionStatsContainer>()?; Ok(Some(async_graphql::Value::from(container.stats.collection.clone()))) }) }));
collection_stats = collection_stats.field(Field::new("recordCount", TypeRef::named_nn(TypeRef::INT), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<CollectionStatsContainer>()?; Ok(Some(async_graphql::Value::from(container.stats.record_count as i32))) }) }));
collection_stats = collection_stats.field(Field::new("uniqueActors", TypeRef::named_nn(TypeRef::INT), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<CollectionStatsContainer>()?; Ok(Some(async_graphql::Value::from(container.stats.unique_actors as i32))) }) }));
collection_stats}
/// Creates the SliceStats GraphQL typepub fn create_slice_stats_type() -> Object { let mut slice_stats = Object::new("SliceStats");
slice_stats = slice_stats.field(Field::new("collections", TypeRef::named_nn_list_nn(TypeRef::STRING), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<SliceStatsContainer>()?; let values: Vec<async_graphql::Value> = container.collections .iter() .map(|c| async_graphql::Value::from(c.clone())) .collect(); Ok(Some(async_graphql::Value::from(values))) }) }));
slice_stats = slice_stats.field(Field::new("collectionStats", TypeRef::named_nn_list_nn("CollectionStats"), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<SliceStatsContainer>()?; let field_values: Vec<FieldValue<'_>> = container.collection_stats .iter() .map(|stats| { let stats_container = CollectionStatsContainer { stats: stats.clone() }; FieldValue::owned_any(stats_container) }) .collect(); Ok(Some(FieldValue::list(field_values))) }) }));
slice_stats = slice_stats.field(Field::new("totalLexicons", TypeRef::named_nn(TypeRef::INT), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<SliceStatsContainer>()?; Ok(Some(async_graphql::Value::from(container.total_lexicons as i32))) }) }));
slice_stats = slice_stats.field(Field::new("totalRecords", TypeRef::named_nn(TypeRef::INT), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<SliceStatsContainer>()?; Ok(Some(async_graphql::Value::from(container.total_records as i32))) }) }));
slice_stats = slice_stats.field(Field::new("totalActors", TypeRef::named_nn(TypeRef::INT), |ctx| { FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<SliceStatsContainer>()?; Ok(Some(async_graphql::Value::from(container.total_actors as i32))) }) }));
slice_stats}
/// Add stats field to NetworkSlicesSlice typepub fn add_stats_field_to_slice( object: Object, database: crate::database::Database,) -> Object { use crate::graphql::schema_builder::RecordContainer;
let db_for_stats = database.clone();
object.field( Field::new( "stats", TypeRef::named_nn("SliceStats"), move |ctx| { let db = db_for_stats.clone(); FieldFuture::new(async move { let container = ctx.parent_value.try_downcast_ref::<RecordContainer>()?; let slice_uri = &container.record.uri;
// Fetch all stats in parallel let (collections, collection_stats, total_lexicons, total_records, total_actors) = tokio::try_join!( db.get_slice_collections_list(slice_uri), db.get_slice_collection_stats(slice_uri), db.get_slice_lexicon_count(slice_uri), db.get_slice_total_records(slice_uri), db.get_slice_total_actors(slice_uri), ) .map_err(|e| async_graphql::Error::new(format!("Failed to get slice statistics: {}", e)))?;
let stats_container = SliceStatsContainer { collections, collection_stats, total_lexicons, total_records, total_actors, };
Ok(Some(FieldValue::owned_any(stats_container))) }) }, ) .description("Get statistics for this slice including collection counts, record counts, and actor counts") )}