diff --git a/clean-install.sh b/clean-install.sh index 6a961f8..d17215a 100755 --- a/clean-install.sh +++ b/clean-install.sh @@ -159,10 +159,23 @@ if [ "$KEEP_DATA" = false ]; then fi done - # 清除 Qdrant collections + # 清除 Qdrant(完整清除所有 collections + 舊 storage 目錄) log " 清除 Qdrant..." - curl -s -X DELETE "http://localhost:6333/collections/momentry_rule1" 2>/dev/null || true - curl -s -X DELETE "http://localhost:6333/collections/_faces_trash" 2>/dev/null || true + QKEY=$(grep -E '^\s*api_key:' ~/qdrant_config.yaml 2>/dev/null | head -1 | awk '{print $2}') + QCOLLS=$(curl -s ${QKEY:+-H "api-key: $QKEY"} http://localhost:6333/collections 2>/dev/null | tr ',' '\n' | grep -o '"name":"[^"]*"' | cut -d'"' -f4 || true) + if [ -n "$QCOLLS" ]; then + for coll in $QCOLLS; do + curl -s -X DELETE ${QKEY:+-H "api-key: $QKEY"} "http://localhost:6333/collections/$coll" >/dev/null 2>&1 || true + done + log " 已刪除 collections: $(echo "$QCOLLS" | wc -l | tr -d ' ') 個" + fi + # 檔案層級清除(涵蓋舊版 storage 路徑) + for qd_dir in "$TARGET_DIR/var/qdrant/collections" "$HOME/qdrant/storage/collections" "$HOME/qdrant/collections"; do + if [ -d "$qd_dir" ]; then + rm -rf "$qd_dir" 2>/dev/null || true + log " 已清除 $qd_dir" + fi + done # 清除 launchd 服務定義 log " 清除 launchd 服務..." diff --git a/sample/README.md b/sample/README.md new file mode 100644 index 0000000..45230e5 --- /dev/null +++ b/sample/README.md @@ -0,0 +1,44 @@ +# Momentry Sample Package — 100 影片(deep scan 已完成) + +可攜式 sample 資料包:100 支 demo 影片(001.mp4 ~ 098.mp4 及變體)+ 已完成的 +deep scan 資料(probe / face / asr / asrx / ocr / cut / scenes / face_cluster / +face_trace / scene_vlm / tkg)+ 對應的 PostgreSQL 資料。 + +## 內容 + +| 項目 | 說明 | +|---|---| +| `videos/` | 100 支 mp4 影片(約 1.5 GB) | +| `output/` | 1172 個 deep scan 輸出 JSON(116 MB) | +| `db/sample_data.sql` | 過濾後的 DB 資料(COPY 格式,session_replication_role=replica 保護 FK) | +| `manifest.tsv` / `manifest.json` | uuid ↔ 檔名對照與清單 | +| `uuids.txt` | 100 個 file_uuid | +| install_sample.sh | **上架** | +| uninstall_sample.sh | **下架** | + +## 上架(在任何 momentry 機器上) + +```bash +bash install_sample.sh # 上架 +# 可用環境變數覆蓋: +# MOMENTRY_DIR=~/momentry MOMENTRY_DB=momentry MOMENTRY_PGUSER=accusys +``` + +下架(移除影片 + output + DB 資料): + +```bash +bash uninstall_sample.sh +``` + +## 注意 + +- 上架前系統需已完成安裝(install.sh),DB schema 為 canonical 71 表版。 +- 匯入使用 `session_replication_role = replica`,匯入後會自動校正 id sequences。 +- 臉部偵測(face_detections)為空:這批 sample 影片原本就無臉部偵測資料, + 臉部/身分面板會是空的;影片卡片、場景、逐字稿 chunks、TKG 皆為完整資料。 +- Qdrant 不需預先存在任何 collection;系統無臉部向量資料。 + +## 產生方式(參考) + +由 203 機器的備份(momentry_backup_before_clean_20261108_205623)打包: +`build_package.sh`(保留在 momentry_installer/sample/)。 diff --git a/sample/build_package.sh b/sample/build_package.sh new file mode 100644 index 0000000..05018f3 --- /dev/null +++ b/sample/build_package.sh @@ -0,0 +1,165 @@ +#!/bin/bash +# build_package.sh — 從 203 的備份 DB + 備份 var/output 建立「可攜 sample package」 +# 用法(在 203 上執行): bash ~/momentry_sample_100_build/build_package.sh +set -euo pipefail +export PATH=/opt/homebrew/bin:$PATH + +SRC_DIR=/Users/accusys/momentry_backup_before_clean_20261108_205623 +SRC_VAR=$SRC_DIR/var/sftpgo/data/demo +SRC_OUT=$SRC_DIR/output +SRC_DB=momentry_sample_src +LIVE_DB=momentry +PKG=/Users/accusys/momentry_sample_100 +LOG=$PKG/logs + +mkdir -p "$PKG"/{videos,output,db,logs,qdrant} +: > "$LOG/db_dump.log" +: > "$LOG/copy_files.log" + +echo "=== [1/5] 選取每個 mp4 檔案最完整的 video row ===" +psql -U accusys -d "$SRC_DB" -q <>'phase'='COMPLETED') DESC NULLS LAST, + v.created_at DESC NULLS LAST; +SQL + +psql -U accusys -d "$SRC_DB" -t -A \ + -c "SELECT COUNT(*) FROM sample_selected;" > "$LOG/selected_count.txt" +echo "selected rows: $(cat "$LOG/selected_count.txt")" | tee -a "$LOG/db_dump.log" +if [ "$(cat "$LOG/selected_count.txt")" -ne 100 ]; then + echo "!! 選取數 != 100,請人工確認" | tee -a "$LOG/db_dump.log" +fi + +# 匯出 uuid 清單供後續腳本使用 +psql -U accusys -d "$SRC_DB" -t -A \ + -c "SELECT file_uuid FROM sample_selected ORDER BY fname;" > "$PKG/uuids.txt" + +echo "=== [2/5] 產生 DB sample SQL(交集欄位 COPY blocks)===" +cat > "$PKG/db/00_header.sql" <<'SQL' +-- momentry sample data (deep scan completed, 100 videos) +-- import with: session_replication_role=replica +SET session_replication_role = replica; +SQL +: > "$PKG/db/sample_tables_manifest.txt" + +# 定義:table|過濾子查詢的來源 +TABLES=( + "videos|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "processor_results|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "monitor_jobs|video_id IN (SELECT video_id FROM sample_selected)" + "scenes|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "chunk|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "speaker_detections|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "trace_profiles|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "trace_operation_history|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "tkg_nodes|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "tkg_edges|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "tkg_operation_log|file_uuid IN (SELECT file_uuid FROM sample_selected)" + "pipeline_progress|file_uuid IN (SELECT file_uuid FROM sample_selected)" +) + +: > "$PKG/db/sample_data.sql" +cat "$PKG/db/00_header.sql" >> "$PKG/db/sample_data.sql" + +for spec in "${TABLES[@]}"; do + t="${spec%%|*}"; cond="${spec#*|}" + # 略過 live DB 沒有的表 + live_exists=$(psql -U accusys -d "$LIVE_DB" -t -A -c "SELECT to_regclass('public.$t') IS NOT NULL;") + if [ "$live_exists" != "t" ]; then + echo "SKIP(live 無表): $t" | tee -a "$LOG/db_dump.log" + continue + fi + # 取來源與 live 的欄位交集(依 live 欄序) + lcols=$(psql -U accusys -d "$LIVE_DB" -t -A -c " + SELECT string_agg(column_name, E'\n' ORDER BY ordinal_position) FROM information_schema.columns + WHERE table_schema='public' AND table_name='$t';") + scols=$(psql -U accusys -d "$SRC_DB" -t -A -c " + SELECT string_agg(column_name, E'\n' ORDER BY ordinal_position) FROM information_schema.columns + WHERE table_schema='public' AND table_name='$t';") + cols=$(comm -12 <(printf '%s\n' "$lcols" | sort) <(printf '%s\n' "$scols" | sort) | tr '\n' ',' | sed 's/,$//') + [ -z "$cols" ] && { echo "SKIP(無交集欄位): $t" | tee -a "$LOG/db_dump.log"; continue; } + # 來源有但 live 沒有的欄位(資料會遺失,記錄) + lost=$(comm -13 <(printf '%s\n' "$lcols" | sort) <(printf '%s\n' "$scols" | sort) | tr '\n' ',' | sed 's/,$//') + [ -n "$lost" ] && echo "WARN($t 來源多欄遭捨棄): $lost" | tee -a "$LOG/db_dump.log" + cnt=$(psql -U accusys -d "$SRC_DB" -t -A -c "SELECT COUNT(*) FROM public.$t WHERE $cond;") + echo "$t=$cnt" | tee -a "$LOG/db_dump.log" + [ "$cnt" = "0" ] && continue + echo "" >> "$PKG/db/sample_data.sql" + echo "COPY public.$t ($cols) FROM STDIN;" >> "$PKG/db/sample_data.sql" + psql -U accusys -d "$SRC_DB" -t -A -X -v ON_ERROR_STOP=1 \ + -c "COPY (SELECT $cols FROM public.$t WHERE $cond) TO STDOUT" \ + >> "$PKG/db/sample_data.sql" 2>>"$LOG/db_dump.log" + echo "\." >> "$PKG/db/sample_data.sql" + echo "$t=$cnt" >> "$PKG/db/sample_tables_manifest.txt" +done + +echo "=== [3/5] 複製影片(100 支)===" +n=0 +for f in "$SRC_VAR"/*.mp4; do + cp -p "$f" "$PKG/videos/" && n=$((n+1)) || echo "COPY FAIL $f" >> "$LOG/copy_files.log" +done +echo "videos copied: $n" | tee -a "$LOG/copy_files.log" + +echo "=== [4/5] 複製 output JSON(依 uuid)===" +outn=0; misses=0 +while read -r uuid; do + found=$(ls "$SRC_OUT"/"$uuid".* 2>/dev/null || true) + if [ -n "$found" ]; then + cp -Rp $found "$PKG/output/" && outn=$((outn+1)) + else + misses=$((misses+1)) + fi +done < "$PKG/uuids.txt" +echo "uuid outputs copied=$outn uuid_misses=$misses" | tee -a "$LOG/copy_files.log" +# 夾帶 uuid 目錄(若有) +while read -r uuid; do + [ -d "$SRC_OUT/$uuid" ] && cp -Rp "$SRC_OUT/$uuid" "$PKG/output/" 2>/dev/null || true +done < "$PKG/uuids.txt" +echo "output files total: $(find "$PKG/output" -maxdepth 1 | wc -l | tr -d ' ')" | tee -a "$LOG/copy_files.log" + +echo "=== [5/5] manifest.json ===" +psql -U accusys -d "$SRC_DB" -t -A -F' ' \ + -c "SELECT file_uuid, fname FROM sample_selected ORDER BY fname;" \ + > "$PKG/manifest.tsv" +/usr/bin/python3 - <<'PY' +import json, os, subprocess +uuids=[l.strip() for l in open('/Users/accusys/momentry_sample_100/uuids.txt') if l.strip()] +tmap={} +for line in open('/Users/accusys/momentry_sample_100/manifest.tsv'): + parts=line.rstrip('\n').split('\t') + if len(parts)==2: tmap[parts[0]]=parts[1] +tables={} +for line in open('/Users/accusys/momentry_sample_100/db/sample_tables_manifest.txt'): + t,_,n=line.strip().partition('=') + tables[t]=int(n) +n_videos=len(os.listdir('/Users/accusys/momentry_sample_100/videos')) +n_out=len(os.listdir('/Users/accusys/momentry_sample_100/output')) +manifest={ + "name":"momentry-sample-100","version":1, + "generated":"2026-11-08", + "source":"203 backup momentry_backup_before_clean_20261108_205623", + "description":"100 sample videos with completed deep scan (probe/face/asr/ocr/cut/scenes/face_cluster/asrx/tkg)", + "uuids":uuids, "uuid_to_name":tmap, + "counts":{"videos":n_videos,"output_files":n_out,"db_tables":tables}, +} +open('/Users/accusys/momentry_sample_100/manifest.json','w').write(json.dumps(manifest,ensure_ascii=False,indent=2)) +print("manifest written") +PY + +echo "=== 完成 ===" +du -sh "$PKG"/* | tee -a "$LOG/db_dump.log" diff --git a/sample/install_sample.sh b/sample/install_sample.sh new file mode 100644 index 0000000..659ef28 --- /dev/null +++ b/sample/install_sample.sh @@ -0,0 +1,77 @@ +#!/bin/bash +# ============================================================= +# momentry sample package (上架) +# 將 100 支 deep scan 已完成的 sample 影片上架到本機 momentry 系統 +# 可在任何 momentry 機器上使用(可攜) +# +# 用法: +# bash install_sample.sh # 上架(在 203 上) +# MOMENTRY_DIR=~momentry bash install_sample.sh +# ============================================================= +set -euo pipefail + +PKG="$(cd "$(dirname "$0")" && pwd)" +MOMENTRY_DIR="${MOMENTRY_DIR:-$HOME/momentry}" +DEMO_DIR="$MOMENTRY_DIR/var/sftpgo/data/demo" +OUT_DIR="$MOMENTRY_DIR/output" +DB="${MOMENTRY_DB:-momentry}" + +# psql 路徑探測(203=/opt/homebrew, 部分機器=~/opt_pgsql) +if [ -x /opt/homebrew/bin/psql ]; then + PSQL=/opt/homebrew/bin/psql +elif [ -x "$HOME/opt_pgsql/bin/psql" ]; then + PSQL="$HOME/opt_pgsql/bin/psql" +else + PSQL=$(command -v psql || true) + [ -n "$PSQL" ] || { echo "!! 找不到 psql"; exit 1; } +fi + +if [ ! -f "$PKG/db/sample_data.sql" ]; then + echo "!! 找不到 $PKG/db/sample_data.sql — 這不是完整的 sample package" + exit 1 +fi + +echo "=== 上架 sample data 到 $MOMENTRY_DIR ===" + +echo "[1/4] 複製影片($(ls "$PKG"/videos/*.mp4 2>/dev/null | wc -l | tr -d ' ') 支)→ $DEMO_DIR" +mkdir -p "$DEMO_DIR" +cp -f "$PKG"/videos/*.mp4 "$DEMO_DIR"/ + +echo "[2/4] 複製 deep scan 輸出 JSON → $OUT_DIR" +mkdir -p "$OUT_DIR" +cp -a "$PKG"/output/. "$OUT_DIR"/ + +echo "[3/4] 匯入 DB sample data → $DB" +"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -v ON_ERROR_STOP=1 -f "$PKG/db/sample_data.sql" + +echo "[4/4] 校正 id sequences" +"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -q <<'SQL' +SELECT format( + 'SELECT CASE WHEN %L IS NOT NULL THEN setval(%L, COALESCE((SELECT max(id) FROM %I), 1), false) END;', + pg_get_serial_sequence(c.oid::regclass::text, 'id'), + pg_get_serial_sequence(c.oid::regclass::text, 'id'), + c.oid::regclass::text +) +FROM pg_class c +JOIN pg_namespace n ON n.oid = c.relnamespace +WHERE n.nspname = 'public' + AND c.relname IN ('videos','processor_results','monitor_jobs','scenes','chunk', + 'speaker_detections','trace_profiles','trace_operation_history', + 'tkg_nodes','tkg_edges','tkg_operation_log','pipeline_progress') + AND EXISTS (SELECT 1 FROM information_schema.columns ic + WHERE ic.table_schema='public' AND ic.table_name=c.relname AND ic.column_name='id') +\gexec +SQL + +echo "" +echo "=== 上架完成。摘要 ===" +"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -t -A -c " +SELECT 'videos='||COUNT(*) FROM videos +UNION ALL SELECT 'processor_results='||COUNT(*) FROM processor_results +UNION ALL SELECT 'monitor_jobs='||COUNT(*) FROM monitor_jobs +UNION ALL SELECT 'scenes='||COUNT(*) FROM scenes +UNION ALL SELECT 'chunk='||COUNT(*) FROM chunk +UNION ALL SELECT 'speaker_detections='||COUNT(*) FROM speaker_detections +UNION ALL SELECT 'trace_profiles='||COUNT(*) FROM trace_profiles +UNION ALL SELECT 'tkg_nodes='||COUNT(*) FROM tkg_nodes;" +echo "影片檔案: $(ls "$DEMO_DIR"/*.mp4 2>/dev/null | wc -l | tr -d ' ') 支 | output JSON: $(find "$OUT_DIR" -maxdepth 1 -type f | wc -l | tr -d ' ') 個" diff --git a/sample/uninstall_sample.sh b/sample/uninstall_sample.sh new file mode 100644 index 0000000..2858e23 --- /dev/null +++ b/sample/uninstall_sample.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# ============================================================= +# momentry sample package (下架) +# 將 sample 影片與其 deep scan 資料從本機 momentry 系統移除 +# +# 用法: +# bash uninstall_sample.sh +# ============================================================= +set -euo pipefail + +PKG="$(cd "$(dirname "$0")" && pwd)" +MOMENTRY_DIR="${MOMENTRY_DIR:-$HOME/momentry}" +DEMO_DIR="$MOMENTRY_DIR/var/sftpgo/data/demo" +OUT_DIR="$MOMENTRY_DIR/output" +DB="${MOMENTRY_DB:-momentry}" + +if [ -x /opt/homebrew/bin/psql ]; then + PSQL=/opt/homebrew/bin/psql +elif [ -x "$HOME/opt_pgsql/bin/psql" ]; then + PSQL="$HOME/opt_pgsql/bin/psql" +else + PSQL=$(command -v psql || true) + [ -n "$PSQL" ] || { echo "!! 找不到 psql"; exit 1; } +fi + +[ -f "$PKG/manifest.tsv" ] || { echo "!! 找不到 manifest.tsv"; exit 1; } + +UUIDS=$(cut -f1 "$PKG/manifest.tsv" | tr '\n' ',' | sed 's/,$//') +FNAME_TABLES="processor_results monitor_jobs scenes chunk speaker_detections trace_profiles trace_operation_history tkg_nodes tkg_edges tkg_operation_log pipeline_progress" + +echo "=== 下架 sample data($(wc -l < "$PKG/manifest.tsv" | tr -d ' ') 支影片)===" +for t in $FNAME_TABLES videos; do + "$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -q -v ON_ERROR_STOP=1 </dev/null || true + [ -d "$OUT_DIR/$uuid" ] && rm -rf "$OUT_DIR/$uuid" || true +done < "$PKG/manifest.tsv" + +echo "[2/3] 刪除影片檔" +while IFS=$'\t' read -r uuid fname; do + [ -n "$fname" ] && rm -f "$DEMO_DIR/$fname" || true +done < "$PKG/manifest.tsv" + +echo "[3/3] 完成" +"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -t -A -c "SELECT 'videos 殘留='||COUNT(*) FROM videos;"