diff --git a/src/admin/query/mod.rs b/src/admin/query/mod.rs index bebbb771..7fe6a2a2 100644 --- a/src/admin/query/mod.rs +++ b/src/admin/query/mod.rs @@ -3,10 +3,12 @@ pub(crate) mod appservice; pub(crate) mod globals; pub(crate) mod presence; pub(crate) mod room_alias; +pub(crate) mod room_state_cache; pub(crate) mod sending; pub(crate) mod users; use clap::Subcommand; +use room_state_cache::room_state_cache; use ruma::{ events::{room::message::RoomMessageEventContent, RoomAccountDataEventType}, RoomAliasId, RoomId, ServerName, UserId, @@ -38,6 +40,10 @@ pub(crate) enum QueryCommand { #[command(subcommand)] RoomAlias(RoomAlias), + /// - rooms/state_cache iterators and getters + #[command(subcommand)] + RoomStateCache(RoomStateCache), + /// - globals.rs iterators and getters #[command(subcommand)] Globals(Globals), @@ -127,6 +133,73 @@ pub(crate) enum RoomAlias { AllLocalAliases, } +#[cfg_attr(test, derive(Debug))] +#[derive(Subcommand)] +pub(crate) enum RoomStateCache { + RoomServers { + room_id: Box, + }, + + ServerRooms { + server: Box, + }, + + RoomMembers { + room_id: Box, + }, + + LocalUsersInRoom { + room_id: Box, + }, + + ActiveLocalUsersInRoom { + room_id: Box, + }, + + RoomJoinedCount { + room_id: Box, + }, + + RoomInvitedCount { + room_id: Box, + }, + + RoomUserOnceJoined { + room_id: Box, + }, + + RoomMembersInvited { + room_id: Box, + }, + + GetInviteCount { + room_id: Box, + user_id: Box, + }, + + GetLeftCount { + room_id: Box, + user_id: Box, + }, + + RoomsJoined { + user_id: Box, + }, + + RoomsLeft { + user_id: Box, + }, + + RoomsInvited { + user_id: Box, + }, + + InviteState { + user_id: Box, + room_id: Box, + }, +} + #[cfg_attr(test, derive(Debug))] #[derive(Subcommand)] /// All the getters and iterators from src/database/key_value/globals.rs @@ -216,6 +289,7 @@ pub(crate) async fn process(command: QueryCommand, _body: Vec<&str>) -> Result appservice(command).await?, QueryCommand::Presence(command) => presence(command).await?, QueryCommand::RoomAlias(command) => room_alias(command).await?, + QueryCommand::RoomStateCache(command) => room_state_cache(command).await?, QueryCommand::Globals(command) => globals(command).await?, QueryCommand::Sending(command) => sending(command).await?, QueryCommand::Users(command) => users(command).await?, diff --git a/src/admin/query/room_state_cache.rs b/src/admin/query/room_state_cache.rs new file mode 100644 index 00000000..5b8ff8bb --- /dev/null +++ b/src/admin/query/room_state_cache.rs @@ -0,0 +1,233 @@ +use ruma::events::room::message::RoomMessageEventContent; + +use super::RoomStateCache; +use crate::{services, Result}; + +pub(crate) async fn room_state_cache(subcommand: RoomStateCache) -> Result { + match subcommand { + RoomStateCache::RoomServers { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services() + .rooms + .state_cache + .room_servers(&room_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::ServerRooms { + server, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services().rooms.state_cache.server_rooms(&server).collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomMembers { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services() + .rooms + .state_cache + .room_members(&room_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::LocalUsersInRoom { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Vec<_> = services() + .rooms + .state_cache + .local_users_in_room(&room_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::ActiveLocalUsersInRoom { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Vec<_> = services() + .rooms + .state_cache + .active_local_users_in_room(&room_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomJoinedCount { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results = services().rooms.state_cache.room_joined_count(&room_id); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomInvitedCount { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results = services().rooms.state_cache.room_invited_count(&room_id); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomUserOnceJoined { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services() + .rooms + .state_cache + .room_useroncejoined(&room_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomMembersInvited { + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services() + .rooms + .state_cache + .room_members_invited(&room_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::GetInviteCount { + room_id, + user_id, + } => { + let timer = tokio::time::Instant::now(); + let results = services() + .rooms + .state_cache + .get_invite_count(&room_id, &user_id); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::GetLeftCount { + room_id, + user_id, + } => { + let timer = tokio::time::Instant::now(); + let results = services() + .rooms + .state_cache + .get_left_count(&room_id, &user_id); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomsJoined { + user_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services() + .rooms + .state_cache + .rooms_joined(&user_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomsInvited { + user_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services() + .rooms + .state_cache + .rooms_invited(&user_id) + .collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::RoomsLeft { + user_id, + } => { + let timer = tokio::time::Instant::now(); + let results: Result> = services().rooms.state_cache.rooms_left(&user_id).collect(); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + RoomStateCache::InviteState { + user_id, + room_id, + } => { + let timer = tokio::time::Instant::now(); + let results = services() + .rooms + .state_cache + .invite_state(&user_id, &room_id); + let query_time = timer.elapsed(); + + Ok(RoomMessageEventContent::text_html( + format!("Query completed in {query_time:?}:\n\n```\n{results:?}```"), + format!("

Query completed in {query_time:?}:

\n
{results:?}\n
"), + )) + }, + } +}