Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
guptadev21 committed Oct 18, 2024
1 parent c282cd5 commit fae3edc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
49 changes: 24 additions & 25 deletions riocli/device/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# 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.device.util import name_to_guid, shared_url_debug_logs, upload_debug_logs
from riocli.utils.spinner import with_spinner


Expand All @@ -37,7 +32,7 @@
"--share",
"-s",
is_flag=True,
help="Share the device logs with Rapyuta support",
help="Generate a public URL for sharing",
default=False,
)
@click.option(
Expand All @@ -60,7 +55,27 @@ def report_device(
spinner=None,
) -> None:
"""
Report a device and get its debug logs in uploads section.
This function facilitates the reporting of a device by uploading its debug logs.
It also provides an option to share the logs with Rapyuta support by generating
a public URL with a specified expiry time.
Usage Examples:
Report a device with confirmation prompt.
$ rio device report DEVICE_NAME
Report a device without confirmation prompt.
$ rio device report -f DEVICE_NAME
Report a device and generate a public URL for sharing.
$ rio device report -s DEVICE_NAME
Report a device with a custom expiry time for the shared URL.
$ rio device report -s -e 10 DEVICE_NAME
"""
if not force:
with spinner.hidden():
Expand All @@ -70,7 +85,7 @@ def report_device(
)
try:
# Upload the debug logs
response = report_device_api_call(device_guid=device_guid)
response = upload_debug_logs(device_guid=device_guid)

if share:
spinner.text = click.style("Creating shared URL...", fg=Colors.YELLOW)
Expand All @@ -87,19 +102,3 @@ 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}")
23 changes: 19 additions & 4 deletions riocli/device/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import functools
import json
import re
Expand All @@ -21,9 +22,8 @@
import click
from munch import Munch
from rapyuta_io import Client
from rapyuta_io.clients import LogUploads
from rapyuta_io.clients.device import Device
from rapyuta_io.clients.device import DeviceStatus
from rapyuta_io.clients import LogUploads, SharedURL
from rapyuta_io.clients.device import Device, DeviceStatus
from rapyuta_io.utils import RestClient
from rapyuta_io.utils.rest_client import HttpMethod

Expand Down Expand Up @@ -75,7 +75,7 @@ def decorated(**kwargs: typing.Any):
return decorated


def report_device_api_call(device_guid: str) -> dict:
def upload_debug_logs(device_guid: str) -> dict:
config = Configuration()
coreapi_host = config.data.get(
"core_api_host", "https://gaapiserver.apps.okd4v2.prod.rapyuta.io"
Expand All @@ -98,6 +98,21 @@ def report_device_api_call(device_guid: str) -> dict:
return response.json()


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() + datetime.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}")


def get_device_name(client: Client, guid: str) -> str:
device = client.get_device(device_id=guid)
return device.name
Expand Down

0 comments on commit fae3edc

Please sign in to comment.