Files
weval-l99/wevia-autointent-sync.sh
2026-04-19 18:05:32 +02:00

47 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# wevia-autointent-sync.sh v3 · /tmp writable by www-data
LOG_FILE=/var/log/wevia-autointent.jsonl
UNMATCHED=/var/www/html/api/unmatched-queries.json
STATE_DIR=/tmp/weval-autointent
mkdir -p "$STATE_DIR" 2>/dev/null
# 1. Append unmatched queries to log
if [ -s "$UNMATCHED" ]; then
python3 << PYEOF
import json, os, datetime
try:
with open("$UNMATCHED") as f:
d = json.load(f)
queries = d if isinstance(d, list) else d.get("queries", [])
if queries:
existing = set()
if os.path.exists("$LOG_FILE"):
with open("$LOG_FILE") as f:
for line in f:
try: existing.add(json.loads(line).get("msg",""))
except: pass
now = datetime.datetime.now().isoformat()
added = 0
with open("$LOG_FILE", "a") as f:
for q in queries:
msg = q.get("msg") or q.get("message") or q.get("query","") if isinstance(q, dict) else str(q)
if msg and msg not in existing:
f.write(json.dumps({"ts": now, "msg": msg, "exec_count": 0, "source": "unmatched-sync"}) + "\n")
added += 1
existing.add(msg)
print(f"synced={added}")
except Exception as e:
print(f"err:{e}")
PYEOF
fi
# 2. Trigger scan
SCAN=$(curl -sk --max-time 8 "http://127.0.0.1/api/wevia-auto-intent.php?action=scan" 2>/dev/null)
PATTERNS=$(echo "$SCAN" | python3 -c "import sys,json; print(json.loads(sys.stdin.read()).get('patterns_found',0))" 2>/dev/null || echo 0)
# 3. State
echo "[$(date -Iseconds)] patterns=$PATTERNS" >> "$STATE_DIR/sync.log" 2>/dev/null
echo "$SCAN" > "$STATE_DIR/last-scan.json" 2>/dev/null
echo "autointent-sync done · patterns=$PATTERNS"