265 lines
No EOL
9.9 KiB
Python
265 lines
No EOL
9.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test routine for WorkflowManager.workflowProcess()
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
import sys
|
|
import os
|
|
import json
|
|
from datetime import datetime, UTC, timedelta
|
|
import uuid
|
|
|
|
print("Starting test_workflow.py...")
|
|
|
|
# Configure logging FIRST, before any other imports
|
|
logging.basicConfig(
|
|
level=logging.DEBUG,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.StreamHandler(sys.stdout),
|
|
logging.FileHandler('test_workflow.log', encoding='utf-8')
|
|
],
|
|
force=True # Force reconfiguration even if already configured
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
print("Logger level:", logger.level)
|
|
logger.info("Logger is working!")
|
|
print("Logger test done")
|
|
|
|
# Set up test configuration
|
|
os.environ['POWERON_CONFIG_FILE'] = 'test_config.ini'
|
|
print("Set POWERON_CONFIG_FILE environment variable")
|
|
|
|
try:
|
|
# Simple imports from modules (same as app.py)
|
|
from modules.interfaces.interfaceAppObjects import User, UserConnection
|
|
from modules.interfaces.interfaceChatObjects import ChatObjects
|
|
from modules.interfaces.interfaceChatModel import UserInputRequest, ChatWorkflow
|
|
from modules.workflow.managerWorkflow import WorkflowManager
|
|
print("All imports successful")
|
|
except Exception as e:
|
|
print(f"Import error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|
|
|
|
def create_test_user() -> User:
|
|
"""Create a test user for the workflow"""
|
|
return User(
|
|
id="test-user-001",
|
|
mandateId="test-mandate-001",
|
|
username="testuser",
|
|
email="test@example.com",
|
|
fullName="Test User",
|
|
enabled=True,
|
|
language="en",
|
|
privilege="user",
|
|
authenticationAuthority="local"
|
|
)
|
|
|
|
def create_test_workflow() -> ChatWorkflow:
|
|
"""Create a test workflow"""
|
|
return ChatWorkflow(
|
|
id="test-workflow-001",
|
|
mandateId="test-mandate-001",
|
|
status="running",
|
|
name="Business Intelligence Analysis Workflow",
|
|
currentRound=1,
|
|
lastActivity=datetime.now(UTC).isoformat(),
|
|
startedAt=datetime.now(UTC).isoformat(),
|
|
logs=[],
|
|
messages=[],
|
|
stats=None,
|
|
tasks=[]
|
|
)
|
|
|
|
def create_test_user_input() -> UserInputRequest:
|
|
"""Create test user input with a meaningful business intelligence task"""
|
|
return UserInputRequest(
|
|
prompt="""Please analyze the quarterly sales data and create a comprehensive business intelligence report.
|
|
|
|
The task involves:
|
|
1. Extract and analyze sales data from the provided Excel files
|
|
2. Identify key trends, patterns, and anomalies in the data
|
|
3. Create visualizations (charts and graphs) to illustrate findings
|
|
4. Generate a professional PowerPoint presentation summarizing the analysis
|
|
5. Create a detailed markdown report with actionable insights
|
|
6. Search for industry benchmarks and best practices to compare our performance
|
|
7. Store the final reports in SharePoint for team access
|
|
|
|
Please ensure the analysis includes:
|
|
- Sales performance by region and product category
|
|
- Month-over-month growth trends
|
|
- Customer segmentation analysis
|
|
- Revenue forecasting for the next quarter
|
|
- Recommendations for improving sales performance
|
|
|
|
The output should be suitable for executive review and include both high-level summaries and detailed technical analysis.""",
|
|
listFileId=["sales_data_q1.xlsx", "sales_data_q2.xlsx", "customer_data.csv"],
|
|
userLanguage="en"
|
|
)
|
|
|
|
async def test_workflow_process():
|
|
print("Inside test_workflow_process()")
|
|
"""Test the workflowProcess function"""
|
|
try:
|
|
logger.info("Starting workflow process test...")
|
|
|
|
# Create test data
|
|
test_user = create_test_user()
|
|
test_workflow = create_test_workflow()
|
|
test_user_input = create_test_user_input()
|
|
|
|
# Create test user in database through AppObjects interface
|
|
from modules.interfaces.interfaceAppObjects import getRootInterface
|
|
from modules.interfaces.interfaceAppModel import AuthAuthority, ConnectionStatus, Token, UserPrivilege
|
|
|
|
root_interface = getRootInterface()
|
|
created_user = root_interface.createUser(
|
|
username=test_user.username,
|
|
password="testpassword123", # Required for local authentication
|
|
email=test_user.email,
|
|
fullName=test_user.fullName,
|
|
language=test_user.language,
|
|
enabled=test_user.enabled,
|
|
privilege=UserPrivilege.USER,
|
|
authenticationAuthority=AuthAuthority.LOCAL
|
|
)
|
|
logger.info(f"Created test user in database: {created_user.id}")
|
|
|
|
# Create test connection through AppObjects interface
|
|
from modules.interfaces.interfaceAppObjects import getInterface as getAppObjects
|
|
app_interface = getAppObjects(created_user)
|
|
test_connection = app_interface.addUserConnection(
|
|
userId=created_user.id,
|
|
authority=AuthAuthority.MSFT,
|
|
externalId="msft-user-123",
|
|
externalUsername="testuser@example.com",
|
|
externalEmail="testuser@example.com",
|
|
status=ConnectionStatus.ACTIVE
|
|
)
|
|
logger.info(f"Created test connection: {test_connection.id}")
|
|
|
|
# Create test token for the connection
|
|
test_token = Token(
|
|
userId=created_user.id,
|
|
authority=AuthAuthority.MSFT,
|
|
tokenAccess="test-access-token-123",
|
|
tokenRefresh="test-refresh-token-456",
|
|
tokenType="bearer",
|
|
expiresAt=datetime.now(UTC).timestamp() + 3600, # 1 hour from now
|
|
createdAt=datetime.now(UTC)
|
|
)
|
|
app_interface.saveToken(test_token)
|
|
logger.info(f"Created test token for connection: {test_token.id}")
|
|
|
|
logger.info(f"Test user: {created_user.username}")
|
|
logger.info(f"Test workflow: {test_workflow.id}")
|
|
|
|
# Log the full prompt in JSON format
|
|
logger.debug("=" * 60)
|
|
logger.debug("USER INPUT PROMPT (JSON):")
|
|
logger.debug("=" * 60)
|
|
prompt_data = {
|
|
"prompt": test_user_input.prompt,
|
|
"listFileId": test_user_input.listFileId,
|
|
"userLanguage": test_user_input.userLanguage
|
|
}
|
|
logger.debug(json.dumps(prompt_data, indent=2, ensure_ascii=False))
|
|
logger.debug("=" * 60)
|
|
|
|
logger.debug(f"Test files: {test_user_input.listFileId}")
|
|
|
|
# Create test workflow in database through ChatObjects interface
|
|
from modules.interfaces.interfaceChatObjects import getInterface as getChatObjects
|
|
|
|
chat_interface = getChatObjects(created_user)
|
|
workflow_data = {
|
|
"name": test_workflow.name,
|
|
"status": test_workflow.status,
|
|
"mandateId": created_user.mandateId,
|
|
"currentRound": test_workflow.currentRound,
|
|
"startedAt": test_workflow.startedAt,
|
|
"lastActivity": test_workflow.lastActivity
|
|
}
|
|
created_workflow = chat_interface.createWorkflow(workflow_data)
|
|
logger.info(f"Created test workflow: {created_workflow.id}")
|
|
|
|
# Update the test_workflow object with the created workflow's ID
|
|
test_workflow.id = created_workflow.id
|
|
|
|
# Initialize WorkflowManager
|
|
workflow_manager = WorkflowManager(chat_interface, created_user)
|
|
logger.info("WorkflowManager initialized")
|
|
|
|
# Test the workflowProcess function
|
|
logger.info("Calling workflowProcess...")
|
|
task = await workflow_manager.workflowProcess(test_user_input, test_workflow)
|
|
|
|
# Log results
|
|
if task:
|
|
logger.debug("Task created successfully!")
|
|
logger.debug(f"Task ID: {task.id}")
|
|
logger.debug(f"Task Status: {task.status}")
|
|
logger.debug(f"Task Feedback: {task.feedback}")
|
|
logger.info(f"Number of actions: {len(task.actionList) if task.actionList else 0}")
|
|
|
|
# Log the full task object in JSON format
|
|
logger.debug("=" * 60)
|
|
logger.debug("TASK OBJECT (JSON):")
|
|
logger.debug("=" * 60)
|
|
task_data = {
|
|
"id": task.id,
|
|
"status": task.status,
|
|
"feedback": task.feedback,
|
|
"actionList": [
|
|
{
|
|
"execMethod": action.execMethod,
|
|
"execAction": action.execAction,
|
|
"execParameters": action.execParameters
|
|
} for action in (task.actionList or [])
|
|
] if task.actionList else []
|
|
}
|
|
logger.debug(json.dumps(task_data, indent=2, ensure_ascii=False))
|
|
logger.debug("=" * 60)
|
|
|
|
if task.actionList:
|
|
for i, action in enumerate(task.actionList):
|
|
logger.info(f"Action {i+1}: {action.execMethod}.{action.execAction}")
|
|
logger.info(f" Parameters: {action.execParameters}")
|
|
else:
|
|
logger.warning("No task was created")
|
|
|
|
logger.info("Test completed successfully!")
|
|
return task
|
|
|
|
except Exception as e:
|
|
logger.error(f"❌ Test failed with error: {str(e)}")
|
|
logger.exception("Full traceback:")
|
|
raise
|
|
|
|
async def main():
|
|
print("Inside main()")
|
|
logger.info("=" * 50)
|
|
logger.info("BUSINESS INTELLIGENCE WORKFLOW TEST")
|
|
logger.info("=" * 50)
|
|
|
|
try:
|
|
task = await test_workflow_process()
|
|
logger.info("=" * 50)
|
|
logger.info("TEST COMPLETED SUCCESSFULLY")
|
|
logger.info("=" * 50)
|
|
return task
|
|
except Exception as e:
|
|
logger.error("=" * 50)
|
|
logger.error("TEST FAILED")
|
|
logger.error("=" * 50)
|
|
raise
|
|
|
|
if __name__ == "__main__":
|
|
print("About to run main()")
|
|
asyncio.run(main())
|
|
print("main() finished") |