from tortoise import fields
from tortoise.models import Model


class StatsTotal(Model):
    id = fields.IntField(pk=True)
    source = fields.CharField(max_length=32)
    table_type = fields.CharField(max_length=32)
    count = fields.IntField()
    ts = fields.DatetimeField()


class IpUploadStats(Model):
    id = fields.IntField(pk=True)
    source = fields.CharField(max_length=32)
    ip = fields.CharField(max_length=64)
    date = fields.DateField()
    upload_count = fields.IntField()
    last_report_at = fields.DatetimeField(null=True)
    status = fields.CharField(max_length=16, default="normal")
    created_at = fields.DatetimeField(auto_now_add=True)

    class Meta:
        unique_together = ("source", "ip", "date")


class ScheduledTaskRun(Model):
    id = fields.IntField(pk=True)
    task_id = fields.CharField(max_length=64)
    task_name = fields.CharField(max_length=64)
    status = fields.CharField(max_length=32)
    started_at = fields.DatetimeField()
    finished_at = fields.DatetimeField()
    duration_ms = fields.IntField()
    error = fields.TextField(null=True)