# Generated by Django 5.2.9 on 2025-12-28 09:35

import django.db.models.deletion
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("grpc", "0008_grpcrequestlog_error_details"),
    ]

    operations = [
        migrations.CreateModel(
            name="GrpcAgentConnectionState",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
                    ),
                ),
                (
                    "machine_id",
                    models.CharField(
                        db_index=True,
                        help_text="Unique machine identifier (e.g., hostname or UUID)",
                        max_length=255,
                        unique=True,
                    ),
                ),
                (
                    "machine_name",
                    models.CharField(
                        db_index=True, help_text="Human-readable machine name", max_length=255
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("connected", "Connected"),
                            ("disconnected", "Disconnected"),
                            ("reconnecting", "Reconnecting"),
                            ("error", "Error"),
                            ("unknown", "Unknown"),
                        ],
                        db_index=True,
                        default="unknown",
                        help_text="Current connection status",
                        max_length=20,
                    ),
                ),
                (
                    "last_known_ip",
                    models.GenericIPAddressField(
                        blank=True, help_text="Last known IP address", null=True
                    ),
                ),
                (
                    "client_version",
                    models.CharField(
                        blank=True, default="", help_text="Agent client version", max_length=50
                    ),
                ),
                (
                    "first_connected_at",
                    models.DateTimeField(
                        auto_now_add=True, help_text="First time this machine connected"
                    ),
                ),
                (
                    "last_connected_at",
                    models.DateTimeField(
                        blank=True, help_text="Last successful connection time", null=True
                    ),
                ),
                (
                    "last_disconnected_at",
                    models.DateTimeField(
                        blank=True, help_text="Last disconnection time", null=True
                    ),
                ),
                (
                    "last_error_message",
                    models.TextField(blank=True, default="", help_text="Last error message"),
                ),
                (
                    "last_error_at",
                    models.DateTimeField(blank=True, help_text="Last error timestamp", null=True),
                ),
                (
                    "consecutive_error_count",
                    models.PositiveIntegerField(
                        default=0, help_text="Number of consecutive errors"
                    ),
                ),
                (
                    "current_rtt_ms",
                    models.FloatField(
                        blank=True, help_text="Current round-trip time in milliseconds", null=True
                    ),
                ),
                (
                    "current_packet_loss_percent",
                    models.FloatField(
                        blank=True, help_text="Current packet loss percentage", null=True
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "verbose_name": "gRPC Agent Connection State",
                "verbose_name_plural": "gRPC Agent Connection States",
                "ordering": ["-last_connected_at"],
                "indexes": [
                    models.Index(
                        fields=["status", "-last_connected_at"],
                        name="grpc_grpcag_status_8b0920_idx",
                    ),
                    models.Index(fields=["machine_id"], name="grpc_grpcag_machine_32d029_idx"),
                ],
            },
        ),
        migrations.CreateModel(
            name="GrpcAgentConnectionMetric",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
                    ),
                ),
                (
                    "timestamp",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, help_text="Metric sample time"
                    ),
                ),
                (
                    "rtt_min_ms",
                    models.FloatField(
                        blank=True, help_text="Minimum RTT in milliseconds", null=True
                    ),
                ),
                (
                    "rtt_max_ms",
                    models.FloatField(
                        blank=True, help_text="Maximum RTT in milliseconds", null=True
                    ),
                ),
                (
                    "rtt_mean_ms",
                    models.FloatField(blank=True, help_text="Mean RTT in milliseconds", null=True),
                ),
                (
                    "rtt_stddev_ms",
                    models.FloatField(
                        blank=True, help_text="RTT standard deviation in milliseconds", null=True
                    ),
                ),
                (
                    "packet_loss_percent",
                    models.FloatField(blank=True, help_text="Packet loss percentage", null=True),
                ),
                (
                    "packets_sent",
                    models.IntegerField(
                        blank=True, help_text="Total packets sent in sample window", null=True
                    ),
                ),
                (
                    "packets_received",
                    models.IntegerField(
                        blank=True, help_text="Total packets received in sample window", null=True
                    ),
                ),
                (
                    "keepalive_sent",
                    models.IntegerField(default=0, help_text="Keepalive pings sent"),
                ),
                (
                    "keepalive_ack",
                    models.IntegerField(default=0, help_text="Keepalive acknowledgments received"),
                ),
                (
                    "keepalive_timeout",
                    models.IntegerField(default=0, help_text="Keepalive timeouts"),
                ),
                (
                    "active_streams",
                    models.IntegerField(
                        blank=True, help_text="Number of active streams", null=True
                    ),
                ),
                (
                    "failed_streams",
                    models.IntegerField(
                        default=0, help_text="Number of failed streams in sample window"
                    ),
                ),
                (
                    "health_status",
                    models.CharField(
                        choices=[
                            ("healthy", "Healthy"),
                            ("degraded", "Degraded"),
                            ("poor", "Poor"),
                            ("unknown", "Unknown"),
                        ],
                        db_index=True,
                        default="unknown",
                        help_text="Calculated health status",
                        max_length=20,
                    ),
                ),
                (
                    "sample_window_seconds",
                    models.IntegerField(
                        default=30, help_text="Duration of sample window in seconds"
                    ),
                ),
                (
                    "connection_state",
                    models.ForeignKey(
                        help_text="Parent connection state",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="metrics",
                        to="grpc.grpcagentconnectionstate",
                    ),
                ),
            ],
            options={
                "verbose_name": "gRPC Agent Connection Metric",
                "verbose_name_plural": "gRPC Agent Connection Metrics",
                "ordering": ["-timestamp"],
                "indexes": [
                    models.Index(
                        fields=["connection_state", "-timestamp"],
                        name="grpc_grpcag_connect_c201f0_idx",
                    ),
                    models.Index(
                        fields=["health_status", "-timestamp"],
                        name="grpc_grpcag_health__ad467a_idx",
                    ),
                ],
            },
        ),
        migrations.CreateModel(
            name="GrpcAgentConnectionEvent",
            fields=[
                (
                    "id",
                    models.UUIDField(
                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
                    ),
                ),
                (
                    "event_type",
                    models.CharField(
                        choices=[
                            ("connected", "Connected"),
                            ("disconnected", "Disconnected"),
                            ("reconnecting", "Reconnecting"),
                            ("error", "Error"),
                        ],
                        db_index=True,
                        help_text="Type of event",
                        max_length=20,
                    ),
                ),
                (
                    "timestamp",
                    models.DateTimeField(
                        auto_now_add=True, db_index=True, help_text="When event occurred"
                    ),
                ),
                (
                    "ip_address",
                    models.GenericIPAddressField(
                        blank=True, help_text="IP address at time of event", null=True
                    ),
                ),
                (
                    "client_version",
                    models.CharField(
                        blank=True,
                        default="",
                        help_text="Client version at time of event",
                        max_length=50,
                    ),
                ),
                (
                    "error_message",
                    models.TextField(
                        blank=True, default="", help_text="Error message if event_type is error"
                    ),
                ),
                (
                    "error_code",
                    models.CharField(
                        blank=True,
                        default="",
                        help_text="Error code (e.g., gRPC status code)",
                        max_length=50,
                    ),
                ),
                (
                    "error_details",
                    models.JSONField(
                        blank=True, help_text="Additional error details as JSON", null=True
                    ),
                ),
                (
                    "duration_seconds",
                    models.IntegerField(
                        blank=True,
                        help_text="Connection duration before this disconnect (seconds)",
                        null=True,
                    ),
                ),
                (
                    "connection_state",
                    models.ForeignKey(
                        help_text="Parent connection state",
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="events",
                        to="grpc.grpcagentconnectionstate",
                    ),
                ),
            ],
            options={
                "verbose_name": "gRPC Agent Connection Event",
                "verbose_name_plural": "gRPC Agent Connection Events",
                "ordering": ["-timestamp"],
                "indexes": [
                    models.Index(
                        fields=["connection_state", "-timestamp"],
                        name="grpc_grpcag_connect_1e3875_idx",
                    ),
                    models.Index(
                        fields=["event_type", "-timestamp"], name="grpc_grpcag_event_t_d28969_idx"
                    ),
                ],
            },
        ),
    ]
