Skip to content

Commit 9120a37

Browse files
committed
Add subcommands for getting and listing all default priorities
1 parent 49e5d7f commit 9120a37

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

src/bin/cratesfyi.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use docs_rs::cdn::CdnBackend;
1010
use docs_rs::db::{self, add_path_into_database, Overrides, Pool, PoolClient};
1111
use docs_rs::repositories::RepositoryStatsUpdater;
1212
use docs_rs::utils::{
13-
get_config, queue_builder, remove_crate_priority, set_crate_priority, ConfigName,
13+
get_config, get_crate_pattern_and_priority, list_crate_priorities, queue_builder,
14+
remove_crate_priority, set_crate_priority, ConfigName,
1415
};
1516
use docs_rs::{
1617
start_web_server, BuildQueue, Config, Context, Index, Metrics, PackageKind, RustwideBuilder,
@@ -234,6 +235,14 @@ impl QueueSubcommand {
234235

235236
#[derive(Debug, Clone, PartialEq, Eq, Subcommand)]
236237
enum PrioritySubcommand {
238+
/// Get priority for a crate
239+
///
240+
/// (returns only the first matching pattern, there may be other matching patterns)
241+
Get { crate_name: String },
242+
243+
/// List priorities for all patterns
244+
List,
245+
237246
/// Set all crates matching a pattern to a priority level
238247
Set {
239248
/// See https://www.postgresql.org/docs/current/functions-matching.html for pattern syntax
@@ -253,19 +262,37 @@ enum PrioritySubcommand {
253262

254263
impl PrioritySubcommand {
255264
fn handle_args(self, ctx: BinContext) -> Result<()> {
265+
let conn = &mut *ctx.conn()?;
256266
match self {
267+
Self::List => {
268+
for (pattern, priority) in list_crate_priorities(conn)? {
269+
println!("{pattern:>20} : {priority:>3}");
270+
}
271+
}
272+
273+
Self::Get { crate_name } => {
274+
if let Some((pattern, priority)) =
275+
get_crate_pattern_and_priority(conn, &crate_name)?
276+
{
277+
println!("{pattern} : {priority}");
278+
} else {
279+
println!("No priority found for {crate_name}");
280+
}
281+
}
282+
257283
Self::Set { pattern, priority } => {
258-
set_crate_priority(&mut *ctx.conn()?, &pattern, priority)
284+
set_crate_priority(conn, &pattern, priority)
259285
.context("Could not set pattern's priority")?;
286+
println!("Set pattern '{pattern}' to priority {priority}");
260287
}
261288

262289
Self::Remove { pattern } => {
263-
if let Some(priority) = remove_crate_priority(&mut *ctx.conn()?, &pattern)
290+
if let Some(priority) = remove_crate_priority(conn, &pattern)
264291
.context("Could not remove pattern's priority")?
265292
{
266-
println!("Removed pattern with priority {priority}");
293+
println!("Removed pattern '{pattern}' with priority {priority}");
267294
} else {
268-
println!("Pattern did not exist and so was not removed");
295+
println!("Pattern '{pattern}' did not exist and so was not removed");
269296
}
270297
}
271298
}

src/utils/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ pub(crate) use self::cargo_metadata::{CargoMetadata, Package as MetadataPackage}
44
pub(crate) use self::copy::copy_dir_all;
55
pub use self::daemon::{start_daemon, watch_registry};
66
pub(crate) use self::html::rewrite_lol;
7-
pub use self::queue::{get_crate_priority, remove_crate_priority, set_crate_priority};
7+
pub use self::queue::{
8+
get_crate_pattern_and_priority, get_crate_priority, list_crate_priorities,
9+
remove_crate_priority, set_crate_priority,
10+
};
811
pub use self::queue_builder::queue_builder;
912
pub(crate) use self::rustc_version::{get_correct_docsrs_style_file, parse_rustc_version};
1013

@@ -62,7 +65,7 @@ pub fn set_config(
6265
) -> anyhow::Result<()> {
6366
let name: &'static str = name.into();
6467
conn.execute(
65-
"INSERT INTO config (name, value)
68+
"INSERT INTO config (name, value)
6669
VALUES ($1, $2)
6770
ON CONFLICT (name) DO UPDATE SET value = $2;",
6871
&[&name, &serde_json::to_value(value)?],

src/utils/queue.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,40 @@ use postgres::Client;
55

66
const DEFAULT_PRIORITY: i32 = 0;
77

8-
/// Get the build queue priority for a crate
9-
pub fn get_crate_priority(conn: &mut Client, name: &str) -> Result<i32> {
8+
/// Get the build queue priority for a crate, returns the matching pattern too
9+
pub fn list_crate_priorities(conn: &mut Client) -> Result<Vec<(String, i32)>> {
10+
Ok(conn
11+
.query("SELECT pattern, priority FROM crate_priorities", &[])?
12+
.into_iter()
13+
.map(|r| (r.get(0), r.get(1)))
14+
.collect())
15+
}
16+
17+
/// Get the build queue priority for a crate with its matching pattern
18+
pub fn get_crate_pattern_and_priority(
19+
conn: &mut Client,
20+
name: &str,
21+
) -> Result<Option<(String, i32)>> {
1022
// Search the `priority` table for a priority where the crate name matches the stored pattern
1123
let query = conn.query(
12-
"SELECT priority FROM crate_priorities WHERE $1 LIKE pattern LIMIT 1",
24+
"SELECT pattern, priority FROM crate_priorities WHERE $1 LIKE pattern LIMIT 1",
1325
&[&name],
1426
)?;
1527

1628
// If no match is found, return the default priority
1729
if let Some(row) = query.get(0) {
18-
Ok(row.get(0))
30+
Ok(Some((row.get(0), row.get(1))))
1931
} else {
20-
Ok(DEFAULT_PRIORITY)
32+
Ok(None)
2133
}
2234
}
2335

36+
/// Get the build queue priority for a crate
37+
pub fn get_crate_priority(conn: &mut Client, name: &str) -> Result<i32> {
38+
Ok(get_crate_pattern_and_priority(conn, name)?
39+
.map_or(DEFAULT_PRIORITY, |(_, priority)| priority))
40+
}
41+
2442
/// Set all crates that match [`pattern`] to have a certain priority
2543
///
2644
/// Note: `pattern` is used in a `LIKE` statement, so it must follow the postgres like syntax

0 commit comments

Comments
 (0)