Skip to content

Commit 89688c2

Browse files
authored
Merge pull request #152 from CamilaAlvarez/feat/hardware-summary
Subcommand hardware summary --name --origin
2 parents efc9cf2 + 23dd19b commit 89688c2

3 files changed

Lines changed: 87 additions & 34 deletions

File tree

docs/results.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ Example:
9292
kci-dev results hardware list --origin maestro --json
9393
```
9494

95+
#### summary
96+
97+
Gives a summary on the builds, boots and tests run of the hardware with `name` for the last seven days
98+
99+
Example:
100+
101+
```sh
102+
kci-dev results hardware summary --name mediatek,mt8195 --origin maestro --json
103+
```
104+
95105
## Common parameters
96106

97107
### --origin

kcidev/libs/dashboard.py

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import json
12
import urllib
23
from datetime import datetime, timedelta
4+
from functools import wraps
35

46
import requests
57

@@ -8,43 +10,56 @@
810
DASHBOARD_API = "https://dashboard.kernelci.org/api/"
911

1012

11-
def dashboard_api_fetch(endpoint, params, use_json, max_retries=3):
12-
base_url = urllib.parse.urljoin(DASHBOARD_API, endpoint)
13-
url = "{}?{}".format(base_url, urllib.parse.urlencode(params))
14-
retries = 0
15-
16-
# Status codes that should trigger a retry
17-
RETRY_STATUS_CODES = [429, 500, 502, 503, 504, 507]
18-
19-
while retries <= max_retries:
20-
try:
21-
r = requests.get(url)
22-
23-
if r.status_code in RETRY_STATUS_CODES:
24-
retries += 1
25-
if retries <= max_retries:
26-
continue
27-
else:
28-
kci_err(f"Failed after {max_retries} retries with 500 error.")
13+
def _dashboard_request(func):
14+
@wraps(func)
15+
def wrapper(endpoint, params, use_json, body=None, max_retries=3):
16+
base_url = urllib.parse.urljoin(DASHBOARD_API, endpoint)
17+
url = "{}?{}".format(base_url, urllib.parse.urlencode(params))
18+
retries = 0
19+
20+
# Status codes that should trigger a retry
21+
RETRY_STATUS_CODES = [429, 500, 502, 503, 504, 507]
22+
23+
while retries <= max_retries:
24+
try:
25+
r = func(url, params, use_json, body)
26+
if r.status_code in RETRY_STATUS_CODES:
27+
retries += 1
28+
if retries <= max_retries:
29+
continue
30+
else:
31+
kci_err(f"Failed after {max_retries} retries with 500 error.")
32+
raise click.Abort()
33+
34+
r.raise_for_status()
35+
36+
data = r.json()
37+
if "error" in data:
38+
if use_json:
39+
kci_msg(data)
40+
else:
41+
kci_msg("json error: " + str(data["error"]))
2942
raise click.Abort()
43+
return data
3044

31-
r.raise_for_status()
32-
33-
data = r.json()
34-
if "error" in data:
35-
if use_json:
36-
kci_msg(data)
37-
else:
38-
kci_msg("json error: " + str(data["error"]))
45+
except requests.exceptions.RequestException as e:
46+
kci_err(f"Failed to fetch from {DASHBOARD_API}: {str(e)}.")
3947
raise click.Abort()
40-
return data
4148

42-
except requests.exceptions.RequestException as e:
43-
kci_err(f"Failed to fetch from {DASHBOARD_API}: {str(e)}.")
44-
raise click.Abort()
49+
kci_err("Unexpected failure in API request")
50+
raise click.Abort()
51+
52+
return wrapper
53+
4554

46-
kci_err("Unexpected failure in API request")
47-
raise click.Abort()
55+
@_dashboard_request
56+
def dashboard_api_post(endpoint, params, use_json, body, max_retries=3):
57+
return requests.post(endpoint, json=body)
58+
59+
60+
@_dashboard_request
61+
def dashboard_api_fetch(endpoint, params, use_json, max_retries=3):
62+
return requests.get(endpoint)
4863

4964

5065
def dashboard_fetch_summary(origin, giturl, branch, commit, arch, use_json):
@@ -122,3 +137,19 @@ def dashboard_fetch_hardware_list(origin, use_json):
122137
"startTimestampInSeconds": int(last_week.timestamp()),
123138
}
124139
return dashboard_api_fetch("hardware/", params, use_json)
140+
141+
142+
def dashboard_fetch_hardware_summary(name, origin, use_json):
143+
# TODO: add extra filters: Commits, date, filter, origin
144+
now = datetime.today()
145+
last_week = now - timedelta(days=7)
146+
body = {
147+
"origin": origin,
148+
"endTimestampInSeconds": str(int(now.timestamp())),
149+
"startTimestampInSeconds": str(int(last_week.timestamp())),
150+
"selectedCommits": {},
151+
"filter": {},
152+
}
153+
return dashboard_api_post(
154+
f"hardware/{urllib.parse.quote_plus(name)}/summary", {}, use_json, body
155+
)

kcidev/subcommands/results/hardware.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import click
2-
from libs.dashboard import dashboard_fetch_hardware_list
2+
from libs.dashboard import (
3+
dashboard_fetch_hardware_list,
4+
dashboard_fetch_hardware_summary,
5+
)
36
from subcommands.results.options import results_display_options
4-
from subcommands.results.parser import cmd_hardware_list
7+
from subcommands.results.parser import cmd_hardware_list, cmd_summary
58

69

710
@click.group(chain=True, help="Get hardware related information from the dashboard")
@@ -16,3 +19,12 @@ def hardware():
1619
def list(origin, use_json):
1720
data = dashboard_fetch_hardware_list(origin, use_json)
1821
cmd_hardware_list(data, use_json)
22+
23+
24+
@hardware.command()
25+
@click.option("--name", required=True, help="Name of the hardware")
26+
@click.option("--origin", default="maestro", help="Select KCIDB origin")
27+
@results_display_options
28+
def summary(name, origin, use_json):
29+
data = dashboard_fetch_hardware_summary(name, origin, use_json)
30+
cmd_summary(data, use_json)

0 commit comments

Comments
 (0)