Skip to content

Commit 2f23992

Browse files
authored
card time client (#1310)
* card time client * card time client fix queuenames split * card time client fix cardtimeinfo class name * card time client fix print
1 parent 844421f commit 2f23992

File tree

5 files changed

+228
-4
lines changed

5 files changed

+228
-4
lines changed

client/paddleflow/cli/statistics.py

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import json
12
import sys
23
import traceback
34

45
import click
56
from paddleflow.cli.output import print_output
6-
from paddleflow.statistics import StatisticsJobInfo, StatisticsJobDetailInfo
7+
from paddleflow.statistics import StatisticsJobInfo, StatisticsJobDetailInfo, CardTimeInfo, Detail
78

89

910
# !/usr/bin/env python3
@@ -108,3 +109,63 @@ def _print_job_statistics_detail(job_statistics_detail_info: StatisticsJobDetail
108109
data.append(v)
109110

110111
print_output(data, headers, output_format, table_format='grid')
112+
113+
114+
@statistics.command()
115+
@click.pass_context
116+
@click.option('-q', '--queue_names', help="queue names, split by ,", type=str)
117+
@click.option('-s', '--start_time', help="start time", type=str)
118+
@click.option('-e', '--end_time', help="end time", type=str)
119+
def card_time(ctx, queue_names, start_time, end_time):
120+
""" get card time by queue name.\n
121+
queue_name: the name of queues you want to get card time.
122+
"""
123+
client = ctx.obj['client']
124+
output_format = ctx.obj['output']
125+
if not queue_names:
126+
click.echo("queue name is required")
127+
sys.exit(1)
128+
129+
if not start_time:
130+
click.echo("start time is required")
131+
sys.exit(1)
132+
133+
if not end_time:
134+
click.echo("end time is required")
135+
sys.exit(1)
136+
137+
queue_names = queue_names.split(",")
138+
_get_cardtime_by_queue_name(client, output_format, queue_names, start_time, end_time)
139+
140+
141+
def _get_cardtime_by_queue_name(cli, output_format, queue_names, start_time, end_time):
142+
valid, response = cli.get_cardtime_by_queue_name(queue_names, start_time, end_time)
143+
if valid:
144+
_print_card_time_info(response, output_format)
145+
else:
146+
click.echo("get queue statistics failed with message[%s]" % response)
147+
sys.exit(1)
148+
149+
150+
def _print_card_time_info(info: CardTimeInfo, output_format):
151+
"""print card time info."""
152+
if len(info.data) == 0:
153+
click.echo("no data")
154+
return
155+
156+
data_res = []
157+
for data in info.data:
158+
detail_res = []
159+
for detail in data.detail:
160+
detail = {
161+
"userName": detail.user_name,
162+
"jobInfo": [job_info.to_json() for job_info in detail.job_info_list],
163+
"jobCount": detail.job_count,
164+
"totalCardTime": detail.total_card_time,
165+
}
166+
detail_res.append(detail)
167+
data_res.append([data.queue_name, data.card_time, data.device_type, detail_res])
168+
169+
headers = ['queueName', 'cardTime', 'deviceType', 'detail']
170+
171+
print_output(data_res, headers, output_format, table_format='json')

client/paddleflow/client.py

+9
Original file line numberDiff line numberDiff line change
@@ -845,3 +845,12 @@ def get_statistics_detail(self, jobid: str, start: int = None, end: int = None,
845845
if not ret:
846846
return ret, res, False
847847
return ret, res, res.truncated
848+
849+
def get_cardtime_by_queue_name(self, queue_names: list, start_time: str, end_time: str):
850+
"""
851+
get_card_time_by_queue_name
852+
"""
853+
self.pre_check()
854+
if queue_names is None or len(queue_names) == 0:
855+
raise PaddleFlowSDKException("InvalidQueueName", "queue name should not be none or empty")
856+
return StatisticsServiceApi.get_cardtime_by_queue_name(self.paddleflow_server, queue_names, start_time, end_time,header=self.header)

client/paddleflow/statistics/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
# -*- coding:utf8 -*-
1919

2020
from .statistics_api import StatisticsServiceApi
21-
from .statistics_info import StatisticsJobInfo, StatisticsJobDetailInfo
21+
from .statistics_info import StatisticsJobInfo, StatisticsJobDetailInfo, CardTimeInfo, Detail

client/paddleflow/statistics/statistics_api.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from paddleflow.common.exception.paddleflow_sdk_exception import PaddleFlowSDKException
2424
from paddleflow.utils import api_client
2525
from paddleflow.common import api
26-
from paddleflow.statistics.statistics_info import StatisticsJobInfo, StatisticsJobDetailInfo
26+
from paddleflow.statistics.statistics_info import StatisticsJobInfo, StatisticsJobDetailInfo, CardTimeInfo
2727

2828

2929
class StatisticsServiceApi(object):
@@ -113,3 +113,37 @@ def get_statistics_detail(cls, host, job_id: str, start=None, end=None,
113113
return False, data['message']
114114
statistics_job_detail_info = StatisticsJobDetailInfo.from_json(data)
115115
return True, statistics_job_detail_info
116+
117+
@classmethod
118+
def get_cardtime_by_queue_name(cls, host, queue_names: list, start_time: str, end_time: str, header=None):
119+
"""
120+
get statistics info, run_id is not supported yet
121+
@param host: host url
122+
@param queue_names: queue names list
123+
@param start_time: start time
124+
@param end_time: end time
125+
@param header: request header
126+
@return: success: bool, resp: StatisticsQueueInfo
127+
"""
128+
if not header:
129+
raise PaddleFlowSDKException("InvalidRequest", "paddleflow should login first")
130+
131+
body = {
132+
"queueNames": queue_names,
133+
"startTime": start_time,
134+
"endTime": end_time,
135+
}
136+
resp = api_client.call_api(method="POST",
137+
url=parse.urljoin(host, api.PADDLE_FLOW_STATISTIC + "/cardTime"),
138+
headers=header,
139+
json=body)
140+
if not resp:
141+
raise PaddleFlowSDKException("Connection Error", "status run failed due to HTTPError")
142+
data = json.loads(resp.text)
143+
# return error resp, return err
144+
if 'message' in data:
145+
return False, data['message']
146+
147+
statistics_queue_info = CardTimeInfo.from_json(data)
148+
149+
return True, statistics_queue_info

client/paddleflow/statistics/statistics_info.py

+121-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
limitations under the License.
1515
"""
1616

17-
#!/usr/bin/env python3
17+
# !/usr/bin/env python3
1818
# -*- coding:utf8 -*-
19+
import json
1920
from typing import List, Mapping
2021

2122

@@ -92,3 +93,122 @@ def from_json(metric_info):
9293
result.task_info.append(task_info)
9394
statistics_job_detail_info.result.append(result)
9495
return statistics_job_detail_info
96+
97+
98+
class JobInfo:
99+
job_id: str
100+
card_time: float
101+
create_time: str
102+
start_time: str
103+
finish_time: str
104+
device_count: int
105+
106+
def __init__(self, job_id: str, card_time: float, create_time: str, start_time: str, finish_time: str,
107+
device_count: int) -> None:
108+
self.job_id = job_id
109+
self.card_time = card_time
110+
self.create_time = create_time
111+
self.start_time = start_time
112+
self.finish_time = finish_time
113+
self.device_count = device_count
114+
115+
def to_json(self):
116+
job_info = {
117+
"jobId": self.job_id,
118+
"cardTime": self.card_time,
119+
"createTime": self.create_time,
120+
"startTime": self.start_time,
121+
"finishTime": self.finish_time,
122+
"deviceCount": self.device_count
123+
}
124+
return job_info
125+
126+
127+
class Detail:
128+
user_name: str
129+
job_info_list: List[JobInfo]
130+
job_count: int
131+
total_card_time: float
132+
133+
def __init__(self, user_name: str, job_info_list: List[JobInfo], job_count: int, total_card_time: float) -> None:
134+
self.user_name = user_name
135+
self.job_info_list = job_info_list
136+
self.job_count = job_count
137+
self.total_card_time = total_card_time
138+
139+
def __str__(self):
140+
json.dumps(self.__dict__)
141+
142+
def to_str(self):
143+
return "Detail: user name: {}, job info list: {}, job count: {}, total card time: {},". \
144+
format(self.user_name, self.job_info_list, self.job_count, self.total_card_time)
145+
146+
147+
class CardTimeResult:
148+
queue_name: str
149+
card_time: float
150+
device_type: str
151+
detail: List[Detail]
152+
153+
def __init__(self, queue_name: str, card_time: float, device_type: str, detail: List[Detail]) -> None:
154+
self.queue_name = queue_name
155+
self.card_time = card_time
156+
self.device_type = device_type
157+
self.detail = detail
158+
159+
def __str__(self):
160+
return "Detail: queue name: {}, card time: {}, device type: {}, detail: {},". \
161+
format(self.queue_name, self.card_time, self.device_type, self.detail)
162+
163+
164+
class CardTimeInfo:
165+
data: List[CardTimeResult]
166+
167+
def __init__(self, data: List[CardTimeResult]) -> None:
168+
self.data = data
169+
170+
def __str__(self) -> str:
171+
""" str """
172+
return "CardTimeInfo: data: {}".format(self.data)
173+
174+
@staticmethod
175+
def from_json(card_time_stat_info):
176+
card_time_info = CardTimeInfo(
177+
data=[],
178+
)
179+
try:
180+
for result_json in card_time_stat_info['data']:
181+
result = CardTimeResult(
182+
queue_name=result_json['queueName'],
183+
card_time=result_json['cardTime'],
184+
device_type=result_json['deviceType'],
185+
detail=[]
186+
)
187+
try:
188+
for detail_json in result_json['detail']:
189+
detail = Detail(
190+
user_name=detail_json['userName'],
191+
job_info_list=[],
192+
job_count=detail_json['jobCount'],
193+
total_card_time=detail_json['totalCardTime']
194+
)
195+
try:
196+
for job_info_json in detail_json['jobInfoList']:
197+
job_info = JobInfo(
198+
job_id=job_info_json['jobId'],
199+
card_time=job_info_json['cardTime'],
200+
create_time=job_info_json['createTime'],
201+
start_time=job_info_json['startTime'],
202+
finish_time=job_info_json['finishTime'],
203+
device_count=job_info_json['deviceCount'],
204+
)
205+
detail.job_info_list.append(job_info)
206+
except:
207+
pass
208+
result.detail.append(detail)
209+
except:
210+
pass
211+
card_time_info.data.append(result)
212+
except:
213+
pass
214+
return card_time_info

0 commit comments

Comments
 (0)