Compute size of the indexed data only if explicitly requested to avoid heavy load on the DB

This commit is contained in:
sabaimran 2023-11-27 12:05:00 -08:00
parent eb5e3096e0
commit 6290b463f5
3 changed files with 25 additions and 3 deletions

View file

@ -4,7 +4,10 @@
<div class="page"> <div class="page">
<div id="content" class="section"> <div id="content" class="section">
<h2 class="section-title">Content</h2> <h2 class="section-title">Content</h2>
<p id="indexed-data-size" class="card-description">{{indexed_data_size_in_mb}} MB used</p> <button id="compute-index-size" class="card-button" onclick="getIndexedDataSize()">
Data Usage
</button>
<p id="indexed-data-size" class="card-description"></p>
<div class="section-cards"> <div class="section-cards">
<div class="card"> <div class="card">
<div class="card-title-row"> <div class="card-title-row">
@ -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 // List user's API keys on page load
listApiKeys(); listApiKeys();

View file

@ -334,6 +334,18 @@ def get_default_config_data():
return constants.empty_config 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]) @api.get("/config/types", response_model=List[str])
@requires(["authenticated"]) @requires(["authenticated"])
def get_config_types( def get_config_types(

View file

@ -141,7 +141,6 @@ def config_page(request: Request):
if user_subscription and user_subscription.renewal_date if user_subscription and user_subscription.renewal_date
else (user_subscription.created_at + timedelta(days=7)).strftime("%d %b %Y") 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)) enabled_content_source = set(EntryAdapters.get_unique_file_sources(user))
successfully_configured = { successfully_configured = {
@ -172,7 +171,6 @@ def config_page(request: Request):
"khoj_cloud_subscription_url": os.getenv("KHOJ_CLOUD_SUBSCRIPTION_URL"), "khoj_cloud_subscription_url": os.getenv("KHOJ_CLOUD_SUBSCRIPTION_URL"),
"is_active": has_required_scope(request, ["premium"]), "is_active": has_required_scope(request, ["premium"]),
"has_documents": has_documents, "has_documents": has_documents,
"indexed_data_size_in_mb": indexed_data_size_in_mb,
}, },
) )