Skip to content

Show and sort by downloads on reverse dependency page #531

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 7 commits into from
Feb 20, 2017
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
1 change: 1 addition & 0 deletions app/models/dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default DS.Model.extend({
default_features: DS.attr('boolean'),
features: DS.attr('string'),
kind: DS.attr('string'),
downloads: DS.attr('number'),

featureList: computed('features', function() {
return this.get('features').split(',');
Expand Down
2 changes: 2 additions & 0 deletions app/styles/crate.scss
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
}
.downloads { @include display-flex; @include align-items(center); }

.rev-dep-downloads {padding-left: 7px}

.quick-links {
ul {
@include display-flex;
Expand Down
11 changes: 6 additions & 5 deletions app/templates/crate/reverse-dependencies.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
</div>
</div>

<div id='crate-all-reverse-dependencies' class='white-rows'>
<div class='white-rows'>
{{#each model as |dependency|}}
<div class='row'>
<div class='crate row'>
<div>
{{#link-to 'crate' dependency.crate_id}}{{dependency.crate_id}}{{/link-to}} requires {{dependency.req}}
</div>
{{#link-to 'crate' dependency.crate_id class='arrow'}}
<img class="right-arrow-all-versions" src="/assets/right-arrow-all-versions.png"/>
{{/link-to}}
<div class='stats downloads'>
<img class='download-clear-back' src="/assets/download-clear-back.png"/>
<span class='num rev-dep-downloads'>{{ format-num dependency.downloads }}</span>
</div>
</div>
{{/each}}
</div>
Expand Down
16 changes: 13 additions & 3 deletions src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct EncodableDependency {
pub features: String,
pub target: Option<String>,
pub kind: Kind,
pub downloads: i32,
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -76,9 +77,17 @@ impl Dependency {
}
}

pub fn encodable(self, crate_name: &str) -> EncodableDependency {
let Dependency { id, version_id, crate_id: _, req, optional,
default_features, features, target, kind } = self;
// `downloads` need only be specified when generating a reverse dependency
pub fn encodable(self, crate_name: &str, downloads: Option<i32>) -> EncodableDependency {
let Dependency { id,
version_id,
crate_id: _,
req,
optional,
default_features,
features,
target,
kind } = self;
EncodableDependency {
id: id,
version_id: version_id,
Expand All @@ -89,6 +98,7 @@ impl Dependency {
features: features.join(","),
target: target,
kind: kind,
downloads: downloads.unwrap_or(0),
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,12 @@ impl Crate {
Ok(rows.iter().map(|r| Model::from_row(&r)).collect())
}

/// Returns (dependency, dependent crate name)
/// Returns (dependency, dependent crate name, dependent crate downloads)
pub fn reverse_dependencies(&self,
conn: &GenericConnection,
offset: i64,
limit: i64)
-> CargoResult<(Vec<(Dependency, String)>, i64)> {
-> CargoResult<(Vec<(Dependency, String, i32)>, i64)> {
let select_sql = "
FROM dependencies
INNER JOIN versions
Expand All @@ -435,21 +435,22 @@ impl Crate {
WHERE dependencies.crate_id = $1
AND versions.num = crates.max_version
";
let fetch_sql = format!("SELECT DISTINCT ON (crate_name)
let fetch_sql = format!("SELECT DISTINCT ON (crate_downloads, crate_name)
dependencies.*,
crates.downloads AS crate_downloads,
crates.name AS crate_name
{}
ORDER BY crate_name ASC
ORDER BY crate_downloads DESC
OFFSET $2
LIMIT $3", select_sql);
let count_sql = format!("SELECT COUNT(DISTINCT(crates.id)) {}",
LIMIT $3",
select_sql);
let count_sql = format!("SELECT COUNT(DISTINCT(crates.id)) {}", select_sql);

let stmt = try!(conn.prepare(&fetch_sql));
let vec: Vec<_> = try!(stmt.query(&[&self.id, &offset, &limit]))
.iter().map(|r| {
(Model::from_row(&r), r.get("crate_name"))
}).collect();
.iter()
.map(|r| (Model::from_row(&r), r.get("crate_name"), r.get("crate_downloads")))
.collect();
let stmt = try!(conn.prepare(&count_sql));
let cnt: i64 = try!(stmt.query(&[&self.id])).iter().next().unwrap().get(0);

Expand Down Expand Up @@ -1172,12 +1173,11 @@ pub fn reverse_dependencies(req: &mut Request) -> CargoResult<Response> {
let name = &req.params()["crate_id"];
let conn = try!(req.tx());
let krate = try!(Crate::find_by_name(conn, &name));
let tx = try!(req.tx());
let (offset, limit) = try!(req.pagination(10, 100));
let (rev_deps, total) = try!(krate.reverse_dependencies(tx, offset, limit));
let rev_deps = rev_deps.into_iter().map(|(dep, crate_name)| {
dep.encodable(&crate_name)
}).collect();
let (rev_deps, total) = try!(krate.reverse_dependencies(conn, offset, limit));
let rev_deps = rev_deps.into_iter()
.map(|(dep, crate_name, downloads)| dep.encodable(&crate_name, Some(downloads)))
.collect();

#[derive(RustcEncodable)]
struct R { dependencies: Vec<EncodableDependency>, meta: Meta }
Expand Down
2 changes: 1 addition & 1 deletion src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub fn dependencies(req: &mut Request) -> CargoResult<Response> {
let tx = try!(req.tx());
let deps = try!(version.dependencies(tx));
let deps = deps.into_iter().map(|(dep, crate_name)| {
dep.encodable(&crate_name)
dep.encodable(&crate_name, None)
}).collect();

#[derive(RustcEncodable)]
Expand Down