From e91572a06fe7aee805e72ba6ce7b446a1f6bdb76 Mon Sep 17 00:00:00 2001
From: Simon
Date: Tue, 12 Jun 2018 23:19:57 -0400
Subject: [PATCH] Homepage stats now work with elasticsearch
---
app.py | 2 +-
database.py | 4 ++--
search/search.py | 11 ++++++++---
templates/home.html | 3 +--
4 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/app.py b/app.py
index 2b8a8c5..2ce196a 100644
--- a/app.py
+++ b/app.py
@@ -133,8 +133,8 @@ def contribute():
@app.route("/")
def home():
- stats = {}
stats = searchEngine.get_global_stats()
+ stats["website_count"] = len(db.get_all_websites())
current_websites = ", ".join(task.url for task in taskDispatcher.get_current_tasks())
return render_template("home.html", stats=stats, current_websites=current_websites)
diff --git a/database.py b/database.py
index fa8b2e6..176ab05 100644
--- a/database.py
+++ b/database.py
@@ -191,7 +191,7 @@ class Database:
cursor.execute("DELETE FROM ApiToken WHERE token=?", (token, ))
conn.commit()
- def _get_websites(self) -> dict:
+ def get_all_websites(self) -> dict:
# todo: mem cache that
with sqlite3.connect(self.db_path) as conn:
@@ -208,7 +208,7 @@ class Database:
def join_search_result(self, page: dict) -> dict:
- websites = self._get_websites()
+ websites = self.get_all_websites()
for hit in page["hits"]["hits"]:
hit["_source"]["website_url"] = websites[hit["_source"]["website_id"]]
diff --git a/search/search.py b/search/search.py
index b9e2c73..60af95a 100644
--- a/search/search.py
+++ b/search/search.py
@@ -222,10 +222,15 @@ class ElasticSearchEngine(SearchEngine):
},
"aggs": {
"total_size": {
- "extended_stats": {"field": "size"}
+ "sum": {"field": "size"}
}
},
"size": 0
- })
+ }, index=self.index_name)
+
+ stats = dict()
+ stats["file_count"] = result["hits"]["total"]
+ stats["file_size"] = result["aggregations"]["total_size"]["value"]
+
+ return stats
- print(result)
diff --git a/templates/home.html b/templates/home.html
index 952f258..9a2e373 100644
--- a/templates/home.html
+++ b/templates/home.html
@@ -10,8 +10,7 @@
{% if stats and stats["file_size"] %}
{{ stats["file_count"] }} files totalling
- ~{{ stats["file_size"] | filesizeformat }} in
- {{ stats["website_paths"] }} folders from {{ stats["website_count"] }} website(s)
+ ~{{ stats["file_size"] | filesizeformat }} from {{ stats["website_count"] }} website(s)
{% endif %}
{% if current_websites %}
Currently indexing {{ current_websites }}