loopable / platform /harness /amazon_semantic.py
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
1ad1eea verified
Raw History Blame Contribute Delete
12.7 kB
"""Read-only Amazon count contract over the existing tenant mirror and Sales summary seam.
Only a fully authorized Administrator may use this unfiltered source summary. Member, field,
row, BU and related scopes require independent source evidence for that narrower population.
No sync, restore, credential read or live API call belongs in this path.
"""
from contextlib import contextmanager
from datetime import date, datetime, time, timedelta, timezone
import json
from pathlib import Path
import re
from harness import amazon_store as AZ, datastore as DS
def _fail(message):
from harness.semantic import ModelError
raise ModelError(message)
def _bounds(date_from, date_to):
values = []
for value in (date_from, date_to):
if not isinstance(value, str) or not re.fullmatch(r'\d{4}-\d{2}-\d{2}', value):
_fail('Amazon order count requires both UTC dates in YYYY-MM-DD format')
try:
values.append(date.fromisoformat(value))
except ValueError:
_fail('Amazon order count requires valid UTC dates')
if values[0] > values[1]:
_fail('Amazon order count start date must precede its end date')
try:
return tuple(datetime.combine(value, time(), timezone.utc)
for value in (values[0], values[1] + timedelta(days=1)))
except OverflowError:
_fail('Amazon order-count end date exceeds the supported UTC date range')
def _unrestricted_bu(user):
from core import perms, users
raw = user.get('bus', 'all')
if raw == 'all':
return 'All' in perms.allowed_bu_labels(user)
# The canonical boundary consumes BU ids. Session projections can carry labels instead.
# Reject empty/unknown inputs before its legacy fallback can turn them into all BUs.
if not isinstance(raw, (list, tuple)) or not raw:
return False
ids = []
by_label = {label: key for key, label in users.BU_LABELS.items()}
for value in raw:
if type(value) is int and value in users.BU_LABELS:
ids.append(value)
elif isinstance(value, str) and value in by_label:
ids.append(by_label[value])
elif value != 'All':
return False
# 'All' is a selector label, not a substitute for the concrete granted population.
ids = sorted(set(ids))
return bool(ids) and 'All' in perms.allowed_bu_labels(dict(user, bus=ids))
@contextmanager
def _cursor(runtime, user):
from core import perm_scope, perms, route_system, user_tables
try:
if runtime is None or not getattr(runtime, 'key', None):
_fail('Amazon order count requires the requesting tenant runtime')
if not isinstance(user, dict) or not user.get('username') or not perms.is_admin(user):
_fail('Amazon source-summary counts require an unrestricted Administrator caller')
if user.get('tenant') != runtime.key:
_fail('Amazon order count requires a caller bound to the requesting tenant')
if not _unrestricted_bu(user) or perms.scope_agent(user) is not None:
_fail('Amazon source summary cannot validate a business-unit or agent-restricted caller')
# Use the native ut_* reader's rows-free, runtime-bound definition wall. Registry
# topics need assistant_entry; user tables may have no entry at all. Lend only this
# read pass so a cached result cannot retain yesterday's definition or recipient wall.
definitions = runtime.get_projection('user_tables', drop=('rows',))
definition = definitions.get('ut_amzn_orders') if isinstance(definitions, dict) else None
if not isinstance(definition, dict) or not definition:
_fail('Amazon orders database has no readable table definition')
lent = user_tables.lend(runtime, user_tables=definitions)
grant = perm_scope.entry(user, 'ut_amzn_orders')
# The core admin bypass must not discard a stored untick or narrowing: an unfiltered
# Sales summary supplies no independent evidence for a field/row-restricted caller.
if grant is not None and (not grant.get('access', True) or grant.get('filter')
or grant.get('hiddenFields')):
_fail('Amazon order count requires unrestricted stored database permissions')
if (not perm_scope.may_read(user, 'ut_amzn_orders', st=lent)
or not perm_scope.nav_may_open(user, 'ut_amzn_orders', st=lent)
or route_system.is_managed(definition) or route_system.is_managed('ut_amzn_orders')):
_fail('Amazon orders database access was refused')
if perm_scope.row_scope_applies(user, 'ut_amzn_orders', st=lent, defn=definition):
_fail('Amazon source summary cannot validate a recipient or row-restricted population')
fields = perm_scope.scoped_fields(user, 'ut_amzn_orders', st=lent)
keys = {f['key'] for f in fields}
declared = {f['key'] for f in definition.get('fields', ())}
if (keys != declared
or not {'amazon_order_id', 'purchase_date', 'order_status'} <= keys):
_fail('Amazon order count dependencies are unavailable to this caller')
runtime.assert_datastore_matches()
# The general runtime guard allows bespoke fixture pins. This source proof needs EXACT
# runtime/file identity, even for an otherwise unowned file. Never infer tenant zero.
path = Path(runtime.datastore_path).resolve()
if path != Path(DS.DB_PATH).resolve() or not path.is_file():
_fail('Amazon order count requires this tenant runtime\'s existing mirror file')
# Amazon readiness is its own completed markers, not Odoo's global backfill gate.
con = DS.ro_cursor()
except Exception as exc:
from harness.semantic import ModelError
if isinstance(exc, ModelError):
raise
_fail(f'Amazon tenant source or permission proof is unavailable ({type(exc).__name__})')
try:
con.execute('BEGIN TRANSACTION')
yield con
finally:
con.close()
def _evidence(con, bounds):
orders = AZ._read_state(con, AZ.entity_key('orders'))
summary = AZ._read_state(con, AZ.entity_key('reconcile_order_metrics'))
if not orders or orders['state'] != 'done':
_fail('Amazon orders have no completed mirror sync')
if not summary or summary['state'] != 'done' or not summary.get('detail'):
_fail('Amazon Sales source summary is unavailable or refused')
try:
if not con.execute('SELECT count(*) FROM amzn_orders').fetchone()[0]:
_fail('Amazon orders mirror is empty; source validation remains unproven')
malformed = con.execute(
"SELECT count(*) FROM amzn_orders WHERE NULLIF(trim(amazon_order_id), '') IS NULL "
"OR TRY_CAST(purchase_date AS TIMESTAMPTZ) IS NULL").fetchone()[0]
if malformed:
_fail('Amazon orders lack valid ids or UTC purchase dates')
conflicting = con.execute(
'SELECT count(*) FROM (SELECT amazon_order_id FROM amzn_orders '
'GROUP BY amazon_order_id HAVING count(DISTINCT CAST(purchase_date AS TIMESTAMPTZ)) > 1 '
"OR count(DISTINCT lower(COALESCE(order_status, ''))) > 1)").fetchone()[0]
if conflicting:
_fail('Amazon order heads have conflicting dates or statuses; sync the deduplicated source')
marker = json.loads(summary['detail'])
if (type(marker.get('count')) is not int or marker['count'] <= 0
or marker.get('column') != '_period_start' or marker.get('range') is not True):
_fail('Amazon Sales source summary has no completed interval evidence')
saved_bounds = tuple(datetime.fromisoformat(marker[k].replace('Z', '+00:00'))
for k in ('start', 'end'))
if any(v.tzinfo is None for v in saved_bounds) or saved_bounds != bounds:
_fail('Requested UTC dates do not match the completed Amazon Sales source summary')
expected = marker['metrics']['orders']
if type(expected) is not int or expected < 0:
_fail('Amazon Sales source summary has no valid orderCount')
except Exception as exc:
from harness.semantic import ModelError
if isinstance(exc, ModelError):
raise
_fail(f'Amazon orders or source summary cannot be read ({type(exc).__name__})')
# Reuse the connector's independent summary-versus-detail seam and cancellation predicate.
evidence = AZ._validate_orders(con)
if evidence.get('state') not in ('ok', 'refused') or 'actual' not in evidence:
_fail('Amazon Sales order-count reconciliation remains unproven')
return evidence['actual'], expected
def validate(date_from, date_to, team_id=None, *, runtime=None, user=None):
if team_id is not None:
_fail('Amazon Sales source summary has no business-unit scope')
bounds = _bounds(date_from, date_to)
with _cursor(runtime, user) as con:
return _evidence(con, bounds)
def existing_summary_window(*, runtime=None, user=None):
"""Read this authorized caller's existing summary interval; never fetch or create it."""
with _cursor(runtime, user) as con:
saved = AZ._read_state(con, AZ.entity_key('reconcile_order_metrics'))
if not saved or saved['state'] != 'done' or not saved.get('detail'):
_fail('Amazon Sales source summary is unavailable or refused')
try:
marker = json.loads(saved['detail'])
start, end = (datetime.fromisoformat(marker[k].replace('Z', '+00:00'))
for k in ('start', 'end'))
if start.tzinfo is None or end.tzinfo is None:
_fail('Amazon Sales source summary requires a UTC interval')
start, end = (v.astimezone(timezone.utc) for v in (start, end))
date_from, date_to = start.date().isoformat(), (end.date() - timedelta(days=1)).isoformat()
_evidence(con, _bounds(date_from, date_to))
return date_from, date_to
except Exception as exc:
from harness.semantic import ModelError
if isinstance(exc, ModelError):
raise
_fail(f'Amazon Sales source interval is unavailable ({type(exc).__name__})')
def query(date_from, date_to, *, runtime=None, user=None, group_by=None, grain=None,
sort=None, limit=1000):
bounds = _bounds(date_from, date_to)
gb = list(group_by or [])
if gb not in ([], ['purchase_date']) or (gb and grain):
_fail('Amazon order count supports UTC purchase date or one time grain')
if grain not in (None, 'day', 'week', 'month'):
_fail('Amazon order-count grain must be day, week or month')
period = ('CAST(CAST(purchase_date AS TIMESTAMPTZ) AT TIME ZONE \'UTC\' AS DATE)'
if gb else f"date_trunc('{grain}', CAST(purchase_date AS TIMESTAMPTZ) AT TIME ZONE 'UTC')"
if grain else None)
column = 'purchase_date' if gb else 'period'
selected = f'{period} AS {column}, ' if period else ''
sql = (f'SELECT {selected}COUNT(DISTINCT amazon_order_id) AS amazon_order_count '
'FROM amzn_orders WHERE CAST(purchase_date AS TIMESTAMPTZ) >= CAST(? AS TIMESTAMPTZ) '
'AND CAST(purchase_date AS TIMESTAMPTZ) < CAST(? AS TIMESTAMPTZ) '
"AND lower(COALESCE(order_status, '')) NOT IN ('cancelled', 'canceled')")
if period:
sql += f' GROUP BY {period}'
if sort:
if sort.lstrip('-') not in (['amazon_order_count', column] if period else ['amazon_order_count']):
_fail('Amazon order-count sort must name a selected measure or date')
sql += f" ORDER BY {sort.lstrip('-')} {'DESC' if sort.startswith('-') else 'ASC'}"
elif period:
sql += f' ORDER BY {column}'
from harness.semantic import MAX_GROUPS
limit = max(1, min(int(limit or 1000), MAX_GROUPS if period else 5000))
with _cursor(runtime, user) as con:
ours, expected = _evidence(con, bounds)
if ours != expected:
_fail(f'Amazon Sales summary counted {expected} orders; mirror counted {ours}')
cur = con.execute(sql + f' LIMIT {limit + 1}', [v.isoformat() for v in bounds])
names = [d[0] for d in cur.description]
rows = [dict(zip(names, row)) for row in cur.fetchall()]
for row in rows:
if period:
row[column] = str(row[column])[:10]
return {'topic': 'amazon_orders', 'rows': rows[:limit], 'row_count': min(len(rows), limit),
'truncated': len(rows) > limit, 'sql': sql, 'measures': ['amazon_order_count'],
'group_by': gb, 'grain': grain}