fsanyoto commited on
Commit
4e84128
·
verified ·
1 Parent(s): d598032

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,4 +1,4 @@
1
  {
2
- "current": "fb1493505",
3
  "releases": []
4
  }
 
1
  {
2
+ "current": "ed6e607f3",
3
  "releases": []
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- fb1493505
 
1
+ ed6e607f3
api/routes_starred.py CHANGED
@@ -402,6 +402,13 @@ def _scan_starred_views(session, keys=None, cache=None, include=None, with_right
402
  if with_rights:
403
  row.update(_home_view_row(
404
  session, (key, ws_key, owners[vid], {**view, "id": vid}, None)))
 
 
 
 
 
 
 
405
  out.append(row)
406
  return out, unread
407
 
@@ -522,9 +529,10 @@ def _payload(session, keys=None, cache=None):
522
 
523
  @router.get("/nav/views")
524
  def nav_views(session: Session = Depends(require_session)):
525
- """Project readable saved View names and their current item rights."""
526
  rows, unread = _scan_starred_views(session, include=lambda view: True, with_rights=True)
527
- return {"views": [{key: row[key] for key in ("database", "id", "name", "capabilities")}
 
528
  for row in rows], "unread": unread}
529
 
530
 
 
402
  if with_rights:
403
  row.update(_home_view_row(
404
  session, (key, ws_key, owners[vid], {**view, "id": vid}, None)))
405
+ # The owner's `itemFolders` is keyed by surface first. An empty id must never match.
406
+ owner_ws = doc.get(owners[vid])
407
+ placed = (((owner_ws.get("itemFolders") or {}).get("views") or {})
408
+ if isinstance(owner_ws, dict) else {})
409
+ fid = str(placed.get(vid) or "") if isinstance(placed, dict) else ""
410
+ if fid:
411
+ row["folderId"] = fid
412
  out.append(row)
413
  return out, unread
414
 
 
529
 
530
  @router.get("/nav/views")
531
  def nav_views(session: Session = Depends(require_session)):
532
+ """Project readable saved View names, current item rights and the owner's folder id."""
533
  rows, unread = _scan_starred_views(session, include=lambda view: True, with_rights=True)
534
+ return {"views": [{key: row[key] for key in ("database", "id", "name", "capabilities", "folderId")
535
+ if key in row}
536
  for row in rows], "unread": unread}
537
 
538
 
platform/harness/amazon_store.py CHANGED
@@ -3186,11 +3186,12 @@ def _app_date_sql(col):
3186
 
3187
 
3188
  def _unquote_mirror_values(con, log=None):
3189
- """D-2682 heal: a mirror table saved with quoted values gets them stripped, in SQL.
3190
 
3191
- -> {mirror: rows}. A base table qualifies when every sampled identity value is wrapped in
3192
- `"`. Every VARCHAR value wrapped in `"` loses one pair, `""` becomes `"`, and the date-named
3193
- columns of an App table become ISO. Idempotent: a healed table no longer qualifies.
 
3194
  """
3195
  healed = {}
3196
  for mirror, kind in _amazon_objects(con):
@@ -3203,14 +3204,9 @@ def _unquote_mirror_values(con, log=None):
3203
  identity = [c for c in _QUOTE_IDENTITY if c in text]
3204
  if not identity:
3205
  continue
3206
- sampled = quoted = 0
3207
- for col in identity:
3208
- q = _quoted_identifier(col)
3209
- n, w = con.execute(
3210
- f"SELECT count(*), count(*) FILTER (WHERE v LIKE '\"%\"') FROM (SELECT {q} AS v "
3211
- f"FROM {_quoted_identifier(mirror)} WHERE {q} IS NOT NULL AND {q} <> '' LIMIT 200)").fetchone()
3212
- sampled, quoted = sampled + n, quoted + w
3213
- if not sampled or quoted != sampled:
3214
  continue
3215
  sets = []
3216
  dated = mirror[len(PREFIX):] in APP_DATE_TABLES
@@ -3223,12 +3219,13 @@ def _unquote_mirror_values(con, log=None):
3223
  sets.append(f"{q} = {plain}")
3224
  con.execute("BEGIN TRANSACTION")
3225
  try:
3226
- con.execute(f"UPDATE {_quoted_identifier(mirror)} SET {', '.join(sets)}")
 
3227
  con.execute("COMMIT")
3228
  except Exception:
 
3229
  con.execute("ROLLBACK")
3230
  raise
3231
- healed[mirror] = con.execute(f"SELECT count(*) FROM {_quoted_identifier(mirror)}").fetchone()[0]
3232
  if log:
3233
  log(f"Amazon quoted values {mirror}: {healed[mirror]:,} rows unquoted")
3234
  return healed
@@ -5205,6 +5202,12 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri
5205
  except Exception as exc: # noqa: BLE001
5206
  report["problems"].append(f"Amazon snapshot collapse failed ({type(exc).__name__}); the next "
5207
  "resync of each snapshot table replaces its earlier copies.")
 
 
 
 
 
 
5208
 
5209
  def write_window(table, cols, types, rows):
5210
  if cx.REPORT_TYPES[table].get("weekly"):
 
3186
 
3187
 
3188
  def _unquote_mirror_values(con, log=None):
3189
+ """D-2682 heal: mirror rows saved with quoted values get them stripped, in SQL.
3190
 
3191
+ -> {mirror: rows healed}. A base table row qualifies when an identity value is wrapped in
3192
+ `"`. Its VARCHAR values wrapped in `"` lose one pair, `""` becomes `"`, and the date-named
3193
+ columns of an App table become ISO. Rows a fixed parse wrote are left alone, so a table
3194
+ holding both kinds still heals. Idempotent: a healed row no longer qualifies.
3195
  """
3196
  healed = {}
3197
  for mirror, kind in _amazon_objects(con):
 
3204
  identity = [c for c in _QUOTE_IDENTITY if c in text]
3205
  if not identity:
3206
  continue
3207
+ table = _quoted_identifier(mirror)
3208
+ stale = " OR ".join(f"{_quoted_identifier(c)} LIKE '\"%\"'" for c in identity)
3209
+ if not con.execute(f"SELECT 1 FROM {table} WHERE {stale} LIMIT 1").fetchone():
 
 
 
 
 
3210
  continue
3211
  sets = []
3212
  dated = mirror[len(PREFIX):] in APP_DATE_TABLES
 
3219
  sets.append(f"{q} = {plain}")
3220
  con.execute("BEGIN TRANSACTION")
3221
  try:
3222
+ healed[mirror] = con.execute(f"SELECT count(*) FROM {table} WHERE {stale}").fetchone()[0]
3223
+ con.execute(f"UPDATE {table} SET {', '.join(sets)} WHERE {stale}")
3224
  con.execute("COMMIT")
3225
  except Exception:
3226
+ healed.pop(mirror, None)
3227
  con.execute("ROLLBACK")
3228
  raise
 
3229
  if log:
3230
  log(f"Amazon quoted values {mirror}: {healed[mirror]:,} rows unquoted")
3231
  return healed
 
5202
  except Exception as exc: # noqa: BLE001
5203
  report["problems"].append(f"Amazon snapshot collapse failed ({type(exc).__name__}); the next "
5204
  "resync of each snapshot table replaces its earlier copies.")
5205
+ # D-2682: a restored PUBLISHED snapshot skips `_after_restore`, so quoted rows heal here.
5206
+ try:
5207
+ changed_tables.update(_unquote_mirror_values(con, log))
5208
+ except Exception as exc: # noqa: BLE001
5209
+ report["problems"].append(f"Amazon quoted value heal failed ({type(exc).__name__}); the next "
5210
+ "resync of each quoted table writes clean values.")
5211
 
5212
  def write_window(table, cols, types, rows):
5213
  if cx.REPORT_TYPES[table].get("weekly"):
platform/harness/semantic.py CHANGED
@@ -995,6 +995,9 @@ def _v_gl_opex(date_from, date_to, team_id):
995
  def _v_ar_outstanding(date_from, date_to, team_id):
996
  """Store-path AR outstanding == live Odoo aggregate (posted out_invoice/out_refund residuals).
997
  No date window — outstanding is an as-of-now balance."""
 
 
 
998
  res = store_query("receivables", ["ar_outstanding"])
999
  ours = res["rows"][0]["ar_outstanding"] if res["rows"] else 0
1000
  live = O.sum_field("account.move",
@@ -1003,6 +1006,27 @@ def _v_ar_outstanding(date_from, date_to, team_id):
1003
  return ours, live
1004
 
1005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1006
  def customer_rollup_oracle(key, client=None, *, partner_ids=None, grouped=False, today=None):
1007
  """Read each customer rollup independently from Odoo."""
1008
  client = client or O.get_odoo()
@@ -1120,6 +1144,7 @@ _VALIDATORS = {
1120
  "order_count": _v_order_count,
1121
  "gl_opex": _v_gl_opex,
1122
  "ar_outstanding": _v_ar_outstanding,
 
1123
  }
1124
  _VALIDATORS.update({key: _v_customer_rollup(key) for key in (
1125
  'customer_order_count', 'customer_last_order', 'customer_open_documents',
 
995
  def _v_ar_outstanding(date_from, date_to, team_id):
996
  """Store-path AR outstanding == live Odoo aggregate (posted out_invoice/out_refund residuals).
997
  No date window — outstanding is an as-of-now balance."""
998
+ # Invoice grain on purpose: the receivables topic is one row per document and the invoice
999
+ # grid drills by `move`. It leaves out unapplied payments, so it is not Overdue and not the
1000
+ # customer field `ar_outstanding` (line grain). Overdue is `_v_ar_overdue`.
1001
  res = store_query("receivables", ["ar_outstanding"])
1002
  ours = res["rows"][0]["ar_outstanding"] if res["rows"] else 0
1003
  live = O.sum_field("account.move",
 
1006
  return ours, live
1007
 
1008
 
1009
+ def _v_ar_overdue(date_from, date_to, team_id):
1010
+ """Store-path Overdue == Odoo receivable lines past due (`ar.overdue_tie`), on one date.
1011
+
1012
+ No date window and no team: Overdue is an as-of-today company balance. The tie runs on the
1013
+ store's own CURRENT_DATE, the date the metric's filter reads. The customer field leg is
1014
+ `ar.overdue_checks`, which `ar.validate()` records.
1015
+ """
1016
+ from modules import ar
1017
+ con = _store_con()
1018
+ try:
1019
+ as_of = con.execute("SELECT CURRENT_DATE").fetchone()[0]
1020
+ finally:
1021
+ con.close()
1022
+ tie = ar.overdue_tie(ar.aged_receivable(as_of))
1023
+ # Only a line-grain binding can tie: a document sum leaves out unapplied payments, so the
1024
+ # gap stays red until the metric reads account_move_line (Rule 8).
1025
+ res = store_query(_model()["metrics"]["ar_overdue"]["topic"], ["ar_overdue"])
1026
+ ours = res["rows"][0]["ar_overdue"] if res["rows"] else 0
1027
+ return ours, tie["b"]
1028
+
1029
+
1030
  def customer_rollup_oracle(key, client=None, *, partner_ids=None, grouped=False, today=None):
1031
  """Read each customer rollup independently from Odoo."""
1032
  client = client or O.get_odoo()
 
1144
  "order_count": _v_order_count,
1145
  "gl_opex": _v_gl_opex,
1146
  "ar_outstanding": _v_ar_outstanding,
1147
+ "ar_overdue": _v_ar_overdue,
1148
  }
1149
  _VALIDATORS.update({key: _v_customer_rollup(key) for key in (
1150
  'customer_order_count', 'customer_last_order', 'customer_open_documents',
platform/modules/ranking_diagnostic.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  Sources (read only, through the tenant Amazon mirror, never written, no SP-API call):
4
  amzn_pricing Sales and Traffic per child ASIN: sessions, units, Buy Box %. No date (F5).
 
5
  amzn_catalog_items sales_ranks JSON, newest _snapshot_date (F6)
6
  amzn_brand_analytics search-term click share, newest _period_start (S78-T15 weeks)
7
  amzn_economics ads charge columns (ads_<type>_amount), the 7 newest days, per SKU to ASIN
@@ -212,18 +213,46 @@ def _asin_expr(cols):
212
  return f"COALESCE({', '.join(parts)})" if parts else None
213
 
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  def _pricing(cur):
216
- """{asin: {sessions, units, sales, buyBox, skus}} over every Sales and Traffic row. No cap (rule 1)."""
 
 
 
 
217
  cols = _columns(cur, PRICING)
218
  asin = _asin_expr(cols)
219
  if asin is None:
220
  return None
221
  rows = cur.execute(
222
- f"SELECT {asin}, {_col(cols, 'sku')}, {_col(cols, SESSIONS, 'DOUBLE')}, {_col(cols, UNITS, 'DOUBLE')}, "
223
- f"{_col(cols, SALES, 'DOUBLE')}, {_col(cols, BUY_BOX, 'DOUBLE')} FROM {PRICING}").fetchall()
 
 
 
 
 
 
 
 
224
  out = {}
225
- for a, sku, sessions, units, sales, box in rows:
226
- if not a:
227
  continue
228
  p = out.setdefault(a, {"sessions": 0.0, "units": 0.0, "sales": 0.0, "skus": [], "_box": []})
229
  p["sessions"] += sessions or 0.0
@@ -243,16 +272,27 @@ def _pricing(cur):
243
 
244
 
245
  def source_totals(cur):
246
- """P10: one SQL sum of sessions and units over the Sales and Traffic table, apart from `_pricing`."""
 
 
 
247
  cols = _columns(cur, PRICING)
248
  asin = _asin_expr(cols)
249
  if asin is None or not {SESSIONS, UNITS} <= cols:
250
  return None
251
- sessions, units, count = cur.execute(
252
- f"SELECT COALESCE(SUM(TRY_CAST(\"{SESSIONS}\" AS DOUBLE)), 0), "
253
- f"COALESCE(SUM(TRY_CAST(\"{UNITS}\" AS DOUBLE)), 0), "
254
- f"COUNT(DISTINCT CASE WHEN TRY_CAST(\"{UNITS}\" AS DOUBLE) > 0 THEN {asin} END) FROM {PRICING}").fetchone()
255
- return {"sessions": round(float(sessions), 4), "units": round(float(units), 4), "asinsWithUnits": int(count)}
 
 
 
 
 
 
 
 
256
 
257
 
258
  def _rank(raw):
@@ -875,6 +915,10 @@ def validate(tenant_key, *, week=None, start=None, end=None, st=None):
875
  bad = [r["week"] for r in rows if not r["ok"]]
876
  detail = (f"{len(rows)} weeks tie sessions and units to the Sales and Traffic totals." if ok else
877
  "These weeks do not tie to the Sales and Traffic totals: " + ", ".join(bad) + ".")
 
 
 
 
878
  return {"ok": ok, "app_total": {k: round(v, 4) for k, v in app.items()},
879
  "independent_total": {k: round(v, 4) for k, v in ind.items()}, "weeks": rows, "detail": detail}
880
 
 
2
 
3
  Sources (read only, through the tenant Amazon mirror, never written, no SP-API call):
4
  amzn_pricing Sales and Traffic per child ASIN: sessions, units, Buy Box %. No date (F5).
5
+ One document per ASIN: the newest landed, never a sum of documents.
6
  amzn_catalog_items sales_ranks JSON, newest _snapshot_date (F6)
7
  amzn_brand_analytics search-term click share, newest _period_start (S78-T15 weeks)
8
  amzn_economics ads charge columns (ads_<type>_amount), the 7 newest days, per SKU to ASIN
 
213
  return f"COALESCE({', '.join(parts)})" if parts else None
214
 
215
 
216
+ DOCUMENT = "_report_document_id"
217
+
218
+
219
+ def _landing(cur):
220
+ """SQL for each pricing row's landing order: DuckDB `rowid`, or 0 when the cursor cannot read it."""
221
+ try:
222
+ cur.execute(f"SELECT rowid FROM {PRICING} LIMIT 0").fetchall()
223
+ return "rowid"
224
+ except Exception: # noqa: BLE001
225
+ return "0"
226
+
227
+
228
+ def _document(cols):
229
+ return f'COALESCE(CAST("{DOCUMENT}" AS VARCHAR), \'\')' if DOCUMENT in cols else "''"
230
+
231
+
232
  def _pricing(cur):
233
+ """{asin: {sessions, units, sales, buyBox, skus}} from each ASIN's newest report document. No cap (rule 1).
234
+
235
+ Pricing rows carry no period, and an incremental sync keeps an older document's row when its
236
+ parent or SKU differs. So an ASIN in two documents keeps only the rows of the document that
237
+ landed last (highest rowid), never a sum of both."""
238
  cols = _columns(cur, PRICING)
239
  asin = _asin_expr(cols)
240
  if asin is None:
241
  return None
242
  rows = cur.execute(
243
+ f"SELECT {asin}, {_document(cols)}, {_landing(cur)}, {_col(cols, 'sku')}, {_col(cols, SESSIONS, 'DOUBLE')}, "
244
+ f"{_col(cols, UNITS, 'DOUBLE')}, {_col(cols, SALES, 'DOUBLE')}, {_col(cols, BUY_BOX, 'DOUBLE')} "
245
+ f"FROM {PRICING}").fetchall()
246
+ landed = {}
247
+ for _a, doc, at, *_rest in rows:
248
+ landed[doc] = max(landed.get(doc, at), at)
249
+ newest = {}
250
+ for a, doc, *_rest in rows:
251
+ if a and (landed[doc], doc) > newest.get(a, (-1, "")):
252
+ newest[a] = (landed[doc], doc)
253
  out = {}
254
+ for a, doc, _at, sku, sessions, units, sales, box in rows:
255
+ if not a or newest[a] != (landed[doc], doc):
256
  continue
257
  p = out.setdefault(a, {"sessions": 0.0, "units": 0.0, "sales": 0.0, "skus": [], "_box": []})
258
  p["sessions"] += sessions or 0.0
 
272
 
273
 
274
  def source_totals(cur):
275
+ """P10: sessions and units in SQL over each ASIN's newest document, apart from `_pricing`'s Python pick.
276
+
277
+ Both apply one rule (newest document per ASIN), so the tie proves the two picks and sums agree.
278
+ `supersededRows` and the raw sums show what the rule left out."""
279
  cols = _columns(cur, PRICING)
280
  asin = _asin_expr(cols)
281
  if asin is None or not {SESSIONS, UNITS} <= cols:
282
  return None
283
+ sessions, units, count, superseded, raw_sessions, raw_units = cur.execute(
284
+ f"WITH r AS (SELECT {asin} AS a, {_document(cols)} AS d, {_landing(cur)} AS i, "
285
+ f"TRY_CAST(\"{SESSIONS}\" AS DOUBLE) AS s, TRY_CAST(\"{UNITS}\" AS DOUBLE) AS u FROM {PRICING}), "
286
+ f"docs AS (SELECT d, MAX(i) AS landed FROM r GROUP BY d), "
287
+ f"best AS (SELECT a, d FROM (SELECT a, d, ROW_NUMBER() OVER (PARTITION BY a ORDER BY landed DESC, d DESC) "
288
+ f"AS n FROM (SELECT DISTINCT r.a, r.d, docs.landed FROM r JOIN docs USING (d) WHERE r.a IS NOT NULL)) "
289
+ f"WHERE n = 1), kept AS (SELECT r.* FROM r JOIN best USING (a, d)) "
290
+ f"SELECT COALESCE(SUM(s), 0), COALESCE(SUM(u), 0), COUNT(DISTINCT CASE WHEN u > 0 THEN a END), "
291
+ f"(SELECT COUNT(*) FROM r WHERE a IS NOT NULL) - COUNT(*), (SELECT COALESCE(SUM(s), 0) FROM r), "
292
+ f"(SELECT COALESCE(SUM(u), 0) FROM r) FROM kept").fetchone()
293
+ return {"sessions": round(float(sessions), 4), "units": round(float(units), 4), "asinsWithUnits": int(count),
294
+ "supersededRows": int(superseded), "rawSessions": round(float(raw_sessions), 4),
295
+ "rawUnits": round(float(raw_units), 4)}
296
 
297
 
298
  def _rank(raw):
 
915
  bad = [r["week"] for r in rows if not r["ok"]]
916
  detail = (f"{len(rows)} weeks tie sessions and units to the Sales and Traffic totals." if ok else
917
  "These weeks do not tie to the Sales and Traffic totals: " + ", ".join(bad) + ".")
918
+ superseded = int((live or {}).get("supersededRows") or 0)
919
+ if superseded:
920
+ detail += (f" {superseded} older Sales and Traffic rows are left out because a newer report covers "
921
+ "the same ASIN.")
922
  return {"ok": ok, "app_total": {k: round(v, 4) for k, v in app.items()},
923
  "independent_total": {k: round(v, 4) for k, v in ind.items()}, "weeks": rows, "detail": detail}
924
 
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- 85a9604dac43592a2c2e82e75b4e8526d8fc2b0763cd1415553dcb628464e8c5
 
1
+ b35a69b08faac6cbe97c8da0a68680946085ae1959a978723651ff4fa6f02e26
web/src/apps/catalog/CatalogApp.tsx CHANGED
@@ -27,6 +27,7 @@ export function CatalogApp({ definition, database, scope, onOpen, onData, editor
27
  if (data.loading) {
28
  return <div className="cg-app-launch-state" role="status" aria-label="Loading Catalog">
29
  <span className="cg-app-launch-line is-wide" />
 
30
  </div>;
31
  }
32
  if (data.error) {
 
27
  if (data.loading) {
28
  return <div className="cg-app-launch-state" role="status" aria-label="Loading Catalog">
29
  <span className="cg-app-launch-line is-wide" />
30
+ {data.progress > 0 && <span>Loading records, {data.progress.toLocaleString()} read</span>}
31
  </div>;
32
  }
33
  if (data.error) {
web/src/apps/catalog/useCatalogData.ts CHANGED
@@ -1,5 +1,5 @@
1
  import { useCallback, useEffect, useState } from "react";
2
- import { typedOpenApi, typedQuery } from "../../typed/client";
3
  import type { QueryRequest, QueryResponse, TypedOpenApi } from "../../typed/client";
4
  import { FILTER_OPS, isFilterGroup } from "../../customer-grid/types";
5
  import type { Field, FilterNode, FilterOp, Row, ViewConfig } from "../../customer-grid/types";
@@ -51,15 +51,51 @@ function projectRow(record: QueryResponse["records"][number], properties: Proper
51
  return row;
52
  }
53
 
54
- async function walkPages(database: string, body: QueryRequest, properties: Properties, signal: AbortSignal) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  const rows: Row[] = [];
56
  const seen = new Set<string>();
57
  let fields: Field[] | undefined;
58
  let cursor: string | null = null;
59
  let returned = 0;
 
60
  do {
61
  signal.throwIfAborted();
62
- const response = await typedQuery(database, { ...body, cursor }, signal);
 
 
63
  signal.throwIfAborted();
64
  if (response.database !== database || !Array.isArray(response.records) ||
65
  response.page?.returned !== response.records.length) {
@@ -75,6 +111,7 @@ async function walkPages(database: string, body: QueryRequest, properties: Prope
75
  }
76
  rows.push(...response.records.map((record) => projectRow(record, properties)));
77
  returned += response.page.returned;
 
78
  if (!response.page.has_more) break;
79
  cursor = response.page.next_cursor;
80
  if (!cursor || seen.has(cursor) || response.page.returned === 0) {
@@ -101,44 +138,50 @@ function typedFilters(scope: CatalogReadScope): QueryRequest["filters"] {
101
  return { conj: scope.filterConj ?? "and", nodes: scope.filters.map((value) => node(value, 1)) } as QueryRequest["filters"];
102
  }
103
 
104
- export async function loadCatalogData(database: string, scope: CatalogReadScope, signal: AbortSignal): Promise<CatalogData> {
 
105
  const properties = await documentProperties(await typedOpenApi(signal), database);
106
  signal.throwIfAborted();
107
- // The limit sizes each page. The cursor walk has no total row limit.
108
  const body: QueryRequest = {
109
- limit: 100,
110
  filters: typedFilters(scope),
111
  sorts: scope.sorts.map((sort) => ({ field: sort.colId, direction: sort.dir })),
112
  };
113
- const filtered = await walkPages(database, body, properties, signal);
114
  // Saved print placements join the complete permitted pool, before the display filter.
115
  const pool = scope.filters.length
116
- ? await walkPages(database, { ...body, filters: null }, properties, signal)
 
117
  : filtered;
118
  return { ...filtered, allRows: pool.rows };
119
  }
120
 
121
- type CatalogState = CatalogData & { key: string; loading: boolean; error: string };
 
122
  const EMPTY: CatalogData = { fields: [], rows: [], allRows: [], returned: 0 };
123
 
124
  export function useCatalogData(database: string, scope: CatalogReadScope) {
125
  const scopeJson = JSON.stringify(scope);
126
  const key = `${database}::${scopeJson}`;
127
  const [attempt, setAttempt] = useState(0);
128
- const [state, setState] = useState<CatalogState>(() => ({ ...EMPTY, key, loading: true, error: "" }));
129
  const retry = useCallback(() => setAttempt((value) => value + 1), []);
130
 
131
  useEffect(() => {
132
  const controller = new AbortController();
133
- setState({ ...EMPTY, key, loading: true, error: "" });
134
- void loadCatalogData(database, JSON.parse(scopeJson) as CatalogReadScope, controller.signal).then((data) => {
135
- if (!controller.signal.aborted) setState({ ...data, key, loading: false, error: "" });
 
 
 
136
  }).catch((error: unknown) => {
137
- if (!controller.signal.aborted) setState({ ...EMPTY, key, loading: false,
138
  error: error instanceof Error ? error.message : "The Catalog could not be loaded." });
139
  });
140
  return () => controller.abort();
141
  }, [database, scopeJson, key, attempt]);
142
 
143
- return { ...(state.key === key ? state : { ...EMPTY, key, loading: true, error: "" }), retry };
144
  }
 
1
  import { useCallback, useEffect, useState } from "react";
2
+ import { TypedRefusal, typedOpenApi, typedQuery } from "../../typed/client";
3
  import type { QueryRequest, QueryResponse, TypedOpenApi } from "../../typed/client";
4
  import { FILTER_OPS, isFilterGroup } from "../../customer-grid/types";
5
  import type { Field, FilterNode, FilterOp, Row, ViewConfig } from "../../customer-grid/types";
 
51
  return row;
52
  }
53
 
54
+ const FALLBACK_PAGE_SIZE = 500;
55
+ // The server's last reported page cap. Its cursor binds the limit, so one walk keeps one size.
56
+ let serverPageSize = FALLBACK_PAGE_SIZE;
57
+
58
+ function pageCap(value: unknown): number | null {
59
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : null;
60
+ }
61
+
62
+ /** Reads the first page at the server's own cap, restarting once if that cap differs. */
63
+ async function firstPage(database: string, body: QueryRequest, signal: AbortSignal) {
64
+ let limit = serverPageSize;
65
+ for (let attempt = 0; ; attempt += 1) {
66
+ signal.throwIfAborted();
67
+ let response: QueryResponse;
68
+ try {
69
+ response = await typedQuery(database, { ...body, limit, cursor: null }, signal);
70
+ } catch (error) {
71
+ const details = error instanceof TypedRefusal && error.code === "query_limit_invalid" &&
72
+ typeof error.details === "object" && error.details !== null
73
+ ? error.details as Record<string, unknown> : null;
74
+ const cap = pageCap(details?.limit);
75
+ if (attempt || cap === null || cap >= limit) throw error;
76
+ serverPageSize = limit = cap;
77
+ continue;
78
+ }
79
+ const cap = pageCap(response.caps?.max_page_size);
80
+ if (cap !== null) serverPageSize = cap;
81
+ if (attempt || cap === null || cap <= limit || !response.page?.has_more) return { response, limit };
82
+ limit = cap;
83
+ }
84
+ }
85
+
86
+ async function walkPages(database: string, body: QueryRequest, properties: Properties, signal: AbortSignal,
87
+ onPage?: (read: number) => void) {
88
  const rows: Row[] = [];
89
  const seen = new Set<string>();
90
  let fields: Field[] | undefined;
91
  let cursor: string | null = null;
92
  let returned = 0;
93
+ let limit = 0;
94
  do {
95
  signal.throwIfAborted();
96
+ let response: QueryResponse;
97
+ if (cursor === null) ({ response, limit } = await firstPage(database, body, signal));
98
+ else response = await typedQuery(database, { ...body, limit, cursor }, signal);
99
  signal.throwIfAborted();
100
  if (response.database !== database || !Array.isArray(response.records) ||
101
  response.page?.returned !== response.records.length) {
 
111
  }
112
  rows.push(...response.records.map((record) => projectRow(record, properties)));
113
  returned += response.page.returned;
114
+ onPage?.(rows.length);
115
  if (!response.page.has_more) break;
116
  cursor = response.page.next_cursor;
117
  if (!cursor || seen.has(cursor) || response.page.returned === 0) {
 
138
  return { conj: scope.filterConj ?? "and", nodes: scope.filters.map((value) => node(value, 1)) } as QueryRequest["filters"];
139
  }
140
 
141
+ export async function loadCatalogData(database: string, scope: CatalogReadScope, signal: AbortSignal,
142
+ onProgress?: (read: number) => void): Promise<CatalogData> {
143
  const properties = await documentProperties(await typedOpenApi(signal), database);
144
  signal.throwIfAborted();
145
+ // The limit sizes each page and firstPage replaces it with the server cap. The walk reads every row.
146
  const body: QueryRequest = {
147
+ limit: FALLBACK_PAGE_SIZE,
148
  filters: typedFilters(scope),
149
  sorts: scope.sorts.map((sort) => ({ field: sort.colId, direction: sort.dir })),
150
  };
151
+ const filtered = await walkPages(database, body, properties, signal, onProgress);
152
  // Saved print placements join the complete permitted pool, before the display filter.
153
  const pool = scope.filters.length
154
+ ? await walkPages(database, { ...body, filters: null }, properties, signal,
155
+ onProgress && ((read) => onProgress(filtered.rows.length + read)))
156
  : filtered;
157
  return { ...filtered, allRows: pool.rows };
158
  }
159
 
160
+ // progress counts rows read so far, so a loading state can show the walk moving.
161
+ type CatalogState = CatalogData & { key: string; loading: boolean; error: string; progress: number };
162
  const EMPTY: CatalogData = { fields: [], rows: [], allRows: [], returned: 0 };
163
 
164
  export function useCatalogData(database: string, scope: CatalogReadScope) {
165
  const scopeJson = JSON.stringify(scope);
166
  const key = `${database}::${scopeJson}`;
167
  const [attempt, setAttempt] = useState(0);
168
+ const [state, setState] = useState<CatalogState>(() => ({ ...EMPTY, key, loading: true, error: "", progress: 0 }));
169
  const retry = useCallback(() => setAttempt((value) => value + 1), []);
170
 
171
  useEffect(() => {
172
  const controller = new AbortController();
173
+ setState({ ...EMPTY, key, loading: true, error: "", progress: 0 });
174
+ const progress = (read: number) => {
175
+ if (!controller.signal.aborted) setState((live) => live.key === key && live.loading ? { ...live, progress: read } : live);
176
+ };
177
+ void loadCatalogData(database, JSON.parse(scopeJson) as CatalogReadScope, controller.signal, progress).then((data) => {
178
+ if (!controller.signal.aborted) setState({ ...data, key, loading: false, error: "", progress: data.allRows.length });
179
  }).catch((error: unknown) => {
180
+ if (!controller.signal.aborted) setState({ ...EMPTY, key, loading: false, progress: 0,
181
  error: error instanceof Error ? error.message : "The Catalog could not be loaded." });
182
  });
183
  return () => controller.abort();
184
  }, [database, scopeJson, key, attempt]);
185
 
186
+ return { ...(state.key === key ? state : { ...EMPTY, key, loading: true, error: "", progress: 0 }), retry };
187
  }
web/src/home/HomePage.tsx CHANGED
@@ -87,7 +87,7 @@ import {
87
  saveModuleFolder,
88
  shareMarkKey,
89
  } from "./homeModel";
90
- import type { AutomationTile, DataHomeLocation, HomeFilter, HomeLayout, HomeFolderTile, ModuleFolder, ModuleFolderIndex, ShareMarks } from "./homeModel";
91
 
92
  // Twin in SheetPage.tsx; this ticket's file fence keeps the timeout local to each chunk.
93
  const FOLDER_WRITE_TIMEOUT_MS = 20000;
@@ -337,6 +337,8 @@ export interface HomeViewTile {
337
  id: string;
338
  name: string;
339
  capabilities?: { edit?: boolean; delete?: boolean };
 
 
340
  }
341
 
342
  function parseHomeView(value: unknown): HomeViewTile | null {
@@ -345,7 +347,14 @@ function parseHomeView(value: unknown): HomeViewTile | null {
345
  if (typeof row.database !== "string" || !row.database || typeof row.id !== "string" || !row.id
346
  || typeof row.name !== "string" || !row.name.trim()) return null;
347
  return { database: row.database, id: row.id, name: row.name,
348
- capabilities: { edit: row.capabilities?.edit === true, delete: row.capabilities?.delete === true } };
 
 
 
 
 
 
 
349
  }
350
 
351
  async function readHomeViews(abort?: AbortSignal): Promise<{ views: HomeViewTile[]; unreadCount: number }> {
@@ -1610,7 +1619,7 @@ export function HomeSurface({
1610
  }}>
1611
  <span className="home-tile-chip home-app-glyph" aria-hidden="true"><SheetGlyph /></span>
1612
  <HomeTileText tile={row} fallback={entries.find((entry) => entry.key === row.database)?.label ?? "View"} />
1613
- <SharedMark mark={shareMarks.get(shareMarkKey("view", row.id))} fade={shareMarksFade} pending={shareMarksPending} />
1614
  </a>
1615
  {known ? <StarButton label={row.name} busy={busy}
1616
  on={starred.views.some((star) => star.database === row.database && star.id === row.id)}
 
87
  saveModuleFolder,
88
  shareMarkKey,
89
  } from "./homeModel";
90
+ import type { AutomationTile, DataHomeLocation, HomeFilter, HomeLayout, HomeFolderTile, ModuleFolder, ModuleFolderIndex, ShareMark, ShareMarks } from "./homeModel";
91
 
92
  // Twin in SheetPage.tsx; this ticket's file fence keeps the timeout local to each chunk.
93
  const FOLDER_WRITE_TIMEOUT_MS = 20000;
 
337
  id: string;
338
  name: string;
339
  capabilities?: { edit?: boolean; delete?: boolean };
340
+ /** The rail folder the View sits in. Absent when unfiled. */
341
+ folderId?: string;
342
  }
343
 
344
  function parseHomeView(value: unknown): HomeViewTile | null {
 
347
  if (typeof row.database !== "string" || !row.database || typeof row.id !== "string" || !row.id
348
  || typeof row.name !== "string" || !row.name.trim()) return null;
349
  return { database: row.database, id: row.id, name: row.name,
350
+ capabilities: { edit: row.capabilities?.edit === true, delete: row.capabilities?.delete === true },
351
+ ...(typeof row.folderId === "string" && row.folderId ? { folderId: row.folderId } : {}) };
352
+ }
353
+
354
+ /** A View's own share mark, else the mark its rail folder passes down. */
355
+ function viewShareMark(marks: ShareMarks, row: HomeViewTile): ShareMark | undefined {
356
+ return marks.get(shareMarkKey("view", row.id))
357
+ ?? (row.folderId ? marks.get(shareMarkKey("folder", row.folderId)) : undefined);
358
  }
359
 
360
  async function readHomeViews(abort?: AbortSignal): Promise<{ views: HomeViewTile[]; unreadCount: number }> {
 
1619
  }}>
1620
  <span className="home-tile-chip home-app-glyph" aria-hidden="true"><SheetGlyph /></span>
1621
  <HomeTileText tile={row} fallback={entries.find((entry) => entry.key === row.database)?.label ?? "View"} />
1622
+ <SharedMark mark={viewShareMark(shareMarks, row)} fade={shareMarksFade} pending={shareMarksPending} />
1623
  </a>
1624
  {known ? <StarButton label={row.name} busy={busy}
1625
  on={starred.views.some((star) => star.database === row.database && star.id === row.id)}
web/src/home/homeModel.ts CHANGED
@@ -416,7 +416,7 @@ export type ShareMark = { direction: "with_me"; by: string }
416
  | { direction: "by_me"; count: number; everyone: boolean };
417
  export type ShareMarks = ReadonlyMap<string, ShareMark>;
418
  export const NO_SHARE_MARKS: ShareMarks = new Map();
419
- export type ShareMarkKind = "database" | "view" | "workbook" | "app" | "agent";
420
 
421
  /** The wire key: `<kind>:<id>`. View ids are bare, as the grant registry stores them. */
422
  export function shareMarkKey(kind: ShareMarkKind, id: string): string {
 
416
  | { direction: "by_me"; count: number; everyone: boolean };
417
  export type ShareMarks = ReadonlyMap<string, ShareMark>;
418
  export const NO_SHARE_MARKS: ShareMarks = new Map();
419
+ export type ShareMarkKind = "database" | "view" | "folder" | "workbook" | "app" | "agent";
420
 
421
  /** The wire key: `<kind>:<id>`. View ids are bare, as the grant registry stores them. */
422
  export function shareMarkKey(kind: ShareMarkKind, id: string): string {