askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
append_log.py · 4.2 KB · 152 lines
| 1 | """ |
| 2 | Cluster Log Replication |
| 3 | |
| 4 | POST /cluster/append_log - Receive log entry replication from another station |
| 5 | |
| 6 | Used for distributed logging: when an object logs on station1, it automatically |
| 7 | replicates to station2 and station3 so all stations have complete audit trail. |
| 8 | |
| 9 | Key difference from state replication: |
| 10 | - State: Last-write-wins (replace) |
| 11 | - Logs: Append-only (never overwrite) |
| 12 | """ |
| 13 | import json |
| 14 | import csv |
| 15 | from pathlib import Path |
| 16 | from dbbasic_web.responses import json as json_response, json_error |
| 17 | |
| 18 | |
| 19 | def POST(request): |
| 20 | """ |
| 21 | Receive log entry from another station and append it |
| 22 | |
| 23 | Request body: |
| 24 | { |
| 25 | "object_id": str, |
| 26 | "entry_id": str, # Unique ID to prevent duplicates |
| 27 | "log_entry": dict, # Log entry with timestamp, level, message, etc. |
| 28 | "source_station": str |
| 29 | } |
| 30 | |
| 31 | Response: |
| 32 | { |
| 33 | "status": "ok" | "duplicate", |
| 34 | "message": str, |
| 35 | "object_id": str |
| 36 | } |
| 37 | """ |
| 38 | try: |
| 39 | if request.body: |
| 40 | data = json.loads(request.body.decode('utf-8')) |
| 41 | else: |
| 42 | return json_error('Request body required', status=400) |
| 43 | except json.JSONDecodeError: |
| 44 | return json_error('Invalid JSON', status=400) |
| 45 | |
| 46 | object_id = data.get('object_id') |
| 47 | entry_id = data.get('entry_id') |
| 48 | log_entry = data.get('log_entry') |
| 49 | source_station = data.get('source_station') |
| 50 | |
| 51 | if not all([object_id, entry_id, log_entry, source_station]): |
| 52 | return json_error( |
| 53 | 'Missing required fields: object_id, entry_id, log_entry, source_station', |
| 54 | status=400 |
| 55 | ) |
| 56 | |
| 57 | # Setup log directory |
| 58 | log_dir = Path(f'data/logs/{object_id}') |
| 59 | log_dir.mkdir(parents=True, exist_ok=True) |
| 60 | log_file = log_dir / 'log.tsv' |
| 61 | |
| 62 | # Check for duplicate (already have this entry_id) |
| 63 | if _has_entry(log_file, entry_id): |
| 64 | return json_response(json.dumps({ |
| 65 | 'status': 'duplicate', |
| 66 | 'message': 'Log entry already exists', |
| 67 | 'object_id': object_id, |
| 68 | 'entry_id': entry_id |
| 69 | })) |
| 70 | |
| 71 | # Append log entry |
| 72 | _append_log_entry(log_file, entry_id, log_entry) |
| 73 | |
| 74 | return json_response(json.dumps({ |
| 75 | 'status': 'ok', |
| 76 | 'message': 'Log entry appended', |
| 77 | 'object_id': object_id, |
| 78 | 'entry_id': entry_id, |
| 79 | 'source_station': source_station |
| 80 | })) |
| 81 | |
| 82 | |
| 83 | def _has_entry(log_file: Path, entry_id: str) -> bool: |
| 84 | """Check if log file already contains this entry_id""" |
| 85 | if not log_file.exists(): |
| 86 | return False |
| 87 | |
| 88 | try: |
| 89 | with open(log_file, 'r', newline='') as f: |
| 90 | reader = csv.DictReader(f, delimiter='\t') |
| 91 | for row in reader: |
| 92 | if row.get('entry_id') == entry_id: |
| 93 | return True |
| 94 | except: |
| 95 | pass |
| 96 | |
| 97 | return False |
| 98 | |
| 99 | |
| 100 | def _append_log_entry(log_file: Path, entry_id: str, log_entry: dict) -> None: |
| 101 | """Append log entry to file""" |
| 102 | |
| 103 | # Add entry_id to log entry |
| 104 | entry = {'entry_id': entry_id, **log_entry} |
| 105 | |
| 106 | # Get existing fieldnames if file exists |
| 107 | fieldnames = _get_fieldnames(log_file) |
| 108 | |
| 109 | # Add any new fields from this entry |
| 110 | for key in entry.keys(): |
| 111 | if key not in fieldnames: |
| 112 | fieldnames.append(key) |
| 113 | |
| 114 | # Append to file |
| 115 | is_new_file = not log_file.exists() |
| 116 | |
| 117 | with open(log_file, 'a', newline='') as f: |
| 118 | writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter='\t') |
| 119 | |
| 120 | if is_new_file: |
| 121 | writer.writeheader() |
| 122 | |
| 123 | writer.writerow(entry) |
| 124 | |
| 125 | |
| 126 | def _get_fieldnames(log_file: Path) -> list: |
| 127 | """Get existing fieldnames from log file""" |
| 128 | if not log_file.exists(): |
| 129 | return ['entry_id', 'timestamp', 'level', 'message'] |
| 130 | |
| 131 | try: |
| 132 | with open(log_file, 'r', newline='') as f: |
| 133 | reader = csv.DictReader(f, delimiter='\t') |
| 134 | return list(reader.fieldnames or ['entry_id', 'timestamp', 'level', 'message']) |
| 135 | except: |
| 136 | return ['entry_id', 'timestamp', 'level', 'message'] |
| 137 | |
| 138 | |
| 139 | def GET(request): |
| 140 | """ |
| 141 | Get log replication status for monitoring |
| 142 | |
| 143 | Response: |
| 144 | { |
| 145 | "status": "ok", |
| 146 | "message": "Log replication endpoint active" |
| 147 | } |
| 148 | """ |
| 149 | return json_response(json.dumps({ |
| 150 | 'status': 'ok', |
| 151 | 'message': 'Log replication endpoint active' |
| 152 | })) |