Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 10:29:04

0001 #!/usr/bin/env python3
0002 """Post a production finding or its resolution to epicprod-live.
0003 
0004 Writes one ``production_finding`` record to the epicprod action stream
0005 (importance high, live), which the epicprod-live subscription publishes and the
0006 room's screen and the assessments read (docs/NOTICE_ROUTING.md, Findings and
0007 resolutions). A resolution names the record it resolves with --resolves. Title
0008 and detail are brief: what happened and to what in under ten words, then one
0009 or two short sentences; evidence goes in --evidence links and record ids.
0010 
0011     source ~/.env
0012     python scripts/post-finding.py --title "Harvester's 421 vanished OSG pilots cleared" \\
0013         --detail "..." --subject-type panda_queue --subject-key BNL_OSG_PanDA_1 \\
0014         --resolves 7656686 --evidence https://... [--dry-run]
0015 
0016 Environment: SWF_MONITOR_HTTP_URL (the monitor base URL) and SWF_API_TOKEN.
0017 """
0018 
0019 import argparse
0020 import json
0021 import os
0022 import sys
0023 import urllib.error
0024 import urllib.request
0025 from datetime import datetime, timezone
0026 
0027 
0028 def main():
0029     parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
0030     parser.add_argument('--title', required=True, help='what happened and to what, under ten words')
0031     parser.add_argument('--detail', required=True, help='one or two short sentences')
0032     parser.add_argument('--subject-type', default='', help='e.g. panda_queue, panda_task')
0033     parser.add_argument('--subject-key', default='', help='e.g. the queue name or JEDI task id')
0034     parser.add_argument('--resolves', type=int, help='the log record id of the finding this resolves')
0035     parser.add_argument('--evidence', nargs='*', default=[], help='evidence URLs')
0036     parser.add_argument('--by', default=os.environ.get('USER', ''), help='who reports (default: $USER)')
0037     parser.add_argument('--dry-run', action='store_true', help='print the record, post nothing')
0038     args = parser.parse_args()
0039     if len(args.title.split()) >= 10:
0040         sys.exit('The title must be under ten words (NOTICE_ROUTING.md, Findings and resolutions)')
0041 
0042     extra = {
0043         'action': 'production_finding', 'outcome': 'ok', 'sublevel': 'high', 'live_default': True,
0044         'username': args.by, 'title': args.title, 'summary': args.detail,
0045         'subject_type': args.subject_type, 'subject_key': args.subject_key,
0046     }
0047     if args.resolves:
0048         extra['resolves'] = args.resolves
0049     if args.evidence:
0050         extra['evidence'] = args.evidence
0051     body = {
0052         'app_name': 'epicprod', 'instance_name': 'post-finding',
0053         'timestamp': datetime.now(timezone.utc).isoformat(),
0054         'level': 20, 'levelname': 'INFO', 'message': args.title,
0055         'module': 'post_finding', 'funcname': 'main', 'lineno': 0,
0056         'process': os.getpid(), 'thread': 0, 'extra_data': extra,
0057     }
0058     if args.dry_run:
0059         print(json.dumps({'dry_run': True, 'record': body}))
0060         return
0061 
0062     base = os.environ.get('SWF_MONITOR_HTTP_URL', '').rstrip('/')
0063     token = os.environ.get('SWF_API_TOKEN', '')
0064     if not base or not token:
0065         sys.exit('SWF_MONITOR_HTTP_URL and SWF_API_TOKEN must be set (source ~/.env)')
0066     request = urllib.request.Request(
0067         f'{base}/api/logs/', data=json.dumps(body).encode(), method='POST',
0068         headers={'Content-Type': 'application/json', 'Authorization': f'Token {token}'})
0069     try:
0070         with urllib.request.urlopen(request, timeout=30) as response:
0071             record = json.loads(response.read() or b'{}')
0072     except urllib.error.HTTPError as exc:
0073         sys.exit(f'HTTP {exc.code} from {base}/api/logs/: {exc.read()[:500].decode(errors="replace")}')
0074     except urllib.error.URLError as exc:
0075         sys.exit(f'{base}/api/logs/ unreachable: {exc.reason}')
0076     print(json.dumps({'record': record.get('id'), 'title': args.title}))
0077 
0078 
0079 if __name__ == '__main__':
0080     main()