Files
warren 205566a0f0 feat: portable sample package (100 videos w/ deep scan); clean-install wipes ALL Qdrant collections + legacy storage dirs
- sample/: build_package.sh (pack from pre-clean backup), install_sample.sh (上架), uninstall_sample.sh (下架), README
- clean-install.sh: delete every collection via /collections API + rm legacy qdrant storage dirs (~/qdrant/storage/collections etc.)
2026-11-08 22:11:50 +08:00

166 lines
7.1 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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 <<SQL
DROP TABLE IF EXISTS sample_names;
CREATE TABLE sample_names(fname text PRIMARY KEY);
DROP TABLE IF EXISTS sample_selected;
CREATE TABLE sample_selected(file_uuid text PRIMARY KEY, fname text NOT NULL, video_id bigint NOT NULL);
SQL
ls "$SRC_VAR"/*.mp4 | while read -r f; do printf '%s\n' "$(basename "$f")"; done \
| psql -U accusys -d "$SRC_DB" -q -c "COPY sample_names FROM STDIN"
psql -U accusys -d "$SRC_DB" -q <<'SQL'
INSERT INTO sample_selected
SELECT DISTINCT ON (split_part(v.file_path,'/',-1))
v.file_uuid, split_part(v.file_path,'/',-1), v.id
FROM videos v
JOIN sample_names n ON n.fname = split_part(v.file_path,'/',-1)
ORDER BY 2,
(v.processing_status->>'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"