Skip to content

Commit 06d9034

Browse files
cdxkerskeptrunedev
authored andcommitted
feature: add timer to usage request
1 parent 554a5ae commit 06d9034

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

server/src/handlers/organization_handler.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use actix_web::{web, HttpRequest, HttpResponse};
2424
use sanitize_html::rules;
2525
use sanitize_html::sanitize_str;
2626
use serde::{Deserialize, Serialize};
27+
use simple_server_timing_header::Timer;
2728
use utoipa::ToSchema;
2829

2930
/// Get Organization
@@ -275,15 +276,23 @@ pub async fn get_organization_usage(
275276
}
276277
}))
277278
} else {
279+
let mut usage_timer = Some(Timer::new());
280+
278281
let extended_usage = get_extended_org_usage_by_id_query(
279282
org_id,
280283
data.date_range.clone(),
281284
clickhouse_client.get_ref(),
282285
pool,
286+
&mut usage_timer,
283287
)
284288
.await?;
285289

286-
Ok(HttpResponse::Ok().json(extended_usage))
290+
Ok(HttpResponse::Ok()
291+
.insert_header((
292+
Timer::header_key(),
293+
usage_timer.expect("Timer should exist").header_value(),
294+
))
295+
.json(extended_usage))
287296
}
288297
}
289298

server/src/operators/organization_operator.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ pub async fn get_extended_org_usage_by_id_query(
501501
date_range: Option<DateRange>,
502502
clickhouse_client: &clickhouse::Client,
503503
pool: web::Data<Pool>,
504+
timer: &mut Option<simple_server_timing_header::Timer>,
504505
) -> Result<ExtendedOrganizationUsageCount, ServiceError> {
505506
let usage = get_org_usage_by_id_query(organization_id, pool).await?;
506507

@@ -521,6 +522,10 @@ pub async fn get_extended_org_usage_by_id_query(
521522
ServiceError::InternalServerError(format!("Error fetching search queries {:?}", e))
522523
})?;
523524

525+
if let Some(timer) = timer {
526+
timer.add("fetched search_count");
527+
}
528+
524529
let message_tokens = clickhouse_client
525530
.query(&format_with_daterange(
526531
"
@@ -538,6 +543,10 @@ pub async fn get_extended_org_usage_by_id_query(
538543
ServiceError::InternalServerError(format!("Error fetching message queries {:?}", e))
539544
})?;
540545

546+
if let Some(timer) = timer {
547+
timer.add("fetched message_count");
548+
}
549+
541550
let bytes_and_tokens_ingested = clickhouse_client
542551
.query(&format_with_daterange(
543552
"
@@ -555,6 +564,10 @@ pub async fn get_extended_org_usage_by_id_query(
555564
ServiceError::InternalServerError(format!("Error fetching ingestion data {:?}", e))
556565
})?;
557566

567+
if let Some(timer) = timer {
568+
timer.add("fetched bytes_ingested and tokens_ingested");
569+
}
570+
558571
let ocr_pages = clickhouse_client
559572
.query(&format_with_daterange(
560573
"
@@ -571,6 +584,10 @@ pub async fn get_extended_org_usage_by_id_query(
571584
ServiceError::InternalServerError(format!("Error fetching ingestion data {:?}", e))
572585
})?;
573586

587+
if let Some(timer) = timer {
588+
timer.add("fetched ocr_pages");
589+
}
590+
574591
let website_pages_scraped = clickhouse_client
575592
.query(&format_with_daterange(
576593
"
@@ -587,6 +604,10 @@ pub async fn get_extended_org_usage_by_id_query(
587604
ServiceError::InternalServerError(format!("Error fetching ingestion data {:?}", e))
588605
})?;
589606

607+
if let Some(timer) = timer {
608+
timer.add("fetched website_pages_scraped");
609+
}
610+
590611
let events_ingested = clickhouse_client
591612
.query(&format_with_daterange(
592613
"
@@ -604,6 +625,10 @@ pub async fn get_extended_org_usage_by_id_query(
604625
ServiceError::InternalServerError(format!("Error fetching ingestion data {:?}", e))
605626
})?;
606627

628+
if let Some(timer) = timer {
629+
timer.add("fetched events_ingested");
630+
}
631+
607632
Ok(ExtendedOrganizationUsageCount {
608633
dataset_count: usage.dataset_count,
609634
user_count: usage.user_count,

0 commit comments

Comments
 (0)