@@ -12,124 +12,14 @@ use crate::controllers::helpers::pagination::PaginationOptions;
12
12
13
13
use crate :: models:: {
14
14
Category , Crate , CrateCategory , CrateKeyword , CrateVersions , Keyword , RecentCrateDownloads ,
15
- TopVersions , User , Version , VersionOwnerAction ,
15
+ User , Version , VersionOwnerAction ,
16
16
} ;
17
17
use crate :: schema:: * ;
18
18
use crate :: util:: errors:: crate_not_found;
19
19
use crate :: views:: {
20
20
EncodableCategory , EncodableCrate , EncodableDependency , EncodableKeyword , EncodableVersion ,
21
21
} ;
22
22
23
- /// Handles the `GET /summary` route.
24
- pub async fn summary ( state : AppState ) -> AppResult < Json < Value > > {
25
- spawn_blocking ( move || {
26
- let config = & state. config ;
27
-
28
- let conn = & mut * state. db_read ( ) ?;
29
- let num_crates: i64 = crates:: table. count ( ) . get_result ( conn) ?;
30
- let num_downloads: i64 = metadata:: table
31
- . select ( metadata:: total_downloads)
32
- . get_result ( conn) ?;
33
-
34
- fn encode_crates (
35
- conn : & mut PgConnection ,
36
- data : Vec < ( Crate , Option < i64 > ) > ,
37
- ) -> AppResult < Vec < EncodableCrate > > {
38
- let recent_downloads = data. iter ( ) . map ( |& ( _, s) | s) . collect :: < Vec < _ > > ( ) ;
39
-
40
- let krates = data. into_iter ( ) . map ( |( c, _) | c) . collect :: < Vec < _ > > ( ) ;
41
-
42
- let versions: Vec < Version > = krates. versions ( ) . load ( conn) ?;
43
- versions
44
- . grouped_by ( & krates)
45
- . into_iter ( )
46
- . map ( TopVersions :: from_versions)
47
- . zip ( krates)
48
- . zip ( recent_downloads)
49
- . map ( |( ( top_versions, krate) , recent_downloads) | {
50
- Ok ( EncodableCrate :: from_minimal (
51
- krate,
52
- Some ( & top_versions) ,
53
- None ,
54
- false ,
55
- recent_downloads,
56
- ) )
57
- } )
58
- . collect ( )
59
- }
60
-
61
- let selection = (
62
- Crate :: as_select ( ) ,
63
- recent_crate_downloads:: downloads. nullable ( ) ,
64
- ) ;
65
-
66
- let new_crates = crates:: table
67
- . left_join ( recent_crate_downloads:: table)
68
- . order ( crates:: created_at. desc ( ) )
69
- . select ( selection)
70
- . limit ( 10 )
71
- . load ( conn) ?;
72
- let just_updated = crates:: table
73
- . left_join ( recent_crate_downloads:: table)
74
- . filter ( crates:: updated_at. ne ( crates:: created_at) )
75
- . order ( crates:: updated_at. desc ( ) )
76
- . select ( selection)
77
- . limit ( 10 )
78
- . load ( conn) ?;
79
-
80
- let mut most_downloaded_query = crates:: table
81
- . left_join ( recent_crate_downloads:: table)
82
- . into_boxed ( ) ;
83
- if !config. excluded_crate_names . is_empty ( ) {
84
- most_downloaded_query =
85
- most_downloaded_query. filter ( crates:: name. ne_all ( & config. excluded_crate_names ) ) ;
86
- }
87
- let most_downloaded = most_downloaded_query
88
- . then_order_by ( crates:: downloads. desc ( ) )
89
- . select ( selection)
90
- . limit ( 10 )
91
- . load ( conn) ?;
92
-
93
- let mut most_recently_downloaded_query = crates:: table
94
- . inner_join ( recent_crate_downloads:: table)
95
- . into_boxed ( ) ;
96
- if !config. excluded_crate_names . is_empty ( ) {
97
- most_recently_downloaded_query = most_recently_downloaded_query
98
- . filter ( crates:: name. ne_all ( & config. excluded_crate_names ) ) ;
99
- }
100
- let most_recently_downloaded = most_recently_downloaded_query
101
- . then_order_by ( recent_crate_downloads:: downloads. desc ( ) )
102
- . select ( selection)
103
- . limit ( 10 )
104
- . load ( conn) ?;
105
-
106
- let popular_keywords = keywords:: table
107
- . order ( keywords:: crates_cnt. desc ( ) )
108
- . limit ( 10 )
109
- . load ( conn) ?
110
- . into_iter ( )
111
- . map ( Keyword :: into)
112
- . collect :: < Vec < EncodableKeyword > > ( ) ;
113
-
114
- let popular_categories = Category :: toplevel ( conn, "crates" , 10 , 0 ) ?
115
- . into_iter ( )
116
- . map ( Category :: into)
117
- . collect :: < Vec < EncodableCategory > > ( ) ;
118
-
119
- Ok ( Json ( json ! ( {
120
- "num_downloads" : num_downloads,
121
- "num_crates" : num_crates,
122
- "new_crates" : encode_crates( conn, new_crates) ?,
123
- "most_downloaded" : encode_crates( conn, most_downloaded) ?,
124
- "most_recently_downloaded" : encode_crates( conn, most_recently_downloaded) ?,
125
- "just_updated" : encode_crates( conn, just_updated) ?,
126
- "popular_keywords" : popular_keywords,
127
- "popular_categories" : popular_categories,
128
- } ) ) )
129
- } )
130
- . await
131
- }
132
-
133
23
/// Handles the `GET /crates/new` special case.
134
24
pub async fn show_new ( app : AppState , req : Parts ) -> AppResult < Json < Value > > {
135
25
show ( app, Path ( "new" . to_string ( ) ) , req) . await
0 commit comments