Browse Source

better update on single account

Johann Woelper 4 years ago
parent
commit
36e30f4fee
7 changed files with 1231 additions and 1035 deletions
  1. 3 3
      Cargo.toml
  2. 1110 934
      accounts.json
  3. BIN
      guntrader
  4. 39 30
      src/main.rs
  5. 17 7
      src/server.rs
  6. 60 60
      src/test.rs
  7. 2 1
      webapp/app.js

+ 3 - 3
Cargo.toml

@@ -29,6 +29,6 @@ features = ["json", "serve"]
 name = "guntrader"
 path = "src/main.rs"
 
-[[bin]]
-name = "test"
-path = "src/test.rs"
+# [[bin]]
+# name = "test"
+# path = "src/test.rs"

File diff suppressed because it is too large
+ 1110 - 934
accounts.json


BIN
guntrader


+ 39 - 30
src/main.rs

@@ -31,41 +31,50 @@ lazy_static! {
 }
 
 
-fn daemon(interval: u64) {
-    loop {
-        debug!(">>> UPD");
+pub fn run_update() {
+
+    debug!(">>> UPD");
+
+    let accounts_unlocked = ACCOUNTS.lock().unwrap();
+    let accounts_cl = accounts_unlocked.clone();
+    drop(accounts_unlocked);
+
+    let client = reqwest::blocking::Client::builder()
+    // .max_idle_per_host(200)
+    // .timeout(Some(core::time::Duration::from_secs(10)))
+    .build()
+    .unwrap();
+
+    let mut threads = vec![];
+
+    for (account_name, account) in accounts_cl {
+        let client = client.clone();
+        let t = thread::spawn(move || {
+            debug!("ACCT {}", account_name);
+            
+            let updated_account = account.updated(&client);
+            let mut accounts = ACCOUNTS.lock().unwrap();
+            accounts.insert(account_name.to_string(), updated_account);
+        });
+        threads.push(t);
+    }
 
-        let accounts_unlocked = ACCOUNTS.lock().unwrap();
-        let accounts_cl = accounts_unlocked.clone();
-        drop(accounts_unlocked);
+    debug!("Joining threads");
+    for t in threads {
+        t.join().unwrap();
+    }
+    debug!("Done.");
 
-        let client = reqwest::blocking::Client::builder()
-        // .max_idle_per_host(200)
-        // .timeout(Some(core::time::Duration::from_secs(10)))
-        .build()
-        .unwrap();
-
-        let mut threads = vec![];
-
-        for (account_name, account) in accounts_cl {
-            let client = client.clone();
-            let t = thread::spawn(move || {
-                debug!("ACCT {}", account_name);
-                
-                let updated_account = account.updated(&client);
-                let mut accounts = ACCOUNTS.lock().unwrap();
-                accounts.insert(account_name.to_string(), updated_account);
-            });
-            threads.push(t);
-        }
 
-        debug!("Joining threads");
-        for t in threads {
-            t.join().unwrap();
-        }
-        debug!("Done.");
+}
 
 
+fn daemon(interval: u64) {
+    loop {
+      
+        run_update();
+
+        // commit to disk
         let accounts_unlocked = ACCOUNTS.lock().unwrap();
         let writer = BufWriter::new(File::create(DBFILE).unwrap());
         serde_json::to_writer_pretty(writer, &*accounts_unlocked).unwrap();

+ 17 - 7
src/server.rs

@@ -54,14 +54,24 @@ fn proxy_gate(url: String)  -> Vec<u8>{
 //     "Base url"
 // }
 
-#[post("/save", data = "<account>")] //<tasks> means that the function below will have a var called tasks
-fn save(account: Json<HashMap<String, Account>>) -> &'static str {
-    dbg!(&account);
-    let mut accounts = ACCOUNTS.lock().unwrap();
-    accounts.extend(account.into_inner());
+#[post("/save", data = "<json_account>")] //<tasks> means that the function below will have a var called tasks
+fn save(json_account: Json<HashMap<String, Account>>) -> &'static str {
+
+    let accountmap = json_account.into_inner();
+    let accountnames: Vec<String> = accountmap.keys().map(|x| x.into()).collect();
+    if let Some(accountname) = accountnames.get(0) {
+        if let Some(account) = accountmap.get(accountname) {
+            let client = reqwest::blocking::Client::builder().build().unwrap();
+            let updated_acc = account.updated(&client);
+            let mut accounts = ACCOUNTS.lock().unwrap();
+            accounts.insert(accountname.into(), updated_acc);
+            drop(accounts);
+            return "Inserted"
+        }
+    }
     // dbg!(&accounts);
-    drop(accounts);
-    "Inserted"
+    // run_update();
+    "Not updated"
 }
 
 #[get("/load/<account>")]

+ 60 - 60
src/test.rs

@@ -1,75 +1,75 @@
-#![feature(proc_macro_hygiene, decl_macro)]
-#[macro_use]
-extern crate serde_derive;
-#[macro_use]
-extern crate rocket;
-#[macro_use]
-extern crate rocket_contrib;
-#[macro_use]
-extern crate lazy_static;
-extern crate serde_json;
+// #![feature(proc_macro_hygiene, decl_macro)]
+// #[macro_use]
+// extern crate serde_derive;
+// #[macro_use]
+// extern crate rocket;
+// #[macro_use]
+// extern crate rocket_contrib;
+// #[macro_use]
+// extern crate lazy_static;
+// extern crate serde_json;
 
-// use serde_json;
-use std::collections::HashMap;
-use std::fs::File;
-use std::io::BufReader;
-use std::io::BufWriter;
-use std::sync::Mutex;
-use std::{thread, time};
+// // use serde_json;
+// use std::collections::HashMap;
+// use std::fs::File;
+// use std::io::BufReader;
+// use std::io::BufWriter;
+// use std::sync::Mutex;
+// use std::{thread, time};
 
-/// Fucking egun is a mess. It does not even use css and is built using tables. This is an attempt to parse it.
-mod query;
-use query::*;
-mod server;
-const DBFILE: &str = "accounts.json";
+// /// Fucking egun is a mess. It does not even use css and is built using tables. This is an attempt to parse it.
+// mod query;
+// use query::*;
+// mod server;
+// const DBFILE: &str = "accounts.json";
 
 
-lazy_static! {
-    static ref ACCOUNTS: Mutex<HashMap<String, Account>> = Mutex::new(HashMap::new());
-}
+// lazy_static! {
+//     static ref ACCOUNTS: Mutex<HashMap<String, Account>> = Mutex::new(HashMap::new());
+// }
 
 
-fn daemon() {
-    println!(">>> Running update...");
+// fn daemon() {
+//     println!(">>> Running update...");
 
-        let accounts_unlocked = ACCOUNTS.lock().unwrap().clone();
+//         let accounts_unlocked = ACCOUNTS.lock().unwrap().clone();
 
-        for (account_name, account) in accounts_unlocked {
-            dbg!(&account_name);
-            let mut accounts = ACCOUNTS.lock().unwrap();
-            accounts.insert(account_name.to_string(), account.updated());
-        }
+//         for (account_name, account) in accounts_unlocked {
+//             dbg!(&account_name);
+//             let mut accounts = ACCOUNTS.lock().unwrap();
+//             accounts.insert(account_name.to_string(), account.updated());
+//         }
 
 
-    let accounts_unlocked = ACCOUNTS.lock().unwrap();
-    let writer = BufWriter::new(File::create(DBFILE).unwrap());
-    serde_json::to_writer_pretty(writer, &*accounts_unlocked).unwrap();
+//     let accounts_unlocked = ACCOUNTS.lock().unwrap();
+//     let writer = BufWriter::new(File::create(DBFILE).unwrap());
+//     serde_json::to_writer_pretty(writer, &*accounts_unlocked).unwrap();
 
-    println!(">>> Done.");
-}
+//     println!(">>> Done.");
+// }
 
-fn main() {
+// fn main() {
     
-    //server::proxy("http://www.egun.de/market/cache/aucimg/100x100/3.7564934.665580173.jpg");
-    server::run();
-    return;
+//     //server::proxy("http://www.egun.de/market/cache/aucimg/100x100/3.7564934.665580173.jpg");
+//     server::run();
+//     return;
 
 
-    match File::open(DBFILE) {
-        Ok(f) => {
-            match serde_json::from_reader::<_, HashMap<String, Account>>(BufReader::new(f)) {
-                Ok(accounts) => {
-                    {
-                        // Lock once, shove loaded ccounts in
-                        let mut accounts_unlocked = ACCOUNTS.lock().unwrap();
-                        *accounts_unlocked = accounts;
-                    }
-                    // start daemon before running web server
-                        daemon();
-                },
-                Err(e) => println!("Parsing account da'a has failed: {:?}", e)
-            }
-        }
-        Err(e) => println!("Opening accounts file failed. {:?}", e),
-    }
-}
+//     match File::open(DBFILE) {
+//         Ok(f) => {
+//             match serde_json::from_reader::<_, HashMap<String, Account>>(BufReader::new(f)) {
+//                 Ok(accounts) => {
+//                     {
+//                         // Lock once, shove loaded ccounts in
+//                         let mut accounts_unlocked = ACCOUNTS.lock().unwrap();
+//                         *accounts_unlocked = accounts;
+//                     }
+//                     // start daemon before running web server
+//                         daemon();
+//                 },
+//                 Err(e) => println!("Parsing account da'a has failed: {:?}", e)
+//             }
+//         }
+//         Err(e) => println!("Opening accounts file failed. {:?}", e),
+//     }
+// }

+ 2 - 1
webapp/app.js

@@ -124,7 +124,8 @@ var app = new Vue({
               method: "POST", 
               body: JSON.stringify(account)
             }).then(res => {
-            //   console.log("Request complete! response:", res);
+              console.log("Request complete! response:", res);
+              self.load_account();
             });
         }