class User(BaseModel): id = peewee.IntegerField(primary_key=True) first_name = peewee.CharField(max_length=32, null=True) last_name = peewee.CharField(max_length=32, null=True) created = DateTimeField(default=datetime.datetime.now) updated = DateTimeField(default=datetime.datetime.now) def __str__(self): return '<User #{id} {first_name}>'.format( id=self.id, first_name=self.first_name, ) @property def name(self): _names = list() if self.first_name: _names.append(str(self.first_name)) if self.last_name: _names.append(str(self.last_name)) return ' '.join(_names) def save(self, *args, **kwargs): self.updated = datetime.datetime.now() return super(User, self).save(*args, **kwargs)
class Job(Model): name = CharField(max_length=500) description = TextField(null=True, default='') role = CharField(max_length=50, index=True) application_id = CharField(max_length=500, null=True) model_version_id = ForeignKeyField(ModelVersion, backref='model_version') serving_version = CharField(max_length=500) data_source_id = ForeignKeyField(DataSourceMeta, backref='data_source_meta') cluster_spec = TextField() status = CharField(max_length=50) k8s_status = CharField(max_length=50, default='unknown') progress = IntegerField(null=True, default=0) group_list = TextField(null=False, default='{}') create_time = DateTimeField(default=datetime.datetime.now) update_time = DateTimeField(null=True) start_time = DateTimeField(null=True) end_time = DateTimeField(null=True) class Meta: database = DB db_table = "job" def to_json(self): return self.__dict__['__data__'] def save(self, *args, **kwargs): self.update_time = datetime.datetime.now() super(Job, self).save(*args, **kwargs)
class Feed(BaseModel): url = CharField() save_path = CharField() title = CharField(default='No name') create_time = DateTimeField(default=datetime.datetime.now) keywords = CharField(default='[]') last_check = DateTimeField(default=datetime.datetime.now) last_add = DateTimeField(default=datetime.datetime.now)
class ChatUser(BaseModel): chat = peewee.ForeignKeyField(Chat, backref='chat_users') user = peewee.ForeignKeyField(User, backref='chat_users') created = DateTimeField(default=datetime.datetime.now) updated = DateTimeField(default=datetime.datetime.now) def __str__(self): return '<ChatUser #{id}>'.format(id=self.id, ) def save(self, *args, **kwargs): self.updated = datetime.datetime.now() return super(ChatUser, self).save(*args, **kwargs)
class Run(BaseModel): FAILED = -2 CANCELED = -1 PENDING = 0 SUBMITTED = 1 RUNNING = 2 DONE = 3 STATUS_CHOICES = ( (FAILED, "Failed"), (CANCELED, "Canceled"), (PENDING, "Pending"), (SUBMITTED, "Submitted"), (RUNNING, "Running"), (DONE, "Done"), ) id = CharField(null=True, primary_key=True) created_at = DateTimeField(default=datetime.now) tool = ForeignKeyField(Tool, backref="runs") tool_version = CharField(null=True) parameter_group = ForeignKeyField(ParameterGroup, backref="runs") task = ForeignKeyField(Task, backref="runs") status = IntegerField(choices=STATUS_CHOICES, default=PENDING) last_step = ForeignKeyField(Step, null=True) iteration = IntegerField(default=0)
class Race(ObraModel): """ A single race at an event, with one or more results. """ id = IntegerField(verbose_name='Race ID', primary_key=True) name = CharField(verbose_name='Race Name', index=True) date = DateField(verbose_name='Race Date') categories = JSONField(verbose_name='Race Categories') starters = IntegerField(verbose_name='Race Starting Field Size', default=0) created = DateTimeField(verbose_name='Results Created') updated = DateTimeField(verbose_name='Results Updated') event = ForeignKeyField(verbose_name='Race Event', model=Event, backref='races', on_update='RESTRICT', on_delete='RESTRICT')
class RunStatistic(BaseModel): TIMEOUT = "TLE" MEMOUT = "MEM" RUNTIME_ERR = "RTE" OUTPUT_LIMIT = "OLE" SUCCESS = "OK" VERDICT_CHOICES = ( (TIMEOUT, "Time Limit Exceeded"), (MEMOUT, "Memory Limit Exceeded"), (RUNTIME_ERR, "Runtime Error"), (OUTPUT_LIMIT, "Output Limit Exceeded"), (SUCCESS, "Run Successfully"), ) created_at = DateTimeField(default=datetime.now) run = ForeignKeyField(Run, backref="statistics", on_delete="cascade", primary_key=True) cpu_time = FloatField(help_text="CPU Time (s)", null=True) wall_time = FloatField(help_text="Wall Clock Time (s)", null=True) max_memory = FloatField(help_text="Max Memory Usage (KiB)", null=True) return_code = IntegerField(help_text="Process Return Code", null=True) verdict = CharField(choices=VERDICT_CHOICES, max_length=3, null=True)
class DataSourceMeta(Model): name = CharField(max_length=500) description = TextField(null=True, default='') path = TextField() create_time = DateTimeField(default=datetime.datetime.now) update_time = DateTimeField(null=True) class Meta: database = DB db_table = "data_source_meta" def __str__(self): return str(self.__dict__['__data__']) def save(self, *args, **kwargs): self.update_time = datetime.datetime.now() super(DataSourceMeta, self).save(*args, **kwargs)
class ModelVersion(Model): model_meta_id = ForeignKeyField(ModelMeta, backref='model_version') commit = CharField(max_length=500) description = CharField(max_length=500) create_time = DateTimeField(default=datetime.datetime.now) update_time = DateTimeField(null=True) class Meta: database = DB db_table = "model_version" def __str__(self): return str(self.__dict__['__data__']) def save(self, *args, **kwargs): self.update_time = datetime.datetime.now() super(ModelVersion, self).save(*args, **kwargs)
class Chat(BaseModel): id = peewee.IntegerField(primary_key=True) type = peewee.CharField(max_length=16) title = peewee.CharField(null=True) clean = peewee.BooleanField(default=False) created = DateTimeField(default=datetime.datetime.now) updated = DateTimeField(default=datetime.datetime.now) def __str__(self): return '<Chat #{id} {title}>'.format( id=self.id, title=self.title, ) def save(self, *args, **kwargs): self.updated = datetime.datetime.now() return super(Chat, self).save(*args, **kwargs)
class MigrateHistory(Model): """ Presents the migrations in database. """ name = CharField() migrated_at = DateTimeField(default=dt.datetime.utcnow) class Meta: database = Router.proxy
class BaseModel(Model): f_create_time = BigIntegerField(null=True) f_create_date = DateTimeField(null=True) f_update_time = BigIntegerField(null=True) f_update_date = DateTimeField(null=True) def to_json(self): return self.__dict__['__data__'] def to_human_model_dict(self, only_primary_with: list = None): model_dict = self.__dict__["__data__"] human_model_dict = {} if not only_primary_with: for k, v in model_dict.items(): human_model_dict[k.lstrip("f").lstrip("_")] = v else: for k in self._meta.primary_key.field_names: human_model_dict[k.lstrip("f").lstrip("_")] = model_dict[k] for k in only_primary_with: human_model_dict[k] = model_dict["f_%s" % k] return human_model_dict @classmethod def get_primary_keys_name(cls): return cls._meta.primary_key.field_names if isinstance( cls._meta.primary_key, CompositeKey) else [cls._meta.primary_key.name] def save(self, *args, **kwargs): if self.f_create_time: self.f_create_date = timestamp_to_date(self.f_create_time) self.f_update_time = current_timestamp() self.f_update_date = timestamp_to_date(self.f_update_time) return super(BaseModel, self).save(*args, **kwargs) @classmethod def update(cls, __data=None, **update): for f_n in {"start", "end"}: if hasattr(cls, f"f_{f_n}_time") and hasattr(cls, f"f_{f_n}_date"): k = operator.attrgetter(f"f_{f_n}_time")(cls) if k in __data and __data[k]: __data[operator.attrgetter(f"f_{f_n}_date")( cls)] = timestamp_to_date(__data[k]) return super().update(__data, **update)
class BaseModel(Model): f_create_time = BigIntegerField(null=True) f_create_date = DateTimeField(null=True) f_update_time = BigIntegerField(null=True) f_update_date = DateTimeField(null=True) def to_json(self): # This function is obsolete return self.to_dict() def to_dict(self): return self.__dict__['__data__'] def to_human_model_dict(self, only_primary_with: list = None): model_dict = self.__dict__['__data__'] if not only_primary_with: return {remove_field_name_prefix(k): v for k, v in model_dict.items()} human_model_dict = {} for k in self._meta.primary_key.field_names: human_model_dict[remove_field_name_prefix(k)] = model_dict[k] for k in only_primary_with: human_model_dict[k] = model_dict[f'f_{k}'] return human_model_dict @property def meta(self) -> Metadata: return self._meta @classmethod def get_primary_keys_name(cls): return cls._meta.primary_key.field_names if isinstance(cls._meta.primary_key, CompositeKey) else [ cls._meta.primary_key.name] @classmethod def getter_by(cls, attr): return operator.attrgetter(attr)(cls) @classmethod def query(cls, reverse=None, order_by=None, **kwargs): filters = [] for f_n, f_v in kwargs.items(): attr_name = 'f_%s' % f_n if not hasattr(cls, attr_name) or f_v is None: continue if type(f_v) in {list, set}: f_v = list(f_v) if is_continuous_field(type(getattr(cls, attr_name))): if len(f_v) == 2: for i, v in enumerate(f_v): if isinstance(v, str) and f_n in auto_date_timestamp_field(): # time type: %Y-%m-%d %H:%M:%S f_v[i] = date_string_to_timestamp(v) lt_value = f_v[0] gt_value = f_v[1] if lt_value is not None and gt_value is not None: filters.append(cls.getter_by(attr_name).between(lt_value, gt_value)) elif lt_value is not None: filters.append(operator.attrgetter(attr_name)(cls) >= lt_value) elif gt_value is not None: filters.append(operator.attrgetter(attr_name)(cls) <= gt_value) else: filters.append(operator.attrgetter(attr_name)(cls) << f_v) else: filters.append(operator.attrgetter(attr_name)(cls) == f_v) if filters: query_records = cls.select().where(*filters) if reverse is not None: if not order_by or not hasattr(cls, f"f_{order_by}"): order_by = "create_time" if reverse is True: query_records = query_records.order_by(cls.getter_by(f"f_{order_by}").desc()) elif reverse is False: query_records = query_records.order_by(cls.getter_by(f"f_{order_by}").asc()) return [query_record for query_record in query_records] else: return [] def save(self, *args, **kwargs): self.f_update_time = current_timestamp() for f_n in AUTO_DATE_TIMESTAMP_FIELD_PREFIX: if getattr(self, f"f_{f_n}_time", None) and hasattr(self, f"f_{f_n}_date"): setattr(self, f"f_{f_n}_date", timestamp_to_date(getattr(self, f"f_{f_n}_time"))) return super(BaseModel, self).save(*args, **kwargs) @classmethod def update(cls, __data=None, **update): if __data: if hasattr(cls, "f_update_time"): __data[operator.attrgetter("f_update_time")(cls)] = current_timestamp() fields = AUTO_DATE_TIMESTAMP_FIELD_PREFIX.copy() # create can not be updated fields.remove("create") for f_n in fields: if hasattr(cls, f"f_{f_n}_time") and hasattr(cls, f"f_{f_n}_date"): k = operator.attrgetter(f"f_{f_n}_time")(cls) if k in __data and __data[k]: __data[operator.attrgetter(f"f_{f_n}_date")(cls)] = timestamp_to_date(__data[k]) return super().update(__data, **update)
class SudokuVerdict(BaseModel): created_at = DateTimeField(default=datetime.now) run = ForeignKeyField(Run, backref="sudoku_verdicts", on_delete="cascade") is_valid = BooleanField()
class Item(BaseModel): title = CharField() magnet_link = CharField() feed = ForeignKeyField(Feed) seen_time = DateTimeField(default=datetime.datetime.now) publish_time = DateTimeField()