fsanyoto commited on
Commit
e048ba6
·
verified ·
1 Parent(s): 11c14e3

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,11 +1,4 @@
1
  {
2
- "current": "b64f97c30f",
3
- "releases": [
4
- {
5
- "version": "v86",
6
- "sha": "8587070fe2",
7
- "date": "2026-11-10",
8
- "subject": "release(sdlc84): QA.md names live's KMS data-key settings before the promote (D-13535)"
9
- }
10
- ]
11
  }
 
1
  {
2
+ "current": "e2461aac6e",
3
+ "releases": []
 
 
 
 
 
 
 
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- b64f97c30f
 
1
+ e2461aac6e
api/registry_source.py CHANGED
@@ -315,6 +315,59 @@ class BatchStore:
315
  return err(503, 'canonical_view_membership_invalid',
316
  'A saved view contains membership entries but no valid record IDs. '
317
  'Repair its membership before reading this database.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  for owner, ws in meta.items():
319
  if not isinstance(ws, dict) or owner.startswith('_'):
320
  continue
 
315
  return err(503, 'canonical_view_membership_invalid',
316
  'A saved view contains membership entries but no valid record IDs. '
317
  'Repair its membership before reading this database.')
318
+ many = getattr(reader, 'iter_view_members_many', None) or getattr(reader, '_iter_view_members_many', None)
319
+ source_cur = getattr(self, '_canonical_source_cursor', None)
320
+ relation = source_cur is not None and bool(selected or self.metadata.get('_scan'))
321
+ memo = 'view_members' if relation else 'view_witness'
322
+ pending = [(owner, vid) for owner, ws in meta.items()
323
+ if isinstance(ws, dict) and not owner.startswith('_')
324
+ for vid, view in (ws.get('views') or {}).items()
325
+ if isinstance(view, dict) and isinstance(view.get('config'), dict)
326
+ and 'memberPids' not in view['config'] and (memo, name, owner, vid) not in self.metadata]
327
+ if pending and callable(stream) and callable(many):
328
+ # One statement reads every stripped membership, so a drill does not stream once per view.
329
+ parts = many(getattr(self._runtime, 'store_key', str)(name), pending,
330
+ getattr(self.metadata[name], 'source_revision', None),
331
+ batch_size=4096, head=None if relation else BATCH_ROWS)
332
+ rows = (row for batch in parts for row in batch)
333
+ def advance():
334
+ try:
335
+ return next(rows, None)
336
+ except RuntimeError as exc:
337
+ from core.request_timing import raise_if_resource_error
338
+ raise_if_resource_error(exc)
339
+ raise err(503, 'canonical_source_unavailable', 'Saved view membership changed or is unavailable.') from exc
340
+ found = {}
341
+ with closing(parts):
342
+ ahead = [advance()]
343
+ for index, (owner, vid) in enumerate(pending):
344
+ first, seen = [None], [0]
345
+ def group():
346
+ while ahead[0] is not None and ahead[0][0] == index:
347
+ raw, ahead[0] = ahead[0][1], advance()
348
+ seen[0] += 1
349
+ try:
350
+ pid = int(raw)
351
+ except (TypeError, ValueError):
352
+ continue
353
+ if first[0] is None:
354
+ first[0] = pid
355
+ yield pid
356
+ if relation:
357
+ from routes_odoo_tables import _membership_relation
358
+ operand = _membership_relation(source_cur, group())
359
+ for _ in group():
360
+ pass
361
+ found[(memo, name, owner, vid)] = (operand, first[0], bool(seen[0]))
362
+ continue
363
+ for _ in group():
364
+ pass
365
+ # A head with no valid ID is left to the per-view stream, which reads past it.
366
+ if first[0] is not None or seen[0] < BATCH_ROWS:
367
+ found[(memo, name, owner, vid)] = first[0], bool(seen[0])
368
+ if ahead[0] is not None:
369
+ raise err(503, 'canonical_source_unavailable', 'Saved view membership changed or is unavailable.')
370
+ self.metadata.update(found)
371
  for owner, ws in meta.items():
372
  if not isinstance(ws, dict) or owner.startswith('_'):
373
  continue
api/routes_amazon_projects.py CHANGED
@@ -653,30 +653,45 @@ def _list_figures(session, records, today):
653
  return out
654
 
655
 
 
 
 
 
 
 
 
656
  @router.get("")
657
  def row_projects(grain: str | None = None, key: str | None = None,
658
  session: Session = Depends(require_session)):
659
  """A row's projects with its ASIN's and Parent ASIN's, each once; every project without a row."""
 
660
  runtime = _reader(session)
661
  can_edit = bool(routes_amazon_profit._may_edit(session))
662
  today = projects._today()
663
  if grain is None and key is None:
664
  records = _call(projects.list_projects, runtime)
665
- figures = _list_figures(session, records, today)
666
  return {"grain": None, "key": None, "canEdit": can_edit, "today": today.isoformat(),
667
- "projects": [{**_project(r, today), "figures": figures.get(r["id"])} for r in records],
668
- "summary": projects.summary(records)}
669
  pair = _pair(grain, key)
670
- above, cause = _mirror(runtime, lambda cur: projects.lineage(cur, *pair), [])
671
- pairs = [pair] + [p for p in above if p != pair]
672
- listed = _distinct(_call(projects.projects_for, runtime, pairs), pairs)
673
- figures = _list_figures(session, [record for _via, record in listed], today)
674
- out = [{**_project(record, today), "via": {"grain": via[0], "key": via[1]}, "figures": figures.get(record["id"])}
675
- for via, record in listed]
676
  return _with_cause({"grain": pair[0], "key": pair[1], "canEdit": can_edit, "today": today.isoformat(), "projects": out,
677
  "summary": projects.summary([record for _via, record in listed])}, cause)
678
 
679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
680
  @router.get("/search")
681
  def search_targets(q: str = "", limit: int = 25, session: Session = Depends(require_session), offset: int = 0):
682
  """A SKU-ordered page from the Amazon mirror; total counts every match, including past the end."""
 
653
  return out
654
 
655
 
656
+ def _row_listed(runtime, pair):
657
+ """`(via, record)` pairs for a row and its ASIN and Parent ASIN, each project once, plus the lineage cause."""
658
+ above, cause = _mirror(runtime, lambda cur: projects.lineage(cur, *pair), [])
659
+ pairs = [pair] + [p for p in above if p != pair]
660
+ return _distinct(_call(projects.projects_for, runtime, pairs), pairs), cause
661
+
662
+
663
  @router.get("")
664
  def row_projects(grain: str | None = None, key: str | None = None,
665
  session: Session = Depends(require_session)):
666
  """A row's projects with its ASIN's and Parent ASIN's, each once; every project without a row."""
667
+ # No series read runs here. Sales and Units come from GET /figures after the list paints.
668
  runtime = _reader(session)
669
  can_edit = bool(routes_amazon_profit._may_edit(session))
670
  today = projects._today()
671
  if grain is None and key is None:
672
  records = _call(projects.list_projects, runtime)
 
673
  return {"grain": None, "key": None, "canEdit": can_edit, "today": today.isoformat(),
674
+ "projects": [_project(r, today) for r in records], "summary": projects.summary(records)}
 
675
  pair = _pair(grain, key)
676
+ listed, cause = _row_listed(runtime, pair)
677
+ out = [{**_project(record, today), "via": {"grain": via[0], "key": via[1]}} for via, record in listed]
 
 
 
 
678
  return _with_cause({"grain": pair[0], "key": pair[1], "canEdit": can_edit, "today": today.isoformat(), "projects": out,
679
  "summary": projects.summary([record for _via, record in listed])}, cause)
680
 
681
 
682
+ @router.get("/figures")
683
+ def row_project_figures(grain: str | None = None, key: str | None = None,
684
+ session: Session = Depends(require_session)):
685
+ """`{"figures": {project id: Dashboard Sales and Units}}` for the same projects GET "" lists, in one call."""
686
+ # Declared before /{project_id}. Nothing is cached, so a withheld answer clears on the next call.
687
+ runtime = _reader(session)
688
+ if grain is None and key is None:
689
+ records = _call(projects.list_projects, runtime)
690
+ else:
691
+ records = [record for _via, record in _row_listed(runtime, _pair(grain, key))[0]]
692
+ return {"figures": _list_figures(session, records, projects._today())}
693
+
694
+
695
  @router.get("/search")
696
  def search_targets(q: str = "", limit: int = 25, session: Session = Depends(require_session), offset: int = 0):
697
  """A SKU-ordered page from the Amazon mirror; total counts every match, including past the end."""
api/sync_runtime.py CHANGED
@@ -70,6 +70,8 @@ EXCLUDED_NAMES = frozenset({
70
  "SYSTEM", "CPU_CORES", "MEMORY", "ACCELERATOR", "GPG_KEY", "RI_DIR",
71
  "AIOS_PARENT_PID", "AIOS_MIRROR_HANDOFF_DIR", "AIOS_UVICORN_WORKERS", "AIOS_MIRROR_VERSIONS",
72
  "AIOS_DUCKDB_MEMORY", "AIOS_DUCKDB_THREADS", "AIOS_WEB_DUCKDB_MEMORY", "AIOS_WEB_DUCKDB_THREADS",
 
 
73
  "AIOS_PROCESS_ROLE", "AIOS_WORKER_SCOPE", "AIOS_SYNC_WRITER_TENANT", "AIOS_SYNC_FLEET_TENANTS",
74
  RUNTIME_ENV})
75
  EXCLUDED_PREFIXES = ("PYTHON", "KUBERNETES_", "SPACE_", "LC_")
 
70
  "SYSTEM", "CPU_CORES", "MEMORY", "ACCELERATOR", "GPG_KEY", "RI_DIR",
71
  "AIOS_PARENT_PID", "AIOS_MIRROR_HANDOFF_DIR", "AIOS_UVICORN_WORKERS", "AIOS_MIRROR_VERSIONS",
72
  "AIOS_DUCKDB_MEMORY", "AIOS_DUCKDB_THREADS", "AIOS_WEB_DUCKDB_MEMORY", "AIOS_WEB_DUCKDB_THREADS",
73
+ # The web box sizes this for a worker beside it. A job derives its own threads from its CPUs.
74
+ "AIOS_WORKER_DUCKDB_THREADS",
75
  "AIOS_PROCESS_ROLE", "AIOS_WORKER_SCOPE", "AIOS_SYNC_WRITER_TENANT", "AIOS_SYNC_FLEET_TENANTS",
76
  RUNTIME_ENV})
77
  EXCLUDED_PREFIXES = ("PYTHON", "KUBERNETES_", "SPACE_", "LC_")
api/sync_worker.py CHANGED
@@ -24,6 +24,9 @@ def configure(env=None):
24
  """Set the worker role, its own DuckDB files and its DuckDB caps. Returns the worker DuckDB path."""
25
  env = os.environ if env is None else env
26
  threads = env.get("AIOS_WORKER_DUCKDB_THREADS")
 
 
 
27
  if not threads and env.get("JOB_ID"):
28
  # An HF job owns its box: DuckDB takes the job's CPUs (at most 4), so a boot restore is not one thread.
29
  cpus = 0
 
24
  """Set the worker role, its own DuckDB files and its DuckDB caps. Returns the worker DuckDB path."""
25
  env = os.environ if env is None else env
26
  threads = env.get("AIOS_WORKER_DUCKDB_THREADS")
27
+ if env.get("JOB_ID"):
28
+ # A job never takes the web's box sizing. AIOS_SYNC_JOB_DUCKDB_THREADS is the operator override.
29
+ threads = env.get("AIOS_SYNC_JOB_DUCKDB_THREADS")
30
  if not threads and env.get("JOB_ID"):
31
  # An HF job owns its box: DuckDB takes the job's CPUs (at most 4), so a boot restore is not one thread.
32
  cpus = 0
api/usage_activity.py CHANGED
@@ -149,6 +149,16 @@ def _history_tables(rt, now):
149
 
150
  def _cells(session, start, stop, now, source):
151
  source["retention"] = {"days": history_rows.RETENTION_DAYS}
 
 
 
 
 
 
 
 
 
 
152
  for table in _history_tables(session.runtime, now):
153
  if table == activity_log.DATABASE_KEY:
154
  continue
 
149
 
150
  def _cells(session, start, stop, now, source):
151
  source["retention"] = {"days": history_rows.RETENTION_DAYS}
152
+ # One statement for every table: a per-table read cost about 120 round trips on staging.
153
+ batched = user_tables.value_change_counts(st=session.runtime, since=start, until=stop,
154
+ retention_days=history_rows.RETENTION_DAYS, extended_entitled=True, kinds=("edit", "restore"))
155
+ if batched is not None:
156
+ # Schema-bound history rows carry no tenant stamp: the bound schema is the tenant boundary.
157
+ for table, actor, at, changed in batched:
158
+ if table != activity_log.DATABASE_KEY:
159
+ for _ in range(changed):
160
+ yield at, actor or "", "cell_edit"
161
+ return
162
  for table in _history_tables(session.runtime, now):
163
  if table == activity_log.DATABASE_KEY:
164
  continue
platform/core/history_rows.py CHANGED
@@ -224,6 +224,46 @@ class PgHistoryStore:
224
  raise
225
  return {}
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  def _prune(self, con, now):
228
  cutoff = _time(now) - timedelta(days=RETENTION_DAYS)
229
  dropped = []
 
224
  raise
225
  return {}
226
 
227
+ def change_counts(self, *, since, until=None, kinds=None, tables=None):
228
+ """Return (table, actor, at, changed cell count) for every table's events in one statement.
229
+
230
+ Values stay sealed: only the changes keys are counted, so no row is decrypted.
231
+ """
232
+ clauses, args = ['at >= %s'], [_time(since)]
233
+ if until is not None:
234
+ # The upper bound lets month partitions prune on both sides.
235
+ clauses.append('at < %s')
236
+ args.append(_time(until))
237
+ for column, values in (('kind', kinds), ('"table"', tables)):
238
+ if values is not None:
239
+ values = list(dict.fromkeys(str(value) for value in values))
240
+ if not values:
241
+ return []
242
+ clauses.append(f'{column} = ANY(%s)')
243
+ args.append(values)
244
+ try:
245
+ with self._connection() as con:
246
+ # A non-object payload is returned whole and counted the way _event parses it.
247
+ cursor = con.execute(
248
+ 'SELECT "table", actor, at, CASE WHEN jsonb_typeof(changes) = \'object\' '
249
+ 'THEN (SELECT count(*) FROM jsonb_object_keys(changes)) END, '
250
+ 'CASE WHEN jsonb_typeof(changes) = \'object\' THEN NULL ELSE changes END '
251
+ f'FROM {self.table} WHERE ' + ' AND '.join(clauses), args)
252
+ counts = []
253
+ while rows := cursor.fetchmany(1024):
254
+ for table_key, actor, at, changed, raw in rows:
255
+ if changed is None:
256
+ raw = (json.loads(raw) if isinstance(raw, str) else raw) or {}
257
+ if not isinstance(raw, dict):
258
+ raise ValueError('History changes must be a JSON object.')
259
+ changed = len(raw)
260
+ counts.append((table_key, actor, _time(at), int(changed)))
261
+ return counts
262
+ except Exception as exc:
263
+ if getattr(exc, 'sqlstate', None) != '42P01':
264
+ raise
265
+ return []
266
+
267
  def _prune(self, con, now):
268
  cutoff = _time(now) - timedelta(days=RETENTION_DAYS)
269
  dropped = []
platform/core/store_pg.py CHANGED
@@ -1934,6 +1934,75 @@ def _iter_view_members(name, path, revision, *, batch_size=2048, tenant_slug=Non
1934
  rows.clear()
1935
 
1936
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1937
  @_request_timing.store_operation("json_object")
1938
  def _get_json_object(name, path=(), *, drop=(), omit=(), tenant_slug=None):
1939
  """Read nested object definitions while removing their population operands in SQL."""
@@ -3121,6 +3190,10 @@ class PgStore:
3121
  def iter_view_members(self, name, path, revision, *, batch_size=2048):
3122
  return _iter_view_members(name, path, revision, batch_size=batch_size, tenant_slug=self.tenant_slug)
3123
 
 
 
 
 
3124
  def get_json_members(self, name, path, ids, *, id_key=None, source_limit=None):
3125
  return _get_json_members(name, path, ids, id_key=id_key, source_limit=source_limit, tenant_slug=self.tenant_slug)
3126
 
 
1934
  rows.clear()
1935
 
1936
 
1937
+ def _iter_view_members_many(name, pairs, revision, *, batch_size=2048, head=None, tenant_slug=None):
1938
+ """Stream the memberships of many (owner, view id) pairs of one document in one statement.
1939
+
1940
+ Yields batches of `(pair index, value)` ordered by pair, then entry. `head` keeps the first N entries per view.
1941
+ """
1942
+ import uuid
1943
+ size = int(batch_size)
1944
+ if not 1 <= size <= 4096:
1945
+ raise ValueError('A view membership batch accepts between 1 and 4096 values')
1946
+ pairs = [(str(owner), str(vid)) for owner, vid in pairs]
1947
+ if not pairs:
1948
+ return
1949
+ owners, vids = [owner for owner, _ in pairs], [vid for _, vid in pairs]
1950
+ limit = None if head is None else int(head)
1951
+ view_rows = _view_rows(name, tenant_slug)
1952
+ if view_rows is not None:
1953
+ from core import view_settings_rows as settings
1954
+ with view_rows._reading() as con:
1955
+ row = con.execute(_q('SELECT rev, updated_at FROM {tbl} WHERE key=%s',
1956
+ 'store_kv', tenant_slug), (str(name),)).fetchone()
1957
+ if tuple(row or ()) != tuple(revision or ()):
1958
+ raise RuntimeError('canonical_source_unavailable: the saved view changed during this read')
1959
+ with con.cursor(name='view_members_' + uuid.uuid4().hex) as cur:
1960
+ cur.execute(settings._sql("SELECT p.i, e.v FROM unnest(%s::text[], %s::text[]) "
1961
+ "WITH ORDINALITY p(owner, vid, i) JOIN {rows} r ON r.workspace_key=%s "
1962
+ "AND r.kind='settings' AND r.row_key=p.owner CROSS JOIN LATERAL "
1963
+ "jsonb_array_elements(CASE WHEN jsonb_typeof(r.value #> "
1964
+ "ARRAY['views', p.vid, 'config', 'memberPids'])='array' THEN r.value #> "
1965
+ "ARRAY['views', p.vid, 'config', 'memberPids'] ELSE '[]'::jsonb END) "
1966
+ "WITH ORDINALITY e(v,n) WHERE %s::bigint IS NULL OR e.n <= %s::bigint "
1967
+ "ORDER BY p.i, e.n", view_rows.tenant_slug), (owners, vids, str(name), limit, limit))
1968
+ while True:
1969
+ batch = cur.fetchmany(size)
1970
+ if not batch:
1971
+ return
1972
+ yield [(int(index) - 1, value) for index, value in batch]
1973
+ return
1974
+ with _pool().connection() as con:
1975
+ with con.cursor(name='view_members_' + uuid.uuid4().hex) as cur:
1976
+ started = time.perf_counter()
1977
+ cur.execute(_q("SELECT kv.rev, kv.updated_at, p.i, e.n, e.v::text FROM {tbl} kv "
1978
+ "CROSS JOIN unnest(%s::text[], %s::text[]) WITH ORDINALITY p(owner, vid, i) "
1979
+ "LEFT JOIN LATERAL jsonb_array_elements(CASE WHEN jsonb_typeof(kv.value #> "
1980
+ "ARRAY[p.owner, 'views', p.vid, 'config', 'memberPids'])='array' THEN kv.value #> "
1981
+ "ARRAY[p.owner, 'views', p.vid, 'config', 'memberPids'] ELSE '[]'::jsonb END) "
1982
+ "WITH ORDINALITY e(v,n) ON %s::bigint IS NULL OR e.n <= %s::bigint "
1983
+ 'WHERE kv.key=%s ORDER BY p.i, e.n', 'store_kv', tenant_slug),
1984
+ (owners, vids, limit, limit, str(name)))
1985
+ _request_timing.record_store('view_members', str(name), milliseconds=(time.perf_counter() - started) * 1000)
1986
+ found = False
1987
+ while True:
1988
+ _request_timing.remaining_budget()
1989
+ started = time.perf_counter()
1990
+ rows = cur.fetchmany(size)
1991
+ elapsed_ms = (time.perf_counter() - started) * 1000
1992
+ if not rows:
1993
+ if not found:
1994
+ raise RuntimeError('canonical_source_unavailable: the saved-view document disappeared')
1995
+ return
1996
+ found = True
1997
+ if any(tuple(row[:2]) != tuple(revision or ()) for row in rows):
1998
+ raise RuntimeError('canonical_source_unavailable: the saved view changed during this read')
1999
+ _request_timing.record_store('view_members', str(name),
2000
+ bytes_read=sum(len(value.encode('utf-8')) for *_, value in rows if value is not None),
2001
+ milliseconds=elapsed_ms)
2002
+ yield [(int(pair) - 1, json.loads(value)) for _, _, pair, index, value in rows if index is not None]
2003
+ rows.clear()
2004
+
2005
+
2006
  @_request_timing.store_operation("json_object")
2007
  def _get_json_object(name, path=(), *, drop=(), omit=(), tenant_slug=None):
2008
  """Read nested object definitions while removing their population operands in SQL."""
 
3190
  def iter_view_members(self, name, path, revision, *, batch_size=2048):
3191
  return _iter_view_members(name, path, revision, batch_size=batch_size, tenant_slug=self.tenant_slug)
3192
 
3193
+ def iter_view_members_many(self, name, pairs, revision, *, batch_size=2048, head=None):
3194
+ return _iter_view_members_many(name, pairs, revision, batch_size=batch_size, head=head,
3195
+ tenant_slug=self.tenant_slug)
3196
+
3197
  def get_json_members(self, name, path, ids, *, id_key=None, source_limit=None):
3198
  return _get_json_members(name, path, ids, id_key=id_key, source_limit=source_limit, tenant_slug=self.tenant_slug)
3199
 
platform/core/user_tables.py CHANGED
@@ -6481,6 +6481,27 @@ def value_history(table_key, row_id=None, *, st=None,
6481
  return {'events': out, 'metering': {'events': len(out), 'bytes': len(encoded)}}
6482
 
6483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6484
  def project_value_history(history, allowed_row_ids, hidden_keys=()):
6485
  """Apply the normal row/field visibility closure to already-retained events."""
6486
  allowed = {str(row_id) for row_id in (allowed_row_ids or ())}
 
6481
  return {'events': out, 'metering': {'events': len(out), 'bytes': len(encoded)}}
6482
 
6483
 
6484
+ def value_change_counts(*, st=None, since=None, until=None, retention_days=HISTORY_RETENTION_DAYS,
6485
+ extended_entitled=False, kinds=None, tables=None):
6486
+ """Count changed cells per retained history event across every table in one read.
6487
+
6488
+ The window is value_history's, narrowed to [since, until). Returns None when the backend
6489
+ has no batched reader, so the caller keeps its per-table path.
6490
+ """
6491
+ days = int(retention_days or HISTORY_RETENTION_DAYS)
6492
+ if days == HISTORY_EXTENDED_DAYS and not extended_entitled:
6493
+ raise PermissionError('90-day history requires a Loopable-admin entitlement')
6494
+ if days not in (HISTORY_RETENTION_DAYS, HISTORY_EXTENDED_DAYS):
6495
+ raise ValueError('history retention must be 30 or 90 days')
6496
+ cutoff = _utc_now() - _dt.timedelta(days=days)
6497
+ from core import history_rows
6498
+ reader = getattr(history_rows.backend(_st(st)), 'change_counts', None)
6499
+ if not callable(reader):
6500
+ return None
6501
+ return reader(since=cutoff if since is None else max(cutoff, since), until=until,
6502
+ kinds=kinds, tables=tables)
6503
+
6504
+
6505
  def project_value_history(history, allowed_row_ids, hidden_keys=()):
6506
  """Apply the normal row/field visibility closure to already-retained events."""
6507
  allowed = {str(row_id) for row_id in (allowed_row_ids or ())}
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- 34475228cf76439815132236fbf14dea70c7a4621ff7bde47c8a58d4c55517bd
 
1
+ b708e0c713d9440532f58f1153efa962a05abccb1b9d42f5c62e15111fd5f798
web/src/apps/amazon-profit/ProjectsList.tsx CHANGED
@@ -19,7 +19,7 @@ import { useEscapeClose } from "../../ui/useEscapeClose";
19
  import { ProjectResultPreview } from "./ProjectResults";
20
  import {
21
  GRAIN_LABEL, PROJECT_PRIORITIES, PROJECT_STATUSES, HealthChip, LoadingRows, Owner,
22
- PriorityGlyph, ProjectRequestError, StatusRing, call, formatDay, messageOf, text,
23
  type Project, type ProjectGrain, type ProjectList, type ProjectListFigure, type ProjectListFigures,
24
  } from "./ProjectsShared";
25
  import "../../styles/sdlc85/project-list.css";
@@ -35,14 +35,24 @@ function dashboardValue(figure: ProjectListFigure | undefined, format: (value: n
35
  return figure?.total == null ? figure?.reason ?? "Unknown" : format(figure.total);
36
  }
37
 
 
 
 
 
 
 
 
 
 
38
  function dashboardTitle(figures: ProjectListFigures | null | undefined, figure?: ProjectListFigure) {
39
  const span = figures?.from && figures.to ? `${formatDay(figures.from)} to ${formatDay(figures.to)}` : "";
40
  return [span, figure?.total != null ? figure.reason : null].filter(Boolean).join(". ") || undefined;
41
  }
42
 
43
- function ProjectFigures({ project, compact = false }: { project: Project; compact?: boolean }) {
44
- const { rollup, impact, figures } = project;
45
- if (!rollup && !impact && !figures) return null;
 
46
  const sales = figures?.sales;
47
  const sold = figures?.units;
48
  // One shared withhold reason prints once in place of both numbers.
@@ -50,10 +60,10 @@ function ProjectFigures({ project, compact = false }: { project: Project; compac
50
  ? sales.reason : null;
51
  if (compact) return <div className="lp-proj-row-figures">
52
  <div>
53
- {withheld ? <span title={dashboardTitle(figures)}>{withheld}</span>
 
54
  : <span title={dashboardTitle(figures, sales)}>Sales {dashboardValue(sales, value => money(value))}</span>}
55
- <span title={rollup?.asOf}>Profit {money(rollup?.netProfit)}</span>
56
- {withheld ? null : <span title={dashboardTitle(figures, sold)}>Units {dashboardValue(sold, value => units(value))}</span>}
57
  </div>
58
  {project.status === "Completed" && project.completedOn ? <div title="Compared with the same days one year earlier">
59
  <span>Impact since {formatDay(project.completedOn)}</span>
@@ -65,7 +75,6 @@ function ProjectFigures({ project, compact = false }: { project: Project; compac
65
  <h4 className="lp-proj-section-title" title={dashboardTitle(figures)}>Totals</h4>
66
  <dl className="lp-proj-facts">
67
  <div><dt>Net sales</dt><dd title={dashboardTitle(figures, sales)}>{dashboardValue(sales, value => money(value))}</dd></div>
68
- <div><dt>Net profit</dt><dd title={rollup?.asOf}>{money(rollup?.netProfit)}</dd></div>
69
  <div><dt>Units</dt><dd title={dashboardTitle(figures, sold)}>{dashboardValue(sold, value => units(value))}</dd></div>
70
  </dl>
71
  <h4 className="lp-proj-section-title">Impact{project.completedOn ? ` since ${formatDay(project.completedOn)}` : ""}</h4>
@@ -142,10 +151,12 @@ function ProjectGroupLabel({ by, value }: { by: ProjectGroupKey; value: string |
142
  }
143
 
144
  export function ProjectTable({ projects, grain, onOpen, empty, visible = DEFAULT_PROJECT_FIELDS,
145
- groups, groupBy, collapsed, onToggleGroup }: {
146
  projects: Project[]; grain?: ProjectGrain; onOpen: (id: string) => void; empty: ReactNode; visible?: string[];
147
  groups?: ProjectGroup[] | null; groupBy?: ProjectGroupKey | null; collapsed?: ReadonlySet<string>;
148
  onToggleGroup?: (key: string) => void;
 
 
149
  }) {
150
  if (!projects.length) return <div className="lp-proj-empty">{empty}</div>;
151
  const columns = [...new Set(visible)].flatMap(key => {
@@ -162,7 +173,8 @@ export function ProjectTable({ projects, grain, onOpen, empty, visible = DEFAULT
162
  <span className="lp-proj-clip">{project.name}</span>
163
  </button>
164
  {via ? <span className="lp-proj-via lp-proj-clip">From {GRAIN_LABEL[via.grain]} {via.key}</span> : null}
165
- </div><ProjectFigures project={project} compact /></td>
 
166
  {columns.map(([key]) => {
167
  let cell: ReactNode;
168
  switch (key) {
@@ -578,6 +590,21 @@ export function ProjectBrowser({ read, grain, onOpen }: {
578
  });
579
  return () => controller.abort();
580
  }, []);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
581
  const closeSearch = () => {
582
  restoreSearchFocus.current = true;
583
  setSearch("");
@@ -782,7 +809,7 @@ export function ProjectBrowser({ read, grain, onOpen }: {
782
  </div>
783
  </div>
784
  <div className="lp-proj-list-layout" data-summary={summary}>
785
- <ProjectTable projects={rows} grain={grain} onOpen={onOpen} visible={visible}
786
  groups={groups} groupBy={groupBy} collapsed={collapsed} onToggleGroup={toggleGroup}
787
  empty={<span>{read.projects.length ? "No projects match" : "No projects yet"}{narrowed ? <button type="button" className="lp-proj-tool" onClick={clearFilters}>Reset filters</button> : null}</span>} />
788
  {summary ? <aside className="lp-proj-summary" aria-label="Project summary">
 
19
  import { ProjectResultPreview } from "./ProjectResults";
20
  import {
21
  GRAIN_LABEL, PROJECT_PRIORITIES, PROJECT_STATUSES, HealthChip, LoadingRows, Owner,
22
+ PriorityGlyph, ProjectRequestError, StatusRing, call, enc, formatDay, messageOf, text,
23
  type Project, type ProjectGrain, type ProjectList, type ProjectListFigure, type ProjectListFigures,
24
  } from "./ProjectsShared";
25
  import "../../styles/sdlc85/project-list.css";
 
35
  return figure?.total == null ? figure?.reason ?? "Unknown" : format(figure.total);
36
  }
37
 
38
+ type ProjectFigureMap = Record<string, ProjectListFigures>;
39
+ const FIGURES_UNREAD = "Sales and units did not load.";
40
+
41
+ // A failed read withholds every row with its reason. The next list load reads again.
42
+ function withheldFigures(projects: Project[], reason: string): ProjectFigureMap {
43
+ const figure = { total: null, reason };
44
+ return Object.fromEntries(projects.map(project => [project.id, { from: null, to: null, sales: figure, units: figure }]));
45
+ }
46
+
47
  function dashboardTitle(figures: ProjectListFigures | null | undefined, figure?: ProjectListFigure) {
48
  const span = figures?.from && figures.to ? `${formatDay(figures.from)} to ${formatDay(figures.to)}` : "";
49
  return [span, figure?.total != null ? figure.reason : null].filter(Boolean).join(". ") || undefined;
50
  }
51
 
52
+ // The stored rollup's net profit is never shown beside window figures. The Dashboard has no profit figure.
53
+ function ProjectFigures({ project, compact = false, loading = false }: { project: Project; compact?: boolean; loading?: boolean }) {
54
+ const { impact, figures } = project;
55
+ if (!loading && !impact && !figures) return null;
56
  const sales = figures?.sales;
57
  const sold = figures?.units;
58
  // One shared withhold reason prints once in place of both numbers.
 
60
  ? sales.reason : null;
61
  if (compact) return <div className="lp-proj-row-figures">
62
  <div>
63
+ {loading ? <span className="lp-proj-muted" aria-busy="true">Loading sales and units</span>
64
+ : withheld ? <span title={dashboardTitle(figures)}>{withheld}</span>
65
  : <span title={dashboardTitle(figures, sales)}>Sales {dashboardValue(sales, value => money(value))}</span>}
66
+ {loading || withheld ? null : <span title={dashboardTitle(figures, sold)}>Units {dashboardValue(sold, value => units(value))}</span>}
 
67
  </div>
68
  {project.status === "Completed" && project.completedOn ? <div title="Compared with the same days one year earlier">
69
  <span>Impact since {formatDay(project.completedOn)}</span>
 
75
  <h4 className="lp-proj-section-title" title={dashboardTitle(figures)}>Totals</h4>
76
  <dl className="lp-proj-facts">
77
  <div><dt>Net sales</dt><dd title={dashboardTitle(figures, sales)}>{dashboardValue(sales, value => money(value))}</dd></div>
 
78
  <div><dt>Units</dt><dd title={dashboardTitle(figures, sold)}>{dashboardValue(sold, value => units(value))}</dd></div>
79
  </dl>
80
  <h4 className="lp-proj-section-title">Impact{project.completedOn ? ` since ${formatDay(project.completedOn)}` : ""}</h4>
 
151
  }
152
 
153
  export function ProjectTable({ projects, grain, onOpen, empty, visible = DEFAULT_PROJECT_FIELDS,
154
+ groups, groupBy, collapsed, onToggleGroup, figures }: {
155
  projects: Project[]; grain?: ProjectGrain; onOpen: (id: string) => void; empty: ReactNode; visible?: string[];
156
  groups?: ProjectGroup[] | null; groupBy?: ProjectGroupKey | null; collapsed?: ReadonlySet<string>;
157
  onToggleGroup?: (key: string) => void;
158
+ /** Sales and Units by project id from GET /figures. Null while that read runs. */
159
+ figures?: ProjectFigureMap | null;
160
  }) {
161
  if (!projects.length) return <div className="lp-proj-empty">{empty}</div>;
162
  const columns = [...new Set(visible)].flatMap(key => {
 
173
  <span className="lp-proj-clip">{project.name}</span>
174
  </button>
175
  {via ? <span className="lp-proj-via lp-proj-clip">From {GRAIN_LABEL[via.grain]} {via.key}</span> : null}
176
+ </div><ProjectFigures project={figures ? { ...project, figures: figures[project.id] ?? null } : project}
177
+ loading={figures === null} compact /></td>
178
  {columns.map(([key]) => {
179
  let cell: ReactNode;
180
  switch (key) {
 
590
  });
591
  return () => controller.abort();
592
  }, []);
593
+ // Rows paint from the list at once. Sales and Units fill in from one GET /figures per list read.
594
+ const [figureRead, setFigureRead] = useState<{ of: ProjectList; byId: ProjectFigureMap } | null>(null);
595
+ useEffect(() => {
596
+ const controller = new AbortController();
597
+ const query = read.grain && read.key ? `?grain=${enc(read.grain)}&key=${enc(read.key)}` : "";
598
+ call<{ figures?: ProjectFigureMap }>(`/figures${query}`, { signal: controller.signal }, FIGURES_UNREAD)
599
+ .then(next => { if (!controller.signal.aborted) setFigureRead({ of: read, byId: next.figures ?? {} }); },
600
+ reason => {
601
+ if (!controller.signal.aborted) {
602
+ setFigureRead({ of: read, byId: withheldFigures(read.projects, messageOf(reason, FIGURES_UNREAD)) });
603
+ }
604
+ });
605
+ return () => controller.abort();
606
+ }, [read]);
607
+ const figures = figureRead?.of === read ? figureRead.byId : null;
608
  const closeSearch = () => {
609
  restoreSearchFocus.current = true;
610
  setSearch("");
 
809
  </div>
810
  </div>
811
  <div className="lp-proj-list-layout" data-summary={summary}>
812
+ <ProjectTable projects={rows} grain={grain} onOpen={onOpen} visible={visible} figures={figures}
813
  groups={groups} groupBy={groupBy} collapsed={collapsed} onToggleGroup={toggleGroup}
814
  empty={<span>{read.projects.length ? "No projects match" : "No projects yet"}{narrowed ? <button type="button" className="lp-proj-tool" onClick={clearFilters}>Reset filters</button> : null}</span>} />
815
  {summary ? <aside className="lp-proj-summary" aria-label="Project summary">
web/src/apps/amazon-profit/ProjectsShared.tsx CHANGED
@@ -55,7 +55,7 @@ export interface Project {
55
  rollup?: { period: string; netSales: number | null; netProfit: number | null; units: number | null; asOf: string } | null;
56
  impact?: { netSales: number | null; units: number | null; priorNetSales: number | null;
57
  priorUnits: number | null; state: string | null } | null;
58
- /** The project Dashboard's default Sales and Units. A null total carries its withhold reason. */
59
  figures?: ProjectListFigures | null;
60
  }
61
 
 
55
  rollup?: { period: string; netSales: number | null; netProfit: number | null; units: number | null; asOf: string } | null;
56
  impact?: { netSales: number | null; units: number | null; priorNetSales: number | null;
57
  priorUnits: number | null; state: string | null } | null;
58
+ /** The project Dashboard's default Sales and Units, read from GET /figures. A null total carries its withhold reason. */
59
  figures?: ProjectListFigures | null;
60
  }
61