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.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.model import filter_summary_lineage
  23. from mindinsight.utils.exceptions import MindInsightException, ParamValueError
  24. BLUEPRINT = Blueprint("lineage", __name__, url_prefix=settings.URL_PATH_PREFIX+settings.API_PREFIX)
  25. @BLUEPRINT.route("/lineagemgr/lineages", methods=["POST"])
  26. def get_lineage():
  27. """
  28. Get lineage.
  29. Returns:
  30. str, the lineage information.
  31. Raises:
  32. MindInsightException: If method fails to be called.
  33. ParamValueError: If parsing json data search_condition fails.
  34. Examples:
  35. >>> POST http://xxxx/v1/mindinsight/lineagemgr/lineages
  36. """
  37. search_condition = request.stream.read()
  38. try:
  39. search_condition = json.loads(search_condition if search_condition else "{}")
  40. except Exception:
  41. raise ParamValueError("Json data parse failed.")
  42. lineage_info = _get_lineage_info(search_condition=search_condition)
  43. return jsonify(lineage_info)
  44. def _get_lineage_info(search_condition):
  45. """
  46. Get lineage info for dataset or model.
  47. Args:
  48. search_condition (dict): Search condition.
  49. Returns:
  50. dict, lineage info.
  51. Raises:
  52. MindInsightException: If method fails to be called.
  53. """
  54. try:
  55. lineage_info = filter_summary_lineage(data_manager=DATA_MANAGER, search_condition=search_condition)
  56. except MindInsightException as exception:
  57. raise MindInsightException(exception.error, exception.message, http_code=400)
  58. return lineage_info
  59. @BLUEPRINT.route("/lineagemgr/lineages", methods=["PUT"])
  60. def update_lineage():
  61. """
  62. Get lineage.
  63. Returns:
  64. str, update the lineage information about cache and tag.
  65. Raises:
  66. MindInsightException: If method fails to be called.
  67. Examples:
  68. >>> PUT http://xxxx/v1/mindinsight/lineagemgr/lineages?train_id=./run1
  69. """
  70. train_id = get_train_id(request)
  71. added_info = request.json
  72. if not isinstance(added_info, dict):
  73. raise ParamValueError("The request body should be a dict.")
  74. update_lineage_object(DATA_MANAGER, train_id, added_info)
  75. return jsonify({"status": "success"})
  76. @BLUEPRINT.route("/datasets/dataset_graph", methods=["GET"])
  77. def get_dataset_graph():
  78. """
  79. Get dataset graph.
  80. Returns:
  81. str, the dataset graph information.
  82. Raises:
  83. MindInsightException: If method fails to be called.
  84. ParamValueError: If summary_dir is invalid.
  85. Examples:
  86. >>> GET http://xxxx/v1/mindinsight/datasets/dataset_graph?train_id=xxx
  87. """
  88. train_id = get_train_id(request)
  89. search_condition = {
  90. 'summary_dir': {
  91. 'in': [train_id]
  92. }
  93. }
  94. result = {}
  95. try:
  96. objects = filter_summary_lineage(DATA_MANAGER, search_condition).get('object')
  97. except MindInsightException as exception:
  98. raise MindInsightException(exception.error, exception.message, http_code=400)
  99. if objects:
  100. lineage_obj = objects[0]
  101. dataset_graph = lineage_obj.get('dataset_graph')
  102. if dataset_graph:
  103. result.update({'dataset_graph': dataset_graph})
  104. result.update({'summary_dir': lineage_obj.get('summary_dir')})
  105. return jsonify(result)
  106. def init_module(app):
  107. """
  108. Init module entry.
  109. Args:
  110. app (Flask): The application obj.
  111. """
  112. app.register_blueprint(BLUEPRINT)