Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
guptadev21 committed Oct 18, 2024
1 parent 260f2af commit c282cd5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
53 changes: 51 additions & 2 deletions riocli/device/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime, timedelta

import click
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients import SharedURL

from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.device.util import name_to_guid, report_device_api_call
from riocli.utils.spinner import with_spinner
Expand All @@ -29,11 +33,31 @@
@click.option(
"--force", "-f", "--silent", "force", is_flag=True, help="Skip confirmation"
)
@click.option(
"--share",
"-s",
is_flag=True,
help="Share the device logs with Rapyuta support",
default=False,
)
@click.option(
"--expiry",
"-e",
help="Expiry time for the shared URL in days",
default=7,
required=False,
show_default=True,
)
@click.argument("device-name", type=str)
@name_to_guid
@with_spinner(text="Upload debugging files...")
def report_device(
device_name: str, device_guid: str, force: bool, spinner=None
device_name: str,
device_guid: str,
force: bool,
share: bool,
expiry: int,
spinner=None,
) -> None:
"""
Report a device and get its debug logs in uploads section.
Expand All @@ -45,7 +69,16 @@ def report_device(
abort=True,
)
try:
report_device_api_call(device_guid=device_guid)
# Upload the debug logs
response = report_device_api_call(device_guid=device_guid)

if share:
spinner.text = click.style("Creating shared URL...", fg=Colors.YELLOW)
public_url = shared_url_debug_logs(
device_guid, response["response"]["data"]["request_uuid"], expiry
)
click.secho(public_url.url, fg=Colors.GREEN)

spinner.text = click.style("Device reported successfully.", fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)

Expand All @@ -54,3 +87,19 @@ def report_device(
"Failed to report device: {}".format(e), fg=Colors.RED
)
spinner.red.fail(Symbols.ERROR)


@with_spinner(text="Creating shared URL...")
def shared_url_debug_logs(device_guid: str, request_id: str, expiry: int, spinner=None):
try:
client = new_client()
device = client.get_device(device_id=device_guid)
expiry_time = datetime.now() + timedelta(days=expiry)

# Create the shared URL
public_url = device.create_shared_url(
SharedURL(request_id, expiry_time=expiry_time)
)
return public_url
except Exception as e:
raise Exception(f"Failed to create shared URL: {e}")
7 changes: 6 additions & 1 deletion riocli/device/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ def report_device_api_call(device_guid: str) -> dict:
RestClient(url).method(HttpMethod.POST).headers(headers).execute(payload={})
)

return handle_server_errors(response)
result = handle_server_errors(response)

if result:
return result

return response.json()


def get_device_name(client: Client, guid: str) -> str:
Expand Down

0 comments on commit c282cd5

Please sign in to comment.