You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lineage_api.py 5.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Lineage restful api."""
  16. import json
  17. import os
  18. from flask import Blueprint, jsonify, request
  19. from mindinsight.conf import settings
  20. from mindinsight.datavisual.utils.tools import get_train_id
  21. from mindinsight.datavisual.data_transform.data_manager import DATA_MANAGER
  22. from mindinsight.lineagemgr.api.model import general_filter_summary_lineage, general_get_summary_lineage
  23. from mindinsight.utils.exceptions import MindInsightException, ParamValueError
  24. from mindinsight.lineagemgr.cache_item_updater import update_lineage_object
  25. BLUEPRINT = Blueprint("lineage", __name__, url_prefix=settings.URL_PATH_PREFIX+settings.API_PREFIX)
  26. @BLUEPRINT.route("/lineagemgr/lineages", methods=["POST"])
  27. def get_lineage():
  28. """
  29. Get lineage.
  30. Returns:
  31. str, the lineage information.
  32. Raises:
  33. MindInsightException: If method fails to be called.
  34. ParamValueError: If parsing json data search_condition fails.
  35. Examples:
  36. >>> POST http://xxxx/v1/mindinsight/lineagemgr/lineages
  37. """
  38. search_condition = request.stream.read()
  39. try:
  40. search_condition = json.loads(search_condition if search_condition else "{}")
  41. except Exception:
  42. raise ParamValueError("Json data parse failed.")
  43. lineage_info = _get_lineage_info(search_condition=search_condition)
  44. return jsonify(lineage_info)
  45. def _get_lineage_info(search_condition):
  46. """
  47. Get lineage info for dataset or model.
  48. Args:
  49. search_condition (dict): Search condition.
  50. Returns:
  51. dict, lineage info.
  52. Raises:
  53. MindInsightException: If method fails to be called.
  54. """
  55. summary_base_dir = str(settings.SUMMARY_BASE_DIR)
  56. try:
  57. lineage_info = general_filter_summary_lineage(
  58. data_manager=DATA_MANAGER,
  59. search_condition=search_condition,
  60. added=True)
  61. lineages = lineage_info['object']
  62. summary_base_dir = os.path.realpath(summary_base_dir)
  63. length = len(summary_base_dir)
  64. for lineage in lineages:
  65. summary_dir = lineage['summary_dir']
  66. summary_dir = os.path.realpath(summary_dir)
  67. if summary_base_dir == summary_dir:
  68. relative_dir = './'
  69. else:
  70. relative_dir = os.path.join(os.curdir, summary_dir[length+1:])
  71. lineage['summary_dir'] = relative_dir
  72. except MindInsightException as exception:
  73. raise MindInsightException(exception.error, exception.message, http_code=400)
  74. return lineage_info
  75. @BLUEPRINT.route("/lineagemgr/lineages", methods=["PUT"])
  76. def update_lineage():
  77. """
  78. Get lineage.
  79. Returns:
  80. str, update the lineage information about cache and tag.
  81. Raises:
  82. MindInsightException: If method fails to be called.
  83. Examples:
  84. >>> PUT http://xxxx/v1/mindinsight/lineagemgr/lineages?train_id=./run1
  85. """
  86. train_id = get_train_id(request)
  87. added_info = request.json
  88. if not isinstance(added_info, dict):
  89. raise ParamValueError("The request body should be a dict.")
  90. update_lineage_object(DATA_MANAGER, train_id, added_info)
  91. return jsonify({"status": "success"})
  92. @BLUEPRINT.route("/datasets/dataset_graph", methods=["GET"])
  93. def get_dataset_graph():
  94. """
  95. Get dataset graph.
  96. Returns:
  97. str, the dataset graph information.
  98. Raises:
  99. MindInsightException: If method fails to be called.
  100. ParamValueError: If summary_dir is invalid.
  101. Examples:
  102. >>> GET http://xxxx/v1/mindinsight/datasets/dataset_graph?train_id=xxx
  103. """
  104. summary_base_dir = str(settings.SUMMARY_BASE_DIR)
  105. summary_dir = get_train_id(request)
  106. try:
  107. dataset_graph = general_get_summary_lineage(
  108. DATA_MANAGER,
  109. summary_dir=summary_dir,
  110. keys=['dataset_graph']
  111. )
  112. except MindInsightException as exception:
  113. raise MindInsightException(exception.error, exception.message, http_code=400)
  114. if dataset_graph:
  115. summary_dir_result = dataset_graph.get('summary_dir')
  116. base_dir_len = len(summary_base_dir)
  117. if summary_base_dir == summary_dir_result:
  118. relative_dir = './'
  119. else:
  120. relative_dir = os.path.join(
  121. os.curdir, summary_dir[base_dir_len + 1:]
  122. )
  123. dataset_graph['summary_dir'] = relative_dir
  124. return jsonify(dataset_graph)
  125. def init_module(app):
  126. """
  127. Init module entry.
  128. Args:
  129. app (Flask): The application obj.
  130. """
  131. app.register_blueprint(BLUEPRINT)