Prune files not in the deploy set (stale build context)
Browse files- platform/core/app_list_query.py +0 -606
- web/src/apps/SkuListTools.tsx +0 -85
platform/core/app_list_query.py
DELETED
|
@@ -1,606 +0,0 @@
|
|
| 1 |
-
"""Compile PostgreSQL App list pages and retain an offline Grid evaluator.
|
| 2 |
-
|
| 3 |
-
PUBLIC production API: compile_list_query(source_sql, columns, *, identity_sql,
|
| 4 |
-
source_params=(), filter=None, sort=None, offset=0, limit=200, today=None,
|
| 5 |
-
default_order_sql=None, clamp_offset=False) -> ListQuerySQL(ctes, params).
|
| 6 |
-
The compiler does no I/O. Values use psycopg %s bindings. Never pass request SQL.
|
| 7 |
-
|
| 8 |
-
source_sql is a trusted SELECT over the FULL permitted, searched source, without
|
| 9 |
-
paging or a row cap. It becomes relation `s`. Column `sql` expressions,
|
| 10 |
-
identity_sql and default_order_sql reference `s`. Identity must be unique,
|
| 11 |
-
non-null and ordered consistently. Reserve the `_alq_` column/CTE prefix.
|
| 12 |
-
Use a native numeric SQL expression for numeric fields, text for choice fields,
|
| 13 |
-
and date/timestamp or ISO text for dates. Numeric comparisons use unrounded Grid
|
| 14 |
-
values. Multiselect needs sql_shape='csv' and SQL normalization to Grid's
|
| 15 |
-
comma-joined representation. Arrays, JSON and grouped ranks are refused.
|
| 16 |
-
|
| 17 |
-
The result's ctes have no WITH prefix. Append them to caller CTEs. `_alq_bounds`
|
| 18 |
-
has (total, page_offset), including empty results. `_alq_page` has (item_id,
|
| 19 |
-
row_number), at most limit rows, ordered by row_number. Params follow textual
|
| 20 |
-
CTE order, starting with source_params. Projection params follow plan.params.
|
| 21 |
-
Only scalar identities/columns enter these CTEs. Project payloads AFTER `_alq_page`.
|
| 22 |
-
No connection, grant, search, cursor or response policy moves into this module.
|
| 23 |
-
default_order_sql preserves the route's order when sort is absent or empty.
|
| 24 |
-
Explicit sorts replace it and always append identity ASC. Blanks sort last.
|
| 25 |
-
clamp_offset=True preserves last-page snapping used by Restock and Aged Stock.
|
| 26 |
-
Ranks use all non-rank conditions as their domain, matching filter_eval.
|
| 27 |
-
|
| 28 |
-
Exact bounded Aged Stock amendment (inside its existing tenant-bound items):
|
| 29 |
-
keep PAGE_SQL's doc/items/searched/filtered CTEs and their bound parameters.
|
| 30 |
-
Add scalar Grid fields to searched, e.g. e.value->>'owner' AS owner.
|
| 31 |
-
Replace bounds/ranked/page/projected and the final SELECT with this composition::
|
| 32 |
-
|
| 33 |
-
fields = [{"key": "owner", "label": "Owner", "type": "user", "sql": "s.owner"}]
|
| 34 |
-
plan = compile_list_query("SELECT * FROM filtered", fields,
|
| 35 |
-
identity_sql="s.iid", filter=filter, sort=sort, offset=start, limit=limit,
|
| 36 |
-
today=tenant_today, clamp_offset=True,
|
| 37 |
-
default_order_sql="s.report DESC, CASE s.action WHEN 'Remove' THEN 0 "
|
| 38 |
-
"WHEN 'Price down' THEN 1 WHEN 'Push with ads' THEN 2 ELSE 3 END, "
|
| 39 |
-
'COALESCE(s.stake, 0) DESC NULLS LAST, s.sku COLLATE "C"')
|
| 40 |
-
sql = prefix_through_filtered + ", " + plan.ctes + ", projected AS (" \
|
| 41 |
-
"SELECT items.v->p.item_id AS v, p.row_number FROM _alq_page p " \
|
| 42 |
-
"CROSS JOIN items) SELECT jsonb_build_object(" \
|
| 43 |
-
"'meta', (SELECT value - 'items' FROM doc), " \
|
| 44 |
-
"'total', (SELECT total FROM _alq_bounds), " \
|
| 45 |
-
"'offset', (SELECT page_offset FROM _alq_bounds), " \
|
| 46 |
-
"'items', COALESCE((SELECT jsonb_agg(v - 'history' - 'fee_history' - 'fees' " \
|
| 47 |
-
"ORDER BY row_number) FROM projected), '[]'::jsonb))"
|
| 48 |
-
result = _select(st, sql, lambda key: (*prefix_params(key), *plan.params))
|
| 49 |
-
|
| 50 |
-
prefix_params retains every existing scope/search binding through filtered.
|
| 51 |
-
Keep the Next tier conditional default expressions and bind their flags inside
|
| 52 |
-
source_sql/source_params, or choose trusted SQL expressions from next_tier.
|
| 53 |
-
Keep existing page-size validation, cursor response and metadata handling.
|
| 54 |
-
Serve list_columns(fields) only after the existing route grant checks.
|
| 55 |
-
Restock follows the same pattern and retains its searched counts projection.
|
| 56 |
-
The other four list owners supply their own full permitted source and projection.
|
| 57 |
-
"""
|
| 58 |
-
|
| 59 |
-
import json
|
| 60 |
-
import math
|
| 61 |
-
import re
|
| 62 |
-
from collections.abc import Mapping
|
| 63 |
-
from dataclasses import dataclass
|
| 64 |
-
from functools import cmp_to_key
|
| 65 |
-
|
| 66 |
-
from core import windows
|
| 67 |
-
from core.external_tools import FILTER_OPS
|
| 68 |
-
from core.filter_contract import MAX_FILTER_VALUES
|
| 69 |
-
from harness import filter_eval
|
| 70 |
-
from harness.filter_sql import MULTI_VALUE_OPS, RANK_OPS, is_rule_active, to_num
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
_EMPTY_OPS = frozenset({"isEmpty", "isNotEmpty"})
|
| 74 |
-
_COMPARISONS = frozenset({"eq", "neq", "gt", "gte", "lt", "lte"})
|
| 75 |
-
_TEXT_OPS = frozenset({"contains", "doesNotContain", "eq", "neq"})
|
| 76 |
-
_NUMERIC = filter_eval.NUMERIC_TYPES
|
| 77 |
-
_DATES = filter_eval.DATE_TYPES
|
| 78 |
-
_TYPES = _NUMERIC | _DATES | {
|
| 79 |
-
"text", "status", "select", "multiselect", "user", "checkbox", "json",
|
| 80 |
-
}
|
| 81 |
-
_LEAF_KEYS = frozenset({
|
| 82 |
-
"id", "colId", "op", "value", "value2", "values", "rhs",
|
| 83 |
-
"dateMode", "dateWindow", "rankScope",
|
| 84 |
-
})
|
| 85 |
-
_NUMBER = re.compile(r"[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\Z")
|
| 86 |
-
# Match aios_grid.clean_filter_tree bounds, refusing excess conditions instead of truncating them.
|
| 87 |
-
_MAX_DEPTH = 3
|
| 88 |
-
_MAX_NODES = 100
|
| 89 |
-
_MAX_SIBLINGS = 50
|
| 90 |
-
_MAX_JSON = 65536
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
def _fail(message):
|
| 94 |
-
raise ValueError(message)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def _pairs(pairs):
|
| 98 |
-
result = {}
|
| 99 |
-
for key, value in pairs:
|
| 100 |
-
if key in result:
|
| 101 |
-
_fail("Query JSON contains a repeated key.")
|
| 102 |
-
result[key] = value
|
| 103 |
-
return result
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
def _decode(value, name, default):
|
| 107 |
-
if value is None:
|
| 108 |
-
return default
|
| 109 |
-
if isinstance(value, str):
|
| 110 |
-
if len(value) > _MAX_JSON:
|
| 111 |
-
_fail(f"The {name} query is too large.")
|
| 112 |
-
try:
|
| 113 |
-
return json.loads(value, object_pairs_hook=_pairs,
|
| 114 |
-
parse_constant=lambda _: _fail("Query numbers must be finite."))
|
| 115 |
-
except (ValueError, RecursionError) as exc:
|
| 116 |
-
raise ValueError(f"The {name} query must be valid JSON.") from exc
|
| 117 |
-
return value
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def _column_map(columns):
|
| 121 |
-
if isinstance(columns, Mapping):
|
| 122 |
-
fields = []
|
| 123 |
-
for key, value in columns.items():
|
| 124 |
-
if not isinstance(value, Mapping) or value.get("key", key) != key:
|
| 125 |
-
_fail("Each column needs a matching key and type.")
|
| 126 |
-
fields.append({**value, "key": key})
|
| 127 |
-
elif isinstance(columns, (list, tuple)):
|
| 128 |
-
fields = columns
|
| 129 |
-
else:
|
| 130 |
-
_fail("Columns must be a field list or mapping.")
|
| 131 |
-
result = {}
|
| 132 |
-
for field in fields:
|
| 133 |
-
if not isinstance(field, Mapping):
|
| 134 |
-
_fail("Each column needs a key and type.")
|
| 135 |
-
key, kind = field.get("key"), field.get("type")
|
| 136 |
-
if not isinstance(key, str) or not key or key in result:
|
| 137 |
-
_fail("Column keys must be unique nonempty strings.")
|
| 138 |
-
if not isinstance(kind, str) or kind not in _TYPES:
|
| 139 |
-
_fail(f"Column {key} has an unsupported type.")
|
| 140 |
-
result[key] = dict(field)
|
| 141 |
-
return result
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
def list_columns(columns):
|
| 145 |
-
"""Return explicit Grid builder fields for an authenticated route's columns response."""
|
| 146 |
-
public = {"key", "label", "type", "options", "description", "width"}
|
| 147 |
-
return [{key: value for key, value in field.items() if key in public}
|
| 148 |
-
for field in _column_map(columns).values()]
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
def _column(key, columns):
|
| 152 |
-
if not isinstance(key, str) or key not in columns:
|
| 153 |
-
_fail("The query names an unknown column.")
|
| 154 |
-
return columns[key]
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
def _scalar(value):
|
| 158 |
-
if type(value) not in (str, int, float) or isinstance(value, float) and not math.isfinite(value):
|
| 159 |
-
_fail("Filter values must be text or finite numbers.")
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
def _number(value):
|
| 163 |
-
_scalar(value)
|
| 164 |
-
try:
|
| 165 |
-
number = float(value)
|
| 166 |
-
except (ValueError, OverflowError) as exc:
|
| 167 |
-
raise ValueError("This filter needs a finite numeric value.") from exc
|
| 168 |
-
if not _NUMBER.fullmatch(str(value).strip()) or not math.isfinite(number):
|
| 169 |
-
_fail("This filter needs a finite numeric value.")
|
| 170 |
-
return number
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
def _day(value):
|
| 174 |
-
if not isinstance(value, str) or windows.ending_date(value) is None:
|
| 175 |
-
_fail("Date filters need an ISO calendar date.")
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
def _date_operand(node, today):
|
| 179 |
-
op = node["op"]
|
| 180 |
-
if op == "within":
|
| 181 |
-
spec = node.get("dateWindow")
|
| 182 |
-
if not isinstance(spec, dict) or set(spec) - {"kind", "n", "from", "to"}:
|
| 183 |
-
_fail("A date window needs a known kind and bounds.")
|
| 184 |
-
kind = spec.get("kind")
|
| 185 |
-
if not isinstance(kind, str) or kind not in windows.WINDOW_KINDS:
|
| 186 |
-
_fail("A date window needs a known kind.")
|
| 187 |
-
allowed = {"kind"}
|
| 188 |
-
if kind in windows.N_KINDS:
|
| 189 |
-
allowed.add("n")
|
| 190 |
-
n = _number(spec.get("n"))
|
| 191 |
-
if n != int(n) or not 1 <= n <= windows.MAX_N:
|
| 192 |
-
_fail("The date window size is out of range.")
|
| 193 |
-
if kind == "custom":
|
| 194 |
-
allowed |= {"from", "to"}
|
| 195 |
-
for key in ("from", "to"):
|
| 196 |
-
if spec.get(key) not in (None, ""):
|
| 197 |
-
_day(spec[key])
|
| 198 |
-
if set(spec) - allowed:
|
| 199 |
-
_fail("The date window has fields for another kind.")
|
| 200 |
-
if windows.normalize(spec) is None:
|
| 201 |
-
_fail("The date window is incomplete.")
|
| 202 |
-
if today is None:
|
| 203 |
-
_fail("Date windows need the tenant's today date.")
|
| 204 |
-
try:
|
| 205 |
-
windows.resolve(spec, today)
|
| 206 |
-
except (ValueError, OverflowError) as exc:
|
| 207 |
-
raise ValueError("The date window is out of range.") from exc
|
| 208 |
-
elif op == "between":
|
| 209 |
-
_day(node.get("value"))
|
| 210 |
-
_day(node.get("value2"))
|
| 211 |
-
else:
|
| 212 |
-
mode = node.get("dateMode", "exact")
|
| 213 |
-
if not isinstance(mode, str) or mode not in windows.ANCHOR_MODES:
|
| 214 |
-
_fail("The date anchor is unknown.")
|
| 215 |
-
if mode != "exact" and today is None:
|
| 216 |
-
_fail("Relative dates need the tenant's today date.")
|
| 217 |
-
if mode == "exact":
|
| 218 |
-
_day(node.get("value"))
|
| 219 |
-
try:
|
| 220 |
-
anchor = windows.resolve_anchor(mode, node.get("value"), today or "1970-01-01")
|
| 221 |
-
except (ValueError, OverflowError) as exc:
|
| 222 |
-
raise ValueError("The date anchor is out of range.") from exc
|
| 223 |
-
if anchor is None:
|
| 224 |
-
_fail("The date anchor is incomplete.")
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
def _leaf(node, columns, today):
|
| 228 |
-
if set(node) - _LEAF_KEYS:
|
| 229 |
-
_fail("The filter has unsupported fields.")
|
| 230 |
-
field = _column(node.get("colId"), columns)
|
| 231 |
-
op, kind = node.get("op"), field["type"]
|
| 232 |
-
if not isinstance(op, str) or op not in FILTER_OPS:
|
| 233 |
-
_fail("The filter operator is unknown.")
|
| 234 |
-
if "id" in node and not isinstance(node["id"], str):
|
| 235 |
-
_fail("Filter ids must be text.")
|
| 236 |
-
for key in ("value", "value2"):
|
| 237 |
-
if key in node:
|
| 238 |
-
_scalar(node[key])
|
| 239 |
-
allowed = _EMPTY_OPS
|
| 240 |
-
if kind in _NUMERIC:
|
| 241 |
-
allowed |= _COMPARISONS | RANK_OPS | {"between", "isAnyOf", "isNoneOf"}
|
| 242 |
-
elif kind in _DATES:
|
| 243 |
-
allowed |= _COMPARISONS | {"between", "within"}
|
| 244 |
-
elif kind not in {"checkbox", "json"}:
|
| 245 |
-
allowed |= _TEXT_OPS | MULTI_VALUE_OPS
|
| 246 |
-
if op not in allowed:
|
| 247 |
-
_fail("The operator does not apply to this column type.")
|
| 248 |
-
if (("dateWindow" in node and op != "within") or ("dateMode" in node and (
|
| 249 |
-
kind not in _DATES or op not in _COMPARISONS))):
|
| 250 |
-
_fail("Date options do not apply to this filter.")
|
| 251 |
-
if "values" in node and op not in MULTI_VALUE_OPS:
|
| 252 |
-
_fail("A value list needs a membership operator.")
|
| 253 |
-
if "rankScope" in node:
|
| 254 |
-
if op not in RANK_OPS or node["rankScope"] not in ("global", {"mode": "global"}):
|
| 255 |
-
_fail("App lists support global ranking only.")
|
| 256 |
-
if "rhs" in node:
|
| 257 |
-
rhs = node["rhs"]
|
| 258 |
-
if (not isinstance(rhs, dict) or set(rhs) != {"kind", "colId"}
|
| 259 |
-
or rhs.get("kind") != "field" or op not in _COMPARISONS | _TEXT_OPS):
|
| 260 |
-
_fail("The filter needs a supported field comparison.")
|
| 261 |
-
other = _column(rhs.get("colId"), columns)["type"]
|
| 262 |
-
if ((kind in _NUMERIC) != (other in _NUMERIC)
|
| 263 |
-
or (kind in _DATES) != (other in _DATES)):
|
| 264 |
-
_fail("Compared fields need compatible types.")
|
| 265 |
-
elif op in MULTI_VALUE_OPS:
|
| 266 |
-
values = node.get("values")
|
| 267 |
-
if (not isinstance(values, list) or not 1 <= len(values) <= MAX_FILTER_VALUES
|
| 268 |
-
or any(not isinstance(v, str) or not v.strip() or len(v.strip()) > 120 for v in values)):
|
| 269 |
-
_fail("Membership filters need a nonempty bounded list of text values.")
|
| 270 |
-
if kind in _NUMERIC:
|
| 271 |
-
for value in values:
|
| 272 |
-
_number(value)
|
| 273 |
-
elif op in RANK_OPS:
|
| 274 |
-
if op not in {"aboveAvg", "belowAvg"}:
|
| 275 |
-
number = _number(node.get("value"))
|
| 276 |
-
upper = {"inQuartile": 4, "inDecile": 10, "inTopPct": 100, "inBottomPct": 100}.get(op, 10000)
|
| 277 |
-
if number != int(number) or not 1 <= number <= upper:
|
| 278 |
-
_fail("The rank operand is out of range.")
|
| 279 |
-
elif op not in _EMPTY_OPS:
|
| 280 |
-
if kind in _DATES:
|
| 281 |
-
_date_operand(node, today)
|
| 282 |
-
elif kind in _NUMERIC:
|
| 283 |
-
_number(node.get("value"))
|
| 284 |
-
if op == "between":
|
| 285 |
-
_number(node.get("value2"))
|
| 286 |
-
elif not isinstance(node.get("value"), str) or not node["value"]:
|
| 287 |
-
_fail("This filter needs a nonempty text value.")
|
| 288 |
-
if not is_rule_active(node, columns):
|
| 289 |
-
_fail("The filter is incomplete.")
|
| 290 |
-
return dict(node)
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
def _filter_tree(value, columns, today):
|
| 294 |
-
raw = _decode(value, "filter", [])
|
| 295 |
-
if isinstance(raw, list):
|
| 296 |
-
raw = {"nodes": raw}
|
| 297 |
-
if not isinstance(raw, dict) or set(raw) - {"conj", "nodes"} or "nodes" not in raw:
|
| 298 |
-
_fail("A filter needs nodes and an optional conjunction.")
|
| 299 |
-
count = 0
|
| 300 |
-
|
| 301 |
-
def group(node, key, depth):
|
| 302 |
-
nonlocal count
|
| 303 |
-
if depth > _MAX_DEPTH:
|
| 304 |
-
_fail("The filter is nested too deeply.")
|
| 305 |
-
conj, nodes = node.get("conj", "and"), node.get(key)
|
| 306 |
-
if conj not in ("and", "or") or not isinstance(nodes, list):
|
| 307 |
-
_fail("A filter group needs nodes and a conjunction of and or or.")
|
| 308 |
-
if len(nodes) > _MAX_SIBLINGS:
|
| 309 |
-
_fail("The filter group has too many conditions.")
|
| 310 |
-
children = []
|
| 311 |
-
for child in nodes:
|
| 312 |
-
count += 1
|
| 313 |
-
if count > _MAX_NODES or not isinstance(child, dict):
|
| 314 |
-
_fail("The filter has too many nodes or a malformed node.")
|
| 315 |
-
if "children" in child:
|
| 316 |
-
if set(child) - {"conj", "children"} or not child["children"]:
|
| 317 |
-
_fail("Nested filter groups need children and a conjunction.")
|
| 318 |
-
children.append(group(child, "children", depth + 1))
|
| 319 |
-
else:
|
| 320 |
-
children.append(_leaf(child, columns, today))
|
| 321 |
-
return {"conj": conj, key: children}
|
| 322 |
-
|
| 323 |
-
return group(raw, "nodes", 1)
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
def _sort_rules(value, columns):
|
| 327 |
-
rules = _decode(value, "sort", [])
|
| 328 |
-
if not isinstance(rules, list):
|
| 329 |
-
_fail("Sort must be a list of column and direction pairs.")
|
| 330 |
-
seen, result = set(), []
|
| 331 |
-
for rule in rules:
|
| 332 |
-
if not isinstance(rule, dict) or set(rule) != {"colId", "dir"}:
|
| 333 |
-
_fail("Each sort needs colId and dir.")
|
| 334 |
-
key = rule["colId"]
|
| 335 |
-
_column(key, columns)
|
| 336 |
-
if rule["dir"] not in ("asc", "desc") or key in seen:
|
| 337 |
-
_fail("Sort directions must be asc or desc with unique columns.")
|
| 338 |
-
if columns[key]["type"] == "json":
|
| 339 |
-
_fail("JSON columns cannot be sorted.")
|
| 340 |
-
seen.add(key)
|
| 341 |
-
result.append(dict(rule))
|
| 342 |
-
return result
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
def _eval_key(key):
|
| 346 |
-
return "field:" + key
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
def _eval_tree(node):
|
| 350 |
-
result = dict(node)
|
| 351 |
-
for key in ("nodes", "children"):
|
| 352 |
-
if key in result:
|
| 353 |
-
result[key] = [_eval_tree(child) for child in result[key]]
|
| 354 |
-
if "colId" in result:
|
| 355 |
-
result["colId"] = _eval_key(result["colId"])
|
| 356 |
-
if "rhs" in result:
|
| 357 |
-
result["rhs"] = {**result["rhs"], "colId": _eval_key(result["rhs"]["colId"])}
|
| 358 |
-
return result
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
def _compare(left, right, sorts, columns):
|
| 362 |
-
for rule in sorts:
|
| 363 |
-
key = rule["colId"]
|
| 364 |
-
a, b = left.get(key), right.get(key)
|
| 365 |
-
a_blank, b_blank = filter_eval._is_blank(a), filter_eval._is_blank(b)
|
| 366 |
-
if a_blank != b_blank:
|
| 367 |
-
return 1 if a_blank else -1
|
| 368 |
-
if a_blank:
|
| 369 |
-
continue
|
| 370 |
-
kind = columns[key]["type"]
|
| 371 |
-
if kind in _NUMERIC:
|
| 372 |
-
a, b = to_num(a), to_num(b)
|
| 373 |
-
elif kind in _DATES:
|
| 374 |
-
a, b = str(a)[:10], str(b)[:10]
|
| 375 |
-
else:
|
| 376 |
-
a, b = filter_eval._as_choice_text(a), filter_eval._as_choice_text(b)
|
| 377 |
-
order = (a > b) - (a < b)
|
| 378 |
-
if order:
|
| 379 |
-
return order if rule["dir"] == "asc" else -order
|
| 380 |
-
return 0
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
def apply_list_query(rows, columns, *, filter=None, sort=None, offset=0, limit=200, today=None):
|
| 384 |
-
"""Evaluate complete offline fixtures, never a production PostgreSQL request path."""
|
| 385 |
-
if type(offset) is not int or offset < 0 or type(limit) is not int or limit < 1:
|
| 386 |
-
_fail("Paging needs a nonnegative offset and a positive limit.")
|
| 387 |
-
if today is not None:
|
| 388 |
-
_day(today)
|
| 389 |
-
fields = _column_map(columns)
|
| 390 |
-
tree = _filter_tree(filter, fields, today)
|
| 391 |
-
sorts = _sort_rules(sort, fields)
|
| 392 |
-
source = list(rows)
|
| 393 |
-
if any(not isinstance(row, Mapping) for row in source):
|
| 394 |
-
_fail("App list rows must be mappings.")
|
| 395 |
-
selected = source
|
| 396 |
-
if tree["nodes"]:
|
| 397 |
-
# Separate evaluator identities from real columns, including a visible pid column.
|
| 398 |
-
eval_fields = {_eval_key(key): {**field, "key": _eval_key(key)} for key, field in fields.items()}
|
| 399 |
-
eval_rows = [{"pid": index, **{_eval_key(key): row.get(key) for key in fields}}
|
| 400 |
-
for index, row in enumerate(source)]
|
| 401 |
-
pids = filter_eval.visible_pids(_eval_tree(tree), eval_rows, eval_fields,
|
| 402 |
-
filter_eval.EvalCtx(today=today))
|
| 403 |
-
selected = [source[index] for index in pids]
|
| 404 |
-
if sorts:
|
| 405 |
-
selected = sorted(selected, key=cmp_to_key(lambda a, b: _compare(a, b, sorts, fields)))
|
| 406 |
-
total = len(selected)
|
| 407 |
-
more = offset + limit < total
|
| 408 |
-
return {"items": selected[offset:offset + limit], "total": total, "offset": offset,
|
| 409 |
-
"limit": limit, "hasMore": more, "nextOffset": offset + limit if more else None}
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
@dataclass(frozen=True)
|
| 413 |
-
class ListQuerySQL:
|
| 414 |
-
"""Public PostgreSQL CTEs and positional bindings for a caller-owned bounded projection."""
|
| 415 |
-
|
| 416 |
-
ctes: str
|
| 417 |
-
params: tuple
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
_SQL_CMP = {"eq": "=", "neq": "<>", "lt": "<", "lte": "<=", "gt": ">", "gte": ">="}
|
| 421 |
-
_SQL_WS = "".join(chr(i) for i in (*range(9, 14), *range(28, 33), 133, 160, 5760,
|
| 422 |
-
*range(8192, 8203), 8232, 8233, 8239, 8287, 12288))
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
def _sql_trim(expr):
|
| 426 |
-
return f"TRIM(BOTH '{_SQL_WS}' FROM CAST({expr} AS TEXT))"
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
def _sql_blank(expr):
|
| 430 |
-
return f"({expr} IS NULL OR {_sql_trim(expr)} IN ('', '(none)'))"
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
def _sql_value(expr, kind):
|
| 434 |
-
if kind in _NUMERIC:
|
| 435 |
-
return f"COALESCE(CAST({expr} AS DOUBLE PRECISION), 0)"
|
| 436 |
-
if kind in _DATES:
|
| 437 |
-
return f"COALESCE(SUBSTR(CAST({expr} AS TEXT), 1, 10), '')"
|
| 438 |
-
return f"LOWER(COALESCE(CAST({expr} AS TEXT), ''))"
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
def _sql_leaf(node, fields, refs, params, today):
|
| 442 |
-
key, op = node["colId"], node["op"]
|
| 443 |
-
kind, raw = fields[key]["type"], refs[key]
|
| 444 |
-
value = _sql_value(raw, kind)
|
| 445 |
-
|
| 446 |
-
def bind(operand):
|
| 447 |
-
params.append(operand)
|
| 448 |
-
return "%s"
|
| 449 |
-
|
| 450 |
-
if op in _EMPTY_OPS:
|
| 451 |
-
blank = _sql_blank(raw)
|
| 452 |
-
return blank if op == "isEmpty" else f"(NOT {blank})"
|
| 453 |
-
if op in MULTI_VALUE_OPS:
|
| 454 |
-
values = [v.strip() for v in node["values"]]
|
| 455 |
-
if op in {"isAnyOf", "isNoneOf"}:
|
| 456 |
-
parts = [f"{value} = {bind(_number(v))}" if kind in _NUMERIC
|
| 457 |
-
else f"{value} = LOWER(CAST({bind(v)} AS TEXT))" for v in values]
|
| 458 |
-
else:
|
| 459 |
-
# PostgreSQL unnest replaces DuckDB's list_transform/list_contains dialect.
|
| 460 |
-
parts = [f"EXISTS (SELECT 1 FROM unnest(string_to_array({value}, ',')) "
|
| 461 |
-
f"AS m(member) WHERE {_sql_trim('m.member')} = LOWER(CAST({bind(v)} AS TEXT)))"
|
| 462 |
-
for v in values]
|
| 463 |
-
joined = (" AND " if op == "hasAllOf" else " OR ").join(parts)
|
| 464 |
-
return f"({'NOT ' if op in {'isNoneOf', 'hasNoneOf'} else ''}({joined}))"
|
| 465 |
-
if op == "between":
|
| 466 |
-
if kind in _NUMERIC:
|
| 467 |
-
lo, hi = sorted((_number(node["value"]), _number(node["value2"])))
|
| 468 |
-
else:
|
| 469 |
-
lo, hi = sorted((node["value"], node["value2"]))
|
| 470 |
-
a, b = bind(lo), bind(hi)
|
| 471 |
-
if kind in _DATES:
|
| 472 |
-
a, b = f"CAST({a} AS TEXT)", f"CAST({b} AS TEXT)"
|
| 473 |
-
return f"({value} >= {a} AND {value} <= {b})"
|
| 474 |
-
if op == "within":
|
| 475 |
-
lo, hi = windows.resolve(node["dateWindow"], today)
|
| 476 |
-
parts = [f"{value} <> ''"]
|
| 477 |
-
if lo is not None:
|
| 478 |
-
parts.append(f"{value} >= CAST({bind(lo)} AS TEXT)")
|
| 479 |
-
if hi is not None:
|
| 480 |
-
parts.append(f"{value} <= CAST({bind(hi)} AS TEXT)")
|
| 481 |
-
return "(" + " AND ".join(parts) + ")"
|
| 482 |
-
if "rhs" in node:
|
| 483 |
-
other = _sql_value(refs[node["rhs"]["colId"]], kind)
|
| 484 |
-
elif kind in _NUMERIC:
|
| 485 |
-
other = bind(_number(node["value"]))
|
| 486 |
-
elif kind in _DATES:
|
| 487 |
-
anchor = windows.resolve_anchor(node.get("dateMode", "exact"), node.get("value"),
|
| 488 |
-
today or "1970-01-01")
|
| 489 |
-
other = f"CAST({bind(anchor)} AS TEXT)"
|
| 490 |
-
else:
|
| 491 |
-
other = f"LOWER(CAST({bind(node['value'])} AS TEXT))"
|
| 492 |
-
if op in {"contains", "doesNotContain"}:
|
| 493 |
-
return f"(strpos({value}, {other}) {'>' if op == 'contains' else '='} 0)"
|
| 494 |
-
return f"({value} {_SQL_CMP[op]} {other})"
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
def _sql_tree(node, fields, refs, params, today, ranks=None, residue=False):
|
| 498 |
-
children = node.get("nodes", node.get("children"))
|
| 499 |
-
if children is not None:
|
| 500 |
-
parts = [_sql_tree(child, fields, refs, params, today, ranks, residue) for child in children]
|
| 501 |
-
parts = [part for part in parts if part is not None]
|
| 502 |
-
return ("(" + (" OR " if node.get("conj") == "or" else " AND ").join(parts) + ")"
|
| 503 |
-
if parts else None)
|
| 504 |
-
if node["op"] in RANK_OPS:
|
| 505 |
-
if residue:
|
| 506 |
-
return None
|
| 507 |
-
return f"(_alq_id IN (SELECT item_id FROM {ranks[id(node)]}))"
|
| 508 |
-
return _sql_leaf(node, fields, refs, params, today)
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
def _sql_rank_nodes(node):
|
| 512 |
-
for child in node.get("nodes", node.get("children", ())):
|
| 513 |
-
if "children" in child:
|
| 514 |
-
yield from _sql_rank_nodes(child)
|
| 515 |
-
elif child["op"] in RANK_OPS:
|
| 516 |
-
yield child
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
def compile_list_query(source_sql, columns, *, identity_sql, source_params=(), filter=None,
|
| 520 |
-
sort=None, offset=0, limit=200, today=None, default_order_sql=None,
|
| 521 |
-
clamp_offset=False):
|
| 522 |
-
"""Compile validated Grid values into a full-scope PostgreSQL identity page with total count."""
|
| 523 |
-
if type(offset) is not int or offset < 0 or type(limit) is not int or limit < 1:
|
| 524 |
-
_fail("Paging needs a nonnegative offset and a positive limit.")
|
| 525 |
-
if type(clamp_offset) is not bool:
|
| 526 |
-
_fail("Offset clamping must be a boolean.")
|
| 527 |
-
if today is not None:
|
| 528 |
-
_day(today)
|
| 529 |
-
for sql in (source_sql, identity_sql):
|
| 530 |
-
if not isinstance(sql, str) or not sql.strip():
|
| 531 |
-
_fail("A trusted source SELECT and unique row identity SQL are required.")
|
| 532 |
-
if default_order_sql is not None and (
|
| 533 |
-
not isinstance(default_order_sql, str) or not default_order_sql.strip()):
|
| 534 |
-
_fail("Default ordering must be trusted SQL or None.")
|
| 535 |
-
fields = _column_map(columns)
|
| 536 |
-
tree = _filter_tree(filter, fields, today)
|
| 537 |
-
sorts = _sort_rules(sort, fields)
|
| 538 |
-
refs, selections = {}, []
|
| 539 |
-
for index, (key, field) in enumerate(fields.items()):
|
| 540 |
-
expr, shape = field.get("sql"), field.get("sql_shape", "scalar")
|
| 541 |
-
if (not isinstance(expr, str) or not expr.strip() or field["type"] == "json"
|
| 542 |
-
or not isinstance(shape, str) or shape not in {"scalar", "csv"}
|
| 543 |
-
or (field["type"] == "multiselect" and shape != "csv")
|
| 544 |
-
or (field["type"] != "multiselect" and shape != "scalar")
|
| 545 |
-
or field.get("aggregate")):
|
| 546 |
-
_fail("SQL columns need typed scalar SQL, or explicit csv SQL for multiselect.")
|
| 547 |
-
refs[key] = f"_alq_c{index}"
|
| 548 |
-
selections.append(f"({expr}) AS {refs[key]}")
|
| 549 |
-
params = list(source_params)
|
| 550 |
-
if not sorts and default_order_sql:
|
| 551 |
-
selections.append("row_number() OVER (ORDER BY " + default_order_sql
|
| 552 |
-
+ f", {identity_sql} ASC) AS _alq_default_n")
|
| 553 |
-
select = ", ".join([f"({identity_sql}) AS _alq_id", *selections])
|
| 554 |
-
ctes = [f"_alq_source AS MATERIALIZED (SELECT {select} FROM ({source_sql}) AS s)"]
|
| 555 |
-
rank_nodes = tuple(_sql_rank_nodes(tree))
|
| 556 |
-
ranks = {}
|
| 557 |
-
if rank_nodes:
|
| 558 |
-
predicate = _sql_tree(tree, fields, refs, params, today, residue=True) or "TRUE"
|
| 559 |
-
ctes.append(f"_alq_rank_domain AS MATERIALIZED (SELECT * FROM _alq_source WHERE {predicate})")
|
| 560 |
-
for index, node in enumerate(rank_nodes):
|
| 561 |
-
name, raw, op = f"_alq_rank_{index}", refs[node["colId"]], node["op"]
|
| 562 |
-
ranks[id(node)] = name
|
| 563 |
-
numeric = f"CAST({raw} AS DOUBLE PRECISION)"
|
| 564 |
-
# Rank blanks are absent. Comparison blanks still coerce to zero.
|
| 565 |
-
stats = (f"SELECT _alq_id AS item_id, {numeric} AS v, "
|
| 566 |
-
f"row_number() OVER (ORDER BY {numeric} ASC, _alq_id ASC) AS a, "
|
| 567 |
-
f"row_number() OVER (ORDER BY {numeric} DESC, _alq_id ASC) AS d, "
|
| 568 |
-
f"count(*) OVER () AS n, avg({numeric}) OVER () AS mean "
|
| 569 |
-
f"FROM _alq_rank_domain WHERE {raw} IS NOT NULL")
|
| 570 |
-
if op in {"aboveAvg", "belowAvg"}:
|
| 571 |
-
predicate = f"v {'>' if op == 'aboveAvg' else '<'} mean"
|
| 572 |
-
else:
|
| 573 |
-
params.append(int(_number(node["value"])))
|
| 574 |
-
if op in {"topN", "bottomN"}:
|
| 575 |
-
predicate = f"{'d' if op == 'topN' else 'a'} <= %s"
|
| 576 |
-
elif op in {"inTopPct", "inBottomPct"}:
|
| 577 |
-
predicate = f"{'d' if op == 'inTopPct' else 'a'} <= CEIL(n * CAST(%s AS NUMERIC) / 100)"
|
| 578 |
-
else:
|
| 579 |
-
buckets = 4 if op == "inQuartile" else 10
|
| 580 |
-
predicate = f"n >= {buckets} AND FLOOR((a - 1) * {buckets} / CAST(n AS NUMERIC)) + 1 = %s"
|
| 581 |
-
ctes.append(f"{name} AS (SELECT item_id FROM ({stats}) AS r WHERE {predicate})")
|
| 582 |
-
predicate = _sql_tree(tree, fields, refs, params, today, ranks) or "TRUE"
|
| 583 |
-
ctes.append(f"_alq_filtered AS MATERIALIZED (SELECT * FROM _alq_source WHERE {predicate})")
|
| 584 |
-
terms = []
|
| 585 |
-
for rule in sorts:
|
| 586 |
-
raw, kind = refs[rule["colId"]], fields[rule["colId"]]["type"]
|
| 587 |
-
terms += [f"CASE WHEN {_sql_blank(raw)} THEN 1 ELSE 0 END ASC",
|
| 588 |
-
f"CASE WHEN {_sql_blank(raw)} THEN NULL ELSE {_sql_value(raw, kind)} END "
|
| 589 |
-
f"{rule['dir'].upper()}"]
|
| 590 |
-
if not sorts and default_order_sql:
|
| 591 |
-
terms.append("s._alq_default_n ASC")
|
| 592 |
-
terms.append("s._alq_id ASC")
|
| 593 |
-
if clamp_offset:
|
| 594 |
-
page_offset = ("CASE WHEN count(*) = 0 THEN 0 ELSE LEAST(CAST(%s AS BIGINT), "
|
| 595 |
-
"CAST(FLOOR((count(*) - 1) / CAST(%s AS NUMERIC)) AS BIGINT) * %s) END")
|
| 596 |
-
params.extend((offset, limit, limit))
|
| 597 |
-
else:
|
| 598 |
-
page_offset = "CAST(%s AS BIGINT)"
|
| 599 |
-
params.append(offset)
|
| 600 |
-
ctes += [f"_alq_bounds AS (SELECT count(*) AS total, {page_offset} AS page_offset FROM _alq_filtered)",
|
| 601 |
-
"_alq_ordered AS (SELECT s._alq_id AS item_id, row_number() OVER (ORDER BY "
|
| 602 |
-
+ ", ".join(terms) + ") AS row_number FROM _alq_filtered AS s)",
|
| 603 |
-
"_alq_page AS (SELECT item_id, row_number FROM _alq_ordered ORDER BY row_number "
|
| 604 |
-
"LIMIT %s OFFSET (SELECT page_offset FROM _alq_bounds))"]
|
| 605 |
-
params.append(limit)
|
| 606 |
-
return ListQuerySQL(",\n".join(ctes), tuple(params))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
web/src/apps/SkuListTools.tsx
DELETED
|
@@ -1,85 +0,0 @@
|
|
| 1 |
-
import { useMemo } from "react";
|
| 2 |
-
import { Funnel, SortAscending } from "@phosphor-icons/react";
|
| 3 |
-
import type { Field, FilterTree, SortSpec } from "../customer-grid/types";
|
| 4 |
-
import { FilterBuilderPanel, Popover, SortBuilder, countConditions } from "../filter-kit";
|
| 5 |
-
import type { FilterBuilderPanelProps } from "../filter-kit";
|
| 6 |
-
import "../styles/sdlc81/sku-list-tools.css";
|
| 7 |
-
|
| 8 |
-
export interface SkuListQuery {
|
| 9 |
-
filters: FilterTree;
|
| 10 |
-
sorts: SortSpec;
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
/** Use on query changes to preserve search and scope while restarting server paging. */
|
| 14 |
-
export function skuListQueryParams(query: SkuListQuery, base?: URLSearchParams): URLSearchParams {
|
| 15 |
-
const params = new URLSearchParams(base);
|
| 16 |
-
params.set("filter", JSON.stringify(query.filters));
|
| 17 |
-
params.set("sort", JSON.stringify(query.sorts));
|
| 18 |
-
params.delete("cursor");
|
| 19 |
-
params.set("offset", "0");
|
| 20 |
-
return params;
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
export interface SkuListToolsProps {
|
| 24 |
-
/** Authenticated column vocabulary. Never infer fields or choices from the loaded page. */
|
| 25 |
-
fields: Field[];
|
| 26 |
-
query: SkuListQuery;
|
| 27 |
-
/** Refetch the server list with the new query. Rows and paging remain the host's responsibility. */
|
| 28 |
-
onChange: (query: SkuListQuery) => void;
|
| 29 |
-
/** Full matching server count. Null means no successful answer yet. */
|
| 30 |
-
total: number | null;
|
| 31 |
-
loading?: boolean;
|
| 32 |
-
error?: boolean;
|
| 33 |
-
canRead?: boolean;
|
| 34 |
-
unavailable?: boolean;
|
| 35 |
-
onRetry?: () => void;
|
| 36 |
-
database?: string | null;
|
| 37 |
-
statusValues?: FilterBuilderPanelProps["statusValues"];
|
| 38 |
-
userOptions?: FilterBuilderPanelProps["userOptions"];
|
| 39 |
-
today?: string;
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
/** Grid controls for a server list. This component accepts no record collection. */
|
| 43 |
-
export function SkuListTools({
|
| 44 |
-
fields, query, onChange, total, loading = false, error = false, canRead = true,
|
| 45 |
-
unavailable = false, onRetry, database = null, statusValues, userOptions, today,
|
| 46 |
-
}: SkuListToolsProps) {
|
| 47 |
-
const fieldByKey = useMemo(() => new Map(fields.map((field) => [field.key, field])), [fields]);
|
| 48 |
-
const filtersReady = fields.length > 0;
|
| 49 |
-
const showCount = canRead && !unavailable && !loading && !error && total !== null;
|
| 50 |
-
const message = !canRead ? "You do not have access to this list"
|
| 51 |
-
: unavailable ? "Products are unavailable"
|
| 52 |
-
: loading ? "Loading products"
|
| 53 |
-
: error ? "Could not load products"
|
| 54 |
-
: !filtersReady ? "Filters are unavailable"
|
| 55 |
-
: total === 0 ? "No matches" : null;
|
| 56 |
-
|
| 57 |
-
return (
|
| 58 |
-
<div className="sku-list-tools" aria-busy={loading}>
|
| 59 |
-
<div className="sku-list-tools__bar" role="toolbar" aria-label="Product list tools">
|
| 60 |
-
{canRead && !unavailable && filtersReady && <>
|
| 61 |
-
<Popover label="Filter" icon={<Funnel size={16} />} active={countConditions(query.filters.nodes) > 0}
|
| 62 |
-
tone="filter" wide>
|
| 63 |
-
{() => <FilterBuilderPanel fields={fields} filters={query.filters}
|
| 64 |
-
onChange={(filters) => onChange({ ...query, filters })} database={database}
|
| 65 |
-
statusValues={statusValues} userOptions={userOptions} today={today} />}
|
| 66 |
-
</Popover>
|
| 67 |
-
<Popover label="Sort" icon={<SortAscending size={16} />} active={query.sorts.length > 0}
|
| 68 |
-
tone="sort" wide>
|
| 69 |
-
{() => <SortBuilder fields={fields} fieldByKey={fieldByKey} sorts={query.sorts}
|
| 70 |
-
onSorts={(sorts) => onChange({ ...query, sorts })} filters={query.filters}
|
| 71 |
-
onFilters={(filters) => onChange({ ...query, filters })} grouped={false} />}
|
| 72 |
-
</Popover>
|
| 73 |
-
</>}
|
| 74 |
-
<span className="sku-list-tools__count" role="status" aria-live="polite" aria-atomic="true">
|
| 75 |
-
{showCount ? `${total.toLocaleString()} ${total === 1 ? "product" : "products"}` : ""}
|
| 76 |
-
</span>
|
| 77 |
-
</div>
|
| 78 |
-
{message && <div className="sku-list-tools__message" role={error && canRead ? "alert" : "status"}>
|
| 79 |
-
<span>{message}</span>
|
| 80 |
-
{error && canRead && !unavailable && !loading && onRetry
|
| 81 |
-
&& <button type="button" className="sku-list-tools__retry" onClick={onRetry}>Retry</button>}
|
| 82 |
-
</div>}
|
| 83 |
-
</div>
|
| 84 |
-
);
|
| 85 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|