@@ -6,8 +6,9 @@ use crate::schema::{
66use crate :: util:: errors:: AppResult ;
77use crate :: views:: { EncodableCategory , EncodableCrate , EncodableKeyword } ;
88use axum:: Json ;
9- use diesel:: QueryResult ;
9+ use diesel:: { ExpressionMethods , JoinOnDsl , NullableExpressionMethods , QueryDsl , SelectableHelper } ;
1010use diesel_async:: AsyncPgConnection ;
11+ use diesel_async:: RunQueryDsl ;
1112use serde_json:: Value ;
1213
1314/// Handles the `GET /summary` route.
@@ -20,123 +21,11 @@ pub async fn summary(state: AppState) -> AppResult<Json<Value>> {
2021 . map ( Category :: into)
2122 . collect :: < Vec < EncodableCategory > > ( ) ;
2223
23- async fn inner (
24- conn : & mut AsyncPgConnection ,
25- config : & crate :: config:: Server ,
26- ) -> QueryResult < (
27- i64 ,
28- i64 ,
29- Vec < Record > ,
30- Vec < Record > ,
31- Vec < Record > ,
32- Vec < Record > ,
33- Vec < EncodableKeyword > ,
34- ) > {
35- use diesel:: {
36- ExpressionMethods , JoinOnDsl , NullableExpressionMethods , QueryDsl , SelectableHelper ,
37- } ;
38- use diesel_async:: RunQueryDsl ;
39-
40- let num_crates: i64 = crates:: table. count ( ) . get_result ( conn) . await ?;
41- let num_downloads: i64 = metadata:: table
42- . select ( metadata:: total_downloads)
43- . get_result ( conn)
44- . await ?;
45-
46- let selection = (
47- Crate :: as_select ( ) ,
48- crate_downloads:: downloads,
49- recent_crate_downloads:: downloads. nullable ( ) ,
50- versions:: num. nullable ( ) ,
51- versions:: yanked. nullable ( ) ,
52- ) ;
53-
54- let new_crates = crates:: table
55- . inner_join ( crate_downloads:: table)
56- . left_join ( recent_crate_downloads:: table)
57- . left_join ( default_versions:: table)
58- . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
59- . order ( crates:: created_at. desc ( ) )
60- . select ( selection)
61- . limit ( 10 )
62- . load ( conn)
63- . await ?;
64- let just_updated = crates:: table
65- . inner_join ( crate_downloads:: table)
66- . left_join ( recent_crate_downloads:: table)
67- . left_join ( default_versions:: table)
68- . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
69- . filter ( crates:: updated_at. ne ( crates:: created_at) )
70- . order ( crates:: updated_at. desc ( ) )
71- . select ( selection)
72- . limit ( 10 )
73- . load ( conn)
74- . await ?;
75-
76- let mut most_downloaded_query = crates:: table
77- . inner_join ( crate_downloads:: table)
78- . left_join ( recent_crate_downloads:: table)
79- . left_join ( default_versions:: table)
80- . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
81- . into_boxed ( ) ;
82- if !config. excluded_crate_names . is_empty ( ) {
83- most_downloaded_query =
84- most_downloaded_query. filter ( crates:: name. ne_all ( & config. excluded_crate_names ) ) ;
85- }
86- let most_downloaded = most_downloaded_query
87- . then_order_by ( crate_downloads:: downloads. desc ( ) )
88- . select ( selection)
89- . limit ( 10 )
90- . load ( conn)
91- . await ?;
92-
93- let mut most_recently_downloaded_query = crates:: table
94- . inner_join ( crate_downloads:: table)
95- . inner_join ( recent_crate_downloads:: table)
96- . left_join ( default_versions:: table)
97- . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
98- . into_boxed ( ) ;
99- if !config. excluded_crate_names . is_empty ( ) {
100- most_recently_downloaded_query = most_recently_downloaded_query
101- . filter ( crates:: name. ne_all ( & config. excluded_crate_names ) ) ;
102- }
103- let most_recently_downloaded = most_recently_downloaded_query
104- . then_order_by ( recent_crate_downloads:: downloads. desc ( ) )
105- . select ( selection)
106- . limit ( 10 )
107- . load ( conn)
108- . await ?;
109-
110- let popular_keywords = keywords:: table
111- . order ( keywords:: crates_cnt. desc ( ) )
112- . limit ( 10 )
113- . load ( conn)
114- . await ?
115- . into_iter ( )
116- . map ( Keyword :: into)
117- . collect :: < Vec < EncodableKeyword > > ( ) ;
118-
119- Ok ( (
120- num_crates,
121- num_downloads,
122- new_crates,
123- just_updated,
124- most_downloaded,
125- most_recently_downloaded,
126- popular_keywords,
127- ) )
128- }
129-
130- let config = & state. config ;
131- let (
132- num_crates,
133- num_downloads,
134- new_crates,
135- just_updated,
136- most_downloaded,
137- most_recently_downloaded,
138- popular_keywords,
139- ) = inner ( & mut conn, config) . await ?;
24+ let num_crates: i64 = crates:: table. count ( ) . get_result ( & mut conn) . await ?;
25+ let num_downloads: i64 = metadata:: table
26+ . select ( metadata:: total_downloads)
27+ . get_result ( & mut conn)
28+ . await ?;
14029
14130 async fn encode_crates (
14231 conn : & mut AsyncPgConnection ,
@@ -168,6 +57,81 @@ pub async fn summary(state: AppState) -> AppResult<Json<Value>> {
16857 . collect ( )
16958 }
17059
60+ let config = & state. config ;
61+
62+ let selection = (
63+ Crate :: as_select ( ) ,
64+ crate_downloads:: downloads,
65+ recent_crate_downloads:: downloads. nullable ( ) ,
66+ versions:: num. nullable ( ) ,
67+ versions:: yanked. nullable ( ) ,
68+ ) ;
69+
70+ let new_crates = crates:: table
71+ . inner_join ( crate_downloads:: table)
72+ . left_join ( recent_crate_downloads:: table)
73+ . left_join ( default_versions:: table)
74+ . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
75+ . order ( crates:: created_at. desc ( ) )
76+ . select ( selection)
77+ . limit ( 10 )
78+ . load ( & mut conn)
79+ . await ?;
80+ let just_updated = crates:: table
81+ . inner_join ( crate_downloads:: table)
82+ . left_join ( recent_crate_downloads:: table)
83+ . left_join ( default_versions:: table)
84+ . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
85+ . filter ( crates:: updated_at. ne ( crates:: created_at) )
86+ . order ( crates:: updated_at. desc ( ) )
87+ . select ( selection)
88+ . limit ( 10 )
89+ . load ( & mut conn)
90+ . await ?;
91+
92+ let mut most_downloaded_query = crates:: table
93+ . inner_join ( crate_downloads:: table)
94+ . left_join ( recent_crate_downloads:: table)
95+ . left_join ( default_versions:: table)
96+ . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
97+ . into_boxed ( ) ;
98+ if !config. excluded_crate_names . is_empty ( ) {
99+ most_downloaded_query =
100+ most_downloaded_query. filter ( crates:: name. ne_all ( & config. excluded_crate_names ) ) ;
101+ }
102+ let most_downloaded = most_downloaded_query
103+ . then_order_by ( crate_downloads:: downloads. desc ( ) )
104+ . select ( selection)
105+ . limit ( 10 )
106+ . load ( & mut conn)
107+ . await ?;
108+
109+ let mut most_recently_downloaded_query = crates:: table
110+ . inner_join ( crate_downloads:: table)
111+ . inner_join ( recent_crate_downloads:: table)
112+ . left_join ( default_versions:: table)
113+ . left_join ( versions:: table. on ( default_versions:: version_id. eq ( versions:: id) ) )
114+ . into_boxed ( ) ;
115+ if !config. excluded_crate_names . is_empty ( ) {
116+ most_recently_downloaded_query = most_recently_downloaded_query
117+ . filter ( crates:: name. ne_all ( & config. excluded_crate_names ) ) ;
118+ }
119+ let most_recently_downloaded = most_recently_downloaded_query
120+ . then_order_by ( recent_crate_downloads:: downloads. desc ( ) )
121+ . select ( selection)
122+ . limit ( 10 )
123+ . load ( & mut conn)
124+ . await ?;
125+
126+ let popular_keywords = keywords:: table
127+ . order ( keywords:: crates_cnt. desc ( ) )
128+ . limit ( 10 )
129+ . load ( & mut conn)
130+ . await ?
131+ . into_iter ( )
132+ . map ( Keyword :: into)
133+ . collect :: < Vec < EncodableKeyword > > ( ) ;
134+
171135 Ok ( Json ( json ! ( {
172136 "num_downloads" : num_downloads,
173137 "num_crates" : num_crates,
0 commit comments