Skip to content

Commit 8b4ba73

Browse files
committed
add node ID getting and filtering
1 parent 07b36dd commit 8b4ba73

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

queries/get_node_ids.sql

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT
2+
id
3+
FROM
4+
devices;

src/generator.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
use sqlx::PgConnection;
1+
use log::{debug, info};
2+
use sqlx::{query_file, PgConnection};
23
use std::io::Write;
34

4-
pub async fn run(db: PgConnection, out: impl Write) -> anyhow::Result<()> {
5-
// ...
5+
pub async fn run(
6+
stats: bool,
7+
verify: bool,
8+
skip: &[u16],
9+
mut db: PgConnection,
10+
out: impl Write,
11+
) -> anyhow::Result<()> {
12+
let mut nodes = get_node_ids(&mut db).await?;
13+
debug!("Got node IDs: {nodes:?}");
14+
15+
debug!("Filtering nodes: {skip:?}");
16+
nodes.retain(|id| skip.contains(id));
17+
info!("Working with nodes: {nodes:?}");
618

719
Ok(())
820
}
21+
22+
async fn get_node_ids(db: &mut PgConnection) -> anyhow::Result<Vec<u16>> {
23+
let results = query_file!("queries/get_node_ids.sql")
24+
.fetch_all(db)
25+
.await?;
26+
27+
let ids: Vec<u16> = results
28+
.iter()
29+
.flat_map(|record| record.id.try_into())
30+
.collect();
31+
32+
Ok(ids)
33+
}

src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
2929
debug!("Connected to database");
3030

3131
match args.command {
32-
cli::Command::Generate { out } => {
32+
cli::Command::Generate {
33+
stats,
34+
verify,
35+
skip,
36+
out,
37+
} => {
3338
let out: &mut dyn Write = match out {
3439
Some(file) => &mut OpenOptions::new().write(true).open(file)?,
3540
None => &mut stdout(),
3641
};
3742

38-
generator::run(db, out).await?
43+
generator::run(stats, verify, &skip, db, out).await?
3944
}
4045
}
4146

0 commit comments

Comments
 (0)