From 6290b463f518a7d2f22954c05d54a2d499256e7d Mon Sep 17 00:00:00 2001 From: sabaimran Date: Mon, 27 Nov 2023 12:05:00 -0800 Subject: [PATCH] Compute size of the indexed data only if explicitly requested to avoid heavy load on the DB --- src/khoj/interface/web/config.html | 14 +++++++++++++- src/khoj/routers/api.py | 12 ++++++++++++ src/khoj/routers/web_client.py | 2 -- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/khoj/interface/web/config.html b/src/khoj/interface/web/config.html index 88fbc70d..96d82131 100644 --- a/src/khoj/interface/web/config.html +++ b/src/khoj/interface/web/config.html @@ -4,7 +4,10 @@

Content

-

{{indexed_data_size_in_mb}} MB used

+ +

@@ -472,6 +475,15 @@ }); } + function getIndexedDataSize() { + document.getElementById("indexed-data-size").innerHTML = "Calculating..."; + fetch('/api/config/index/size') + .then(response => response.json()) + .then(data => { + document.getElementById("indexed-data-size").innerHTML = data.indexed_data_size_in_mb + " MB used"; + }); + } + // List user's API keys on page load listApiKeys(); diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index cb0606f1..ae125980 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -334,6 +334,18 @@ def get_default_config_data(): return constants.empty_config +@api.get("/config/index/size", response_model=Dict[str, int]) +@requires(["authenticated"]) +async def get_indexed_data_size(request: Request, common: CommonQueryParams): + user = request.user.object + indexed_data_size_in_mb = await sync_to_async(EntryAdapters.get_size_of_indexed_data_in_mb)(user) + return Response( + content=json.dumps({"indexed_data_size_in_mb": math.ceil(indexed_data_size_in_mb)}), + media_type="application/json", + status_code=200, + ) + + @api.get("/config/types", response_model=List[str]) @requires(["authenticated"]) def get_config_types( diff --git a/src/khoj/routers/web_client.py b/src/khoj/routers/web_client.py index 8ce9dbe3..7907f99e 100644 --- a/src/khoj/routers/web_client.py +++ b/src/khoj/routers/web_client.py @@ -141,7 +141,6 @@ def config_page(request: Request): if user_subscription and user_subscription.renewal_date else (user_subscription.created_at + timedelta(days=7)).strftime("%d %b %Y") ) - indexed_data_size_in_mb = math.ceil(EntryAdapters.get_size_of_indexed_data_in_mb(user)) enabled_content_source = set(EntryAdapters.get_unique_file_sources(user)) successfully_configured = { @@ -172,7 +171,6 @@ def config_page(request: Request): "khoj_cloud_subscription_url": os.getenv("KHOJ_CLOUD_SUBSCRIPTION_URL"), "is_active": has_required_scope(request, ["premium"]), "has_documents": has_documents, - "indexed_data_size_in_mb": indexed_data_size_in_mb, }, )