Skip to content

Commit 8bfb650

Browse files
committed
compute wl usage from precomp table
1 parent ece67b3 commit 8bfb650

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

fe/src/main.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ async fn main() -> Result<()> {
113113
state.pool.get().await?.batch_execute(SCHEMA).await?;
114114

115115
tokio::spawn(update_daily_user_queries(state.pool.clone()));
116+
tokio::spawn(update_wl_daily_user_queries(state.pool.clone()));
117+
116118
let listener = tokio::net::TcpListener::bind("0.0.0.0:8001").await?;
117119
axum::serve(listener, service(state)).await?;
118120
Ok(())
@@ -245,6 +247,7 @@ fn service(state: web::State) -> IntoMakeServiceWithConnectInfo<Router, SocketAd
245247
.into_make_service_with_connect_info::<SocketAddr>()
246248
}
247249

250+
#[tracing::instrument(skip_all)]
248251
async fn update_daily_user_queries(pool: deadpool_postgres::Pool) {
249252
loop {
250253
tokio::time::sleep(Duration::from_secs(30)).await;
@@ -276,8 +279,47 @@ async fn update_daily_user_queries(pool: deadpool_postgres::Pool) {
276279
)
277280
.await;
278281
match res {
279-
Ok(_) => tracing::info!("updated daily user queries"),
280-
Err(e) => tracing::error!("{} updating daily user queries", e),
282+
Ok(_) => tracing::info!("updated"),
283+
Err(e) => tracing::error!("{}", e),
284+
}
285+
}
286+
}
287+
288+
#[tracing::instrument(skip_all)]
289+
async fn update_wl_daily_user_queries(pool: deadpool_postgres::Pool) {
290+
loop {
291+
tokio::time::sleep(Duration::from_secs(30)).await;
292+
let pg = match pool.get().await {
293+
Ok(pg) => pg,
294+
Err(e) => {
295+
tracing::error!("getting pg from pool for daily user queries: {}", e);
296+
continue;
297+
}
298+
};
299+
let res = pg
300+
.query(
301+
"
302+
insert into wl_daily_user_queries (provision_key, org, day, n, updated_at)
303+
select
304+
k.provision_key,
305+
k.org,
306+
date_trunc('day', q.created_at)::date as day,
307+
count(*)::bigint,
308+
now()
309+
from user_queries q
310+
join wl_api_keys k on q.api_key = k.secret
311+
where q.created_at >= date_trunc('day', now())
312+
and q.created_at < date_trunc('day', now() + interval '1 day')
313+
group by k.provision_key, k.org, date_trunc('day', q.created_at)::date
314+
on conflict (provision_key, org, day)
315+
do update set n = excluded.n, updated_at = excluded.updated_at;
316+
",
317+
&[],
318+
)
319+
.await;
320+
match res {
321+
Ok(_) => tracing::info!("updated"),
322+
Err(e) => tracing::error!("{}", e),
281323
}
282324
}
283325
}

fe/src/schema.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ create table if not exists daily_user_queries (
141141
primary key (owner_email, day)
142142
);
143143

144+
create table if not exists wl_daily_user_queries(
145+
provision_key text not null,
146+
org text not null,
147+
day date not null,
148+
n int8 not null,
149+
updated_at timestamptz not null default now(),
150+
primary key (provision_key, org, day)
151+
);
152+
144153
create table if not exists config (
145154
enabled bool default true,
146155
name text,

fe/src/whitelabel.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,24 @@ pub async fn usage(
6565
Ok(Json(
6666
pg.query(
6767
"
68-
select count(*)::int8 as num_reqs
69-
from user_queries
70-
where api_key in (select secret from wl_api_keys where provision_key = $1 and org = $2)
71-
and date_part('year', created_at)::int8 = $3
72-
and date_part('month', created_at)::int8 = $4
73-
",
68+
select coalesce(sum(n)::int8, 0) as n
69+
from wl_daily_user_queries
70+
where provision_key = $3
71+
and org = $4
72+
and day >= make_date($1, $2, 1)
73+
and day < make_date($1, $2, 1) + interval '1 month';
74+
",
7475
&[
76+
&(req.year as i32),
77+
&(req.month as i32),
7578
&provision_key.secret,
7679
&req.org,
77-
&(req.year as i64),
78-
&(req.month as i64),
7980
],
8081
)
8182
.await?
8283
.iter()
8384
.map(|row| UsageResponse {
84-
num_reqs: row.get("num_reqs"),
85+
num_reqs: row.get("n"),
8586
})
8687
.next()
8788
.unwrap_or(UsageResponse { num_reqs: 0 }),

0 commit comments

Comments
 (0)