class User(BaseModel): user_id = BigAutoField(primary_key=True) username = CharField(33) password = CharField(60) first_login_sub = BooleanField(default=True) free_to_play = BooleanField(default=False) front_character = SmallIntegerField(default=0)
class ComponentSummary(DataBaseModel): _mapper = {} @classmethod def model(cls, table_index=None, date=None): if not table_index: table_index = date.strftime( '%Y%m%d') if date else datetime.datetime.now().strftime( '%Y%m%d') class_name = 'ComponentSummary_%s' % table_index ModelClass = TrackingMetric._mapper.get(class_name, None) if ModelClass is None: class Meta: db_table = '%s_%s' % ('t_component_summary', table_index) attrs = {'__module__': cls.__module__, 'Meta': Meta} ModelClass = type("%s_%s" % (cls.__name__, table_index), (cls, ), attrs) ComponentSummary._mapper[class_name] = ModelClass return ModelClass() f_id = BigAutoField(primary_key=True) f_job_id = CharField(max_length=25, index=True) f_role = CharField(max_length=25, index=True) f_party_id = CharField(max_length=10, index=True) f_component_name = TextField() f_task_id = CharField(max_length=50, null=True, index=True) f_task_version = CharField(max_length=50, null=True, index=True) f_summary = LongTextField()
class ModelTag(DataBaseModel): f_id = BigAutoField(primary_key=True) f_m_id = CharField(max_length=25, null=False) f_t_id = BigIntegerField(null=False) class Meta: db_table = "t_model_tag"
class TrackingMetric(DataBaseModel): _mapper = {} @classmethod def model(cls, table_index=None, date=None): if not table_index: table_index = date.strftime( '%Y%m%d') if date else datetime.datetime.now().strftime( '%Y%m%d') class_name = 'TrackingMetric_%s' % table_index ModelClass = TrackingMetric._mapper.get(class_name, None) if ModelClass is None: class Meta: db_table = '%s_%s' % ('t_tracking_metric', table_index) attrs = {'__module__': cls.__module__, 'Meta': Meta} ModelClass = type("%s_%s" % (cls.__name__, table_index), (cls,), attrs) TrackingMetric._mapper[class_name] = ModelClass return ModelClass() f_id = BigAutoField(primary_key=True) f_job_id = CharField(max_length=25, index=True) f_component_name = TextField() f_task_id = CharField(max_length=100, null=True, index=True) f_task_version = BigIntegerField(null=True, index=True) f_role = CharField(max_length=50, index=True) f_party_id = CharField(max_length=10, index=True) f_metric_namespace = CharField(max_length=180, index=True) f_metric_name = CharField(max_length=180, index=True) f_key = CharField(max_length=200) f_value = TextField() f_type = IntegerField(index=True) # 0 is data, 1 is meta
class Tag(DataBaseModel): f_id = BigAutoField(primary_key=True) f_name = CharField(max_length=100, index=True, unique=True) f_desc = TextField(null=True) class Meta: db_table = "t_tags"
class Article(BaseModel): article_id = BigAutoField(primary_key=True) status = SmallIntegerField(null=False, index=True) name = TextField(null=False) body = TextField(null=False) created_date = DateTimeField(index=True, default=datetime.utcnow) modified_date = DateTimeField(null=True, index=True) deleted_date = DateTimeField(null=True, index=True)
class Tag(DataBaseModel): f_id = BigAutoField(primary_key=True) f_name = CharField(max_length=100, index=True, unique=True) f_desc = TextField(null=True) f_create_time = BigIntegerField(default=current_timestamp()) f_update_time = BigIntegerField(default=current_timestamp()) class Meta: db_table = "t_tags"
class UserDO(BaseDO): class Meta: table_name = 'user' id = BigAutoField() user = CharField() source = CharField() likes = FloatField() reviews = IntegerField() gmt_create = DateTimeField() gmt_modified = DateTimeField()
class MenuDO(BaseDO): class Meta: table_name = 'menu' id = BigAutoField() outlet_id = ForeignKeyField(OutletDO) brand = CharField() price = DoubleField() volume = IntegerField() name = CharField() gmt_create = DateTimeField() gmt_modified = DateTimeField()
class ReviewDO(BaseDO): class Meta: table_name = 'review' id = BigAutoField() user = ForeignKeyField(UserDO) outlet = ForeignKeyField(OutletDO) date = DateField() review = CharField() rating = DoubleField() gmt_create = DateTimeField() gmt_modified = DateTimeField()
class VariableTable(BaseModel): id = BigAutoField(primary_key=True) is_top_level = BooleanField(null=True) encoding = SmallIntegerField(default=0) # primitive values boolean_value = BooleanField(null=True) number_value = DoubleField(null=True) string_value = TextField(null=True) # list_elements # map_elements # linked value variable_value = DeferredForeignKey("VariableTable", null=True) # allow different interpretations (such as interpreting a string as regex, or as a datetime) interpretation = ForeignKeyField(Interpretation, null=True) @property def type(self): if self.encoding == Encoding.null: return type(None) elif self.encoding == Encoding.boolean: return bool elif self.encoding == Encoding.number: return float elif self.encoding == Encoding.string: return str elif self.encoding == Encoding.list: return list elif self.encoding == Encoding.map: return dict elif self.encoding == Encoding.variable: return VariableTable @property def value(self): if self.encoding == Encoding.null: return None elif self.encoding == Encoding.boolean: return self.boolean_value elif self.encoding == Encoding.number: return self.number_value elif self.encoding == Encoding.string: return self.string_value elif self.encoding == Encoding.list: return self.list_elements elif self.encoding == Encoding.map: return self.map_elements elif self.encoding == Encoding.variable: return self.variable_value
class DataTableTracking(DataBaseModel): f_table_id = BigAutoField(primary_key=True) f_table_name = CharField(max_length=300, null=True) f_table_namespace = CharField(max_length=300, null=True) f_job_id = CharField(max_length=25, index=True, null=True) f_have_parent = BooleanField(default=False) f_parent_number = IntegerField(default=0) f_parent_table_name = CharField(max_length=500, null=True) f_parent_table_namespace = CharField(max_length=500, null=True) f_source_table_name = CharField(max_length=500, null=True) f_source_table_namespace = CharField(max_length=500, null=True) class Meta: db_table = "t_data_table_tracking"
class ClassificationModel(Model): id = BigAutoField() item = CharField() city = CharField() classification = IntegerField() frequency = SmallIntegerField() extra_detail = CharField() image_hash = CharField() is_deleted = BooleanField() created_at = DateTimeField() updated_at = DateTimeField() class Meta: database = db table_name = 'tb_classification'
class OutletDO(BaseDO): class Meta: table_name = 'outlet' id = BigAutoField() id_outlet = CharField(index=True, unique=True) name = CharField() source = CharField() address = CharField() country = CharField(max_length=2) phone = CharField() rating = FloatField() reviews_nr = IntegerField() gmt_create = DateTimeField() gmt_modified = DateTimeField()
class Character(BaseModel): character_id = BigAutoField(primary_key=True) user = ForeignKeyField(User, backref='characters') name = CharField(33) unapproved_name = CharField(33, default="") name_rejected = BooleanField(default=False) free_to_play = BooleanField(default=False) shirt_color = BigIntegerField() shirt_style = BigIntegerField() pants_color = BigIntegerField() hair_style = BigIntegerField() hair_color = BigIntegerField() lh = BigIntegerField() rh = BigIntegerField() eyebrow_style = BigIntegerField() eye_style = BigIntegerField() mouth_style = BigIntegerField() last_zone = IntegerField(default=0) last_instance = IntegerField(default=0) last_clone = BigIntegerField(default=0) last_login = DateTimeField(default=datetime.now)
class MachineLearningModelInfo(DataBaseModel): f_id = BigAutoField(primary_key=True) f_role = CharField(max_length=50, index=True) f_party_id = CharField(max_length=10, index=True) f_roles = JSONField() f_job_id = CharField(max_length=25) f_model_id = CharField(max_length=100, index=True) f_model_version = CharField(max_length=100, index=True) f_loaded_times = IntegerField(default=0) f_size = BigIntegerField(default=0) f_create_time = BigIntegerField(default=0) f_update_time = BigIntegerField(default=0) f_description = TextField(null=True, default='') f_initiator_role = CharField(max_length=50, index=True) f_initiator_party_id = CharField(max_length=50, index=True, default=-1) f_runtime_conf = JSONField() f_work_mode = IntegerField() f_dsl = JSONField() f_train_runtime_conf = JSONField(default={}) f_imported = IntegerField(default=0) f_job_status = CharField(max_length=50) class Meta: db_table = "t_machine_learning_model_info"
class BaseModel(Model): id = BigAutoField(primary_key=True) class Meta: database = db
class ListElementTable(BaseModel): id = BigAutoField(primary_key=True) parent = ForeignKeyField(VariableTable, backref="list_elements") key = IntegerField(default=0) variable = ForeignKeyField(VariableTable)
class BHM_Spiders_Generic_Superset(CatalogdbModel): # Not using reflection here to preserve Tom's notes. pk = BigAutoField() # Parameters derived from eROSITA eSASS catalogue # Chosen to match X-ray columns defined in eROSITA/SDSS-V MoU (v2.0, April 2019) ero_version = TextField(index=True, null=True) # string identifying this eROSITA data # reduction version ero_souuid = TextField(index=True, null=True) # string identifying this X-ray source ero_flux = FloatField(null=True) # X-ray flux, 0.5-8keV band, erg/cm2/s ero_flux_err = FloatField(null=True) # X-ray flux uncertainty, 0.5-8keV band, # erg/cm2/s ero_ext = FloatField(null=True) # X-ray extent parameter - arcsec ero_ext_err = FloatField(null=True) # X-ray extent parameter uncertainty - arcsec ero_ext_like = FloatField(null=True) # X-ray extent likelihood ero_det_like = FloatField(null=True) # X-ray detection likelihood ero_ra = DoubleField(index=True, null=True) # X-ray position, RA, ICRS, degrees ero_dec = DoubleField(index=True, null=True) # X-ray position, Dec, ICRS, degrees ero_radec_err = FloatField(null=True) # X-ray position uncertainty, arcsec # Parameters describing the cross-matching of X-ray to optical/IR catalogue(s) # 'ML+NWAY', 'LR' , 'SDSS_REDMAPPER', 'LS_REDMAPPER', 'HSC_REDMAPPER', 'MCMF' etc xmatch_method = TextField(null=True) # version identifier for cross-matching algorithm xmatch_version = TextField(null=True) # separation between X-ray position and opt positions - arcsec xmatch_dist = FloatField(null=True) # measure of quality of xmatch (e.g. p_any for Nway, LR) xmatch_metric = FloatField(null=True) # flavour of match, quality flags, e.g. NWAY match_flag - treat as bitmask xmatch_flags = BigIntegerField(null=True) # Parameters that describe the major class of the object target_class = TextField(null=True) # TBD, but e.g. 'unknown', 'AGN', 'Star', 'Galaxy' target_priority = IntegerField(null=True) # allows priority ranking based on # info not available in catalogdb target_has_spec = IntegerField(null=True) # (bitmask) allows flagging of targets # that have a redshift from a catalogue # that might not be listed in catalogdb # follow bit pattern in spec compilation # values < 0 means 'unknown' # Parameters derived from the cross-matched opt/IR catalogue # which optical catalogue(and version) provided this counterpart, # e.g. 'ls_dr8', 'ps1_dr2' ... will also be the origin of the photometry columns below best_opt = TextField(null=True) # arithmetically derived from ls_release, ls_brickid and ls_objid # ls_id = ls_objid + ls_brickid * 2**16 + ls_release * 2**40 # - make sure that we have a common definition within CatalogDB # must be used when ls was the main source of counterparts to # erosita source, otherwise is optional ls_id = BigIntegerField(index=True, null=True) # Pan-STARRS1-DR2 object id (= ObjectThin.ObjID = StackObjectThin.ObjID) # Must be used when ps1-dr2(+unWISE) was the main source of counterparts # to an erosita source, otherwise is optional ps1_dr2_objid = BigIntegerField(index=True, null=True) # derived from legacysurvey sweeps OPT_REF_ID when OPT_REF_CAT='G2' # must be used when ls was the main source of counterparts to erosita source, # otherwise is optional # - SPIDERS team should also pre-match to gaia dr2 when using counterparts # from non-LS catalogues gaia_dr2_source_id = BigIntegerField(index=True, null=True) # Corresponds to the unWISE catalog band-merged 'unwise_objid' # - should be used when ps1-dr2+unWISE was the main source of # counterparts to erosita sources, otherwise is optional unwise_dr1_objid = CharField(index=True, null=True, max_length=16) # provisional: # Corresponds to the DES dr1 coadd_object_id # - must be used when DES-dr1 was the primary source of counterpart to an # erosita source, otherwise is optional des_dr1_coadd_object_id = BigIntegerField(index=True, null=True) # Corresponds to the SDSS DR16 photoObj https://www.sdss.org/dr13/help/glossary/#ObjID # - must be used when SDSS photoObj was the primary source of counterpart # to an erosita source, otherwise is optional sdss_dr16_objid = BigIntegerField(index=True, null=True) # included for convenience, but are copied from columns in other tables opt_ra = DoubleField(index=True, null=True) opt_dec = DoubleField(index=True, null=True) opt_pmra = FloatField(null=True) opt_pmdec = FloatField(null=True) opt_epoch = FloatField(null=True) # For convenience we send a subset of magnitude columns over to the database # - the full set of columns is available via a database JOIN to e.g. main ls_dr8 catalogue # Note to self: Be careful with use of modelflux, fiberflux, fiber2flux etc! opt_modelflux_g = FloatField(null=True) opt_modelflux_ivar_g = FloatField(null=True) opt_modelflux_r = FloatField(null=True) opt_modelflux_ivar_r = FloatField(null=True) opt_modelflux_r = FloatField(null=True) opt_modelflux_ivar_r = FloatField(null=True) opt_modelflux_i = FloatField(null=True) opt_modelflux_ivar_i = FloatField(null=True) opt_modelflux_z = FloatField(null=True) opt_modelflux_ivar_z = FloatField(null=True) ls = ForeignKeyField(Legacy_Survey_DR8, field='ls_id', backref='+') gaia = ForeignKeyField(Gaia_DR2, object_id_name='gaia_dr2_source_id', backref='+') tic = ForeignKeyField(TIC_v8, field='gaia_int', column_name='gaia_dr2_source_id', object_id_name='gaia_dr2_source_id', backref='+') class Meta: use_reflection = False
class MapElementTable(BaseModel): id = BigAutoField(primary_key=True) parent = ForeignKeyField(VariableTable, backref="map_elements") key = TextField() variable = ForeignKeyField(VariableTable)
class TweetModel(ModelBase): id = BigAutoField(primary_key=True) user = ForeignKeyField(UserModel, backref="tweets") content = TextField() datetime = DateTimeField(default=datetime.now)
class TableBase(BaseModel): """Base Table with Hard Delete""" def inttime(): return int(time.time()) id = BigAutoField() created_at = TimestampField(default=inttime) updated_at = TimestampField(default=0) @classmethod def do_create(cls): return cls.create() @classmethod def do_update(cls, __data=None, **update): if isinstance(__data, dict): __data["updated_at"] = time return cls.update(__data=__data, **update) else: return cls.update(__data=__data, updated_at=int(time.time()), **update) @classmethod def do_delete(cls): return cls.delete() @classmethod def do_select(cls, *fields): return cls.select(*fields) def save(self, force_insert=False, only=None): self.updated_at = int(time.time()) return super().save(force_insert, only) @classmethod def migrations(cls): """No Migrations""" return [] @classmethod def table_setup(cls) -> bool: """Magic to create the table returns true if a table is created""" tv = TableVersion.get_or_none(name=cls._meta.table_name) if not cls.table_exists(): cls.create_table() tv = TableVersion() tv.name = cls._meta.table_name tv.version = 0 tv.save() return True else: migrations = cls.migrations() if migrations == []: tv.version = 0 else: current_version = tv.version - 1 with Database.db.atomic(): for migration in migrations[current_version:]: migrate(*migration) tv.version = len(migrations) if isinstance(migrations, list) else 0 tv.save() return False