Merge pull request #42 from oxbridge-econ/abdur/get-user-doc-count
Browse files- app/app.py +2 -1
- app/controllers/entity.py +29 -0
- app/routes/entity.py +32 -0
- app/routes/summary.py +0 -1
app/app.py
CHANGED
|
@@ -10,7 +10,7 @@ from fastapi.responses import JSONResponse
|
|
| 10 |
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 11 |
from apscheduler.triggers.cron import CronTrigger
|
| 12 |
|
| 13 |
-
from routes import category, summary, keyword, lda # pylint: disable=import-error
|
| 14 |
|
| 15 |
|
| 16 |
class Config: # pylint: disable=too-few-public-methods
|
|
@@ -172,6 +172,7 @@ app.include_router(category.router)
|
|
| 172 |
app.include_router(summary.router)
|
| 173 |
app.include_router(keyword.router)
|
| 174 |
app.include_router(lda.router)
|
|
|
|
| 175 |
|
| 176 |
@app.get("/_health")
|
| 177 |
def health():
|
|
|
|
| 10 |
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 11 |
from apscheduler.triggers.cron import CronTrigger
|
| 12 |
|
| 13 |
+
from routes import category, summary, keyword, lda, entity # pylint: disable=import-error
|
| 14 |
|
| 15 |
|
| 16 |
class Config: # pylint: disable=too-few-public-methods
|
|
|
|
| 172 |
app.include_router(summary.router)
|
| 173 |
app.include_router(keyword.router)
|
| 174 |
app.include_router(lda.router)
|
| 175 |
+
app.include_router(entity.router)
|
| 176 |
|
| 177 |
@app.get("/_health")
|
| 178 |
def health():
|
app/controllers/entity.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Controller for entity-related operations."""
|
| 2 |
+
from models.database import entity_collection
|
| 3 |
+
|
| 4 |
+
def retrieve_hot_entity():
|
| 5 |
+
"""Retrieves the top 200 documents from a MongoDB collection,
|
| 6 |
+
sorted by the occurrence field in descending order.
|
| 7 |
+
The function returns a dict containing the count of documents and a list of documents
|
| 8 |
+
Parameters
|
| 9 |
+
None
|
| 10 |
+
|
| 11 |
+
Type: dict
|
| 12 |
+
Structure:
|
| 13 |
+
|
| 14 |
+
Count (int): The number of documents retrieved (up to 200).
|
| 15 |
+
Items (list): A list of dictionaries, each containing:
|
| 16 |
+
|
| 17 |
+
entity: The value of the entity field from the document.
|
| 18 |
+
entityType: The value of the entityType field from the document.
|
| 19 |
+
total_occurrence: The value of the occurrence field from the document.
|
| 20 |
+
"""
|
| 21 |
+
result = list(entity_collection.find(
|
| 22 |
+
{},
|
| 23 |
+
{'entity': 1, 'entityType': 1, 'total_occurrence': '$occurrence', '_id': 0}
|
| 24 |
+
).sort('occurrence', -1).limit(200))
|
| 25 |
+
res = {
|
| 26 |
+
"Count": len(result),
|
| 27 |
+
"Items": result
|
| 28 |
+
}
|
| 29 |
+
return res
|
app/routes/entity.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This module defines the /lda route for the FastAPI application."""
|
| 2 |
+
|
| 3 |
+
import logging
|
| 4 |
+
from fastapi import APIRouter
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
from controllers.entity import retrieve_hot_entity # pylint: disable=import-error
|
| 7 |
+
|
| 8 |
+
# Configure logger
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
# Create FastAPI Router
|
| 12 |
+
router = APIRouter(prefix="/entity", tags=["entity"])
|
| 13 |
+
|
| 14 |
+
@router.get('/hot')
|
| 15 |
+
async def get_hot_entity():
|
| 16 |
+
"""
|
| 17 |
+
Handles GET requests to retrieve hot entity.
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
dict: JSON response containing count and a list of hot entity.
|
| 21 |
+
"""
|
| 22 |
+
return JSONResponse(content=retrieve_hot_entity())
|
| 23 |
+
|
| 24 |
+
@router.post('/hot')
|
| 25 |
+
async def post_hot_entity():
|
| 26 |
+
"""
|
| 27 |
+
Handles GET requests to retrieve hot entity.
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
dict: JSON response containing count and a list of hot entity.
|
| 31 |
+
"""
|
| 32 |
+
return JSONResponse(content=retrieve_hot_entity())
|
app/routes/summary.py
CHANGED
|
@@ -62,7 +62,6 @@ async def get_user_document_stats(
|
|
| 62 |
detail="Internal server error while retrieving document statistics."
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
@router.get('')
|
| 67 |
async def get_summary() -> JSONResponse:
|
| 68 |
"""
|
|
|
|
| 62 |
detail="Internal server error while retrieving document statistics."
|
| 63 |
)
|
| 64 |
|
|
|
|
| 65 |
@router.get('')
|
| 66 |
async def get_summary() -> JSONResponse:
|
| 67 |
"""
|