| """
|
| SQLAlchemy ORM Models
|
|
|
| Defines database schema for AegisLM evaluation framework.
|
| Tables: tenants, users, evaluation_runs, evaluation_results, model_versions,
|
| dataset_versions, monitoring_metrics, alerts, release_validations, api_keys,
|
| workers, audit_logs
|
| """
|
|
|
| import uuid
|
| from datetime import datetime
|
| from typing import Any, Dict, Optional
|
|
|
| from sqlalchemy import (
|
| JSON,
|
| DateTime,
|
| Float,
|
| ForeignKey,
|
| Integer,
|
| String,
|
| Text,
|
| Uuid,
|
| Boolean,
|
| )
|
| from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
|
|
|
|
| class Base(DeclarativeBase):
|
| """Base class for all SQLAlchemy models."""
|
|
|
| pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| class Tenant(Base):
|
| """
|
| Represents a tenant organization in the multi-tenant system.
|
|
|
| Each tenant is an organization (company/team) that owns its own
|
| users, API keys, models, evaluation jobs, monitoring data, and reports.
|
| """
|
|
|
| __tablename__ = "tenants"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the tenant"
|
| )
|
|
|
| name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| index=True,
|
| doc="Tenant organization name"
|
| )
|
|
|
| plan_type: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| default="free",
|
| doc="Plan type: free, basic, pro, enterprise"
|
| )
|
|
|
| job_quota: Mapped[int] = mapped_column(
|
| Integer,
|
| nullable=False,
|
| default=10,
|
| doc="Maximum number of concurrent jobs allowed"
|
| )
|
|
|
| api_rate_limit: Mapped[int] = mapped_column(
|
| Integer,
|
| nullable=False,
|
| default=100,
|
| doc="API requests per minute limit"
|
| )
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When the tenant was created"
|
| )
|
|
|
| active: Mapped[bool] = mapped_column(
|
| default=True,
|
| index=True,
|
| doc="Whether this tenant is active"
|
| )
|
|
|
|
|
| tenant_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional tenant metadata"
|
| )
|
|
|
|
|
| users: Mapped[list["User"]] = relationship(
|
| "User",
|
| back_populates="tenant",
|
| cascade="all, delete-orphan",
|
| doc="Users belonging to this tenant"
|
| )
|
|
|
| api_keys: Mapped[list["APIKey"]] = relationship(
|
| "APIKey",
|
| back_populates="tenant",
|
| cascade="all, delete-orphan",
|
| doc="API keys belonging to this tenant"
|
| )
|
|
|
| evaluation_runs: Mapped[list["EvaluationRun"]] = relationship(
|
| "EvaluationRun",
|
| back_populates="tenant",
|
| cascade="all, delete-orphan",
|
| doc="Evaluation runs belonging to this tenant"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<Tenant(id={self.id}, name={self.name}, plan={self.plan_type})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class User(Base):
|
| """
|
| Represents a user within a tenant organization.
|
|
|
| Each user belongs to a tenant and has a specific role that determines
|
| their access permissions within the platform.
|
| """
|
|
|
| __tablename__ = "users"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the user"
|
| )
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant"
|
| )
|
|
|
| email: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| index=True,
|
| doc="User email address"
|
| )
|
|
|
| role: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| default="viewer",
|
| doc="User role: admin, engineer, viewer, api_client"
|
| )
|
|
|
| password_hash: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Hashed password"
|
| )
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When the user was created"
|
| )
|
|
|
| active: Mapped[bool] = mapped_column(
|
| default=True,
|
| index=True,
|
| doc="Whether this user is active"
|
| )
|
|
|
| last_login: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="Last login timestamp"
|
| )
|
|
|
|
|
| user_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional user metadata"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| back_populates="users",
|
| doc="Parent tenant"
|
| )
|
|
|
| audit_logs: Mapped[list["AuditLog"]] = relationship(
|
| "AuditLog",
|
| back_populates="user",
|
| cascade="all, delete-orphan",
|
| doc="Audit logs for this user"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<User(id={self.id}, email={self.email}, role={self.role})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class EvaluationRun(Base):
|
| """
|
| Represents a single evaluation run.
|
|
|
| Tracks the overall lifecycle and results of evaluating a model
|
| against a specific dataset version.
|
| """
|
|
|
| __tablename__ = "evaluation_runs"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the evaluation run"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
| timestamp: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When the evaluation run was initiated"
|
| )
|
|
|
| model_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Name of the model being evaluated"
|
| )
|
|
|
| model_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="Version identifier of the model"
|
| )
|
|
|
| dataset_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="Version of the dataset used"
|
| )
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| default="pending",
|
| doc="Status: pending, running, completed, failed, cancelled"
|
| )
|
|
|
| config_hash: Mapped[str] = mapped_column(
|
| String(64),
|
| nullable=False,
|
| doc="SHA256 hash of configuration for reproducibility"
|
| )
|
|
|
| composite_score: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Final composite robustness score (0-1)"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| back_populates="evaluation_runs",
|
| doc="Parent tenant"
|
| )
|
|
|
| results: Mapped[list["EvaluationResult"]] = relationship(
|
| "EvaluationResult",
|
| back_populates="run",
|
| cascade="all, delete-orphan",
|
| doc="Individual evaluation results for each sample"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<EvaluationRun(id={self.id}, model={self.model_name}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class EvaluationResult(Base):
|
| """
|
| Individual evaluation result for a single sample.
|
|
|
| Contains scores for each metric (hallucination, toxicity, bias, confidence)
|
| and the raw model output.
|
| """
|
|
|
| __tablename__ = "evaluation_results"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the result"
|
| )
|
|
|
| run_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("evaluation_runs.id", ondelete="CASCADE"),
|
| nullable=False,
|
| doc="Reference to parent evaluation run"
|
| )
|
|
|
| sample_id: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Identifier of the sample from the dataset"
|
| )
|
|
|
| attack_type: Mapped[Optional[str]] = mapped_column(
|
| String(100),
|
| nullable=True,
|
| doc="Type of attack applied (if any)"
|
| )
|
|
|
| mutation_type: Mapped[Optional[str]] = mapped_column(
|
| String(100),
|
| nullable=True,
|
| doc="Type of mutation applied (if any)"
|
| )
|
|
|
|
|
| hallucination: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Hallucination score (0 = no hallucination, 1 = full hallucination)"
|
| )
|
|
|
| toxicity: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Toxicity score (0 = non-toxic, 1 = toxic)"
|
| )
|
|
|
| bias: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Bias score (0 = unbiased, 1 = biased)"
|
| )
|
|
|
| confidence: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Model confidence score (0 = low, 1 = high)"
|
| )
|
|
|
|
|
| robustness: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Computed robustness score for this sample"
|
| )
|
|
|
|
|
| raw_output: Mapped[Optional[str]] = mapped_column(
|
| Text,
|
| nullable=True,
|
| doc="Raw model output text"
|
| )
|
|
|
| processed_prompt: Mapped[Optional[str]] = mapped_column(
|
| Text,
|
| nullable=True,
|
| doc="Prompt after attack/mutation processing"
|
| )
|
|
|
|
|
| result_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional result metadata"
|
| )
|
|
|
| processing_time_ms: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Time taken to process this sample in milliseconds"
|
| )
|
|
|
|
|
| run: Mapped["EvaluationRun"] = relationship(
|
| "EvaluationRun",
|
| back_populates="results",
|
| doc="Parent evaluation run"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<EvaluationResult(id={self.id}, sample={self.sample_id})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class ModelVersion(Base):
|
| """
|
| Tracks registered model versions.
|
|
|
| Stores metadata about models that have been registered
|
| for evaluation in the system.
|
| """
|
|
|
| __tablename__ = "model_versions"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier"
|
| )
|
|
|
| model_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| index=True,
|
| doc="Model name (e.g., meta-llama/Llama-2-7b-hf)"
|
| )
|
|
|
| model_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="Version string (e.g., v1.0, latest)"
|
| )
|
|
|
| parameters: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Model parameters (num_params, vocab_size, etc.)"
|
| )
|
|
|
| checksum: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| doc="SHA256 checksum of model weights"
|
| )
|
|
|
| registered_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When this model version was registered"
|
| )
|
|
|
| last_used: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="When this model was last used in evaluation"
|
| )
|
|
|
| is_active: Mapped[bool] = mapped_column(
|
| default=True,
|
| doc="Whether this model version is available for evaluation"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<ModelVersion(name={self.model_name}, version={self.model_version})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class DatasetVersion(Base):
|
| """
|
| Tracks registered dataset versions.
|
|
|
| Stores metadata about datasets that have been registered
|
| for evaluation in the system.
|
| """
|
|
|
| __tablename__ = "dataset_versions"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier"
|
| )
|
|
|
| dataset_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| index=True,
|
| doc="Dataset name (e.g., aegislm-harmful-queries)"
|
| )
|
|
|
| version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="Version string (e.g., v1.0)"
|
| )
|
|
|
| checksum: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| doc="SHA256 checksum of dataset file"
|
| )
|
|
|
| dataset_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional dataset metadata (num_samples, categories, etc.)"
|
| )
|
|
|
| registered_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When this dataset version was registered"
|
| )
|
|
|
| is_active: Mapped[bool] = mapped_column(
|
| default=True,
|
| doc="Whether this dataset version is available for evaluation"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<DatasetVersion(name={self.dataset_name}, version={self.version})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class MonitoringMetric(Base):
|
| """
|
| Stores per-inference metrics for continuous monitoring.
|
|
|
| Tracks real-time evaluation results for drift detection
|
| and longitudinal robustness tracking.
|
| """
|
|
|
| __tablename__ = "monitoring_metrics"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the metric"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
| timestamp: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| index=True,
|
| doc="When this metric was recorded"
|
| )
|
|
|
| model_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Name of the model being monitored"
|
| )
|
|
|
| model_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| index=True,
|
| doc="Version of the model"
|
| )
|
|
|
|
|
| hallucination: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Hallucination score (0 = no hallucination, 1 = full hallucination)"
|
| )
|
|
|
| toxicity: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Toxicity score (0 = non-toxic, 1 = toxic)"
|
| )
|
|
|
| bias: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Bias score (0 = unbiased, 1 = biased)"
|
| )
|
|
|
| confidence: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Model confidence score (0 = low, 1 = high)"
|
| )
|
|
|
| robustness: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Composite robustness score (0-1)"
|
| )
|
|
|
|
|
| category: Mapped[Optional[str]] = mapped_column(
|
| String(100),
|
| nullable=True,
|
| index=True,
|
| doc="User category for segmentation"
|
| )
|
|
|
| processing_time_ms: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Time taken to process this inference in milliseconds"
|
| )
|
|
|
| request_id: Mapped[Optional[str]] = mapped_column(
|
| String(255),
|
| nullable=True,
|
| index=True,
|
| doc="Request identifier for tracing"
|
| )
|
|
|
| metric_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional metric metadata"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<MonitoringMetric(id={self.id}, model={self.model_name}, timestamp={self.timestamp})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class Alert(Base):
|
| """
|
| Stores alerts generated from drift detection.
|
|
|
| Tracks detected drifts and their resolution status.
|
| """
|
|
|
| __tablename__ = "alerts"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the alert"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
| alert_type: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| index=True,
|
| doc="Type of alert (hallucination_drift, toxicity_drift, etc.)"
|
| )
|
|
|
| severity: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| doc="Severity level: low, medium, high, critical"
|
| )
|
|
|
| model_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Model being monitored"
|
| )
|
|
|
| model_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| index=True,
|
| doc="Model version"
|
| )
|
|
|
| metric_name: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| doc="Metric that triggered the alert"
|
| )
|
|
|
|
|
| baseline_value: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Baseline metric value"
|
| )
|
|
|
| current_value: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Current metric value when alert triggered"
|
| )
|
|
|
| drift_magnitude: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Magnitude of drift (absolute difference)"
|
| )
|
|
|
| threshold: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Threshold that was exceeded"
|
| )
|
|
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| index=True,
|
| doc="When alert was created"
|
| )
|
|
|
| resolved_at: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="When alert was resolved"
|
| )
|
|
|
| is_resolved: Mapped[bool] = mapped_column(
|
| default=False,
|
| index=True,
|
| doc="Whether alert has been resolved"
|
| )
|
|
|
|
|
| alert_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional alert metadata"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<Alert(id={self.id}, type={self.alert_type}, severity={self.severity})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class ReleaseValidation(Base):
|
| """
|
| Stores release validation records for audit trail.
|
|
|
| Tracks the results of release validation checks when comparing
|
| new model versions against previous versions.
|
| """
|
|
|
| __tablename__ = "release_validations"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the validation record"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
|
|
| prev_model_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Previous model name"
|
| )
|
|
|
| prev_model_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="Previous model version"
|
| )
|
|
|
| new_model_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="New model name"
|
| )
|
|
|
| new_model_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="New model version"
|
| )
|
|
|
|
|
| dataset_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Dataset used for evaluation"
|
| )
|
|
|
| dataset_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="Dataset version"
|
| )
|
|
|
|
|
| delta_robustness: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Change in robustness (prev - new)"
|
| )
|
|
|
| delta_hallucination: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Change in hallucination (new - prev)"
|
| )
|
|
|
| delta_toxicity: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Change in toxicity (new - prev)"
|
| )
|
|
|
| delta_bias: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Change in bias (new - prev)"
|
| )
|
|
|
| delta_confidence: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Change in confidence (new - prev)"
|
| )
|
|
|
|
|
| prev_robustness: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Previous model robustness score"
|
| )
|
|
|
| prev_hallucination: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Previous model hallucination score"
|
| )
|
|
|
| prev_toxicity: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Previous model toxicity score"
|
| )
|
|
|
| prev_bias: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Previous model bias score"
|
| )
|
|
|
| prev_confidence: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Previous model confidence score"
|
| )
|
|
|
|
|
| new_robustness: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="New model robustness score"
|
| )
|
|
|
| new_hallucination: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="New model hallucination score"
|
| )
|
|
|
| new_toxicity: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="New model toxicity score"
|
| )
|
|
|
| new_bias: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="New model bias score"
|
| )
|
|
|
| new_confidence: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="New model confidence score"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| doc="Validation status: PASS, FAIL, CONDITIONAL_PASS, BLOCKED"
|
| )
|
|
|
|
|
| sample_count: Mapped[int] = mapped_column(
|
| Integer,
|
| nullable=False,
|
| doc="Number of samples evaluated"
|
| )
|
|
|
|
|
| config_hash_match: Mapped[bool] = mapped_column(
|
| default=True,
|
| doc="Whether configuration hashes matched"
|
| )
|
|
|
|
|
| timestamp: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| index=True,
|
| doc="When validation was performed"
|
| )
|
|
|
|
|
| prev_run_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
| Uuid,
|
| nullable=True,
|
| doc="Previous evaluation run ID"
|
| )
|
|
|
| new_run_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
| Uuid,
|
| nullable=True,
|
| doc="New evaluation run ID"
|
| )
|
|
|
|
|
| validation_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional validation metadata"
|
| )
|
|
|
|
|
| baseline_updated: Mapped[bool] = mapped_column(
|
| default=False,
|
| doc="Whether monitoring baseline was updated after this validation"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<ReleaseValidation(id={self.id}, prev={self.prev_model_name}/{self.prev_model_version}, new={self.new_model_name}/{self.new_model_version}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class APIKey(Base):
|
| """
|
| Stores API keys for public API access.
|
|
|
| Tracks API keys for the Evaluation-as-a-Service (EaaS) platform.
|
| Each API key is scoped to a specific tenant.
|
| """
|
|
|
| __tablename__ = "api_keys"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the API key"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
| key_hash: Mapped[str] = mapped_column(
|
| String(64),
|
| nullable=False,
|
| unique=True,
|
| index=True,
|
| doc="SHA256 hash of the API key"
|
| )
|
|
|
| owner: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| index=True,
|
| doc="Owner identifier for the API key"
|
| )
|
|
|
| rate_limit: Mapped[int] = mapped_column(
|
| Integer,
|
| default=100,
|
| doc="Rate limit (requests per minute)"
|
| )
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When this API key was created"
|
| )
|
|
|
| last_used: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="When this API key was last used"
|
| )
|
|
|
| active: Mapped[bool] = mapped_column(
|
| default=True,
|
| index=True,
|
| doc="Whether this API key is active"
|
| )
|
|
|
|
|
| evaluation_mode_restriction: Mapped[Optional[str]] = mapped_column(
|
| String(20),
|
| nullable=True,
|
| doc="Restricted evaluation mode: lightweight or full"
|
| )
|
|
|
| mutation_enabled: Mapped[bool] = mapped_column(
|
| default=True,
|
| doc="Whether mutation is enabled for this key"
|
| )
|
|
|
| monitoring_only: Mapped[bool] = mapped_column(
|
| default=False,
|
| doc="Whether monitoring-only mode is enforced"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| back_populates="api_keys",
|
| doc="Parent tenant"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<APIKey(id={self.id}, owner={self.owner}, tenant_id={self.tenant_id}, active={self.active})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class Worker(Base):
|
| """
|
| Tracks registered worker nodes in the distributed evaluation system.
|
|
|
| Stores metadata about workers including GPU capacity, health status,
|
| and active job counts for scheduling decisions.
|
| """
|
|
|
| __tablename__ = "workers"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the worker"
|
| )
|
|
|
| worker_id: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| unique=True,
|
| index=True,
|
| doc="Unique worker identifier (hostname-pid format)"
|
| )
|
|
|
| hostname: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| doc="Worker hostname"
|
| )
|
|
|
| gpu_count: Mapped[int] = mapped_column(
|
| Integer,
|
| default=0,
|
| doc="Number of GPUs available on this worker"
|
| )
|
|
|
| gpu_memory_total: Mapped[int] = mapped_column(
|
| Integer,
|
| default=0,
|
| doc="Total GPU memory in MB"
|
| )
|
|
|
| gpu_memory_used: Mapped[int] = mapped_column(
|
| Integer,
|
| default=0,
|
| doc="Currently used GPU memory in MB"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| default="registered",
|
| index=True,
|
| doc="Worker status"
|
| )
|
|
|
| last_heartbeat: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="Last heartbeat timestamp"
|
| )
|
|
|
| active_jobs: Mapped[int] = mapped_column(
|
| Integer,
|
| default=0,
|
| doc="Number of currently active jobs"
|
| )
|
|
|
| max_concurrent_jobs: Mapped[int] = mapped_column(
|
| Integer,
|
| default=1,
|
| doc="Maximum concurrent jobs this worker can handle"
|
| )
|
|
|
|
|
| gpu_usage_percent: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Current GPU usage percentage (0-100)"
|
| )
|
|
|
|
|
| capabilities: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Worker capabilities (supported models, etc.)"
|
| )
|
|
|
|
|
| worker_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional worker metadata"
|
| )
|
|
|
|
|
| registered_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When worker was registered"
|
| )
|
|
|
|
|
| version: Mapped[int] = mapped_column(
|
| Integer,
|
| default=1,
|
| doc="Worker version for optimistic locking"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<Worker(id={self.id}, worker_id={self.worker_id}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class AuditLog(Base):
|
| """
|
| Immutable audit log for tracking user actions.
|
|
|
| This table is append-only - no updates or deletes are allowed.
|
| All entries are immutable to maintain audit integrity.
|
| """
|
|
|
| __tablename__ = "audit_logs"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the audit log entry"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
|
|
| user_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("users.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="User who performed the action"
|
| )
|
|
|
|
|
| action: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| index=True,
|
| doc="Action performed (e.g., JOB_CREATED, USER_LOGIN)"
|
| )
|
|
|
| resource_type: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| index=True,
|
| doc="Type of resource affected (e.g., job, user, api_key)"
|
| )
|
|
|
| resource_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
| Uuid,
|
| nullable=True,
|
| doc="ID of the affected resource"
|
| )
|
|
|
|
|
| timestamp: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| index=True,
|
| doc="When the action occurred"
|
| )
|
|
|
|
|
| action_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional action metadata"
|
| )
|
|
|
|
|
| request_id: Mapped[Optional[str]] = mapped_column(
|
| String(255),
|
| nullable=True,
|
| index=True,
|
| doc="Request ID for tracing"
|
| )
|
|
|
| ip_address: Mapped[Optional[str]] = mapped_column(
|
| String(45),
|
| nullable=True,
|
| doc="Client IP address"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| default="success",
|
| doc="Action status: success, failure, pending"
|
| )
|
|
|
|
|
| error_message: Mapped[Optional[str]] = mapped_column(
|
| Text,
|
| nullable=True,
|
| doc="Error message if action failed"
|
| )
|
|
|
|
|
| entry_hash: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| index=True,
|
| doc="SHA256 hash for audit chain integrity verification"
|
| )
|
|
|
|
|
| previous_hash: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| doc="Hash of previous audit entry for chain linkage"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| doc="Parent tenant"
|
| )
|
|
|
| user: Mapped["User"] = relationship(
|
| "User",
|
| back_populates="audit_logs",
|
| doc="User who performed the action"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<AuditLog(id={self.id}, action={self.action}, tenant_id={self.tenant_id})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class TenantUsage(Base):
|
| """
|
| Tracks usage metrics per tenant for billing purposes.
|
|
|
| Stores monthly usage for GPU hours, API calls, evaluations, and storage.
|
| """
|
|
|
| __tablename__ = "tenant_usage"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the usage record"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
|
|
| year: Mapped[int] = mapped_column(
|
| Integer,
|
| nullable=False,
|
| doc="Year of the billing period"
|
| )
|
|
|
| month: Mapped[int] = mapped_column(
|
| Integer,
|
| nullable=False,
|
| doc="Month of the billing period (1-12)"
|
| )
|
|
|
|
|
| gpu_hours: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Total GPU hours used in this period"
|
| )
|
|
|
| api_calls: Mapped[int] = mapped_column(
|
| Integer,
|
| default=0,
|
| doc="Total API calls in this period"
|
| )
|
|
|
| evaluations: Mapped[int] = mapped_column(
|
| Integer,
|
| default=0,
|
| doc="Total evaluations performed in this period"
|
| )
|
|
|
| storage_gb: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Storage used in GB"
|
| )
|
|
|
| monitoring_events: Mapped[int] = mapped_column(
|
| Integer,
|
| default=0,
|
| doc="Total monitoring events in this period"
|
| )
|
|
|
|
|
| base_fee: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Base subscription fee for this period"
|
| )
|
|
|
| gpu_cost: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="GPU usage cost for this period"
|
| )
|
|
|
| evaluation_cost: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Evaluation overage cost for this period"
|
| )
|
|
|
| total_cost: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Total cost for this period"
|
| )
|
|
|
|
|
| quota_utilization_percent: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Overall quota utilization percentage"
|
| )
|
|
|
| is_over_quota: Mapped[bool] = mapped_column(
|
| default=False,
|
| doc="Whether usage exceeded quota this period"
|
| )
|
|
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When this record was created"
|
| )
|
|
|
| updated_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| onupdate=datetime.utcnow,
|
| doc="When this record was last updated"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| doc="Parent tenant"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<TenantUsage(tenant_id={self.tenant_id}, year={self.year}, month={self.month}, cost=${self.total_cost:.2f})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class Subscription(Base):
|
| """
|
| Tracks tenant subscription information.
|
|
|
| Stores subscription plan, period dates, and status.
|
| """
|
|
|
| __tablename__ = "subscriptions"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the subscription"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant"
|
| )
|
|
|
|
|
| plan_type: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| doc="Plan type: free, pro, enterprise, research_partner"
|
| )
|
|
|
|
|
| current_period_start: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="Start of current billing period"
|
| )
|
|
|
| current_period_end: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="End of current billing period"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| default="active",
|
| doc="Subscription status: active, trialing, past_due, cancelled, paused"
|
| )
|
|
|
|
|
| auto_renew: Mapped[bool] = mapped_column(
|
| default=True,
|
| doc="Whether subscription auto-renews"
|
| )
|
|
|
|
|
| feature_overrides: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Feature overrides for this subscription"
|
| )
|
|
|
|
|
| custom_gss_weights: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Custom GSS weight configuration"
|
| )
|
|
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When subscription was created"
|
| )
|
|
|
| updated_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| onupdate=datetime.utcnow,
|
| doc="When subscription was last updated"
|
| )
|
|
|
| cancelled_at: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="When subscription was cancelled"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| doc="Parent tenant"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<Subscription(tenant_id={self.tenant_id}, plan={self.plan_type}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class Invoice(Base):
|
| """
|
| Tracks invoices for billing.
|
|
|
| Stores invoice details, line items, and payment status.
|
| """
|
|
|
| __tablename__ = "invoices"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the invoice"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant"
|
| )
|
|
|
| invoice_number: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| unique=True,
|
| doc="Human-readable invoice number"
|
| )
|
|
|
|
|
| period_start: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="Start of billing period"
|
| )
|
|
|
| period_end: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="End of billing period"
|
| )
|
|
|
|
|
| subtotal: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Subtotal before tax"
|
| )
|
|
|
| tax: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Tax amount"
|
| )
|
|
|
| total: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Total amount due"
|
| )
|
|
|
| currency: Mapped[str] = mapped_column(
|
| String(3),
|
| default="USD",
|
| doc="Currency code"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| default="draft",
|
| doc="Invoice status: draft, issued, paid, overdue, cancelled, refunded"
|
| )
|
|
|
|
|
| paid_at: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="When invoice was paid"
|
| )
|
|
|
| payment_method: Mapped[Optional[str]] = mapped_column(
|
| String(50),
|
| nullable=True,
|
| doc="Payment method used"
|
| )
|
|
|
| due_date: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="Payment due date"
|
| )
|
|
|
|
|
| line_items: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Invoice line items"
|
| )
|
|
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When invoice was created"
|
| )
|
|
|
| issued_at: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="When invoice was issued"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| doc="Parent tenant"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<Invoice(invoice_number={self.invoice_number}, tenant_id={self.tenant_id}, total=${self.total:.2f}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class Contract(Base):
|
| """
|
| Tracks enterprise contracts with custom terms.
|
|
|
| Stores contract metadata, custom pricing, and SLA terms.
|
| """
|
|
|
| __tablename__ = "contracts"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the contract"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant"
|
| )
|
|
|
|
|
| contract_number: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| unique=True,
|
| doc="Human-readable contract number"
|
| )
|
|
|
| contract_type: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| doc="Contract type: standard, custom, reseller, research, trial"
|
| )
|
|
|
|
|
| start_date: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="Contract start date"
|
| )
|
|
|
| end_date: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="Contract end date"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| default="draft",
|
| doc="Contract status: draft, pending, active, expired, terminated, renewed"
|
| )
|
|
|
|
|
| custom_base_fee: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Custom monthly base fee"
|
| )
|
|
|
| custom_gpu_rate: Mapped[Optional[float]] = mapped_column(
|
| Float,
|
| nullable=True,
|
| doc="Custom GPU rate per hour"
|
| )
|
|
|
| discount_percent: Mapped[float] = mapped_column(
|
| Float,
|
| default=0.0,
|
| doc="Discount percentage"
|
| )
|
|
|
|
|
| custom_evaluations_quota: Mapped[Optional[int]] = mapped_column(
|
| Integer,
|
| nullable=True,
|
| doc="Custom evaluations quota"
|
| )
|
|
|
| custom_gpu_hours_quota: Mapped[Optional[int]] = mapped_column(
|
| Integer,
|
| nullable=True,
|
| doc="Custom GPU hours quota"
|
| )
|
|
|
| custom_concurrent_jobs: Mapped[Optional[int]] = mapped_column(
|
| Integer,
|
| nullable=True,
|
| doc="Custom concurrent jobs limit"
|
| )
|
|
|
|
|
| sla_uptime_percent: Mapped[float] = mapped_column(
|
| Float,
|
| default=99.9,
|
| doc="SLA uptime percentage"
|
| )
|
|
|
| sla_max_job_start_delay_seconds: Mapped[int] = mapped_column(
|
| Integer,
|
| default=60,
|
| doc="Max job start delay in seconds"
|
| )
|
|
|
| sla_incident_response_hours: Mapped[int] = mapped_column(
|
| Integer,
|
| default=4,
|
| doc="Incident response time in hours"
|
| )
|
|
|
| sla_support_level: Mapped[str] = mapped_column(
|
| String(20),
|
| default="community",
|
| doc="Support level: community, email, 24x7"
|
| )
|
|
|
|
|
| data_retention_days: Mapped[int] = mapped_column(
|
| Integer,
|
| default=90,
|
| doc="Data retention days"
|
| )
|
|
|
| allow_data_export: Mapped[bool] = mapped_column(
|
| default=True,
|
| doc="Whether data export is allowed"
|
| )
|
|
|
|
|
| dedicated_cluster: Mapped[bool] = mapped_column(
|
| default=False,
|
| doc="Whether dedicated cluster is provided"
|
| )
|
|
|
|
|
| notes: Mapped[Optional[str]] = mapped_column(
|
| Text,
|
| nullable=True,
|
| doc="Contract notes"
|
| )
|
|
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When contract was created"
|
| )
|
|
|
| updated_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| onupdate=datetime.utcnow,
|
| doc="When contract was last updated"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| doc="Parent tenant"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<Contract(contract_number={self.contract_number}, tenant_id={self.tenant_id}, type={self.contract_type}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class BillingEvent(Base):
|
| """
|
| Immutable billing event log for audit trail.
|
|
|
| This table is append-only - no updates or deletes are allowed.
|
| All entries are immutable to maintain billing audit integrity.
|
|
|
| Schema: tenant_id, event_type, amount, currency, timestamp, status, payment_provider_id
|
| """
|
|
|
| __tablename__ = "billing_events"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the billing event"
|
| )
|
|
|
|
|
| tenant_id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| ForeignKey("tenants.id", ondelete="CASCADE"),
|
| nullable=False,
|
| index=True,
|
| doc="Reference to parent tenant for isolation"
|
| )
|
|
|
|
|
| event_type: Mapped[str] = mapped_column(
|
| String(50),
|
| nullable=False,
|
| index=True,
|
| doc="Event type: payment_succeeded, payment_failed, subscription_created, subscription_cancelled, invoice_issued, invoice_paid, refund_issued, grace_period_started, grace_period_ended, account_suspended, account_reactivated"
|
| )
|
|
|
|
|
| amount: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Event amount"
|
| )
|
|
|
| currency: Mapped[str] = mapped_column(
|
| String(3),
|
| default="USD",
|
| doc="Currency code (ISO 4217)"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| doc="Event status: pending, completed, failed, cancelled, refunded"
|
| )
|
|
|
|
|
| payment_provider_id: Mapped[Optional[str]] = mapped_column(
|
| String(100),
|
| nullable=True,
|
| doc="Payment provider's reference ID (e.g., Stripe payment intent ID)"
|
| )
|
|
|
|
|
| subscription_id: Mapped[Optional[str]] = mapped_column(
|
| String(100),
|
| nullable=True,
|
| doc="Subscription ID from payment provider"
|
| )
|
|
|
|
|
| invoice_id: Mapped[Optional[str]] = mapped_column(
|
| String(100),
|
| nullable=True,
|
| doc="Invoice ID if applicable"
|
| )
|
|
|
|
|
| customer_id: Mapped[Optional[str]] = mapped_column(
|
| String(100),
|
| nullable=True,
|
| doc="Customer ID from payment provider"
|
| )
|
|
|
|
|
| plan_type: Mapped[Optional[str]] = mapped_column(
|
| String(50),
|
| nullable=True,
|
| doc="Plan type if applicable"
|
| )
|
|
|
|
|
| failure_reason: Mapped[Optional[str]] = mapped_column(
|
| String(255),
|
| nullable=True,
|
| doc="Failure reason if payment failed"
|
| )
|
|
|
|
|
| event_metadata: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Additional event metadata"
|
| )
|
|
|
|
|
| timestamp: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| index=True,
|
| doc="When the event occurred"
|
| )
|
|
|
|
|
| event_hash: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| doc="SHA256 hash for billing event chain integrity verification"
|
| )
|
|
|
|
|
| previous_hash: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| doc="Hash of previous billing event for chain linkage"
|
| )
|
|
|
|
|
| tenant: Mapped["Tenant"] = relationship(
|
| "Tenant",
|
| doc="Parent tenant"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<BillingEvent(id={self.id}, tenant_id={self.tenant_id}, event_type={self.event_type}, amount={self.amount} {self.currency}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| class Certificate(Base):
|
| """
|
| Stores public certification records for the AegisLM certification registry.
|
|
|
| These certificates are publicly verifiable and contain the evaluation
|
| results for AI models under the GSS-1 standard.
|
| """
|
|
|
| __tablename__ = "certificates"
|
|
|
| id: Mapped[uuid.UUID] = mapped_column(
|
| Uuid,
|
| primary_key=True,
|
| default=uuid.uuid4,
|
| doc="Unique identifier for the certificate"
|
| )
|
|
|
|
|
| certificate_id: Mapped[str] = mapped_column(
|
| String(36),
|
| nullable=False,
|
| unique=True,
|
| index=True,
|
| doc="Public certificate ID (UUID format)"
|
| )
|
|
|
|
|
| model_name: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| index=True,
|
| doc="Name of the certified model"
|
| )
|
|
|
| model_version: Mapped[str] = mapped_column(
|
| String(100),
|
| nullable=False,
|
| doc="Version of the model"
|
| )
|
|
|
| organization: Mapped[str] = mapped_column(
|
| String(255),
|
| nullable=False,
|
| index=True,
|
| doc="Organization owning the model"
|
| )
|
|
|
|
|
| gss_score: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Composite Robustness Score (R) from GSS-1"
|
| )
|
|
|
| RSI: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Robustness Stability Index"
|
| )
|
|
|
| RiskIndex: Mapped[float] = mapped_column(
|
| Float,
|
| nullable=False,
|
| doc="Risk Index from GSS-1"
|
| )
|
|
|
|
|
| certification_tier: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| index=True,
|
| doc="GSS certification tier: Tier A, Tier B, Tier C, Tier D"
|
| )
|
|
|
| risk_tier: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| index=True,
|
| doc="Risk tier: LOW, MODERATE, HIGH, CRITICAL"
|
| )
|
|
|
|
|
| individual_metrics: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Individual component scores (H, T, B, C)"
|
| )
|
|
|
|
|
| evaluation_date: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="Date of evaluation"
|
| )
|
|
|
| valid_until: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| nullable=False,
|
| doc="Certificate validity end date"
|
| )
|
|
|
|
|
| audit_hash: Mapped[str] = mapped_column(
|
| String(64),
|
| nullable=False,
|
| doc="SHA256 hash of the evaluation audit trail"
|
| )
|
|
|
| verification_signature: Mapped[str] = mapped_column(
|
| String(256),
|
| nullable=False,
|
| doc="Digital signature for certificate verification"
|
| )
|
|
|
|
|
| gss_version: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| default="1.0",
|
| doc="GSS version used for evaluation"
|
| )
|
|
|
| dataset_hash: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| doc="SHA256 hash of the dataset used"
|
| )
|
|
|
| config_hash: Mapped[Optional[str]] = mapped_column(
|
| String(64),
|
| nullable=True,
|
| doc="SHA256 hash of evaluation configuration"
|
| )
|
|
|
| sample_size: Mapped[Optional[int]] = mapped_column(
|
| Integer,
|
| nullable=True,
|
| doc="Number of samples evaluated"
|
| )
|
|
|
|
|
| confidence_interval: Mapped[Optional[Dict[str, Any]]] = mapped_column(
|
| JSON,
|
| nullable=True,
|
| doc="Statistical confidence interval"
|
| )
|
|
|
|
|
| status: Mapped[str] = mapped_column(
|
| String(20),
|
| nullable=False,
|
| default="active",
|
| index=True,
|
| doc="Certificate status: active, revoked, expired"
|
| )
|
|
|
| revocation_reason: Mapped[Optional[str]] = mapped_column(
|
| String(255),
|
| nullable=True,
|
| doc="Reason for revocation if status is revoked"
|
| )
|
|
|
| revoked_at: Mapped[Optional[datetime]] = mapped_column(
|
| DateTime,
|
| nullable=True,
|
| doc="Timestamp when certificate was revoked"
|
| )
|
|
|
|
|
| sector: Mapped[Optional[str]] = mapped_column(
|
| String(50),
|
| nullable=True,
|
| index=True,
|
| doc="Industry sector"
|
| )
|
|
|
| model_size: Mapped[Optional[str]] = mapped_column(
|
| String(20),
|
| nullable=True,
|
| doc="Model size category: small, medium, large, xl"
|
| )
|
|
|
|
|
| created_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| doc="When certificate was created"
|
| )
|
|
|
| updated_at: Mapped[datetime] = mapped_column(
|
| DateTime,
|
| default=datetime.utcnow,
|
| onupdate=datetime.utcnow,
|
| doc="When certificate was last updated"
|
| )
|
|
|
| def __repr__(self) -> str:
|
| return f"<Certificate(id={self.id}, model={self.model_name}, tier={self.certification_tier}, status={self.status})>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| async def create_tables():
|
| """Create all tables in the database."""
|
| from backend.db.session import create_database_engine
|
|
|
| engine = create_database_engine()
|
| async with engine.begin() as conn:
|
| await conn.run_sync(Base.metadata.create_all)
|
| await engine.dispose()
|
|
|