from lager.cache import PersistentCache
import json
import time
class DeviceHistory:
"""Track device test history."""
def __init__(self):
self.cache = PersistentCache()
def _key(self, serial):
return f'device_history:{serial}'
def record_test(self, serial, result, notes=''):
"""Record a test result for a device."""
key = self._key(serial)
data = self.cache.load(key)
if data:
history = json.loads(data.decode())
else:
history = {'serial': serial, 'tests': []}
history['tests'].append({
'timestamp': time.time(),
'result': result,
'notes': notes
})
self.cache.store(key, json.dumps(history).encode())
def get_history(self, serial):
"""Get test history for a device."""
data = self.cache.load(self._key(serial))
if data:
return json.loads(data.decode())
return None
def get_pass_rate(self, serial):
"""Calculate pass rate for a device."""
history = self.get_history(serial)
if not history or not history['tests']:
return None
passes = sum(1 for t in history['tests'] if t['result'] == 'PASS')
return passes / len(history['tests']) * 100
# Usage
tracker = DeviceHistory()
# Record test results
tracker.record_test('SN-001', 'PASS', 'Initial test')
tracker.record_test('SN-001', 'FAIL', 'Power supply issue')
tracker.record_test('SN-001', 'PASS', 'Rework complete')
# Get history
history = tracker.get_history('SN-001')
print(f"Device SN-001 has {len(history['tests'])} test records")
print(f"Pass rate: {tracker.get_pass_rate('SN-001'):.1f}%")