1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
use anyhow::{bail, Context, Result};
use clap::{value_parser, ArgAction, Parser};
use futures::{stream, FutureExt, StreamExt};
use lambo::config::Config;
use sqlx::sqlite::SqlitePoolOptions;
use std::{fs, path::PathBuf};
use stderrlog::StdErrLog;
#[derive(Debug, Parser)]
struct Args {
/// The path to the lambo configuration file.
config_path: PathBuf,
/// The path to the CSV file, as exported from Canvas's gradebook.
csv_path: PathBuf,
/// Decreases the log level.
#[clap(
short,
long,
conflicts_with("verbose"),
action = ArgAction::Count,
value_parser = value_parser!(u8).range(..=2)
)]
quiet: u8,
/// Increases the log level.
#[clap(
short,
long,
conflicts_with("quiet"),
action = ArgAction::Count,
value_parser = value_parser!(u8).range(..=3)
)]
verbose: u8,
}
#[tokio::main]
async fn main() -> Result<()> {
// Parse the arguments.
let args = Args::parse();
// Set up logging.
{
let mut logger = StdErrLog::new();
match args.quiet {
0 => logger.verbosity(1 + args.verbose as usize),
1 => logger.verbosity(0),
2 => logger.quiet(true),
// UNREACHABLE: A maximum of two occurrences of quiet are allowed.
_ => unreachable!(),
};
// UNWRAP: No other logger should be set up.
logger.show_module_names(true).init().unwrap()
}
// Parse the config file.
let config = Config::read_from_file(&args.config_path)?;
// Connect to the database.
let db = SqlitePoolOptions::new()
.connect(&config.database_url)
.await
.with_context(|| format!("failed to connect to database at {:?}", config.database_url))?;
// Run any necessary migrations.
sqlx::migrate!().run(&db).await.with_context(|| {
format!(
"failed to run migrations on database at {:?}",
config.database_url
)
})?;
// Read the CSV file.
let csv_string = fs::read_to_string(&args.csv_path)
.with_context(|| format!("failed to read {:?}", args.csv_path))?;
// Skip the first two lines; Canvas puts two lines of headers in...
let (i, _) = csv_string
.match_indices('\n')
.nth(1)
.context("invalid CSV file (not enough lines)")?;
let csv_string = &csv_string[i..];
// Parse the CSV file.
let mut csv = csv::ReaderBuilder::new()
.has_headers(false)
.from_reader(csv_string.as_bytes())
.records()
.collect::<Result<Vec<_>, _>>()
.with_context(|| format!("failed to parse {:?}", args.csv_path))?;
// Remove the test student.
csv.retain(|record| &record[0] != "Student, Test");
// Collect all the X.500s, checking that they were of the right form.
let x500s = csv
.into_iter()
.map(|record| {
let email = &record[3];
if let Some(x500) = email.strip_suffix("@umn.edu") {
Ok(x500.to_lowercase())
} else {
bail!("not a valid UMN email: {:?}", email)
}
})
.collect::<Result<Vec<_>>>()?;
// Insert them all in the database.
//
// Looks like sqlx doesn't actually have bulk insert? WTF?
//
// https://github.com/launchbadge/sqlx/issues/294
let db = &db;
let errors = stream::iter(x500s)
.map(|x500| async move {
sqlx::query!(
"INSERT OR IGNORE INTO student_x500s (x500) VALUES (?)",
x500
)
.execute(db)
.await
.context("failed to insert X.500s")
})
.filter_map(|future| future.map(|r| r.err()))
.collect::<Vec<_>>()
.await;
if !errors.is_empty() {
log::error!("encountered {} errors:", errors.len());
for error in errors {
log::error!("{:?}", error);
}
bail!("failed to insert X.500s")
}
// Count the number of X.500s.
let x500_count = sqlx::query!("SELECT COUNT(x500) as count from student_x500s")
.fetch_one(db)
.await
.context("failed to get a count of X.500s")?;
log::info!("We now have {} student X.500s", x500_count.count);
Ok(())
}
|