|
4 | 4 | from tes_client import BaseTESClient |
5 | 5 | from data_processor import DataProcessor |
6 | 6 | from statistical_analyzer import StatisticalAnalyzer |
| 7 | +from submission_api_session import SubmissionAPISession |
7 | 8 | import numpy as np |
8 | 9 | import os |
9 | 10 | from string import Template |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class AnalysisRunner: |
13 | | - def __init__(self, |
14 | | - tes_client: BaseTESClient = AnalyticsTES(), |
15 | | - token: str = None, |
16 | | - project: str = None): |
17 | | - self.analysis_orchestrator = AnalysisOrchestrator(tes_client=tes_client, token=token, project=project) |
18 | | - self.tes_client = self.analysis_orchestrator.tes_client |
| 14 | + def __init__(self, tes_client: BaseTESClient = AnalyticsTES(), project: str = None): |
| 15 | + self.analysis_orchestrator = None |
| 16 | + self.tes_client = tes_client |
| 17 | + self.project = project |
19 | 18 | # Own instances for aggregation and analysis |
20 | 19 | self.data_processor = DataProcessor() |
21 | 20 | self.statistical_analyzer = StatisticalAnalyzer() |
@@ -43,57 +42,58 @@ def run_analysis(self, |
43 | 42 | Returns: |
44 | 43 | Dict[str, Any]: Analysis results |
45 | 44 | """ |
| 45 | + with SubmissionAPISession() as token_session: |
| 46 | + self.analysis_orchestrator = AnalysisOrchestrator(self.tes_client, token_session=token_session, project=self.project) |
46 | 47 |
|
47 | | - task_name, task_description, bucket, tres = self.analysis_orchestrator.setup_analysis(analysis_type, task_name, task_description, bucket, tres) |
48 | | - |
49 | | - # Check if we should run on existing data (returns early if so) |
50 | | - existing_data_result = self.check_analysis_on_existing_data(analysis_type, user_query, tres) |
51 | | - if existing_data_result is not None: |
52 | | - return existing_data_result |
53 | | - |
54 | | - ### create the TES message for the analysis |
55 | | - |
56 | | - self.tes_client.set_tes_messages( |
57 | | - query=user_query, |
58 | | - analysis_type=analysis_type, |
59 | | - task_name=task_name, |
60 | | - task_description=task_description, |
61 | | - output_format="json", |
62 | | - ) |
63 | | - self.tes_client.set_tags(tres=self.analysis_orchestrator.tres) |
64 | | - five_Safes_TES_message = self.tes_client.create_FiveSAFES_TES_message() |
65 | | - |
66 | | - |
67 | | - # Submit task and collect results (common workflow) |
68 | | - try: |
69 | | - task_id, data = self.analysis_orchestrator._submit_and_collect_results( |
70 | | - five_Safes_TES_message, |
71 | | - bucket, |
72 | | - output_format="json", |
73 | | - submit_message=f"Submitting {analysis_type} analysis to {len(self.analysis_orchestrator.tres)} TREs..." |
74 | | - ) |
75 | | - |
76 | | - # Process and analyze data (aggregation moved to this class) |
77 | | - print("Processing and analyzing data...") |
78 | | - raw_aggregated_data = self.data_processor.aggregate_data(data, analysis_type) |
| 48 | + task_name, task_description, bucket, tres = self.analysis_orchestrator.setup_analysis(analysis_type, task_name, task_description, bucket, tres) |
79 | 49 |
|
80 | | - analysis_result = self.statistical_analyzer.analyze_data(raw_aggregated_data, analysis_type) |
| 50 | + # Check if we should run on existing data (returns early if so) |
| 51 | + existing_data_result = self.check_analysis_on_existing_data(analysis_type, user_query, tres) |
| 52 | + if existing_data_result is not None: |
| 53 | + return existing_data_result |
81 | 54 |
|
82 | | - # Store the aggregated values in the centralized dict |
83 | | - self._store_aggregated_values(analysis_type) |
| 55 | + ### create the TES message for the analysis |
84 | 56 |
|
85 | | - return { |
86 | | - 'analysis_type': analysis_type, |
87 | | - 'result': analysis_result, |
88 | | - 'task_id': task_id, |
89 | | - 'tres_used': tres, |
90 | | - 'data_sources': len(data), |
91 | | - 'complete_query': user_query |
92 | | - } |
93 | | - |
94 | | - except Exception as e: |
95 | | - print(f"Analysis failed: {str(e)}") |
96 | | - raise |
| 57 | + self.tes_client.set_tes_messages( |
| 58 | + query=user_query, |
| 59 | + analysis_type=analysis_type, |
| 60 | + task_name=task_name, |
| 61 | + task_description=task_description, |
| 62 | + output_format="json", |
| 63 | + ) |
| 64 | + self.tes_client.set_tags(tres=self.analysis_orchestrator.tres) |
| 65 | + five_Safes_TES_message = self.tes_client.create_FiveSAFES_TES_message() |
| 66 | + |
| 67 | + # Submit task and collect results (common workflow) |
| 68 | + try: |
| 69 | + task_id, data = self.analysis_orchestrator._submit_and_collect_results( |
| 70 | + five_Safes_TES_message, |
| 71 | + bucket, |
| 72 | + output_format="json", |
| 73 | + submit_message=f"Submitting {analysis_type} analysis to {len(self.analysis_orchestrator.tres)} TREs..." |
| 74 | + ) |
| 75 | + |
| 76 | + # Process and analyze data (aggregation moved to this class) |
| 77 | + print("Processing and analyzing data...") |
| 78 | + raw_aggregated_data = self.data_processor.aggregate_data(data, analysis_type) |
| 79 | + |
| 80 | + analysis_result = self.statistical_analyzer.analyze_data(raw_aggregated_data, analysis_type) |
| 81 | + |
| 82 | + # Store the aggregated values in the centralized dict |
| 83 | + self._store_aggregated_values(analysis_type) |
| 84 | + |
| 85 | + return { |
| 86 | + 'analysis_type': analysis_type, |
| 87 | + 'result': analysis_result, |
| 88 | + 'task_id': task_id, |
| 89 | + 'tres_used': tres, |
| 90 | + 'data_sources': len(data), |
| 91 | + 'complete_query': user_query |
| 92 | + } |
| 93 | + |
| 94 | + except Exception as e: |
| 95 | + print(f"Analysis failed: {str(e)}") |
| 96 | + raise |
97 | 97 |
|
98 | 98 | def _store_aggregated_values(self, analysis_type: str): |
99 | 99 | """ |
|
0 commit comments