gongxiangp2/scripts/query_sku_folder_db.py
2026-05-07 16:37:22 +08:00

59 lines
1.9 KiB
Python

#!/usr/bin/env python3
import argparse
import json
import sqlite3
def parse_args():
parser = argparse.ArgumentParser(description="Query sku folder sqlite database.")
parser.add_argument(
"--db-path",
default="/root/gongxiangp2/output/history-sku-folders.db",
help="SQLite database file path.",
)
return parser.parse_args()
def main():
args = parse_args()
conn = sqlite3.connect(args.db_path)
cur = conn.cursor()
result = {
"total_rows": cur.execute("select count(*) from sku_folder_scan_items").fetchone()[0],
"with_spu": cur.execute("select count(*) from sku_folder_scan_items where spu != ''").fetchone()[0],
"with_detail_dir_sibling": cur.execute(
"select count(*) from sku_folder_scan_items where has_detail_dir_sibling = 1"
).fetchone()[0],
"with_main_dir_sibling": cur.execute(
"select count(*) from sku_folder_scan_items where has_main_dir_sibling = 1"
).fetchone()[0],
"with_detail_and_main_siblings": cur.execute(
"select count(*) from sku_folder_scan_items where has_detail_and_main_siblings = 1"
).fetchone()[0],
"top_dirs": cur.execute(
"""
select directory_name, count(*) as c
from sku_folder_scan_items
group by directory_name
order by c desc
limit 20
"""
).fetchall(),
"sample": cur.execute(
"""
select spu, relative_path, file_count, image_count, child_dir_count,
has_detail_dir_sibling, has_main_dir_sibling, has_detail_and_main_siblings
from sku_folder_scan_items
where has_detail_and_main_siblings = 1
order by relative_path
limit 20
"""
).fetchall(),
}
conn.close()
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()