velocity-python 0.2.6__py3-none-any.whl → 0.2.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
velocity/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = version = "0.2.6"
1
+ __version__ = version = "0.2.7"
2
2
 
3
3
  import importlib as _importlib
4
4
 
velocity/aws/failures.py CHANGED
@@ -61,6 +61,13 @@ logger = logging.getLogger(__name__)
61
61
  EVENT_TYPE = "velocity.failure"
62
62
  EVENT_VERSION = 1
63
63
  TOPIC_CONFIG_KEY = "FailureTopicArn"
64
+ # Where a person reads failures (a failure inbox screen); the email links it.
65
+ INBOX_CONFIG_KEY = "FailureInboxUrl"
66
+ # The site's IANA zone; the email shows when it happened there.
67
+ TIMEZONE_CONFIG_KEY = "SiteTimezone"
68
+ # SNS caps an email subject at 100 characters of plain ASCII.
69
+ EMAIL_SUBJECT_LIMIT = 100
70
+ SUMMARY_LIMIT = 300
64
71
  SOURCES = frozenset({"queue", "scheduler", "web", "webhook", "job"})
65
72
 
66
73
  MESSAGE_LIMIT = 1000
@@ -541,10 +548,17 @@ def build_event(
541
548
  "log_group": f"/aws/lambda/{function}" if function else None,
542
549
  "log_stream": os.environ.get("AWS_LAMBDA_LOG_STREAM_NAME") or None,
543
550
  "classification": None,
551
+ # for people (render_text / the alert email): where it failed, a
552
+ # one-line summary and what to do, from the exception's `summary` /
553
+ # `hint` attributes or `extra`
554
+ "where": frame or None,
555
+ "summary": _person_text(exc, extra, "summary", keys),
556
+ "hint": _person_text(exc, extra, "hint", keys),
544
557
  }
545
558
  if extra:
546
559
  event["extra"] = payload_excerpt(
547
- {k: v for k, v in extra.items() if k != "message"}, keys
560
+ {k: v for k, v in extra.items() if k not in ("message", "summary", "hint")},
561
+ keys,
548
562
  )
549
563
  event["classification"] = _run_classify(
550
564
  classify if classify is not None else _classify, exc, event
@@ -552,55 +566,162 @@ def build_event(
552
566
  return event
553
567
 
554
568
 
569
+ def _person_text(exc, extra, name, keys):
570
+ """An exception's ``summary``/``hint`` attribute, else ``extra[name]``;
571
+ scrubbed and trimmed, or None."""
572
+ value = getattr(exc, name, None) if exc is not None else None
573
+ if not value and extra:
574
+ value = extra.get(name)
575
+ if not value:
576
+ return None
577
+ return _truncate(scrub_text(" ".join(str(value).split()), keys), SUMMARY_LIMIT)
578
+
579
+
580
+ def _local_time(occurred_at: Optional[str]) -> str:
581
+ """``Oct 20, 2026, 11:18 AM MDT`` in the site's zone (``SiteTimezone``),
582
+ else UTC; the raw value when it is not a timestamp."""
583
+ if not occurred_at:
584
+ return ""
585
+ try:
586
+ when = datetime.datetime.fromisoformat(str(occurred_at).replace("Z", "+00:00"))
587
+ except ValueError:
588
+ return str(occurred_at)
589
+ zone_name = None
590
+ try:
591
+ zone_name = _config(TIMEZONE_CONFIG_KEY)
592
+ except Exception:
593
+ zone_name = None
594
+ if zone_name:
595
+ try:
596
+ from zoneinfo import ZoneInfo
597
+
598
+ when = when.astimezone(ZoneInfo(str(zone_name)))
599
+ except Exception:
600
+ when = when.astimezone(datetime.timezone.utc)
601
+ hour = when.strftime("%I").lstrip("0") or "12"
602
+ return f"{when.strftime('%b')} {when.day}, {when.year}, {hour}:{when.strftime('%M %p %Z')}"
603
+
604
+
605
+ def _cloudwatch_escape(value: str) -> str:
606
+ from urllib.parse import quote
607
+
608
+ return quote(quote(value, safe=""), safe="").replace("%", "$")
609
+
610
+
611
+ def log_link(event: Optional[Dict[str, Any]]) -> str:
612
+ """The CloudWatch console page of the event's log stream, or ""."""
613
+ if not event or not event.get("log_group"):
614
+ return ""
615
+ region = os.environ.get("AWS_REGION") # set in every Lambda
616
+ if not region:
617
+ return ""
618
+ url = (
619
+ f"https://{region}.console.aws.amazon.com/cloudwatch/home?region={region}"
620
+ f"#logsV2:log-groups/log-group/{_cloudwatch_escape(event['log_group'])}"
621
+ )
622
+ if event.get("log_stream"):
623
+ url += f"/log-events/{_cloudwatch_escape(event['log_stream'])}"
624
+ return url
625
+
626
+
627
+ def _handled(event: Dict[str, Any]) -> Optional[bool]:
628
+ extra = event.get("extra")
629
+ if isinstance(extra, dict) and "handled" in extra:
630
+ return bool(extra["handled"])
631
+ return None
632
+
633
+
634
+ _SOURCE_WORDS = {
635
+ "job": "a background job",
636
+ "queue": "a queued message",
637
+ "scheduler": "a scheduled task",
638
+ "request": "a request",
639
+ }
640
+
641
+
555
642
  def render_text(event: Optional[Dict[str, Any]]) -> str:
556
- """A plain-text rendering of an (already scrubbed) event, for email or
557
- chat fallbacks. Contains nothing the event does not."""
643
+ """The failure for a person (the alert email, fallbacks): what failed,
644
+ the error in words, what to do, when (site-local time), whether it was
645
+ handled, where to look. Nothing beyond the event, the configured inbox
646
+ link and a log link."""
558
647
  if not event:
559
648
  return ""
560
- lines = [
561
- f"{event.get('site') or '?'} {event.get('env') or '?'} "
562
- f"{event.get('source') or '?'} failure"
563
- + (f" [{event['classification']}]" if event.get("classification") else ""),
564
- "",
565
- ]
566
- for key in (
567
- "function",
568
- "action",
569
- "job_id",
570
- "request_id",
571
- "message_id",
572
- "receive_count",
573
- "exception_type",
574
- "message",
575
- "occurred_at",
576
- "fingerprint",
577
- "log_group",
578
- "log_stream",
649
+ site = event.get("site") or "?"
650
+ env = event.get("env") or "?"
651
+ exc_name = (event.get("exception_type") or "").rsplit(".", 1)[-1]
652
+ source = _SOURCE_WORDS.get(event.get("source"), event.get("source") or "something")
653
+ lines = [f"{site} ({env}): {source} failed", ""]
654
+
655
+ what = event.get("action") or event.get("source") or "?"
656
+ extra = event.get("extra") if isinstance(event.get("extra"), dict) else {}
657
+ details = [str(extra[k]) for k in ("capability",) if extra.get(k)]
658
+ subject = extra.get("subject")
659
+ lines.append(
660
+ f"What: {what}"
661
+ + (f" ({', '.join(details)})" if details else "")
662
+ + (f", for {subject}" if subject else "")
663
+ )
664
+ error = event.get("summary") or event.get("message") or exc_name or "?"
665
+ lines.append(f"Error: {error}" + (f" ({exc_name})" if exc_name else ""))
666
+ if event.get("hint"):
667
+ lines.append(f"To do: {event['hint']}")
668
+ if event.get("occurred_at"):
669
+ lines.append(f"When: {_local_time(event['occurred_at'])}")
670
+ handled = _handled(event)
671
+ if handled is True:
672
+ lines.append(
673
+ "Handled: yes; it was recorded and the work is tried again on its next run"
674
+ )
675
+ elif handled is False:
676
+ lines.append("Handled: no; the function stopped on it")
677
+ if event.get("classification"):
678
+ lines.append(f"Class: {event['classification']}")
679
+ if event.get("receive_count"):
680
+ lines.append(f"Attempt: {event['receive_count']}")
681
+ lines.append("")
682
+ if event.get("function"):
683
+ lines.append(f"Function: {event['function']}")
684
+ link = log_link(event)
685
+ if link:
686
+ lines.append(f"Log: {link}")
687
+ inbox = None
688
+ try:
689
+ inbox = _config(INBOX_CONFIG_KEY)
690
+ except Exception:
691
+ inbox = None
692
+ if inbox:
693
+ lines.append(f"Inbox: {inbox}")
694
+ lines.append("")
695
+ if event.get("where"):
696
+ lines.append(f"Where it failed: {event['where']}")
697
+ if (
698
+ event.get("summary")
699
+ and event.get("message")
700
+ and event["message"] != event["summary"]
579
701
  ):
580
- value = event.get(key)
581
- if value not in (None, ""):
582
- lines.append(f"{key}: {value}")
583
- for key in ("payload_excerpt", "extra"):
584
- if event.get(key):
585
- lines.append(f"{key}: {_render(event[key])}")
586
- if event.get("traceback"):
587
- lines.extend(["", "Traceback (scrubbed):", event["traceback"]])
702
+ lines.append(f"Raw message: {event['message']}")
703
+ if event.get("fingerprint"):
704
+ lines.append(f"Reference: {str(event['fingerprint'])[:12]}")
705
+ while lines and not lines[-1]:
706
+ lines.pop()
588
707
  return "\n".join(lines)
589
708
 
590
709
 
591
710
  def subject_line(event: Optional[Dict[str, Any]]) -> str:
592
- """A one-line summary (≤ 200 chars) for an alert subject."""
711
+ """One line for an alert subject: site, env, what failed and the error in
712
+ words. Plain ASCII, at most 100 characters (SNS's email subject rules)."""
593
713
  if not event:
594
714
  return "Failure"
595
715
  exc_name = (event.get("exception_type") or "Failure").rsplit(".", 1)[-1]
596
- parts = [
597
- f"{(event.get('env') or '?').title()} {event.get('site') or ''}".strip(),
598
- event.get("source") or "",
599
- event.get("action") or "",
600
- ]
601
- label = f" [{event['classification']}]" if event.get("classification") else ""
602
- head = " ".join(part for part in parts if part)
603
- return _truncate(f"{head}: {exc_name}{label}", 200)
716
+ what = event.get("action") or event.get("source") or "something"
717
+ error = event.get("summary") or event.get("message") or exc_name
718
+ text = (
719
+ f"{event.get('site') or '?'} {event.get('env') or '?'}: {what} failed: {error}"
720
+ )
721
+ text = " ".join(text.split()).encode("ascii", "ignore").decode("ascii")
722
+ if len(text) > EMAIL_SUBJECT_LIMIT:
723
+ text = text[: EMAIL_SUBJECT_LIMIT - 3].rstrip() + "..."
724
+ return text
604
725
 
605
726
 
606
727
  def _run_fallback(event: Dict[str, Any]) -> None:
@@ -701,13 +822,7 @@ def report(
701
822
  return None
702
823
 
703
824
  try:
704
- _client().publish(
705
- TopicArn=topic_arn,
706
- Message=_dumps(event),
707
- MessageAttributes={
708
- "velocity_event": {"DataType": "String", "StringValue": "failure"}
709
- },
710
- )
825
+ _client().publish(**publish_request(topic_arn, event))
711
826
  except Exception:
712
827
  logger.exception(
713
828
  "failures: publish to %s failed (fingerprint %s)",
@@ -718,6 +833,43 @@ def report(
718
833
  return event
719
834
 
720
835
 
836
+ # SNS refuses a message over 256 KB; the email text is dropped to a short
837
+ # form well before that (the inbox keeps the whole event).
838
+ _SNS_MESSAGE_BUDGET = 240 * 1024
839
+
840
+
841
+ def publish_request(topic_arn: str, event: Dict[str, Any]) -> Dict[str, Any]:
842
+ """The ``sns.publish`` arguments for one event. Subscribers each get their
843
+ own form (``MessageStructure="json"``): a Lambda (the failure inbox) and
844
+ every other protocol the event's JSON, unchanged; an ``email``
845
+ subscription the text a person reads (:func:`render_text`) under a
846
+ readable subject. The ``handled`` attribute ("true"/"false"/"unknown")
847
+ lets a subscription filter: the person's backstop email takes only
848
+ failures that were not handled."""
849
+ machine = _dumps(event)
850
+ person = render_text(event)
851
+ message = json.dumps({"default": machine, "email": person})
852
+ if len(message.encode("utf-8")) > _SNS_MESSAGE_BUDGET:
853
+ person = person[:4000] + "\n\n(shortened; the failure inbox has the rest)"
854
+ message = json.dumps({"default": machine, "email": person})
855
+ handled = _handled(event)
856
+ return {
857
+ "TopicArn": topic_arn,
858
+ "Message": message,
859
+ "MessageStructure": "json",
860
+ "Subject": subject_line(event),
861
+ "MessageAttributes": {
862
+ "velocity_event": {"DataType": "String", "StringValue": "failure"},
863
+ "handled": {
864
+ "DataType": "String",
865
+ "StringValue": (
866
+ "unknown" if handled is None else ("true" if handled else "false")
867
+ ),
868
+ },
869
+ },
870
+ }
871
+
872
+
721
873
  def is_enabled() -> bool:
722
874
  """Whether ``FailureTopicArn`` resolves (events will be published)."""
723
875
  return bool(_config(TOPIC_CONFIG_KEY))
@@ -27,7 +27,22 @@ from velocity.db.core.lazy_engine import LazyEngine
27
27
  engine = LazyEngine(velocity.db.postgres.initialize)
28
28
 
29
29
 
30
- cognito_client = boto3.client("cognito-idp")
30
+ # The Cognito client is built on first use, not at import: creating it loads
31
+ # Cognito's whole API model and resolves its endpoint (~13 MB and ~2 s on a
32
+ # 128 MB Lambda), which every function importing a handler paid — a
33
+ # Scheduler or queue worker that never signs anyone in included. Tests may
34
+ # replace this attribute; whatever it holds is what `_cognito()` returns.
35
+ cognito_client = None
36
+
37
+
38
+ def _cognito():
39
+ """The shared Cognito client, created on first use."""
40
+ global cognito_client
41
+ if cognito_client is None:
42
+ cognito_client = boto3.client("cognito-idp")
43
+ return cognito_client
44
+
45
+
31
46
  logger = get_logger("velocity.aws.handlers.context")
32
47
 
33
48
  # Cognito User Pool id shape, e.g. "us-east-1_aBcD1234". Used to distinguish a
@@ -1036,18 +1051,19 @@ class Context:
1036
1051
  # one Cognito round trip per user per UserCacheSeconds per container
1037
1052
  # (velocity.cache); a lookup that fails is never cached
1038
1053
  def resolve():
1054
+ client = _cognito()
1039
1055
  try:
1040
1056
  self.perf.start("cognito admin_get_user")
1041
- response = cognito_client.admin_get_user(
1057
+ response = client.admin_get_user(
1042
1058
  UserPoolId=user_pool_id,
1043
1059
  Username=user_sub,
1044
1060
  )
1045
1061
  self.perf.log("cognito admin_get_user")
1046
1062
  resolved_username = response.get("Username")
1047
- except cognito_client.exceptions.UserNotFoundException:
1063
+ except client.exceptions.UserNotFoundException:
1048
1064
  try:
1049
1065
  self.perf.start("cognito list_users")
1050
- list_response = cognito_client.list_users(
1066
+ list_response = client.list_users(
1051
1067
  UserPoolId=user_pool_id,
1052
1068
  Filter=f'sub = "{user_sub}"',
1053
1069
  Limit=1,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: velocity-python
3
- Version: 0.2.6
3
+ Version: 0.2.7
4
4
  Summary: A rapid application development library for interfacing with data storage
5
5
  Author-email: Velocity Team <info@codeclubs.org>
6
6
  License-Expression: MIT
@@ -1,4 +1,4 @@
1
- velocity/__init__.py,sha256=zI7efmr4RDHUvu7Mw3sLtHToFQqy55c8QIuqnJD8ZRo,362
1
+ velocity/__init__.py,sha256=yFS8i5a6vkF9fvBhXU1pEodyDqxw4rkW3ZYGgOHV8g4,362
2
2
  velocity/cache.py,sha256=pthvw7gDVEXRzkTcQtA5YVf4Q1IxAamWsba4UReT0RE,8933
3
3
  velocity/logging.py,sha256=MAsZap1cDSdOBNRc_9ezQxup5dHOS0ARxU9NI_-UK3E,4037
4
4
  velocity/aws/__init__.py,sha256=XJJ79gnPlhg3aJij713PB8-vjQOt-BkqxYeGCBOxAu8,3460
@@ -8,7 +8,7 @@ velocity/aws/blobs.py,sha256=2TlTZ3KPxBdXDJ99aVSgDLR11wPQ8O3BbtNbY8mFQ28,9691
8
8
  velocity/aws/dirty_pipeline.py,sha256=DQHsCPQPWwiy_E7nxX8kO3DgvQTFkVDaDOjlBcmblyE,3331
9
9
  velocity/aws/email.py,sha256=CAvXTAUxJqMaT8ylpRwRcVEA4YLFMUuHCrwYLVE4G1E,6243
10
10
  velocity/aws/facets.py,sha256=vAXbp2PSjCTUzFpI84iUHvQoPXD1N-DJ6qIz9UhitFo,22574
11
- velocity/aws/failures.py,sha256=9H0yvo-mH2bfNytYAvDoocsOSjjGfDrRwwcAoB-SPrI,25172
11
+ velocity/aws/failures.py,sha256=8rXWAikJZTVv7bybmcXI5geR6F_dfMw6ns-icuj9EN0,31378
12
12
  velocity/aws/feature_flags.py,sha256=G2S1Ifjjex1QEmaAnAwRISyawxfGTdzaKUh3150M5aw,9604
13
13
  velocity/aws/identity.py,sha256=u6f_oaVhDa3tNatsQkeXZPJDhMfXCzeDo1zGoTqW_Yo,5547
14
14
  velocity/aws/job_lifecycle.py,sha256=6ltzdrkSjU_pY0eU6UaGbGh_1X5RhE5F4hypEH6MoRA,2735
@@ -48,7 +48,7 @@ velocity/aws/deploy/reconcile.py,sha256=3xZXXiiVeCw4nUV8lKyk1XCgRbpNCLVYXIXbOLXe
48
48
  velocity/aws/deploy/targets.py,sha256=vmZzGbLDKNtWkwd2ado_ZhT8AT6nXYbJD5nIgDiBzRc,6632
49
49
  velocity/aws/handlers/__init__.py,sha256=0yvVADxMHK8JOhgGRxMIz1XkNGlUk08AOmdR4qhNP2U,906
50
50
  velocity/aws/handlers/base_handler.py,sha256=xa1H5lS3CCxuM5_LXva2bmvWxUzxARIqkjnkp7bux3s,19143
51
- velocity/aws/handlers/context.py,sha256=7tfhzhidxWEJExVpu5Uz_BWbGlC7Xj0n8sqyr67f0jI,47135
51
+ velocity/aws/handlers/context.py,sha256=V6HOhVanplSibhmfNO_QgUQXkaQer3fLYfs2fDdl0zg,47707
52
52
  velocity/aws/handlers/context_factory.py,sha256=8rhYKrylzwQxBFqBckmF7jgWGZhfomLOT_Yu404CtJQ,784
53
53
  velocity/aws/handlers/exceptions.py,sha256=vChotaBjpPhvb_g058fGTnOG-vH1ZDMmHVwlj980DEg,2731
54
54
  velocity/aws/handlers/lambda_handler.py,sha256=WfB_tBoLZsQREUR3ZS4pkLLwevqozjJsA_yfHBrcc5Y,21358
@@ -223,9 +223,9 @@ velocity/payment/stripe_adapter.py,sha256=6EmVBZZchg2s3Teya0eQGA_8pvSK_teizr036U
223
223
  velocity/payment/stripe_mirror.py,sha256=6GKJk_PjAgySqFEuxbcuV423OfuGXupVTiGu9QwS5W8,12320
224
224
  velocity/testing/__init__.py,sha256=WtYMDHgCLxOq6mlemcOYV5Y9HqF_2QQgSfuAjFRg69Q,731
225
225
  velocity/testing/fake_db.py,sha256=phgdulk5owf3noUDpEfI7yFsVcKxRGZ82f2BJPYnaiU,53226
226
- velocity_python-0.2.6.dist-info/licenses/LICENSE,sha256=aoN245GG8s9oRUU89KNiGTU4_4OtnNmVi4hQeChg6rM,1076
227
- velocity_python-0.2.6.dist-info/METADATA,sha256=un8yKry0URcUzfnJhcmA7PolBjxeQvmk2WH7Ag0KHo0,17782
228
- velocity_python-0.2.6.dist-info/WHEEL,sha256=a-udL2m4V_6Yn8b-uKzWb12RjRxwwMo6ohYFM24tpaY,91
229
- velocity_python-0.2.6.dist-info/entry_points.txt,sha256=nXUKJHkOGQCSPoagdmMNd_qycrle1LVU-GhYknLGM1M,56
230
- velocity_python-0.2.6.dist-info/top_level.txt,sha256=JW2vJPmodgdgSz7H6yoZvnxF8S3fTMIv-YJWCT1sNW0,9
231
- velocity_python-0.2.6.dist-info/RECORD,,
226
+ velocity_python-0.2.7.dist-info/licenses/LICENSE,sha256=aoN245GG8s9oRUU89KNiGTU4_4OtnNmVi4hQeChg6rM,1076
227
+ velocity_python-0.2.7.dist-info/METADATA,sha256=jAHTQgrhvkAti0u-f01xNGRUki6kHB7euSsM3l-iPBY,17782
228
+ velocity_python-0.2.7.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
229
+ velocity_python-0.2.7.dist-info/entry_points.txt,sha256=nXUKJHkOGQCSPoagdmMNd_qycrle1LVU-GhYknLGM1M,56
230
+ velocity_python-0.2.7.dist-info/top_level.txt,sha256=JW2vJPmodgdgSz7H6yoZvnxF8S3fTMIv-YJWCT1sNW0,9
231
+ velocity_python-0.2.7.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (84.2.0)
2
+ Generator: setuptools (84.2.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5