@@ -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) ]
248251async 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}
0 commit comments