Skip to content

Show most popular keywords and categories on the home page #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/styles/home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@

#home-crates {
@include flex-wrap(wrap);
@include justify-content(center);
@include justify-content(left);

> div {
margin: 0;
Expand Down
10 changes: 10 additions & 0 deletions app/templates/components/category-list.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul>
{{#each categories as |category|}}
<li>
{{#link-to 'category' category.slug class='name'}}
<span>{{ category.category }} ({{ format-num category.crates_cnt }})</span>
<img class="right-arrow" src="/assets/right-arrow.png"/>
{{/link-to}}
</li>
{{/each}}
</ul>
10 changes: 10 additions & 0 deletions app/templates/components/keyword-list.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul>
{{#each keywords as |keyword|}}
<li>
{{#link-to 'keyword' keyword class='name'}}
<span>{{ keyword.id }} ({{ format-num keyword.crates_cnt }})</span>
<img class="right-arrow" src="/assets/right-arrow.png"/>
{{/link-to}}
</li>
{{/each}}
</ul>
8 changes: 8 additions & 0 deletions app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@
<h2>Just Updated</h2>
{{crate-list crates=model.just_updated}}
</div>
<div id='keywords'>
<h2>Popular Keywords {{#link-to 'keywords'}}(see all){{/link-to}}</h2>
{{keyword-list keywords=model.popular_keywords}}
</div>
<div id='categories'>
<h2>Popular Categories {{#link-to 'categories'}}(see all){{/link-to}}</h2>
{{category-list categories=model.popular_categories}}
</div>
</div>
63 changes: 36 additions & 27 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,40 @@ impl Category {
Ok(rows.iter().next().unwrap().get("count"))
}

pub fn toplevel(conn: &GenericConnection,
sort: &str,
limit: i64,
offset: i64) -> CargoResult<Vec<Category>> {

let sort_sql = match sort {
"crates" => "ORDER BY crates_cnt DESC",
_ => "ORDER BY category ASC",
};

// Collect all the top-level categories and sum up the crates_cnt of
// the crates in all subcategories
let stmt = try!(conn.prepare(&format!(
"SELECT c.id, c.category, c.slug, c.description, c.created_at, \
COALESCE (( \
SELECT sum(c2.crates_cnt)::int \
FROM categories as c2 \
WHERE c2.slug = c.slug \
OR c2.slug LIKE c.slug || '::%' \
), 0) as crates_cnt \
FROM categories as c \
WHERE c.category NOT LIKE '%::%' {} \
LIMIT $1 OFFSET $2",
sort_sql
)));

let categories: Vec<_> = try!(stmt.query(&[&limit, &offset]))
.iter()
.map(|row| Model::from_row(&row))
.collect();

Ok(categories)
}

pub fn subcategories(&self, conn: &GenericConnection)
-> CargoResult<Vec<Category>> {
let stmt = try!(conn.prepare("\
Expand Down Expand Up @@ -183,34 +217,9 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
let (offset, limit) = try!(req.pagination(10, 100));
let query = req.query();
let sort = query.get("sort").map_or("alpha", String::as_str);
let sort_sql = match sort {
"crates" => "ORDER BY crates_cnt DESC",
_ => "ORDER BY category ASC",
};

// Collect all the top-level categories and sum up the crates_cnt of
// the crates in all subcategories
let stmt = try!(conn.prepare(&format!(
"SELECT c.id, c.category, c.slug, c.description, c.created_at, \
COALESCE (( \
SELECT sum(c2.crates_cnt)::int \
FROM categories as c2 \
WHERE c2.slug = c.slug \
OR c2.slug LIKE c.slug || '::%' \
), 0) as crates_cnt \
FROM categories as c \
WHERE c.category NOT LIKE '%::%' {} \
LIMIT $1 OFFSET $2",
sort_sql
)));

let categories: Vec<_> = try!(stmt.query(&[&limit, &offset]))
.iter()
.map(|row| {
let category: Category = Model::from_row(&row);
category.encodable()
})
.collect();
let categories = try!(Category::toplevel(conn, sort, limit, offset));
let categories = categories.into_iter().map(Category::encodable).collect();

// Query for the total count of categories
let total = try!(Category::count_toplevel(conn));
Expand Down
37 changes: 23 additions & 14 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ impl Keyword {
}))))
}

pub fn all(conn: &GenericConnection, sort: &str, limit: i64, offset: i64)
-> CargoResult<Vec<Keyword>> {

let sort_sql = match sort {
"crates" => "ORDER BY crates_cnt DESC",
_ => "ORDER BY keyword ASC",
};

let stmt = try!(conn.prepare(&format!("SELECT * FROM keywords {}
LIMIT $1 OFFSET $2",
sort_sql)));

let keywords: Vec<_> = try!(stmt.query(&[&limit, &offset]))
.iter()
.map(|row| Model::from_row(&row))
.collect();

Ok(keywords)
}

pub fn valid_name(name: &str) -> bool {
if name.len() == 0 { return false }
name.chars().next().unwrap().is_alphanumeric() &&
Expand Down Expand Up @@ -131,20 +151,9 @@ pub fn index(req: &mut Request) -> CargoResult<Response> {
let (offset, limit) = try!(req.pagination(10, 100));
let query = req.query();
let sort = query.get("sort").map(|s| &s[..]).unwrap_or("alpha");
let sort_sql = match sort {
"crates" => "ORDER BY crates_cnt DESC",
_ => "ORDER BY keyword ASC",
};

// Collect all the keywords
let stmt = try!(conn.prepare(&format!("SELECT * FROM keywords {}
LIMIT $1 OFFSET $2",
sort_sql)));
let mut keywords = Vec::new();
for row in try!(stmt.query(&[&limit, &offset])).iter() {
let keyword: Keyword = Model::from_row(&row);
keywords.push(keyword.encodable());
}

let keywords = try!(Keyword::all(conn, sort, limit, offset));
let keywords = keywords.into_iter().map(Keyword::encodable).collect();

// Query for the total count of keywords
let total = try!(Keyword::count(conn));
Expand Down
14 changes: 14 additions & 0 deletions src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,20 +650,34 @@ pub fn summary(req: &mut Request) -> CargoResult<Response> {
let most_downloaded = try!(tx.prepare("SELECT * FROM crates \
ORDER BY downloads DESC LIMIT 10"));

let popular_keywords = try!(Keyword::all(tx, "crates", 10, 0));
let popular_keywords = popular_keywords.into_iter()
.map(Keyword::encodable)
.collect();

let popular_categories = try!(Category::toplevel(tx, "crates", 10, 0));
let popular_categories = popular_categories.into_iter()
.map(Category::encodable)
.collect();

#[derive(RustcEncodable)]
struct R {
num_downloads: i64,
num_crates: i64,
new_crates: Vec<EncodableCrate>,
most_downloaded: Vec<EncodableCrate>,
just_updated: Vec<EncodableCrate>,
popular_keywords: Vec<EncodableKeyword>,
popular_categories: Vec<EncodableCategory>,
}
Ok(req.json(&R {
num_downloads: num_downloads,
num_crates: num_crates,
new_crates: try!(to_crates(new_crates)),
most_downloaded: try!(to_crates(most_downloaded)),
just_updated: try!(to_crates(just_updated)),
popular_keywords: popular_keywords,
popular_categories: popular_categories,
}))
}

Expand Down