Improve the frontend
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""add_supplier_trust_metrics
|
||||
|
||||
Revision ID: add_supplier_trust_metrics
|
||||
Revises: 93d6ea3dc888
|
||||
Create Date: 2025-10-20 12:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'add_supplier_trust_metrics'
|
||||
down_revision = '93d6ea3dc888'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Add trust and auto-approval metrics to suppliers table"""
|
||||
|
||||
# Add trust and auto-approval metric columns
|
||||
op.add_column('suppliers', sa.Column('trust_score', sa.Float(), nullable=False, server_default='0.0'))
|
||||
op.add_column('suppliers', sa.Column('is_preferred_supplier', sa.Boolean(), nullable=False, server_default='false'))
|
||||
op.add_column('suppliers', sa.Column('auto_approve_enabled', sa.Boolean(), nullable=False, server_default='false'))
|
||||
op.add_column('suppliers', sa.Column('total_pos_count', sa.Integer(), nullable=False, server_default='0'))
|
||||
op.add_column('suppliers', sa.Column('approved_pos_count', sa.Integer(), nullable=False, server_default='0'))
|
||||
op.add_column('suppliers', sa.Column('on_time_delivery_rate', sa.Float(), nullable=False, server_default='0.0'))
|
||||
op.add_column('suppliers', sa.Column('fulfillment_rate', sa.Float(), nullable=False, server_default='0.0'))
|
||||
op.add_column('suppliers', sa.Column('last_performance_update', sa.DateTime(timezone=True), nullable=True))
|
||||
|
||||
# Create index for trust score queries
|
||||
op.create_index('ix_suppliers_trust_score', 'suppliers', ['trust_score'], unique=False)
|
||||
op.create_index('ix_suppliers_preferred', 'suppliers', ['is_preferred_supplier'], unique=False)
|
||||
op.create_index('ix_suppliers_auto_approve', 'suppliers', ['auto_approve_enabled'], unique=False)
|
||||
|
||||
# Update existing active suppliers to have reasonable default trust scores
|
||||
# Suppliers with good ratings and history get higher initial trust
|
||||
op.execute("""
|
||||
UPDATE suppliers
|
||||
SET
|
||||
trust_score = LEAST(1.0, GREATEST(0.0,
|
||||
(COALESCE(quality_rating, 0) / 5.0 * 0.4) +
|
||||
(COALESCE(delivery_rating, 0) / 5.0 * 0.4) +
|
||||
(CASE WHEN total_orders > 10 THEN 0.2 ELSE total_orders / 50.0 END)
|
||||
)),
|
||||
is_preferred_supplier = (
|
||||
total_orders >= 10 AND
|
||||
quality_rating >= 4.0 AND
|
||||
delivery_rating >= 4.0 AND
|
||||
status = 'active'
|
||||
),
|
||||
auto_approve_enabled = (
|
||||
total_orders >= 20 AND
|
||||
quality_rating >= 4.5 AND
|
||||
delivery_rating >= 4.5 AND
|
||||
status = 'active'
|
||||
),
|
||||
total_pos_count = total_orders,
|
||||
approved_pos_count = total_orders,
|
||||
on_time_delivery_rate = COALESCE(delivery_rating / 5.0, 0.0),
|
||||
fulfillment_rate = COALESCE(quality_rating / 5.0, 0.0),
|
||||
last_performance_update = NOW()
|
||||
WHERE status = 'active'
|
||||
""")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Remove trust and auto-approval metrics from suppliers table"""
|
||||
|
||||
# Drop indexes
|
||||
op.drop_index('ix_suppliers_auto_approve', table_name='suppliers')
|
||||
op.drop_index('ix_suppliers_preferred', table_name='suppliers')
|
||||
op.drop_index('ix_suppliers_trust_score', table_name='suppliers')
|
||||
|
||||
# Drop columns
|
||||
op.drop_column('suppliers', 'last_performance_update')
|
||||
op.drop_column('suppliers', 'fulfillment_rate')
|
||||
op.drop_column('suppliers', 'on_time_delivery_rate')
|
||||
op.drop_column('suppliers', 'approved_pos_count')
|
||||
op.drop_column('suppliers', 'total_pos_count')
|
||||
op.drop_column('suppliers', 'auto_approve_enabled')
|
||||
op.drop_column('suppliers', 'is_preferred_supplier')
|
||||
op.drop_column('suppliers', 'trust_score')
|
||||
Reference in New Issue
Block a user