Skip to content

Add package dependents info #145

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
46 changes: 6 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ once_cell = "1.5.2"
opener = "0.5.0"
reqwest = { version = "0.11.18", features = ["blocking", "json"] }
rpassword = "5.0.1"
semver = { version = "0.11.0", features = ["serde"] }
semver = { version = "1.0.17", features = ["serde"] }
serde = { version = "1.0.116", features = ["derive"] }
serde_json = "1.0.58"
structopt = "0.3.18"
Expand Down
2 changes: 1 addition & 1 deletion src/package_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::package_name::PackageName;
/// * `roblox/[email protected]`
/// * `lpghatguy/[email protected]`
/// * `foo/bar@1`
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PackageReq {
name: PackageName,
version_req: VersionReq,
Expand Down
2 changes: 1 addition & 1 deletion wally-registry-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ reqwest = { version = "0.11.0", features = ["blocking", "json"] }
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "91f6288ea4aeb3d5a502b2f18b2b9677a85463ea", features = ["json"] }
rusoto_core = { version = "0.48.0", optional = true }
rusoto_s3 = { version = "0.48.0", optional = true }
semver = "0.11.0"
semver = "1.0.17"
serde = { version = "1.0.120", features = ["derive"] }
serde_json = "1.0.61"
tantivy = "0.16.1"
Expand Down
38 changes: 36 additions & 2 deletions wally-registry-backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod storage;
#[cfg(test)]
mod tests;

use std::collections::HashSet;
use std::convert::TryInto;
use std::io::{Cursor, Read, Seek};
use std::sync::RwLock;
Expand Down Expand Up @@ -38,7 +39,8 @@ use rocket::{
State,
};
use rocket::{Build, Request, Response};
use semver::Version;
use semver::{Version, VersionReq};
use serde::Serialize;
use serde_json::json;
use storage::StorageMode;
use zip::ZipArchive;
Expand Down Expand Up @@ -104,6 +106,37 @@ async fn package_info(
Ok(Json(serde_json::to_value(metadata)?))
}

// TODO! Move this to a proper home in some analytics module
#[derive(Serialize)]
struct AnalyticsMetadata {
dependents: Vec<(PackageName, VersionReq)>,
}

#[get("/v1/package-analytics/<scope>/<name>")]
async fn package_analytics(
_read: Result<ReadAccess, Error>,
search_backend: &State<RwLock<SearchBackend>>,
scope: String,
name: String,
) -> Result<Json<serde_json::Value>, Error> {
_read?;

let package_name = PackageName::new(scope, name)
.context("error parsing package name")
.status(Status::BadRequest)?;

if let Ok(search_backend) = search_backend.read() {
Ok(Json(serde_json::to_value(AnalyticsMetadata {
dependents: search_backend.get_dependents(&package_name),
})?))
} else {
Err(
format_err!("Unexpected error fetching analytics metadata. Try again later.")
.status(Status::InternalServerError),
)
}
}

#[get("/v1/package-search?<query>")]
async fn package_search(
search_backend: &State<RwLock<SearchBackend>>,
Expand Down Expand Up @@ -254,7 +287,8 @@ pub fn server(figment: Figment) -> rocket::Rocket<Build> {
package_contents,
publish,
package_info,
package_search
package_search,
package_analytics,
],
)
.manage(storage_backend)
Expand Down
Loading