more timing shenanigans
This commit is contained in:
1 parent
6df07f4b53
commit
3ca6797a06
2 files changed
+59
-31
No files matched your search
@@ -0,0 +1,18 @@
|
||||
use tokio::time::Instant;
|
||||
|
||||
pub struct LdaTimer<'s>{timer: Instant, message: &'s str}
|
||||
|
||||
impl LdaTimer<'_> {
|
||||
pub fn new(message: &str) -> LdaTimer {
|
||||
LdaTimer { timer: Instant::now(), message }
|
||||
}
|
||||
/// display time since struct made
|
||||
pub fn ping(&self, message: &str) {
|
||||
println!("{message} {:?}", self.timer.elapsed());
|
||||
}
|
||||
}
|
||||
impl Drop for LdaTimer<'_> {
|
||||
fn drop(&mut self) {
|
||||
println!("{} {:?}", self.message, self.timer.elapsed())
|
||||
}
|
||||
}
|
||||
+41
-31
@@ -2,23 +2,28 @@ use actix_cors::Cors;
|
||||
use actix_web::{App, HttpResponse, HttpServer, Responder, get, http::header, web};
|
||||
use chrono::{NaiveDate, TimeDelta};
|
||||
use icalendar::{Calendar, CalendarComponent, Component};
|
||||
use regex::Regex;
|
||||
use reqwest::Client;
|
||||
|
||||
mod funny_timer;
|
||||
// use regex::Regex;
|
||||
|
||||
const ICS_URL: &str = "https://calendar.google.com/calendar/ical/i7p4ol56gi8sqe0dq7uc3m0o4c%40group.calendar.google.com/public/basic.ics";
|
||||
|
||||
#[get("/filtered-ics")]
|
||||
async fn filtered_ics(now: web::Data<NaiveDate>) -> impl Responder {
|
||||
let cumah = funny_timer::LdaTimer::new("total time idfk");
|
||||
let now: NaiveDate = **now;
|
||||
let client = Client::new();
|
||||
let ics_url = ICS_URL;
|
||||
|
||||
let timer = tokio::time::Instant::now();
|
||||
|
||||
let timer = tokio::time::Instant::now();
|
||||
// Fetch the .ics content
|
||||
let response = match client.get(ics_url).send().await {
|
||||
Ok(resp) => resp,
|
||||
Err(e) => return HttpResponse::BadGateway().body(format!("Failed to fetch ICS file: {e}")),
|
||||
};
|
||||
println!("got a response in {:?}", timer.elapsed());
|
||||
cumah.ping("function time after response");
|
||||
|
||||
let body = match response.text().await {
|
||||
Ok(text) => text,
|
||||
@@ -27,9 +32,10 @@ async fn filtered_ics(now: web::Data<NaiveDate>) -> impl Responder {
|
||||
.body(format!("Failed to read response body: {e}"));
|
||||
}
|
||||
};
|
||||
println!("reading file took {:?}", timer.elapsed());
|
||||
|
||||
let timer = tokio::time::Instant::now();
|
||||
println!("with reading file: {:?}", timer.elapsed());
|
||||
cumah.ping("function time after reading");
|
||||
|
||||
let timer = tokio::time::Instant::now();
|
||||
// Parse the .ics content
|
||||
let calendar = match body.parse::<Calendar>() {
|
||||
Ok(cal) => cal,
|
||||
@@ -39,24 +45,28 @@ async fn filtered_ics(now: web::Data<NaiveDate>) -> impl Responder {
|
||||
}
|
||||
};
|
||||
println!("parsing took {:?}", timer.elapsed());
|
||||
|
||||
cumah.ping("function time after parsing");
|
||||
|
||||
let start_date = now - TimeDelta::days(365);
|
||||
let end_date = now + TimeDelta::days(365);
|
||||
// Filter events by summary and date range
|
||||
let timer = tokio::time::Instant::now();
|
||||
let filtered_events = filter_events_by_date_and_regex(calendar, start_date, end_date);
|
||||
println!("filtering took {:?}", timer.elapsed());
|
||||
|
||||
cumah.ping("function time after filtering");
|
||||
|
||||
// Create a new calendar with filtered events
|
||||
let filtered_calendar = Calendar {
|
||||
components: filtered_events,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
cumah.ping("hi");
|
||||
|
||||
// Serialize the filtered calendar
|
||||
let timer = tokio::time::Instant::now();
|
||||
let ics_output = filtered_calendar.to_string();
|
||||
println!("string building took {:?}", timer.elapsed());
|
||||
cumah.ping("function time after building the string");
|
||||
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/calendar")
|
||||
@@ -96,29 +106,29 @@ fn filter_events_by_date_and_regex(
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn filter_events(calendar: Calendar, pattern: &str) -> Result<Calendar, String> {
|
||||
let summary_filter = Regex::new(pattern).map_err(|_| "Invalid regex pattern")?;
|
||||
let filtered_components: Vec<CalendarComponent> = calendar
|
||||
.components
|
||||
.into_iter()
|
||||
.filter_map(|component| match component {
|
||||
CalendarComponent::Event(event)
|
||||
if event
|
||||
.get_summary()
|
||||
.is_some_and(|s| summary_filter.is_match(s)) =>
|
||||
{
|
||||
Some(CalendarComponent::Event(event))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
// #[allow(dead_code)]
|
||||
// fn filter_events(calendar: Calendar, pattern: &str) -> Result<Calendar, String> {
|
||||
// let summary_filter = Regex::new(pattern).map_err(|_| "Invalid regex pattern")?;
|
||||
// let filtered_components: Vec<CalendarComponent> = calendar
|
||||
// .components
|
||||
// .into_iter()
|
||||
// .filter_map(|component| match component {
|
||||
// CalendarComponent::Event(event)
|
||||
// if event
|
||||
// .get_summary()
|
||||
// .is_some_and(|s| summary_filter.is_match(s)) =>
|
||||
// {
|
||||
// Some(CalendarComponent::Event(event))
|
||||
// }
|
||||
// _ => None,
|
||||
// })
|
||||
// .collect();
|
||||
|
||||
Ok(Calendar {
|
||||
components: filtered_components,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
// Ok(Calendar {
|
||||
// components: filtered_components,
|
||||
// ..Default::default()
|
||||
// })
|
||||
// }
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
|
||||
Reference in new issue
Block a user