Display metric units in web UI, make seed idempotent, simplify code
- Load m.unit in get_domain() so MetricBound carries units from DB - Add Unit column to domains list template - Make load_transport_seed() idempotent with IntegrityError handling and metric unit backfill for existing DBs - Remove unused imports (json, sqlite3, Entity) - Simplify combinator loop to list comprehension - Merge duplicate conditional/valid branches in pipeline - Consolidate duplicated SQL in get_all_results() - Expand CLAUDE.md with fuller architecture docs and conventions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
|
||||
@@ -204,7 +204,7 @@ class Repository:
|
||||
if not row:
|
||||
return None
|
||||
weights = self.conn.execute(
|
||||
"""SELECT m.name, dmw.weight, dmw.norm_min, dmw.norm_max, dmw.metric_id
|
||||
"""SELECT m.name, m.unit, dmw.weight, dmw.norm_min, dmw.norm_max, dmw.metric_id
|
||||
FROM domain_metric_weights dmw
|
||||
JOIN metrics m ON dmw.metric_id = m.id
|
||||
WHERE dmw.domain_id = ?""",
|
||||
@@ -218,7 +218,7 @@ class Repository:
|
||||
MetricBound(
|
||||
metric_name=w["name"], weight=w["weight"],
|
||||
norm_min=w["norm_min"], norm_max=w["norm_max"],
|
||||
metric_id=w["metric_id"],
|
||||
metric_id=w["metric_id"], unit=w["unit"] or "",
|
||||
)
|
||||
for w in weights
|
||||
],
|
||||
@@ -411,26 +411,17 @@ class Repository:
|
||||
|
||||
def get_all_results(self, domain_name: str, status: str | None = None) -> list[dict]:
|
||||
"""Return all results for a domain, optionally filtered by combo status."""
|
||||
query = """SELECT cr.*, c.hash, c.status as combo_status, d.name as domain_name
|
||||
FROM combination_results cr
|
||||
JOIN combinations c ON cr.combination_id = c.id
|
||||
JOIN domains d ON cr.domain_id = d.id
|
||||
WHERE d.name = ?"""
|
||||
params: list = [domain_name]
|
||||
if status:
|
||||
rows = self.conn.execute(
|
||||
"""SELECT cr.*, c.hash, c.status as combo_status, d.name as domain_name
|
||||
FROM combination_results cr
|
||||
JOIN combinations c ON cr.combination_id = c.id
|
||||
JOIN domains d ON cr.domain_id = d.id
|
||||
WHERE d.name = ? AND c.status = ?
|
||||
ORDER BY cr.composite_score DESC""",
|
||||
(domain_name, status),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = self.conn.execute(
|
||||
"""SELECT cr.*, c.hash, c.status as combo_status, d.name as domain_name
|
||||
FROM combination_results cr
|
||||
JOIN combinations c ON cr.combination_id = c.id
|
||||
JOIN domains d ON cr.domain_id = d.id
|
||||
WHERE d.name = ?
|
||||
ORDER BY cr.composite_score DESC""",
|
||||
(domain_name,),
|
||||
).fetchall()
|
||||
query += " AND c.status = ?"
|
||||
params.append(status)
|
||||
query += " ORDER BY cr.composite_score DESC"
|
||||
rows = self.conn.execute(query, params).fetchall()
|
||||
results = []
|
||||
for r in rows:
|
||||
combo = self.get_combination(r["combination_id"])
|
||||
|
||||
@@ -25,9 +25,7 @@ def generate_combinations(
|
||||
raise ValueError(f"No entities found for dimension '{dim}'")
|
||||
entity_groups.append(entities)
|
||||
|
||||
combinations = []
|
||||
for entity_tuple in itertools.product(*entity_groups):
|
||||
combo = Combination(entities=list(entity_tuple))
|
||||
combinations.append(combo)
|
||||
|
||||
return combinations
|
||||
return [
|
||||
Combination(entities=list(entity_tuple))
|
||||
for entity_tuple in itertools.product(*entity_groups)
|
||||
]
|
||||
|
||||
@@ -157,14 +157,13 @@ class Pipeline:
|
||||
result.pass1_blocked += 1
|
||||
self._update_run_counters(run_id, result, current_pass=1)
|
||||
continue # blocked — skip remaining passes
|
||||
elif cr.status == "conditional":
|
||||
combo.status = "valid"
|
||||
self.repo.update_combination_status(combo.id, "valid")
|
||||
result.pass1_conditional += 1
|
||||
else:
|
||||
combo.status = "valid"
|
||||
self.repo.update_combination_status(combo.id, "valid")
|
||||
result.pass1_valid += 1
|
||||
if cr.status == "conditional":
|
||||
result.pass1_conditional += 1
|
||||
else:
|
||||
result.pass1_valid += 1
|
||||
|
||||
self._update_run_counters(run_id, result, current_pass=1)
|
||||
elif 1 in passes:
|
||||
|
||||
@@ -265,22 +265,35 @@ INTERPLANETARY = Domain(
|
||||
|
||||
|
||||
def load_transport_seed(repo) -> dict:
|
||||
"""Load all transport seed data into the repository. Returns counts."""
|
||||
"""Load all transport seed data into the repository. Idempotent — safe to re-run."""
|
||||
import sqlite3
|
||||
from physcom.db.repository import Repository
|
||||
repo: Repository
|
||||
|
||||
counts = {"platforms": 0, "power_sources": 0, "domains": 0}
|
||||
|
||||
for entity in PLATFORMS:
|
||||
repo.add_entity(entity)
|
||||
counts["platforms"] += 1
|
||||
try:
|
||||
repo.add_entity(entity)
|
||||
counts["platforms"] += 1
|
||||
except sqlite3.IntegrityError:
|
||||
pass
|
||||
|
||||
for entity in POWER_SOURCES:
|
||||
repo.add_entity(entity)
|
||||
counts["power_sources"] += 1
|
||||
try:
|
||||
repo.add_entity(entity)
|
||||
counts["power_sources"] += 1
|
||||
except sqlite3.IntegrityError:
|
||||
pass
|
||||
|
||||
repo.add_domain(URBAN_COMMUTING)
|
||||
repo.add_domain(INTERPLANETARY)
|
||||
counts["domains"] = 2
|
||||
for domain in (URBAN_COMMUTING, INTERPLANETARY):
|
||||
try:
|
||||
repo.add_domain(domain)
|
||||
counts["domains"] += 1
|
||||
except sqlite3.IntegrityError:
|
||||
pass
|
||||
# Backfill metric units on existing DBs (ensure_metric is idempotent).
|
||||
for mb in domain.metric_bounds:
|
||||
repo.ensure_metric(mb.metric_name, unit=mb.unit)
|
||||
|
||||
return counts
|
||||
|
||||
Reference in New Issue
Block a user