File size: 3,873 Bytes
c2e02ba
 
 
 
 
2646146
b235ac9
c2e02ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2646146
 
c2e02ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b235ac9
c2e02ba
1bfa10c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""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