cargo fmt

This commit is contained in:
Armaël Guéneau 2024-11-23 13:10:18 +01:00
parent 83d8be55da
commit 8b2a65d6f6

View file

@ -116,14 +116,28 @@ impl Db {
fn from_path(path: &Path, classifier: &Classifier) -> anyhow::Result<Self> {
let file = File::open(path)?;
let (users, is_spam) = serde_json::from_reader(BufReader::new(file))?;
let mut db = Db { users, is_spam, tokens: HashMap::new(), score: HashMap::new() };
let mut db = Db {
users,
is_spam,
tokens: HashMap::new(),
score: HashMap::new(),
};
db.recompute_tokens();
db.recompute_scores(classifier);
Ok(db)
}
fn from_users(users: HashMap<UserId, UserData>, is_spam: HashMap<UserId, bool>, classifier: &Classifier) -> Db {
let mut db = Db { users, is_spam, tokens: HashMap::new(), score: HashMap::new() };
fn from_users(
users: HashMap<UserId, UserData>,
is_spam: HashMap<UserId, bool>,
classifier: &Classifier,
) -> Db {
let mut db = Db {
users,
is_spam,
tokens: HashMap::new(),
score: HashMap::new(),
};
db.recompute_tokens();
db.recompute_scores(classifier);
db
@ -131,7 +145,8 @@ impl Db {
fn store_to_path(&self, path: &Path) -> anyhow::Result<()> {
let file = File::create(path)?;
let dat: (&HashMap<UserId, UserData>, &HashMap<UserId, bool>) = (&self.users, &self.is_spam);
let dat: (&HashMap<UserId, UserData>, &HashMap<UserId, bool>) =
(&self.users, &self.is_spam);
serde_json::to_writer(BufWriter::new(file), &dat)?;
Ok(())
}
@ -329,12 +344,7 @@ async fn load_db() -> anyhow::Result<(Db, Classifier)> {
let db: Db = if db_path.is_file() {
Db::from_path(db_path, &classifier)?
} else {
let db =
Db::from_users(
get_users_data(&forge).await?,
HashMap::new(),
&classifier
);
let db = Db::from_users(get_users_data(&forge).await?, HashMap::new(), &classifier);
db.store_to_path(db_path)?;
db
};
@ -389,7 +399,7 @@ struct AppState {
#[derive(Debug, Deserialize)]
struct SortSetting {
sort: Option<String>
sort: Option<String>,
}
#[get("/")]
@ -399,8 +409,8 @@ async fn index(data: web::Data<AppState>, q: web::Query<SortSetting>) -> impl Re
let db = &data.db.lock().unwrap();
eprintln!("scoring users...");
let mut users: Vec<_> =
unclassified_users(db).into_iter()
let mut users: Vec<_> = unclassified_users(db)
.into_iter()
.map(|(id, u)| (id, u, *db.score.get(id).unwrap()))
.collect();
let mut rng = rand::thread_rng();
@ -410,15 +420,12 @@ async fn index(data: web::Data<AppState>, q: web::Query<SortSetting>) -> impl Re
eprintln!("sorting...");
match q.sort.as_ref().map(|s| s.as_str()) {
Some("legit") =>
// sort "legit first": by increasing score
users.sort_by_key(|(_, _, score)| (score * 1000.) as u64),
Some("random") =>
Some("legit") => users.sort_by_key(|(_, _, score)| (score * 1000.) as u64),
// keep the random order
(),
_ =>
Some("random") => (),
// sort "spam first": by decreasing score
users.sort_by_key(|(_, _, score)| 1000 - (score * 1000.) as u64),
_ => users.sort_by_key(|(_, _, score)| 1000 - (score * 1000.) as u64),
};
users.truncate(50);
@ -427,7 +434,10 @@ async fn index(data: web::Data<AppState>, q: web::Query<SortSetting>) -> impl Re
let mut context = tera::Context::new();
context.insert("users", &users);
context.insert("unclassified_users_count", &(users_count - classified_count));
context.insert(
"unclassified_users_count",
&(users_count - classified_count),
);
context.insert("total_users_count", &users_count);
eprintln!("rendering template...");
let page = TEMPLATES.render("index.html", &context).unwrap();
@ -436,24 +446,23 @@ async fn index(data: web::Data<AppState>, q: web::Query<SortSetting>) -> impl Re
}
#[post("/")]
async fn apply(
data: web::Data<AppState>,
req: web::Form<HashMap<i64, String>>,
) -> impl Responder {
async fn apply(data: web::Data<AppState>, req: web::Form<HashMap<i64, String>>) -> impl Responder {
eprintln!("POST /");
let db = &mut data.db.lock().unwrap();
let classifier = &mut data.classifier.lock().unwrap();
let updates: Vec<(UserId, bool)> =
req.iter()
let updates: Vec<(UserId, bool)> = req
.iter()
.map(|(id, classification)| (UserId(*id), classification == "spam"))
.collect();
set_spam(db, classifier, &updates);
db.store_to_path(Path::new("db.json")).unwrap(); // FIXME
classifier.save(&mut File::create(Path::new("model.json")).unwrap(), false).unwrap(); // FIXME
classifier
.save(&mut File::create(Path::new("model.json")).unwrap(), false)
.unwrap(); // FIXME
HttpResponse::SeeOther()
.insert_header(("Location", ""))