Files
tianxuan/scripts/debug_db2.py
T

67 lines
2.7 KiB
Python

"""Debug: test _save_features_to_db step by step"""
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ['DJANGO_SETTINGS_MODULE'] = 'tianxuan.settings'
import django; django.setup()
from analysis.session_store import SessionStore
from analysis.data_loader import load_csv_directory
from analysis.entity_aggregator import aggregate_by_entity
from analysis.entity_detector import detect_entity_column
import asyncio
from analysis.tool_registry import _handle_run_clustering
from analysis import tool_registry
store = SessionStore()
store.drop_all()
print('1. Store cleared')
csv_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'data', 'test_flows.csv')
lf, schema, rc, fc, mem = load_csv_directory(csv_path)
store.store_dataset('ds', lf, schema=schema, metadata={'row_count': rc, 'csv_glob': csv_path})
entity_col = detect_entity_column('ds')['recommended']
agg_lf, features_list = aggregate_by_entity(lf, entity_col, schema)
df_agg = agg_lf.collect()
entity_schema = dict(zip(features_list, ['']*len(features_list)))
store.store_dataset('entity_data', agg_lf, schema=entity_schema, metadata={'row_count': len(df_agg), 'csv_glob': csv_path})
print('2. Dataset stored')
clust_result = asyncio.run(_handle_run_clustering(dataset_id='entity_data',
cluster_columns=[c for c in features_list if not c.startswith('_')][:8],
algorithm='hdbscan', params={}, random_state=42))
cid = clust_result['cluster_result_id']
print(f'3. Clustering done: {cid}, n_clusters={clust_result.get("n_clusters")}')
# Manual check
ce = store.get_cluster_result(cid)
if ce:
pid = ce.get('parent_dataset_id', '')
print(f'4. Cluster entry: parent_dataset_id={pid}')
de = store.get_dataset(pid)
if de:
cg = de.get('metadata', {}).get('csv_glob', 'MISSING')
print(f'5. Dataset entry exists, csv_glob={cg}')
else:
print('5. FAIL: dataset entry not found!')
# List all datasets
print(f' Available datasets: {store.list_datasets()}')
else:
print('4. FAIL: cluster entry not found!')
# Direct test
print('6. Test features...')
from analysis.models import AnalysisRun
runs_before = AnalysisRun.objects.count()
print(f' Runs before: {runs_before}')
result = tool_registry._save_features_to_db(cid, [{
'feature_name': 'test', 'cluster_label': 0,
'mean': 1.0, 'std': 0.5, 'global_mean': 0.5, 'global_std': 0.3,
'distinguishing_score': 2.0, 'distinguishing_method': 'zscore'
}])
print(f' Result: {result}')
print(f' Runs after: {AnalysisRun.objects.count()}')
from analysis.models import ClusterResult, ClusterFeature
print(f' ClusterResult: {ClusterResult.objects.count()}')
print(f' ClusterFeature: {ClusterFeature.objects.count()}')