|
"""Summary controller package for handling summary page related operations.""" |
|
import os |
|
import importlib |
|
from concurrent.futures import ThreadPoolExecutor |
|
from typing import Dict, Any |
|
from .utils import get_content_flow_data, get_entity_analysis_data |
|
from .utils import get_sentiment_analysis_data |
|
|
|
def _run_process(args): |
|
""" |
|
Dynamically imports and runs the 'process' function from a specified summary module. |
|
|
|
Args: |
|
args (tuple): |
|
- module (str): The name of the summary module folder to import from. |
|
- chart_id (str): The name of the chart module to import and run. |
|
|
|
Returns: |
|
Any: The result returned by the 'process' function of the specified module. |
|
|
|
Raises: |
|
ModuleNotFoundError: If the specified summary module does not exist. |
|
AttributeError: If the 'process' function is not found in the module. |
|
""" |
|
module, chart_id = args |
|
return importlib.import_module(f"controllers.summary.{module}.{chart_id}").process() |
|
|
|
|
|
def process(module): |
|
""" |
|
Processes all Python chart modules within a specified subdirectory. |
|
|
|
Args: |
|
module (str): The name of the subdirectory (module) containing chart Python files. |
|
|
|
Returns: |
|
list: A list of results returned by processing each chart Python file. |
|
|
|
Notes: |
|
- Only files ending with ".py" and not named "__init__.py" are considered. |
|
- Utility files (utils.py, common.py, etc.) are automatically excluded. |
|
- Chart files are processed concurrently using a thread pool. |
|
- The helper function `_run_process` is used to process each chart file. |
|
""" |
|
current_dir = os.path.join(os.path.dirname(__file__), module) |
|
chart_ids = [ |
|
f[:-3] for f in os.listdir(current_dir) |
|
if f.endswith(".py") and f not in ("__init__.py",) |
|
] |
|
with ThreadPoolExecutor() as executor: |
|
charts = list(executor.map( |
|
_run_process, |
|
[(module, chart_id) for chart_id in sorted(chart_ids)] |
|
)) |
|
return charts |
|
|
|
|
|
def get_summary_data(include_content: bool = True, |
|
include_entity: bool = True, include_sentiment: bool = True) -> Dict[str, Any]: |
|
""" |
|
Get complete summary dashboard data for all time periods. |
|
|
|
This function aggregates content flow, entity analysis, and sentiment analysis data across |
|
three time periods: today, week, and month. |
|
|
|
Args: |
|
include_content (bool, optional): Whether to include content flow data. |
|
Defaults to True. |
|
include_entity (bool, optional): Whether to include entity analysis data. |
|
Defaults to True. |
|
include_sentiment (bool, optional): Whether to include sentiment analysis data. |
|
Defaults to True. |
|
|
|
Returns: |
|
Dict[str, Any]: Summary data containing content, entity, and/or sentiment information: |
|
- "content": Content flow data for today/week/month (if include_content=True) |
|
- "entity": Entity analysis data for today/week/month (if include_entity=True) |
|
- "sentiment": Sentiment analysis data for today/week/month (if include_sentiment=True) |
|
""" |
|
summary = {} |
|
if include_content: |
|
summary["content"] = { |
|
"today": get_content_flow_data("today"), |
|
"week": get_content_flow_data("week"), |
|
"month": get_content_flow_data("month") |
|
} |
|
if include_entity: |
|
summary["entity"] = { |
|
"today": get_entity_analysis_data("today"), |
|
"week": get_entity_analysis_data("week"), |
|
"month": get_entity_analysis_data("month") |
|
} |
|
if include_sentiment: |
|
summary["sentiment"] = { |
|
"today": get_sentiment_analysis_data("today"), |
|
"week": get_sentiment_analysis_data("week"), |
|
"month": get_sentiment_analysis_data("month") |
|
} |
|
return summary |
|
|