Repository Public
1#!/usr/bin/env python32# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project : Fabri·Ka10# File : scripts/phase2_stats.py11# Description : Phase 2 — mesure « APRÈS » : mêmes requêtes que12# 03-BASELINE.md (2026-08-19).13# Module : scripts14# Repository : https://git.spboucher.ai/fabri-ka15# Created : 2026-08-1916# Last Modified : 2026-08-1917#18# =============================================================================19# AUTHOR & PROFESSIONAL AFFILIATION20# =============================================================================21#22# Author : Simon-Pierre Boucher23# Position : Professor of Finance24# Institution : Université du Québec en Outaouais (UQO)25# Department : Département des sciences administratives26# Organization : SP Boucher27#28# Website : https://spboucher.ai29# Git : https://git.spboucher.ai30#31# =============================================================================32# OFFICIAL CONTACT DIRECTORY33# =============================================================================34#35# General : info@spboucher.ai36# Professional : contact@spboucher.ai37# Direct : simon@spboucher.ai38# Research : research@spboucher.ai39# Development : dev@spboucher.ai40# Support : support@spboucher.ai41# Security/Admin : admin@spboucher.ai42# Notifications : notifications@spboucher.ai43#44# Preferred Contact: contact@spboucher.ai45#46# =============================================================================47# COPYRIGHT & INTELLECTUAL PROPERTY48# =============================================================================49#50# Copyright (c) 202651# Simon-Pierre Boucher52#53# All rights reserved where applicable.54#55# This file is governed by the applicable project license,56# copyright notices, institutional agreements and relevant law.57#58# Unless expressly authorized by the applicable license or59# rights holder, copying, redistribution, sublicensing,60# publication and commercial exploitation are not permitted.61#62# Existing third-party rights and notices remain unaffected.63#64# =============================================================================65# CONFIDENTIALITY & SECURITY66# =============================================================================67#68# Classification : Public69# Distribution : Public repository; use is governed by the project license70# Security Contact : admin@spboucher.ai71#72# Access to this source does not independently grant permission73# to redistribute or disclose confidential materials.74#75# Do not embed credentials, private keys, access tokens,76# certificates or confidential personal information.77#78# =============================================================================79# LICENSE & MAINTENANCE80# =============================================================================81#82# License : None specified (no license file in this repository)83# Maintainer : Simon-Pierre Boucher84# Technical Contact: dev@spboucher.ai85#86# =============================================================================87# END OF SP BOUCHER SOURCE HEADER88# =============================================================================8990"""Phase 2 — mesure « APRÈS » : mêmes requêtes que 03-BASELINE.md (2026-08-19).9192Sort un bloc texte prêt à coller dans 05-ENRICHISSEMENT-RAPPORT.md.93Usage : .venv/bin/python scripts/phase2_stats.py94"""95import json96import os97import sys9899ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))100sys.path.insert(0, ROOT)101from fabrika import db as fdb # noqa: E402102103con = fdb.connect()104one = lambda sql, a=(): con.execute(sql, a).fetchone()[0]105rows = lambda sql, a=(): [tuple(r) for r in con.execute(sql, a)]106107ACT = "active=1"108PUB = "active=1 AND listing_status='published'"109110print("## Produits")111print("total en base :", one("SELECT COUNT(*) FROM products"))112print("actifs :", one(f"SELECT COUNT(*) FROM products WHERE {ACT}"))113print("publiés :", one(f"SELECT COUNT(*) FROM products WHERE {PUB}"))114print("par listing_status :", rows("SELECT listing_status, COUNT(*) FROM products "115 f"WHERE {ACT} GROUP BY listing_status ORDER BY 2 DESC"))116print("sur devis (price_on_request, actifs) :",117 one(f"SELECT COUNT(*) FROM products WHERE {ACT} AND price_on_request=1"))118print("devises :", rows(f"SELECT currency, COUNT(*) FROM products WHERE {ACT} "119 "GROUP BY currency ORDER BY 2 DESC"))120121n_pub = one(f"SELECT COUNT(*) FROM products WHERE {PUB}")122def pct(n): return f"{n} ({100.0*n/n_pub:.1f} %)"123print("\n## Complétude par champ (produits publiés)")124print("prix (>0) :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND price>0")))125print("≥1 image :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND images<>'[]' AND images IS NOT NULL")))126print("description non vide :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND description<>''")))127print("details :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND details IS NOT NULL AND details<>'{{}}'")))128print("product_type :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND product_type<>''")))129print("vendor :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND vendor<>''")))130print("multi-images (≥2) :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND json_array_length(images)>=2")))131print("plafond images atteint (=15) :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND json_array_length(images)=15")))132print("anciennes troncatures possibles (desc 590-600) :",133 pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND length(description) BETWEEN 590 AND 600")))134print("desc ≥ 590 car. :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND length(description)>=590")))135print("desc tronquée au nouveau cap (≥4990) :", pct(one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND length(description)>=4990")))136print("longueur moyenne description :", round(one(f"SELECT AVG(length(description)) FROM products WHERE {PUB} AND description<>''") or 0), "car.")137print("prix >10k / >100k (publiés) :",138 one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND price>10000"), "/",139 one(f"SELECT COUNT(*) FROM products WHERE {PUB} AND price>100000"))140print("prix max publié :", one(f"SELECT MAX(price) FROM products WHERE {PUB}"))141142print("\n## Catégories (publiés)")143print(rows(f"SELECT category, COUNT(*) FROM products WHERE {PUB} GROUP BY category ORDER BY 2 DESC"))144145print("\n## Boutiques")146print("registre :", one("SELECT COUNT(*) FROM stores"))147print("avec ≥1 produit publié :", one("SELECT COUNT(*) FROM stores WHERE product_count>0"))148print("city :", one("SELECT COUNT(*) FROM stores WHERE COALESCE(city,'')<>''"))149print("lat/lng :", one("SELECT COUNT(*) FROM stores WHERE lat IS NOT NULL AND lng IS NOT NULL"))150print("email :", one("SELECT COUNT(*) FROM stores WHERE COALESCE(email,'')<>''"))151print("phone :", one("SELECT COUNT(*) FROM stores WHERE COALESCE(phone,'')<>''"))152print("shipping_info :", one("SELECT COUNT(*) FROM stores WHERE COALESCE(shipping_info,'')<>''"))153print("store_kind :", rows("SELECT store_kind, COUNT(*) FROM stores WHERE COALESCE(store_kind,'')<>'' GROUP BY store_kind"))154print("categories boutique :", one("SELECT COUNT(*) FROM stores WHERE categories IS NOT NULL AND categories<>'[]'"))155print("logo :", one("SELECT COUNT(*) FROM stores WHERE COALESCE(logo_url,'')<>''"))156print("description_meta :", one("SELECT COUNT(*) FROM stores WHERE COALESCE(description_meta,'')<>''"))157print("région :", one("SELECT COUNT(*) FROM stores WHERE COALESCE(region,'')<>''"))158159reg = json.load(open(os.path.join(ROOT, "data", "stores.json")))["stores"]160print("\n## Registre stores.json")161print("boutiques :", len(reg))162print("enabled :", sum(1 for s in reg if s.get("enabled")))163print("avec endpoint :", sum(1 for s in reg if s.get("catalog_endpoint")))164print("jamais re-sondées (sans last_reprobe, sans endpoint) :",165 sum(1 for s in reg if not s.get("catalog_endpoint") and not s.get("last_reprobe")))166print("geo (lat/lng) :", sum(1 for s in reg if s.get("geo")))167print("store_kind :", sum(1 for s in reg if s.get("store_kind")))168169print("\n## Détails (échantillon 20 000 publiés)")170import random171det_rows = [r[0] for r in con.execute(172 f"SELECT details FROM products WHERE {PUB} AND details IS NOT NULL LIMIT 20000")]173keys = {}174for d in det_rows:175 try:176 for k in json.loads(d):177 keys[k] = keys.get(k, 0) + 1178 except Exception:179 pass180print({k: v for k, v in sorted(keys.items(), key=lambda x: -x[1])[:14]})181182print("\n## Santé sync (24 h)")183import time184t0 = time.time() - 86400185print("syncs/erreurs 24 h :",186 one("SELECT COUNT(*) FROM sync_log WHERE ts>?", (t0,)),187 one("SELECT COUNT(*) FROM sync_log WHERE ts>? AND status='error'", (t0,)))188con.close()189