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 4.5 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. from flask import Blueprint, jsonify, request
  18. from mindinsight.conf import settings
  19. from mindinsight.datavisual.utils.tools import get_train_id
  20. from mindinsight.datavisual.data_transform.data_manager import DATA_MANAGER
  21. from mindinsight.lineagemgr.cache_item_updater import update_lineage_object
  22. from mindinsight.lineagemgr.common.validator.validate import validate_train_id
  23. from mindinsight.lineagemgr.model import filter_summary_lineage
  24. from mindinsight.utils.exceptions import MindInsightException, ParamValueError
  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. try:
  56. lineage_info = filter_summary_lineage(data_manager=DATA_MANAGER, search_condition=search_condition)
  57. except MindInsightException as exception:
  58. raise MindInsightException(exception.error, exception.message, http_code=400)
  59. return lineage_info
  60. @BLUEPRINT.route("/lineagemgr/lineages", methods=["PUT"])
  61. def update_lineage():
  62. """
  63. Get lineage.
  64. Returns:
  65. str, update the lineage information about cache and tag.
  66. Raises:
  67. MindInsightException: If method fails to be called.
  68. Examples:
  69. >>> PUT http://xxxx/v1/mindinsight/lineagemgr/lineages?train_id=./run1
  70. """
  71. train_id = get_train_id(request)
  72. added_info = request.json
  73. if not isinstance(added_info, dict):
  74. raise ParamValueError("The request body should be a dict.")
  75. update_lineage_object(DATA_MANAGER, train_id, added_info)
  76. return jsonify({"status": "success"})
  77. @BLUEPRINT.route("/datasets/dataset_graph", methods=["GET"])
  78. def get_dataset_graph():
  79. """
  80. Get dataset graph.
  81. Returns:
  82. str, the dataset graph information.
  83. Raises:
  84. MindInsightException: If method fails to be called.
  85. ParamValueError: If summary_dir is invalid.
  86. Examples:
  87. >>> GET http://xxxx/v1/mindinsight/datasets/dataset_graph?train_id=xxx
  88. """
  89. train_id = get_train_id(request)
  90. validate_train_id(train_id)
  91. search_condition = {
  92. 'summary_dir': {
  93. 'in': [train_id]
  94. }
  95. }
  96. result = {}
  97. try:
  98. objects = filter_summary_lineage(data_manager=DATA_MANAGER, search_condition=search_condition).get('object')
  99. except MindInsightException as exception:
  100. raise MindInsightException(exception.error, exception.message, http_code=400)
  101. if objects:
  102. lineage_obj = objects[0]
  103. dataset_graph = lineage_obj.get('dataset_graph')
  104. if dataset_graph:
  105. result.update({'dataset_graph': dataset_graph})
  106. result.update({'summary_dir': lineage_obj.get('summary_dir')})
  107. return jsonify(result)
  108. def init_module(app):
  109. """
  110. Init module entry.
  111. Args:
  112. app (Flask): The application obj.
  113. """
  114. app.register_blueprint(BLUEPRINT)