use std::sync::Arc;
use rho_protocol::{
ErrorCode, RpcError, SessionId, SessionInspectParams, SessionInspectResponse, SessionReport,
SessionReportData, SessionReportParams, SessionReportResponse,
};
use super::{LoadedSession, Sessions};
use crate::app::{
compaction_report::{session_compaction_report, CompactionReport},
context_report::ContextReport,
daemon_host::jobs::SessionJob,
};
struct ReadReport {
typed: SessionReportData,
shown: serde_json::Value,
}
impl Sessions {
pub(in crate::app::daemon_host) async fn inspect(
&self,
params: SessionInspectParams,
) -> Result<SessionInspectResponse, RpcError> {
let read = self.read_report(¶ms.session, params.report).await?;
Ok(SessionInspectResponse { report: read.shown })
}
pub(in crate::app::daemon_host) async fn report(
&self,
params: SessionReportParams,
) -> Result<SessionReportResponse, RpcError> {
let read = self.read_report(¶ms.session, params.report).await?;
Ok(SessionReportResponse::new(read.typed))
}
async fn read_report(
&self,
session_id: &SessionId,
report: SessionReport,
) -> Result<ReadReport, RpcError> {
let session = self.between_turns(session_id).await?;
let runtime = session.runtime.lock().await;
let runtime = runtime.as_ref().ok_or_else(not_loaded)?;
let (shown, typed) = match report {
SessionReport::Context => {
let report = runtime.context_report();
(
serde_json::to_value(ContextReport::from(&report)),
SessionReportData::Context(report),
)
}
SessionReport::Compaction => {
let report =
session_compaction_report(&runtime.compaction_diagnostics(&session.config()));
(
serde_json::to_value(CompactionReport::from(&report)),
SessionReportData::Compaction(Box::new(report)),
)
}
SessionReport::ProviderContext => {
let auths = super::options::available_auths(runtime.credentials()).await;
let report = runtime.provider_context_report(&session.config(), &auths);
(
serde_json::to_value(&report),
SessionReportData::ProviderContext((&report).into()),
)
}
SessionReport::Hooks => {
let snapshot = runtime.hooks_report();
(
serde_json::to_value(snapshot.report()),
SessionReportData::Hooks(snapshot.session_report()),
)
}
_ => {
return Err(RpcError::new(
ErrorCode::InvalidParams,
"this daemon does not know the report",
))
}
};
let shown =
shown.map_err(|error| RpcError::new(ErrorCode::InternalError, error.to_string()))?;
Ok(ReadReport { typed, shown })
}
pub(super) async fn between_turns(
&self,
session_id: &SessionId,
) -> Result<Arc<LoadedSession>, RpcError> {
self.refuse_when_stopping()?;
let session = self
.loaded_session(session_id)
.await?
.ok_or_else(not_loaded)?;
match self.jobs.session_job(&session.id) {
SessionJob::Busy(job) if job == session.job => {
return Err(RpcError::new(
ErrorCode::SessionBusy,
"the session is in a turn; ask again once it ends",
));
}
SessionJob::Busy(job) => {
return Err(RpcError::new(
ErrorCode::SessionBusy,
format!(
"job {job} is writing session {}; ask again once it ends",
session.id
),
));
}
SessionJob::New | SessionJob::Last(_) => {}
}
Ok(session)
}
}
pub(super) fn not_loaded() -> RpcError {
RpcError::new(
ErrorCode::SessionNotLoaded,
"the daemon has not loaded the session; attach to it first",
)
}
#[cfg(test)]
#[path = "inspect_tests.rs"]
mod tests;