Initial commit: Quality App v2 - FG Scan Module with Reports
This commit is contained in:
135
documentation/debug_scripts/_test_fg_scans.py
Normal file
135
documentation/debug_scripts/_test_fg_scans.py
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Script to generate test FG scan data for quality reports testing
|
||||
Run: python3 _test_fg_scans.py
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
import random
|
||||
|
||||
# Add parent directory to path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
|
||||
|
||||
from app.config import Config
|
||||
from app.database import get_db
|
||||
|
||||
|
||||
def generate_test_scans():
|
||||
"""Generate realistic test FG scan data"""
|
||||
try:
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
|
||||
# Create table if not exists
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS scanfg_orders (
|
||||
Id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
operator_code VARCHAR(4) NOT NULL,
|
||||
CP_full_code VARCHAR(15) NOT NULL,
|
||||
OC1_code VARCHAR(4) NOT NULL,
|
||||
OC2_code VARCHAR(4) NOT NULL,
|
||||
quality_code TINYINT(3) NOT NULL,
|
||||
date DATE NOT NULL,
|
||||
time TIME NOT NULL,
|
||||
approved_quantity INT DEFAULT 0,
|
||||
rejected_quantity INT DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
INDEX idx_cp (CP_full_code),
|
||||
INDEX idx_date (date),
|
||||
INDEX idx_operator (operator_code),
|
||||
UNIQUE KEY unique_cp_date (CP_full_code, date)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
""")
|
||||
db.commit()
|
||||
print("✓ Table 'scanfg_orders' ready")
|
||||
|
||||
# Sample data
|
||||
operators = ['OP01', 'OP02', 'OP03', 'OP04']
|
||||
cp_codes = [f'CP{str(i).zfill(8)}-{str(j).zfill(4)}' for i in range(1, 6) for j in range(1, 4)]
|
||||
oc1_codes = ['OC01', 'OC02', 'OC03', 'OC04']
|
||||
oc2_codes = ['OC10', 'OC20', 'OC30', 'OC40']
|
||||
defect_codes = ['000', '001', '002', '003', '004', '005'] # 000 = approved, others = defects
|
||||
|
||||
# Generate scans for last 10 days
|
||||
scans_created = 0
|
||||
for days_back in range(10):
|
||||
date = (datetime.now() - timedelta(days=days_back)).date()
|
||||
|
||||
# Generate 20-50 scans per day
|
||||
num_scans = random.randint(20, 50)
|
||||
for _ in range(num_scans):
|
||||
hour = random.randint(6, 18)
|
||||
minute = random.randint(0, 59)
|
||||
second = random.randint(0, 59)
|
||||
time = f'{hour:02d}:{minute:02d}:{second:02d}'
|
||||
|
||||
operator = random.choice(operators)
|
||||
cp_code = random.choice(cp_codes)
|
||||
oc1_code = random.choice(oc1_codes)
|
||||
oc2_code = random.choice(oc2_codes)
|
||||
# 90% approved, 10% rejected
|
||||
defect_code = random.choice(['000'] * 9 + ['001', '002', '003', '004', '005'])
|
||||
|
||||
try:
|
||||
insert_query = """
|
||||
INSERT INTO scanfg_orders
|
||||
(operator_code, CP_full_code, OC1_code, OC2_code, quality_code, date, time)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
cursor.execute(insert_query, (operator, cp_code, oc1_code, oc2_code, defect_code, date, time))
|
||||
scans_created += 1
|
||||
except Exception as e:
|
||||
# Skip duplicate entries
|
||||
pass
|
||||
|
||||
db.commit()
|
||||
print(f"✓ Generated {scans_created} test scans")
|
||||
|
||||
# Show summary
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) as total,
|
||||
SUM(CASE WHEN quality_code = '000' THEN 1 ELSE 0 END) as approved,
|
||||
SUM(CASE WHEN quality_code != '000' THEN 1 ELSE 0 END) as rejected
|
||||
FROM scanfg_orders
|
||||
""")
|
||||
result = cursor.fetchone()
|
||||
|
||||
print(f"\nDatabase Summary:")
|
||||
print(f" Total Scans: {result[0]}")
|
||||
print(f" Approved: {result[1] or 0}")
|
||||
print(f" Rejected: {result[2] or 0}")
|
||||
print(f" Approval Rate: {((result[1] or 0) / (result[0] or 1) * 100):.1f}%")
|
||||
|
||||
# Show sample by date
|
||||
cursor.execute("""
|
||||
SELECT date, COUNT(*) as count,
|
||||
SUM(CASE WHEN quality_code = '000' THEN 1 ELSE 0 END) as approved
|
||||
FROM scanfg_orders
|
||||
GROUP BY date
|
||||
ORDER BY date DESC
|
||||
LIMIT 10
|
||||
""")
|
||||
|
||||
print(f"\nScans by Date (Last 10 Days):")
|
||||
print(f" {'Date':<12} {'Total':<8} {'Approved':<10} {'Rate':<8}")
|
||||
print(f" {'-'*40}")
|
||||
for row in cursor.fetchall():
|
||||
date_str = str(row[0])
|
||||
total = row[1]
|
||||
approved = row[2] or 0
|
||||
rate = (approved / total * 100) if total > 0 else 0
|
||||
print(f" {date_str:<12} {total:<8} {approved:<10} {rate:.1f}%")
|
||||
|
||||
cursor.close()
|
||||
db.close()
|
||||
|
||||
print("\n✅ Test data generation completed successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
generate_test_scans()
|
||||
Reference in New Issue
Block a user