systemprompt_database/repository/service/
listing.rs1use systemprompt_identifiers::{InstanceId, ServiceName};
7use systemprompt_manifest::services::ServiceModule;
8
9use super::model::{ServiceConfig, ServiceRow, into_configs};
10use super::repo::ServiceRepository;
11use crate::error::DatabaseResult;
12
13impl ServiceRepository {
14 pub async fn list_all_agent_service_names(&self) -> DatabaseResult<Vec<ServiceName>> {
15 let rows = sqlx::query!(
16 r#"SELECT name FROM services WHERE instance_id = $1 AND module_name = 'agent'"#,
17 self.instance_id.as_str()
18 )
19 .fetch_all(&*self.pool)
20 .await?;
21 Ok(rows.into_iter().map(|r| ServiceName::new(r.name)).collect())
22 }
23
24 pub async fn list_mcp_services(&self) -> DatabaseResult<Vec<ServiceConfig>> {
25 self.list_services_by_type(ServiceModule::Mcp).await
26 }
27
28 pub async fn list_all_running_services(&self) -> DatabaseResult<Vec<ServiceConfig>> {
29 let rows = sqlx::query_as!(
30 ServiceRow,
31 r#"
32 SELECT instance_id as "instance_id: InstanceId", name, module_name, status, pid, port, binary_mtime,
33 heartbeat_at::text as "heartbeat_at!",
34 created_at::text as "created_at!", updated_at::text as "updated_at!"
35 FROM services
36 WHERE instance_id = $1 AND status = 'running'
37 ORDER BY name
38 "#,
39 self.instance_id.as_str()
40 )
41 .fetch_all(&*self.pool)
42 .await?;
43 into_configs(rows)
44 }
45
46 pub async fn list_running_services_by_module(
47 &self,
48 module_name: ServiceModule,
49 ) -> DatabaseResult<Vec<ServiceConfig>> {
50 let rows = sqlx::query_as!(
51 ServiceRow,
52 r#"
53 SELECT instance_id as "instance_id: InstanceId", name, module_name, status, pid, port, binary_mtime,
54 heartbeat_at::text as "heartbeat_at!",
55 created_at::text as "created_at!", updated_at::text as "updated_at!"
56 FROM services
57 WHERE instance_id = $1 AND module_name = $2 AND status = 'running'
58 ORDER BY name
59 "#,
60 self.instance_id.as_str(),
61 module_name.as_str()
62 )
63 .fetch_all(&*self.pool)
64 .await?;
65 into_configs(rows)
66 }
67
68 pub async fn count_running_services(
69 &self,
70 module_name: ServiceModule,
71 ) -> DatabaseResult<usize> {
72 let row = sqlx::query!(
73 r#"SELECT COUNT(*) as "count!" FROM services
74 WHERE instance_id = $1 AND module_name = $2 AND status = 'running'"#,
75 self.instance_id.as_str(),
76 module_name.as_str()
77 )
78 .fetch_one(&*self.pool)
79 .await?;
80 Ok(usize::try_from(row.count).unwrap_or(0))
81 }
82
83 pub async fn list_running_services_with_pid(&self) -> DatabaseResult<Vec<ServiceConfig>> {
84 self.list_all_running_services().await
85 }
86
87 pub async fn list_services_by_type(
88 &self,
89 module_name: ServiceModule,
90 ) -> DatabaseResult<Vec<ServiceConfig>> {
91 let rows = sqlx::query_as!(
92 ServiceRow,
93 r#"
94 SELECT instance_id as "instance_id: InstanceId", name, module_name, status, pid, port, binary_mtime,
95 heartbeat_at::text as "heartbeat_at!",
96 created_at::text as "created_at!", updated_at::text as "updated_at!"
97 FROM services
98 WHERE instance_id = $1 AND module_name = $2
99 ORDER BY name
100 "#,
101 self.instance_id.as_str(),
102 module_name.as_str()
103 )
104 .fetch_all(&*self.pool)
105 .await?;
106 into_configs(rows)
107 }
108}