add sorting options

This commit is contained in:
Armaël Guéneau 2024-11-23 12:42:53 +01:00
parent 0e17ff90e6
commit 83d8be55da
3 changed files with 32 additions and 58 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,4 @@
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use forgejo_api::{Auth, Forgejo};
use lazy_static::lazy_static;
use rand::prelude::*;
@ -370,54 +370,6 @@ fn set_spam(db: &mut Db, classifier: &mut Classifier, ids: &[(UserId, bool)]) {
}
}
/*
async fn main_() -> anyhow::Result<()> {
println!("got {} users", db.users.len());
let mut rng = rand::thread_rng();
users.shuffle(&mut rng);
users.sort_by_key(|(_, _, _, score)| 1000 - (score * 1000.) as u64);
for (user_id, user, text, _) in users {
println!("{:#?}", user);
let score = classifier.score(text);
println!("SCORE: {}", score);
let c = {
let mut resp = String::new();
loop {
println!("SPAM? (y/n/?) ");
std::io::stdin().read_line(&mut resp)?;
match resp.as_str() {
"y\n" => break Spam,
"n\n" => break Legit,
"?\n" => break Unknown,
_ => resp.clear()
}
}
};
match c {
Spam => classifier.train_spam(&text),
Legit => classifier.train_ham(&text),
Unknown => ()
}
db.classification.insert(*user_id, c);
{
classifier.save(&mut File::create(model_path)?, false)?;
let file = File::create(db_path)?;
serde_json::to_writer(BufWriter::new(file), &db)?;
}
}
Ok(())
}
*/
lazy_static! {
pub static ref TEMPLATES: Tera = {
match Tera::new("templates/**/*.html") {
@ -435,8 +387,13 @@ struct AppState {
classifier: Mutex<Classifier>,
}
#[derive(Debug, Deserialize)]
struct SortSetting {
sort: Option<String>
}
#[get("/")]
async fn index(data: web::Data<AppState>) -> impl Responder {
async fn index(data: web::Data<AppState>, q: web::Query<SortSetting>) -> impl Responder {
eprintln!("GET /");
let db = &data.db.lock().unwrap();
@ -447,10 +404,22 @@ async fn index(data: web::Data<AppState>) -> impl Responder {
.map(|(id, u)| (id, u, *db.score.get(id).unwrap()))
.collect();
let mut rng = rand::thread_rng();
eprintln!("randomizing...");
users.shuffle(&mut rng);
eprintln!("sorting...");
users.sort_by_key(|(_, _, score)| 1000 - (score * 1000.) as u64);
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") =>
// keep the random order
(),
_ =>
// sort "spam first": by decreasing score
users.sort_by_key(|(_, _, score)| 1000 - (score * 1000.) as u64),
};
users.truncate(50);
let users_count = db.users.len();
@ -483,11 +452,11 @@ async fn apply(
set_spam(db, classifier, &updates);
db.store_to_path(Path::new("db.json")).unwrap();
classifier.save(&mut File::create(Path::new("model.json")).unwrap(), false).unwrap();
db.store_to_path(Path::new("db.json")).unwrap(); // FIXME
classifier.save(&mut File::create(Path::new("model.json")).unwrap(), false).unwrap(); // FIXME
HttpResponse::SeeOther()
.insert_header(("Location", "/"))
.insert_header(("Location", ""))
.finish()
}

View file

@ -107,11 +107,16 @@
}
</style>
<body>
<form method="post">
<div class="main">
<div class="stats">
Users: unclassified: {{unclassified_users_count}} | total: {{total_users_count}}
</div>
<div class="sort-options">
<a href="/?sort=spam">Sort: Spam first</a> |
<a href="/?sort=legit">Sort: Legit first</a> |
<a href="/?sort=random">Sort: Random</a>
</div>
<form method="post">
<div class="users">
{% for id_user_score in users %}
{% set user_id = id_user_score[0] %}
@ -139,7 +144,7 @@
</div>
<div class="user-card">
<div class="user-name">
<div><strong>{{ user.login }}</strong></div>
<div><strong><a href="https://git.deuxfleurs.fr/{{user.login}}">{{ user.login }}</a></strong></div>
{%- if user.full_name %}<div><strong>({{ user.full_name }})</strong></div>{% endif -%}
</div>
<div class="user-info">
@ -170,8 +175,8 @@
</div>
<input type="submit" value="Apply" class="button" style="width: 200px; height: 30px"/>
</div>
</form>
</div>
</body>
</html>