Files
udc-parser/parser/backfill_changeover.py
cproudlock 5c707c3cd4 Initial commit: UDC/CLM parser for ShopDB
Python parsers for Universal Data Collector (UDC) and Control Loop
Monitor (CLM) data from CNC machines.

- clmparser.py: Main parser for CLM JSON data files
  - Incremental imports using file hashes
  - Extracts parts, measurements, tool data, violations
  - Calculates cycle times and changeovers

- udcparser.py: Parser for raw UDC log files
  - Application events, errors, connections

- config.py: Database configuration (dev/prod)
- backfill_changeover.py: One-time migration utility

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-16 07:54:54 -05:00

48 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Backfill changeover values for udcparts"""
import mysql.connector
conn = mysql.connector.connect(
host='127.0.0.1', port=3306, user='root',
password='rootpassword', database='shopdb'
)
cursor = conn.cursor()
# Get all machines
cursor.execute("SELECT DISTINCT machinenumber FROM udcsessions ORDER BY machinenumber")
machines = [row[0] for row in cursor.fetchall()]
total_updated = 0
for machine in machines:
# Get all parts for this machine ordered by start time
cursor.execute("""
SELECT p.partrunid, p.programstart, p.programend
FROM udcparts p
JOIN udcsessions s ON p.sessionid = s.sessionid
WHERE s.machinenumber = %s AND p.programstart IS NOT NULL
ORDER BY p.programstart ASC
""", (machine,))
parts = cursor.fetchall()
prev_end = None
updates = []
for partrunid, programstart, programend in parts:
if prev_end and programstart:
changeover = int((programstart - prev_end).total_seconds())
# Only set if reasonable (> 0 and < 24 hours)
if 0 < changeover < 86400:
updates.append((changeover, partrunid))
prev_end = programend if programend else prev_end
# Batch update
if updates:
cursor.executemany("UPDATE udcparts SET changeover = %s WHERE partrunid = %s", updates)
conn.commit()
total_updated += len(updates)
print(f"Machine {machine}: {len(updates)} changeovers set")
print(f"\nTotal updated: {total_updated}")
cursor.close()
conn.close()