@@ -4,37 +4,36 @@ use std::collections::HashMap;
44
55use conduit:: { Request , Response } ;
66use conduit_router:: RequestParams ;
7- use diesel:: prelude:: * ;
87use diesel:: pg:: PgConnection ;
98use diesel:: pg:: upsert:: * ;
9+ use diesel:: prelude:: * ;
1010use diesel_full_text_search:: * ;
1111use license_exprs;
1212use pg:: GenericConnection ;
1313use pg:: rows:: Row ;
14- use pg;
1514use rustc_serialize:: hex:: ToHex ;
1615use rustc_serialize:: json;
1716use semver;
1817use time:: { Timespec , Duration } ;
1918use url:: Url ;
2019
21- use { Model , User , Keyword , Version , Category , Badge , Replica } ;
2220use app:: { App , RequestApp } ;
21+ use badge:: EncodableBadge ;
22+ use category:: EncodableCategory ;
2323use db:: RequestTransaction ;
2424use dependency:: { Dependency , EncodableDependency } ;
2525use download:: { VersionDownload , EncodableVersionDownload } ;
2626use git;
2727use keyword:: EncodableKeyword ;
28- use category :: EncodableCategory ;
29- use badge :: EncodableBadge ;
28+ use owner :: { EncodableOwner , Owner , Rights , OwnerKind , Team , rights , CrateOwner } ;
29+ use schema :: * ;
3030use upload;
3131use user:: RequestUser ;
32- use owner:: { EncodableOwner , Owner , Rights , OwnerKind , Team , rights, CrateOwner } ;
3332use util:: errors:: NotFound ;
3433use util:: { read_le_u32, read_fill} ;
3534use util:: { RequestUtils , CargoResult , internal, ChainError , human} ;
3635use version:: EncodableVersion ;
37- use schema :: * ;
36+ use { Model , User , Keyword , Version , Category , Badge , Replica } ;
3837
3938#[ derive( Clone , Queryable , Identifiable , AsChangeset ) ]
4039pub struct Crate {
@@ -719,40 +718,50 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
719718
720719/// Handles the `GET /summary` route.
721720pub fn summary ( req : & mut Request ) -> CargoResult < Response > {
722- let tx = req. tx ( ) ?;
723- let num_crates = Crate :: count ( tx) ?;
724- let num_downloads = {
725- let stmt = tx. prepare ( "SELECT total_downloads FROM metadata" ) ?;
726- let rows = stmt. query ( & [ ] ) ?;
727- rows. iter ( ) . next ( ) . unwrap ( ) . get ( "total_downloads" )
728- } ;
721+ use schema:: crates:: dsl:: * ;
729722
730- let to_crates = |stmt : pg:: stmt:: Statement | -> CargoResult < Vec < _ > > {
731- let rows = stmt. query ( & [ ] ) ?;
732- rows. iter ( ) . map ( |r| {
733- let krate: Crate = Model :: from_row ( & r) ;
734- let max_version = krate. max_version ( tx) ?;
735- Ok ( krate. minimal_encodable ( max_version, None ) )
736- } ) . collect ( )
723+ let conn = req. db_conn ( ) ?;
724+ let num_crates = crates. count ( ) . get_result ( conn) ?;
725+ let num_downloads = metadata:: table. select ( metadata:: total_downloads)
726+ . get_result ( conn) ?;
727+
728+ let encode_crates = |krates : Vec < Crate > | -> CargoResult < Vec < _ > > {
729+ Version :: belonging_to ( & krates)
730+ . load :: < Version > ( conn) ?
731+ . grouped_by ( & krates)
732+ . into_iter ( )
733+ . map ( |versions| Version :: max ( versions. into_iter ( ) . map ( |v| v. num ) ) )
734+ . zip ( krates)
735+ . map ( |( max_version, krate) | {
736+ Ok ( krate. minimal_encodable ( max_version, None ) )
737+ } ) . collect ( )
737738 } ;
738- let new_crates = tx. prepare ( "SELECT * FROM crates \
739- ORDER BY created_at DESC LIMIT 10") ?;
740- let just_updated = tx. prepare ( "SELECT * FROM crates \
741- WHERE updated_at::timestamp(0) !=
742- created_at::timestamp(0)
743- ORDER BY updated_at DESC LIMIT 10" ) ?;
744- let most_downloaded = tx. prepare ( "SELECT * FROM crates \
745- ORDER BY downloads DESC LIMIT 10") ?;
746-
747- let popular_keywords = Keyword :: all ( tx, "crates" , 10 , 0 ) ?;
748- let popular_keywords = popular_keywords. into_iter ( )
749- . map ( Keyword :: encodable)
750- . collect ( ) ;
751-
752- let popular_categories = Category :: toplevel ( tx, "crates" , 10 , 0 ) ?;
753- let popular_categories = popular_categories. into_iter ( )
754- . map ( Category :: encodable)
755- . collect ( ) ;
739+
740+ let new_crates = crates. order ( created_at. desc ( ) )
741+ . select ( ALL_COLUMNS )
742+ . limit ( 10 )
743+ . load ( conn) ?;
744+ let just_updated = crates. filter ( updated_at. ne ( created_at) )
745+ . order ( updated_at. desc ( ) )
746+ . select ( ALL_COLUMNS )
747+ . limit ( 10 )
748+ . load ( conn) ?;
749+ let most_downloaded = crates. order ( downloads. desc ( ) )
750+ . select ( ALL_COLUMNS )
751+ . limit ( 10 )
752+ . load ( conn) ?;
753+
754+ let popular_keywords = keywords:: table. order ( keywords:: crates_cnt. desc ( ) )
755+ . limit ( 10 )
756+ . load ( conn) ?
757+ . into_iter ( )
758+ . map ( Keyword :: encodable)
759+ . collect ( ) ;
760+
761+ let popular_categories = Category :: toplevel ( conn, "crates" , 10 , 0 ) ?
762+ . into_iter ( )
763+ . map ( Category :: encodable)
764+ . collect ( ) ;
756765
757766 #[ derive( RustcEncodable ) ]
758767 struct R {
@@ -767,9 +776,9 @@ pub fn summary(req: &mut Request) -> CargoResult<Response> {
767776 Ok ( req. json ( & R {
768777 num_downloads : num_downloads,
769778 num_crates : num_crates,
770- new_crates : to_crates ( new_crates) ?,
771- most_downloaded : to_crates ( most_downloaded) ?,
772- just_updated : to_crates ( just_updated) ?,
779+ new_crates : encode_crates ( new_crates) ?,
780+ most_downloaded : encode_crates ( most_downloaded) ?,
781+ just_updated : encode_crates ( just_updated) ?,
773782 popular_keywords : popular_keywords,
774783 popular_categories : popular_categories,
775784 } ) )
0 commit comments