Skip to content

Commit

Permalink
v0.4.4: Fix Time Stat API Returning String Instead of Vector (#15)
Browse files Browse the repository at this point in the history
* fix: timing stats endpoint should return reqs as array

* chore: bump version to 0.4.4

* fix: unlock recent requests mutex as soon as it's done
  • Loading branch information
ewang2002 committed Feb 19, 2023
1 parent 4164b80 commit bf460fd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webreg_scraper"
version = "0.4.3"
version = "0.4.4"
authors = ["Edward Wang"]
edition = "2021"
description = "A scraper and/or API for UC San Diego's WebReg enrollment system."
Expand Down
16 changes: 4 additions & 12 deletions src/api/status_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,10 @@ pub async fn api_get_timing_stats(
move |term_info| async move {
let num_requests = term_info.tracker.num_requests.load(Ordering::SeqCst);
let time_spent = term_info.tracker.total_time_spent.load(Ordering::SeqCst);
let recent_requests = format!(
"[{}]",
term_info
.tracker
.recent_requests
.lock()
.await
.iter()
.map(|amt| amt.to_string())
.collect::<Vec<_>>()
.join(", ")
);
let recent_requests = {
let temp = term_info.tracker.recent_requests.lock().await;
temp.iter().copied().collect::<Vec<_>>()
};

(
StatusCode::OK,
Expand Down
20 changes: 13 additions & 7 deletions src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use {
use crate::types::TermInfo;
use crate::util::get_pretty_time;

const MAX_RECENT_REQUESTS: usize = 100;
const MAX_RECENT_REQUESTS: usize = 2000;
const CLEANED_CSV_HEADER: &str = "time,enrolled,available,waitlisted,total";

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -354,13 +354,19 @@ pub async fn track_webreg_enrollment(info: &TermInfo, stop_flag: &Arc<AtomicBool
.total_time_spent
.fetch_add(time_spent, Ordering::SeqCst);

// Add the most recent request to the deque, removing the oldest if necessary.
let mut recent_requests = info.tracker.recent_requests.lock().await;
while recent_requests.len() >= MAX_RECENT_REQUESTS {
recent_requests.pop_front();
}
// Put this part of the code in its own scope so that
// we unlock the mutex as soon as we're done with it.
// Otherwise, we'd have to wait until the sleep call
// is done before the mutex is unlocked.
{
// Add the most recent request to the deque, removing the oldest if necessary.
let mut recent_requests = info.tracker.recent_requests.lock().await;
while recent_requests.len() >= MAX_RECENT_REQUESTS {
recent_requests.pop_front();
}

recent_requests.push_back(time_spent);
recent_requests.push_back(time_spent);
}

// Sleep between requests so we don't get ourselves banned by webreg
tokio::time::sleep(Duration::from_secs_f64(info.cooldown)).await;
Expand Down

0 comments on commit bf460fd

Please sign in to comment.