#!/usr/bin/env python3 import json from datetime import datetime from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, Text from sqlalchemy.orm import declarative_base, relationship, sessionmaker # Definiere dieselben Modelle, wie im Bundle-Erfassungsskript: Base = declarative_base() class Bundle(Base): __tablename__ = 'bundles' id = Column(Integer, primary_key=True) machine_name = Column(String, unique=True) human_name = Column(String) current_version_id = Column(Integer, ForeignKey('bundle_versions.id')) current_version = relationship("BundleVersion", uselist=False, foreign_keys=[current_version_id]) versions = relationship("BundleVersion", back_populates="bundle", foreign_keys='BundleVersion.bundle_id') sales_history = relationship("BundleSalesHistory", back_populates="bundle") class BundleVersion(Base): __tablename__ = 'bundle_versions' id = Column(Integer, primary_key=True) bundle_id = Column(Integer, ForeignKey('bundles.id')) version_hash = Column(String) version_data = Column(Text) # als JSON-String timestamp = Column(DateTime, default=datetime.utcnow) bundle = relationship("Bundle", back_populates="versions") class BundleSalesHistory(Base): __tablename__ = 'bundle_sales_history' id = Column(Integer, primary_key=True) bundle_id = Column(Integer, ForeignKey('bundles.id')) bundles_sold = Column(Float) timestamp = Column(DateTime, default=datetime.utcnow) bundle = relationship("Bundle", back_populates="sales_history") def main(): # Verbindung zur vorhandenen SQLite-Datenbank engine = create_engine('sqlite:///bundles.db') Session = sessionmaker(bind=engine) session = Session() # Alle Bundles abfragen bundles = session.query(Bundle).all() if not bundles: print("Die Datenbank enthält noch keine Bundles.") return for bundle in bundles: print("---------------------------------------------------") print(f"Bundle-ID: {bundle.id}") print(f"Machine Name: {bundle.machine_name}") print(f"Human Name: {bundle.human_name}") if bundle.current_version: print(f"Aktuelle Version (ID): {bundle.current_version.id}") print(f"Version Hash: {bundle.current_version.version_hash}") try: version_info = json.loads(bundle.current_version.version_data) # Optional: gebe z. B. den 'human_name' oder weitere Felder aus print(f"Version-Daten (gekürzt): {json.dumps(version_info, indent=2)[:300]} ...") except Exception: print("Version-Daten (roher Text):") print(bundle.current_version.version_data) print(f"Letzte Aktualisierung: {bundle.current_version.timestamp}") else: print("Keine Version hinterlegt.") # Alle Versionen anzeigen print("\nHistorische Versionen:") for version in bundle.versions: print(f" Version-ID: {version.id}, Timestamp: {version.timestamp}, Hash: {version.version_hash}") # Verkaufszahlen anzeigen print("\nVerkaufshistorie:") if bundle.sales_history: for sale in bundle.sales_history: print(f" {sale.timestamp}: {sale.bundles_sold} verkauft") else: print(" Keine Verkaufsdaten vorhanden.") print("---------------------------------------------------") if __name__ == "__main__": main()