feat: caching is really powerful
This commit is contained in:
parent
35896efa04
commit
b714571b85
1 changed files with 77 additions and 14 deletions
|
@ -1,6 +1,7 @@
|
||||||
use ldap3::{LdapConn, Scope, SearchEntry};
|
use ldap3::{LdapConn, Scope, SearchEntry};
|
||||||
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
|
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
|
use std::collections::HashMap;
|
||||||
use wolves_oxidised::WolvesUser;
|
use wolves_oxidised::WolvesUser;
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
|
@ -8,9 +9,13 @@ async fn main() -> tide::Result<()> {
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
let db = db_init(&config).await.unwrap();
|
let db = db_init(&config).await.unwrap();
|
||||||
|
|
||||||
|
dbg!("&config");
|
||||||
update_wolves(&config, &db).await;
|
update_wolves(&config, &db).await;
|
||||||
|
dbg!("complete - update_wolves");
|
||||||
update_wolves_id(&db).await;
|
update_wolves_id(&db).await;
|
||||||
|
dbg!("complete - update_wolves_id");
|
||||||
update_ldap(&config, &db).await;
|
update_ldap(&config, &db).await;
|
||||||
|
dbg!("complete - update_ldap");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -46,6 +51,31 @@ async fn update_wolves_id(db: &Pool<Sqlite>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_account_hashmap(db: &Pool<Sqlite>) -> HashMap<String, Accounts> {
|
||||||
|
// get all users, toss in hashmap then query for differences?
|
||||||
|
let mut users_existing = HashMap::new();
|
||||||
|
match sqlx::query_as::<_, Accounts>(
|
||||||
|
r#"
|
||||||
|
SELECT *
|
||||||
|
FROM accounts
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(res) => {
|
||||||
|
for account in &res {
|
||||||
|
users_existing.insert(account.user.to_owned(), account.to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
dbg!(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
users_existing
|
||||||
|
}
|
||||||
|
|
||||||
async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
||||||
let mut ldap = match LdapConn::new(&config.ldap_host) {
|
let mut ldap = match LdapConn::new(&config.ldap_host) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
|
@ -63,6 +93,8 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let existing_accounts = get_account_hashmap(db).await;
|
||||||
|
|
||||||
// use this to pre load a large chunk of data
|
// use this to pre load a large chunk of data
|
||||||
if let Ok(x) = ldap.search(
|
if let Ok(x) = ldap.search(
|
||||||
"ou=users,dc=skynet,dc=ie",
|
"ou=users,dc=skynet,dc=ie",
|
||||||
|
@ -101,20 +133,34 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !tmp_account.user.is_empty() {
|
if !tmp_account.user.is_empty() {
|
||||||
sqlx::query_as::<_, Accounts>(
|
match existing_accounts.get(&tmp_account.user) {
|
||||||
"
|
None => {
|
||||||
INSERT OR REPLACE INTO accounts (user, uid, mail, student_id, secure)
|
update_accounts(db, &tmp_account).await;
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
}
|
||||||
",
|
Some(old) => {
|
||||||
)
|
let mut differnt = false;
|
||||||
.bind(&tmp_account.user)
|
|
||||||
.bind(tmp_account.uid)
|
if old.mail != tmp_account.mail {
|
||||||
.bind(&tmp_account.mail)
|
differnt = true;
|
||||||
.bind(&tmp_account.student_id)
|
}
|
||||||
.bind(tmp_account.secure)
|
if old.secure != tmp_account.secure {
|
||||||
.fetch_optional(db)
|
differnt = true;
|
||||||
.await
|
}
|
||||||
.ok();
|
if old.uid != tmp_account.uid {
|
||||||
|
differnt = true;
|
||||||
|
}
|
||||||
|
if old.student_id != tmp_account.student_id {
|
||||||
|
differnt = true;
|
||||||
|
}
|
||||||
|
if old.id_wolves != tmp_account.id_wolves {
|
||||||
|
differnt = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if differnt {
|
||||||
|
update_accounts(db, &tmp_account).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,6 +170,23 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
||||||
ldap.unbind().unwrap();
|
ldap.unbind().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update_accounts(db: &Pool<Sqlite>, tmp_account: &Accounts) {
|
||||||
|
sqlx::query_as::<_, Accounts>(
|
||||||
|
"
|
||||||
|
INSERT OR REPLACE INTO accounts (user, uid, mail, student_id, secure)
|
||||||
|
VALUES (?1, ?2, ?3, ?4, ?5)
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.bind(&tmp_account.user)
|
||||||
|
.bind(tmp_account.uid)
|
||||||
|
.bind(&tmp_account.mail)
|
||||||
|
.bind(&tmp_account.student_id)
|
||||||
|
.bind(tmp_account.secure)
|
||||||
|
.fetch_optional(db)
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
|
||||||
async fn update_account(db: &Pool<Sqlite>, account: &WolvesUser) {
|
async fn update_account(db: &Pool<Sqlite>, account: &WolvesUser) {
|
||||||
sqlx::query_as::<_, AccountWolves>(
|
sqlx::query_as::<_, AccountWolves>(
|
||||||
"
|
"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue