@@ -8,6 +8,13 @@ use std::collections::HashMap;
88use std:: sync:: { Arc , RwLock } ;
99use tauri:: { AppHandle , Runtime } ;
1010
11+ #[ derive( serde:: Deserialize , Debug ) ]
12+ pub struct GetDatasourcesByServerOptions {
13+ pub from : Option < u32 > ,
14+ pub size : Option < u32 > ,
15+ pub query : Option < String > ,
16+ }
17+
1118lazy_static ! {
1219 static ref DATASOURCE_CACHE : Arc <RwLock <HashMap <String , HashMap <String , DataSource >>>> =
1320 Arc :: new( RwLock :: new( HashMap :: new( ) ) ) ;
@@ -25,7 +32,7 @@ pub fn save_datasource_to_cache(server_id: &str, datasources: Vec<DataSource>) {
2532#[ allow( dead_code) ]
2633pub fn get_datasources_from_cache ( server_id : & str ) -> Option < HashMap < String , DataSource > > {
2734 let cache = DATASOURCE_CACHE . read ( ) . unwrap ( ) ; // Acquire read lock
28- // dbg!("cache: {:?}", &cache);
35+ // dbg!("cache: {:?}", &cache);
2936 let server_cache = cache. get ( server_id) ?; // Get the server's cache
3037 Some ( server_cache. clone ( ) )
3138}
@@ -41,7 +48,7 @@ pub async fn refresh_all_datasources<R: Runtime>(_app_handle: &AppHandle<R>) ->
4148 // dbg!("fetch datasources for server: {}", &server.id);
4249
4350 // Attempt to get datasources by server, and continue even if it fails
44- let connectors = match get_datasources_by_server ( server. id . as_str ( ) ) . await {
51+ let connectors = match get_datasources_by_server ( server. id . as_str ( ) , None ) . await {
4552 Ok ( connectors) => {
4653 // Process connectors only after fetching them
4754 let connectors_map: HashMap < String , DataSource > = connectors
@@ -83,13 +90,48 @@ pub async fn refresh_all_datasources<R: Runtime>(_app_handle: &AppHandle<R>) ->
8390}
8491
8592#[ tauri:: command]
86- pub async fn get_datasources_by_server ( id : & str ) -> Result < Vec < DataSource > , String > {
93+ pub async fn get_datasources_by_server (
94+ id : & str ,
95+ options : Option < GetDatasourcesByServerOptions > ,
96+ ) -> Result < Vec < DataSource > , String > {
97+ let from = options. as_ref ( ) . and_then ( |opt| opt. from ) . unwrap_or ( 0 ) ;
98+ let size = options. as_ref ( ) . and_then ( |opt| opt. size ) . unwrap_or ( 10000 ) ;
99+ let query = options
100+ . and_then ( |opt| opt. query )
101+ . unwrap_or ( String :: default ( ) ) ;
102+
103+ let mut body = serde_json:: json!( {
104+ "from" : from,
105+ "size" : size,
106+ } ) ;
107+
108+ if !query. is_empty ( ) {
109+ body[ "query" ] = serde_json:: json!( {
110+ "bool" : {
111+ "must" : [ {
112+ "query_string" : {
113+ "fields" : [ "combined_fulltext" ] ,
114+ "query" : query,
115+ "fuzziness" : "AUTO" ,
116+ "fuzzy_prefix_length" : 2 ,
117+ "fuzzy_max_expansions" : 10 ,
118+ "fuzzy_transpositions" : true ,
119+ "allow_leading_wildcard" : false
120+ }
121+ } ]
122+ }
123+ } ) ;
124+ }
125+
87126 // Perform the async HTTP request outside the cache lock
88- let resp = HttpClient :: get ( id, "/datasource/_search" , None )
89- . await
90- . map_err ( |e| {
91- format ! ( "Error fetching datasource: {}" , e)
92- } ) ?;
127+ let resp = HttpClient :: post (
128+ id,
129+ "/datasource/_search" ,
130+ None ,
131+ Some ( reqwest:: Body :: from ( body. to_string ( ) ) ) ,
132+ )
133+ . await
134+ . map_err ( |e| format ! ( "Error fetching datasource: {}" , e) ) ?;
93135
94136 // Parse the search results from the response
95137 let datasources: Vec < DataSource > = parse_search_results ( resp) . await . map_err ( |e| {
0 commit comments