mysql_helper.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # coding:utf-8
  2. from sqlalchemy import create_engine
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy import Column, String, Integer, Float, DATETIME
  5. Base = declarative_base()
  6. class AnnotatedData(Base):
  7. # 训练数据对照表
  8. __tablename__ = 'annotatedData'
  9. id = Column(Integer, primary_key=True)
  10. tenantID = Column(Integer, comment="租户ID")
  11. projectId = Column(Integer, comment="项目ID")
  12. infoId = Column(String(100), comment="信息ID")
  13. label = Column(String(255), comment="标签标识")
  14. createTime = Column(DATETIME, comment="创建时间")
  15. class Model(Base):
  16. # 模型表
  17. __tablename__ = 'model'
  18. id = Column(Integer, primary_key=True)
  19. createperson = Column(String(100), comment="创建人")
  20. createTime = Column(DATETIME, comment='创建时间')
  21. sampleData = Column(Integer, comment='模型数据量')
  22. recallRate = Column(Float(11, 2), comment='召回率')
  23. precision = Column(Float(11, 2), comment='精度')
  24. accuracyRate = Column(Float(11, 2), comment='准确率')
  25. state = Column(Integer(), comment='是否是默认模型 0 不是 1 是')
  26. modelFile = Column(String(255), comment='模型文件(oss存储)')
  27. projectId = Column(Integer, comment='项目标识')
  28. class Project(Base):
  29. # 项目表
  30. __tablename__ = 'project'
  31. id = Column(Integer, primary_key=True)
  32. name = Column(String(255), comment="项目名称")
  33. labels = Column(String(255), comment='标签集')
  34. type = Column(Integer, comment='多标签')
  35. userId = Column(Integer, comment='用户id')
  36. model = Column(Integer, comment='模型ID')
  37. focusField = Column(String(255), comment='模型文件(oss存储)')
  38. createTime = Column(DATETIME, comment='创建时间')
  39. totalCount = Column(Integer, comment="总数")
  40. def init_db(mysql_config):
  41. """
  42. 根据类创建数据库表
  43. :return:
  44. """
  45. db = mysql_config.get("db")
  46. ip = mysql_config.get("ip")
  47. port = mysql_config.get("port")
  48. user = mysql_config.get("user")
  49. pwd = mysql_config.get("pwd")
  50. charset = mysql_config.get("charset")
  51. engine = create_engine(
  52. f"mysql+pymysql://{user}:{pwd}@{ip}:{port}/{db}?charset={charset}",
  53. max_overflow=0, # 超过连接池⼤⼩外最多创建的连接
  54. pool_size=5, # 连接池⼤⼩
  55. pool_timeout=30, # 池中没有线程最多等待的时间,否则报错
  56. pool_recycle=-1 # 多久之后对线程池中的线程进⾏⼀次连接的回收
  57. )
  58. return engine