Team Hoa, EPIC R08 (UI/Application Separation) - Team Hoa scope only
(R08-T11 -> T14; R08-T01->T10 belong to Team Duy/Team Nam).
- R08-T11: ui/schedule_task_tab.py (795 lines) -> presentation/scheduling/
{kanban_board_widget,calendar_view_widget,ai_task_creator_dialog,
ai_task_import_dialog,run_history_dialog}.py + schedule_task_tab.py
shell. Kanban CRUD/drag-drop now goes through
application/scheduling/task_application_service.py (R07-T04) instead of
~30 lines of inline if/elif per drag target.
- R08-T12: ui/folder_tab.py (1587 lines, the largest of the four) ->
presentation/folder/{workspace_file_tree,document_preview_manager,
code_editor,office_document_renderer,ai_file_editor_dialog,
ai_edit_model_resolver,ai_edit_pipeline}.py + folder_tab.py shell.
Closes the R06-T05 loop: FileWorkspaceService existed since R06 with
zero production call sites (confirmed by grep); every plain-text write
(save/create/write_content) now goes through it, gaining path
containment and a Python-syntax warning the original code never had.
Pure helpers (_read_text, _is_probably_text, _pptx_available,
_split_code_block, _parse_ai_output) moved to
application/workspaces/{file_preview_helpers,ai_edit_output}.py.
- R08-T13: ui/dashboard_tab.py (437 lines) -> presentation/dashboard/
{token_usage_card_widget,usage_chart_widget,habits_widget}.py +
dashboard_tab.py shell, backed by a new
application/monitoring/dashboard_query_service.py (pricing/period/
summary queries the three widgets used to each recompute separately).
Directory-ownership note left in the checklist for Team Nam.
- R08-T14: ui/structure_graph_view.py (1035 lines) ->
presentation/graph/{graph_scene_items,graph_renderer,
graph_messages_view,graph_qa_widget}.py + structure_graph_view.py
shell. Extraction helpers (_pdf_to_markdown, _extract_file_contents)
moved to application/workspaces/graph_index_service.py (pure Python).
Renderer and Q&A panel talk only through signals
(node_selected/graph_rendered/raw_json_ready/project_changed) - neither
imports the other.
- presentation/shared/web_engine_support.py: HAS_WEB_ENGINE, previously
duplicated (folder_tab imported it FROM structure_graph_view.py) - now
one shared flag instead of one screen importing another screen's module.
All four old ui/*.py files deleted; app.py and ui/workspace_tab.py updated
to the new import paths (each god-file only had 1-2 real construction
sites, so import sites were updated directly rather than kept as a
strangler-fig shim - unlike core/tools.py at R05, which had dozens).
pytest: 377 pass (+94 vs the R07 baseline of 328; same 4 pre-existing
failures as the R05/R06 baseline, unrelated to this work).
scripts/check_imports.py: PASS. python -c "import cowork_local.app": OK.
Every new file < 400 lines (largest: graph_renderer.py, 391).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
109 lines
5.2 KiB
Python
109 lines
5.2 KiB
Python
"""TokenUsageCardWidget — the stat-card grid + budget card of the Dashboard
|
|
(R08-T13, extracted from ``ui/dashboard_tab.py::DashboardTab``, lines
|
|
116-141/226-254/337-346 of the original 437-line file: the card grid layout,
|
|
``_apply_budget``, ``_refresh_budget``, and ``refresh()``'s card-filling
|
|
section).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
|
|
from PySide6.QtWidgets import QGridLayout, QWidget
|
|
|
|
from cowork_local.application.monitoring import DashboardQueryService
|
|
from cowork_local.i18n import tr
|
|
from cowork_local.ui.icons import icon
|
|
from cowork_local.ui.widgets import BudgetCard, StatCard, fmt_tokens
|
|
|
|
|
|
class TokenUsageCardWidget(QWidget):
|
|
"""Cost is the headline this screen exists for, so it gets a card twice
|
|
the height of the rest instead of being the fifth of five identical
|
|
tiles — with six equal cards nothing said which number mattered."""
|
|
|
|
def __init__(self, ctx, query: DashboardQueryService, parent=None):
|
|
super().__init__(parent)
|
|
self.ctx = ctx
|
|
self._query = query
|
|
|
|
cards_grid = QGridLayout(self)
|
|
cards_grid.setSpacing(8)
|
|
self.card_total = StatCard()
|
|
self.card_in = StatCard()
|
|
self.card_out = StatCard()
|
|
self.card_cache = StatCard()
|
|
self.card_cost = StatCard().as_hero()
|
|
# Hero on the left, spanning both rows; the four supporting figures
|
|
# fill a 2x2 block beside it.
|
|
cards_grid.addWidget(self.card_cost, 0, 0, 2, 1)
|
|
for i, card in enumerate((self.card_total, self.card_in,
|
|
self.card_out, self.card_cache)):
|
|
cards_grid.addWidget(card, i // 2, 1 + i % 2)
|
|
# Budget: remaining/budget, direct entry, auto-warns red past 85% used.
|
|
self.budget_card = BudgetCard()
|
|
self.budget_card.apply_btn.setIcon(icon("check"))
|
|
self.budget_card.apply_btn.clicked.connect(self._apply_budget)
|
|
cards_grid.addWidget(self.budget_card, 0, 3, 2, 1)
|
|
for col, stretch in ((0, 3), (1, 2), (2, 2), (3, 3)):
|
|
cards_grid.setColumnStretch(col, stretch)
|
|
|
|
self.retranslate()
|
|
|
|
def retranslate(self) -> None:
|
|
self.budget_card.apply_btn.setToolTip(tr("usage.budget_apply_tooltip"))
|
|
self.budget_card.budget_spin.setToolTip(tr("usage.budget_spin_tooltip"))
|
|
|
|
def refresh(self, start: date, end: date) -> None:
|
|
summary = self._query.summary(start, end)
|
|
s, pricing, costs = summary["stats"], summary["pricing"], summary["costs"]
|
|
from cowork_local.core import usage_tracker as ut
|
|
|
|
est_note = (tr("dashboard.estimated_note", pct=int(s["estimated_share"] * 100))
|
|
if s["estimated_share"] > 0 else "")
|
|
self.card_total.set(tr("dashboard.card_total"), fmt_tokens(s["total"]),
|
|
tr("dashboard.card_turns", n=s["turns"]))
|
|
self.card_in.set(tr("dashboard.card_in"), fmt_tokens(s["in"]),
|
|
ut.format_cost(costs["in"], pricing))
|
|
self.card_out.set(tr("dashboard.card_out"), fmt_tokens(s["out"]),
|
|
ut.format_cost(costs["out"], pricing))
|
|
self.card_cache.set(tr("dashboard.card_cache"), fmt_tokens(s["cache"]),
|
|
ut.format_cost(costs["cache"], pricing))
|
|
self.card_cost.set(tr("dashboard.card_cost"),
|
|
ut.format_cost(summary["total_cost"], pricing, digits=2), est_note)
|
|
self._refresh_budget()
|
|
|
|
def _apply_budget(self) -> None:
|
|
"""Persist the spin box's value as the new budget — starts a fresh
|
|
remaining-balance window (spend before now is no longer counted)."""
|
|
ccy = (self.ctx.config.data.get("usage") or {}).get("currency", "USD")
|
|
self._query.set_budget(self.budget_card.budget_spin.value(), ccy)
|
|
self.ctx.save()
|
|
self._refresh_budget()
|
|
|
|
def _refresh_budget(self) -> None:
|
|
from cowork_local.core import model_pricing as mp
|
|
from cowork_local.core import usage_tracker as ut
|
|
|
|
pricing = self._query.pricing()
|
|
status = self._query.budget_status()
|
|
if status is None:
|
|
self.budget_card.set(tr("usage.budget_title"), "—", tr("usage.budget_no_budget"))
|
|
self.budget_card.budget_spin.setValue(0.0)
|
|
return
|
|
remaining_disp = mp.convert(status["remaining_usd"], "USD",
|
|
pricing.get("currency", "USD"), self.ctx.config)
|
|
amount_disp = mp.convert(status["amount_usd"], "USD",
|
|
pricing.get("currency", "USD"), self.ctx.config)
|
|
value = (f"{ut.format_cost(status['remaining_usd'], pricing, digits=2)}"
|
|
f" / {ut.format_cost(status['amount_usd'], pricing, digits=2)}")
|
|
pct = int(round(status["pct_used"] * 100))
|
|
sub = tr("usage.budget_over_warning") if status["over_85"] else tr("usage.budget_used_pct", pct=pct)
|
|
self.budget_card.set(tr("usage.budget_title"), value, sub, warn=status["over_85"])
|
|
# keep the entry field showing the CURRENT budget (in display currency)
|
|
# — only when it doesn't already have unsaved focus/edits from the user.
|
|
if not self.budget_card.budget_spin.hasFocus():
|
|
self.budget_card.budget_spin.setValue(round(amount_disp, 2))
|
|
|
|
|
|
__all__ = ["TokenUsageCardWidget"]
|