tracking.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. from datetime import datetime
  18. import click
  19. from contextlib import closing
  20. import requests
  21. from flow_client.flow_cli.utils import cli_args
  22. from flow_client.flow_cli.utils.cli_utils import (prettify, preprocess, download_from_request,
  23. access_server, check_abs_path)
  24. @click.group(short_help="Component Operations")
  25. @click.pass_context
  26. def tracking(ctx):
  27. """
  28. \b
  29. Provides numbers of component operational commands, including metrics, parameters and etc.
  30. For more details, please check out the help text.
  31. """
  32. pass
  33. @tracking.command("metrics", short_help="Component Metrics Command")
  34. @cli_args.JOBID_REQUIRED
  35. @cli_args.ROLE_REQUIRED
  36. @cli_args.PARTYID_REQUIRED
  37. @cli_args.COMPONENT_NAME_REQUIRED
  38. @click.pass_context
  39. def metrics(ctx, **kwargs):
  40. """
  41. \b
  42. - DESCRIPTION:
  43. Query the List of Metrics.
  44. \b
  45. - USAGE:
  46. flow component metrics -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0
  47. """
  48. config_data, dsl_data = preprocess(**kwargs)
  49. access_server('post', ctx, 'tracking/component/metrics', config_data)
  50. @tracking.command("metric-all", short_help="Component Metric All Command")
  51. @cli_args.JOBID_REQUIRED
  52. @cli_args.ROLE_REQUIRED
  53. @cli_args.PARTYID_REQUIRED
  54. @cli_args.COMPONENT_NAME_REQUIRED
  55. @click.pass_context
  56. def metric_all(ctx, **kwargs):
  57. """
  58. \b
  59. - DESCRIPTION:
  60. Query All Metric Data.
  61. \b
  62. - USAGE:
  63. flow component metric-all -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0
  64. """
  65. config_data, dsl_data = preprocess(**kwargs)
  66. access_server('post', ctx, 'tracking/component/metric/all', config_data)
  67. @tracking.command("parameters", short_help="Component Parameters Command")
  68. @cli_args.JOBID_REQUIRED
  69. @cli_args.ROLE_REQUIRED
  70. @cli_args.PARTYID_REQUIRED
  71. @cli_args.COMPONENT_NAME_REQUIRED
  72. @click.pass_context
  73. def parameters(ctx, **kwargs):
  74. """
  75. \b
  76. - DESCRIPTION:
  77. Query the parameters of a specified component.
  78. \b
  79. - USAGE:
  80. flow component parameters -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0
  81. """
  82. config_data, dsl_data = preprocess(**kwargs)
  83. access_server('post', ctx, 'tracking/component/parameters', config_data)
  84. @tracking.command("output-data", short_help="Component Output Data Command")
  85. @cli_args.JOBID_REQUIRED
  86. @cli_args.ROLE_REQUIRED
  87. @cli_args.PARTYID_REQUIRED
  88. @cli_args.COMPONENT_NAME_REQUIRED
  89. @cli_args.OUTPUT_PATH_REQUIRED
  90. @click.option('-l', '--limit', type=click.INT, default=-1,
  91. help='limit count, defaults is -1 (download all output data)')
  92. @click.pass_context
  93. def output_data(ctx, **kwargs):
  94. """
  95. \b
  96. - DESCRIPTION:
  97. Download the Output Data of A Specified Component.
  98. \b
  99. - USAGE:
  100. flow component output-data -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0 --output-path ./examples/
  101. """
  102. config_data, dsl_data = preprocess(**kwargs)
  103. tar_file_name = 'job_{}_{}_{}_{}_output_data.tar.gz'.format(config_data['job_id'],
  104. config_data['component_name'],
  105. config_data['role'],
  106. config_data['party_id'])
  107. extract_dir = os.path.join(config_data['output_path'], tar_file_name.replace('.tar.gz', ''))
  108. with closing(access_server('get', ctx, 'tracking/component/output/data/download',
  109. config_data, False, stream=True)) as response:
  110. if response.status_code == 200:
  111. try:
  112. download_from_request(http_response=response, tar_file_name=tar_file_name, extract_dir=extract_dir)
  113. res = {
  114. 'retcode': 0,
  115. 'directory': os.path.abspath(extract_dir),
  116. 'retmsg': 'Download successfully, please check {} directory'.format(
  117. os.path.abspath(extract_dir))}
  118. except BaseException:
  119. res = {'retcode': 100,
  120. 'retmsg': 'Download failed, please check if the parameters are correct.'}
  121. else:
  122. try:
  123. res = response.json() if isinstance(response, requests.models.Response) else response
  124. except Exception:
  125. res = {'retcode': 100,
  126. 'retmsg': 'Download failed, for more details please check logs/fate_flow/fate_flow_stat.log.'}
  127. prettify(res)
  128. @tracking.command("output-model", short_help="Component Output Model Command")
  129. @cli_args.JOBID_REQUIRED
  130. @cli_args.ROLE_REQUIRED
  131. @cli_args.PARTYID_REQUIRED
  132. @cli_args.COMPONENT_NAME_REQUIRED
  133. @click.pass_context
  134. def output_model(ctx, **kwargs):
  135. """
  136. \b
  137. - DESCRIPTION:
  138. Query the Model of A Speicied Component.
  139. \b
  140. - USAGE:
  141. flow component output-model -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0
  142. """
  143. config_data, dsl_data = preprocess(**kwargs)
  144. access_server('post', ctx, 'tracking/component/output/model', config_data)
  145. @tracking.command("output-data-table", short_help="Component Output Data Table Command")
  146. @cli_args.JOBID_REQUIRED
  147. @cli_args.ROLE_REQUIRED
  148. @cli_args.PARTYID_REQUIRED
  149. @cli_args.COMPONENT_NAME_REQUIRED
  150. @click.pass_context
  151. def output_data_table(ctx, **kwargs):
  152. """
  153. \b
  154. - DESCRIPTION:
  155. View Table Name and Namespace.
  156. \b
  157. - USAGE:
  158. flow component output-data-table -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0
  159. """
  160. config_data, dsl_data = preprocess(**kwargs)
  161. access_server('post', ctx, 'tracking/component/output/data/table', config_data)
  162. @tracking.command("get-summary", short_help="Download Component Summary Command")
  163. @cli_args.JOBID_REQUIRED
  164. @cli_args.ROLE_REQUIRED
  165. @cli_args.PARTYID_REQUIRED
  166. @cli_args.COMPONENT_NAME_REQUIRED
  167. @cli_args.OUTPUT_PATH
  168. @click.pass_context
  169. def get_summary(ctx, **kwargs):
  170. """
  171. \b
  172. - DESCRIPTION:
  173. Download summary of a specified component and save it as a json file.
  174. \b
  175. - USAGE:
  176. flow component get-summary -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0
  177. flow component get-summary -j $JOB_ID -r host -p 10000 -cpn hetero_feature_binning_0 -o ./examples/
  178. """
  179. config_data, dsl_data = preprocess(**kwargs)
  180. if config_data.get("output_path"):
  181. if not os.path.isdir(config_data.get("output_path")):
  182. res = {
  183. "retcode": 100,
  184. "retmsg": "Please input a valid directory path."
  185. }
  186. else:
  187. config_data["filename"] = "summary_{}_{}.json".format(config_data['component_name'],
  188. datetime.now().strftime('%Y%m%d%H%M%S'))
  189. config_data["output_path"] = os.path.join(check_abs_path(
  190. config_data["output_path"]), config_data["filename"])
  191. with closing(access_server("post", ctx, "tracking/component/summary/download",
  192. config_data, False, stream=True)) as response:
  193. if response.status_code == 200:
  194. with open(config_data["output_path"], "wb") as fout:
  195. for chunk in response.iter_content(1024):
  196. if chunk:
  197. fout.write(chunk)
  198. res = {
  199. "retcode": 0,
  200. "retmsg": "The summary of component <{}> has been stored successfully. "
  201. "File path is: {}.".format(config_data["component_name"],
  202. config_data["output_path"])
  203. }
  204. else:
  205. try:
  206. res = response.json() if isinstance(response, requests.models.Response) else response
  207. except Exception:
  208. res = {"retcode": 100,
  209. "retmsg": "Download component summary failed, "
  210. "for more details, please check logs/fate_flow/fate_flow_stat.log."}
  211. prettify(res)
  212. else:
  213. access_server("post", ctx, "tracking/component/summary/download", config_data)