Skip to content

Commit f5086d9

Browse files
committed
Optimize get all_applications function from database
1 parent 9d2d761 commit f5086d9

File tree

2 files changed

+66
-43
lines changed

2 files changed

+66
-43
lines changed

fplus-database/src/database/applications.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,61 @@
1-
use sea_orm::{entity::*, query::*, DbErr};
1+
use sea_orm::{entity::*, query::*, DbBackend, DbErr};
22
use crate::models::applications::{Column, ActiveModel, Entity as Application, Model as ApplicationModel};
33
use crate::get_database_connection;
44
use sha1::{Sha1, Digest};
5+
use chrono::{DateTime, Utc, TimeZone};
56

67
/**
78
* Get all applications from the database
89
*
910
* # Returns
1011
* @return Result<Vec<ApplicationModel>, sea_orm::DbErr> - The result of the operation
1112
*/
12-
pub async fn get_applications() ->Result<Vec<ApplicationModel>, sea_orm::DbErr> {
13+
pub async fn get_applications() -> Result<Vec<ApplicationModel>, sea_orm::DbErr> {
1314
let conn = get_database_connection().await?;
14-
Application::find()
15-
.order_by(Column::Owner, Order::Asc)
16-
.order_by(Column::Repo, Order::Asc)
17-
.all(&conn).await
15+
16+
//Get all applications from the database.
17+
//Distinct on is not supported in sea_orm yet, so we have to use raw SQL
18+
let app_data = JsonValue::find_by_statement(Statement::from_sql_and_values(
19+
DbBackend::Postgres,
20+
r#"
21+
SELECT DISTINCT ON (owner, repo, id)
22+
a.id,
23+
a.owner,
24+
a.repo,
25+
a.pr_number,
26+
a.application,
27+
a.updated_at,
28+
a.sha,
29+
a.path
30+
FROM
31+
applications a
32+
ORDER BY
33+
a.owner,
34+
a.repo,
35+
a.id,
36+
a.pr_number DESC
37+
"#,
38+
[],
39+
))
40+
.all(&conn)
41+
.await?;
42+
43+
//Iterate over the results and convert them to ApplicationModel
44+
let mut applications: Vec<ApplicationModel> = Vec::new();
45+
for app in app_data {
46+
applications.push(ApplicationModel {
47+
id: app.get("id").unwrap().as_str().unwrap().to_string(),
48+
owner: app.get("owner").unwrap().as_str().unwrap().to_string(),
49+
repo: app.get("repo").unwrap().as_str().unwrap().to_string(),
50+
pr_number: app.get("pr_number").unwrap().as_i64().unwrap() as i64,
51+
application: Some(app.get("application").unwrap().as_str().unwrap().to_string()),
52+
updated_at: Utc.from_utc_datetime(&app.get("updated_at").unwrap().as_str().unwrap().parse::<DateTime<Utc>>().unwrap().naive_utc()),
53+
sha: Some(app.get("sha").unwrap().as_str().unwrap().to_string()),
54+
path: Some(app.get("path").unwrap().as_str().unwrap().to_string()),
55+
});
56+
}
57+
Ok(applications)
58+
1859
}
1960

2061
/**

fplus-lib/src/core/mod.rs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -290,44 +290,26 @@ impl LDNApplication {
290290
}
291291

292292
pub async fn all_applications() -> Result<Vec<(ApplicationFile, String, String)>, Vec<LDNError>> {
293-
//TODO: Avoid filtering by allocators. Simply get all active & merged applications from the database.
294-
let allocators = match database::allocators::get_allocators().await {
295-
Ok(allocs) => allocs,
296-
Err(e) => return Err(vec![LDNError::Load(format!("Failed to retrieve allocators: {}", e))]),
297-
}; let mut all_apps: Vec<(ApplicationFile, String, String)> = Vec::new();
298-
let mut errors: Vec<LDNError> = Vec::new();
299-
300-
for allocator in allocators {
301-
match Self::active(allocator.owner.clone(), allocator.repo.clone(), None).await {
302-
Ok(apps) => {
303-
for app in apps {
304-
all_apps.push((app, allocator.owner.clone(), allocator.repo.clone()));
305-
}
306-
},
307-
Err(e) => {
308-
errors.push(e);
309-
eprintln!("Failed to process active applications for allocator {}:{}", allocator.repo, allocator.owner);
310-
},
311-
}
312-
313-
match Self::merged(allocator.owner.clone(), allocator.repo.clone()).await {
314-
Ok(merged_apps) => {
315-
for (_, app) in merged_apps {
316-
all_apps.push((app, allocator.owner.clone(), allocator.repo.clone()));
317-
}
318-
},
319-
Err(e) => {
320-
errors.push(e);
321-
eprintln!("Failed to process merged applications for allocator {}:{}", allocator.repo, allocator.owner);
322-
},
323-
}
324-
}
325-
326-
if errors.is_empty() {
327-
Ok(all_apps)
328-
} else {
329-
Err(errors)
293+
let db_apps = database::applications::get_applications().await;
294+
let mut all_apps: Vec<(ApplicationFile, String, String)> = Vec::new();
295+
match db_apps {
296+
Ok(apps) => {
297+
for app in apps {
298+
let app_file = match ApplicationFile::from_str(&app.application.unwrap()) {
299+
Ok(app) => app,
300+
Err(e) => {
301+
return Err(vec![LDNError::Load(format!("Failed to parse application file from DB /// {}", e))]);
302+
}
303+
};
304+
all_apps.push((app_file, app.owner, app.repo));
305+
}
306+
return Ok(all_apps);
307+
},
308+
Err(e) => {
309+
return Err(vec![LDNError::Load(format!("Failed to retrieve applications from the database /// {}", e))]);
310+
},
330311
}
312+
331313
}
332314

333315

0 commit comments

Comments
 (0)