constant.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import os
  2. import re
  3. import time
  4. from typing import List
  5. from app import models, schemas
  6. from sqlalchemy.orm import Session
  7. import app.utils.send_util as send_util
  8. from configs.settings import DefaultOption, config
  9. python_image = config.get('TASK_IMAGES', 'python')
  10. java_image = config.get('TASK_IMAGES', 'java')
  11. def create_constant(db: Session, item: schemas.ConstantCreate):
  12. db_item = models.Constant(**item.dict())
  13. db.add(db_item)
  14. db.commit()
  15. db.refresh(db_item)
  16. return db_item
  17. def get_constant_list(db: Session, type: str):
  18. res: List[models.Constant] = db.query(models.Constant).filter(
  19. models.Constant.type == type).all()
  20. return [r.value for r in res]
  21. def find_and_update(db: Session, type: str, value: str):
  22. res: List[models.Constant] = db.query(models.Constant)\
  23. .filter(models.Constant.type == type)\
  24. .filter(models.Constant.value == value).all()
  25. if len(res) == 0:
  26. db_item = models.Constant(**{
  27. 'type': type,
  28. 'value': value
  29. })
  30. db.add(db_item)
  31. db.commit()
  32. db.refresh(db_item)
  33. def delete_constant(db: Session, type: str, value: str):
  34. db_item = db.query(models.Constant)\
  35. .filter(models.Constant.type == type)\
  36. .filter(models.Constant.value == value).first()
  37. if not db_item:
  38. raise Exception('未找到该常量')
  39. else:
  40. db.query(models.Constant)\
  41. .filter(models.Constant.type == type)\
  42. .filter(models.Constant.value == value).delete()
  43. db.commit()
  44. db.flush()
  45. return db_item
  46. def get_task_images(language: str, type: int, auth_token: str):
  47. if type == 0:
  48. if language == 'python':
  49. return [python_image]
  50. elif language == 'java':
  51. return [java_image]
  52. else:
  53. res_list = []
  54. language_res = send_util.get_ai_mode_language(auth_token)
  55. language_list = list(filter(lambda x: (bool(re.search(language, str(x), re.IGNORECASE))), [
  56. language['value'] for language in language_res['data']]))
  57. for language in language_list:
  58. image_res = send_util.get_ai_images(language, auth_token)
  59. for image in image_res['data']:
  60. source_type = image['sourceType']
  61. image_url = ''
  62. # 区分电信云环境与腾讯云环境
  63. if os.environ.get('APP_ENV') in ['idcprod']:
  64. image_url = str(image['mirrorContainerName']).split(':')[0]
  65. elif os.environ.get('APP_ENV') in ['idctest']:
  66. image_url = str(image['mirrorRepistoryUrl']) if image['mirrorRepistoryUrl'] else str(image['mirrorContainerName']).split(':')[0]
  67. else:
  68. # 腾讯云环境这两种类型的镜像需要特殊处理
  69. if source_type in ['IMPORT', 'ONLINEBUILD']:
  70. mirror_repistory_url = image['mirrorRepistoryUrl'] if 'mirrorRepistoryUrl' in image.keys(
  71. ) else None
  72. if mirror_repistory_url != None:
  73. image_url = str(mirror_repistory_url).replace(
  74. ':443', '').split(':')[0]
  75. else:
  76. image_url_list = str(image['mirrorContainerName']).split(':')[
  77. 0].split('/')
  78. image_url = f'{image_url_list[0]}/aiplatform/docker/{image_url_list[1]}__{image_url_list[2]}'
  79. else:
  80. image_url = str(
  81. image['mirrorContainerName']).split(':')[0]
  82. res_list.append(
  83. {
  84. 'image_id': image['mirrorId'],
  85. 'image_url': image_url,
  86. 'image_name': image['mirrorShowName'],
  87. }
  88. )
  89. return res_list
  90. def get_image_version(image_id: str, auth_token: str):
  91. res = send_util.get_ai_image_version(image_id, auth_token)
  92. res_list = []
  93. for version_dict in res['data']:
  94. res_list.append(version_dict['versionName'])
  95. return res_list