From 642f1a7bcced8ad7ff8b532833487b6a9de35de8 Mon Sep 17 00:00:00 2001 From: Marc Abramowitz Date: Tue, 23 Jul 2024 21:45:01 -0700 Subject: [PATCH] Check existence of multiple views in 1 query instead of multiple queries. This is more efficient because it lets us check for all views in one query instead of multiple queries. --- litellm/proxy/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index df3b68593..b08d7a30f 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -844,6 +844,30 @@ class PrismaClient: If the view doesn't exist, one will be created. """ + + # Check to see if all of the necessary views exist and if they do, simply return + # This is more efficient because it lets us check for all views in one + # query instead of multiple queries. + try: + ret = await self.db.query_raw( + """ + SELECT SUM(1) FROM pg_views + WHERE schemaname = 'public' AND viewname IN ( + 'LiteLLM_VerificationTokenView', + 'MonthlyGlobalSpend', + 'Last30dKeysBySpend', + 'Last30dModelsBySpend', + 'MonthlyGlobalSpendPerKey', + 'Last30dTopEndUsersSpend' + ) + """ + ) + if ret[0]['sum'] == 6: + print("All necessary views exist!") # noqa + return + except Exception: + pass + try: # Try to select one row from the view await self.db.query_raw(