Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yubozhao committed Apr 28, 2022
1 parent 1d8adeb commit c49831f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 39 deletions.
5 changes: 5 additions & 0 deletions bentoctl/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def init(save_path, do_not_generate):
if not do_not_generate:
generated_files = deployment_config.generate()
print_generated_files_list(generated_files)
return deployment_config


@bentoctl.command()
Expand Down Expand Up @@ -114,6 +115,7 @@ def generate(deployment_config_file, values_only, save_path):
destination_dir=save_path, values_only=values_only
)
print_generated_files_list(generated_files)
return deployment_config


@bentoctl.command()
Expand Down Expand Up @@ -172,6 +174,7 @@ def build(
print_generated_files_list(generated_files)
else:
console.print(f"[green]Create docker image: {local_docker_tag}[/]")
return deployment_config


@bentoctl.command()
Expand All @@ -194,6 +197,7 @@ def destroy(deployment_config_file):
terraform_destroy()
deployment_config.delete_repository()
console.print(f"Deleted the repository {deployment_config.repository_name}")
return deployment_config


@bentoctl.command()
Expand All @@ -211,6 +215,7 @@ def apply(deployment_config_file):
deployment_config = DeploymentConfig.from_file(deployment_config_file)
if deployment_config.template_type.startswith("terraform"):
terraform_apply()
return deployment_config


# subcommands
Expand Down
32 changes: 23 additions & 9 deletions bentoctl/cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import functools
import sys
import time

import click

from bentoctl.exceptions import BentoctlException
from bentoctl.utils import set_debug_mode
from bentoctll.utils.usage_stats import BENTOCTL_DO_NOT_TRACK, cli_events_map
from bentoctl.utils.usage_stats import (
BENTOCTL_DO_NOT_TRACK,
cli_events_map,
CliEvent,
track,
)

DEBUG_ENV_VAR = "BENTOCTL_DEBUG"

Expand Down Expand Up @@ -59,21 +65,28 @@ def wrapper(do_not_track: bool, *args, **kwargs):
os.environs["BENTOCTL_DO_NOT_TRACK"] = str(True)
return func(*args, **kwargs)
start_time = time.time_ns()
if (cmd_group.name in cli_events_map and command_name in cli_events_map[cmd_group.name]):
if cmd_group.name in cli_events_map:
# If cli command is build or operator related, we will add additoinal properties
get_tracking_event = functools.partial(
cli_events_map[cmd_group.name][command_name],
cli_events_map[cmd_group.name],
cmd_group.name,
command_name
command_name,
)
elif cmd_group.name == "operator":

def get_tracking_event(return_value):
return CliEvent(
cmd_group.name, command_name, operator=kwargs.get("name", None)
)

else:

def get_tracking_event(ret):
return {
cmd_group=cmd_group.name,
cmd_name=command_name,
}
return CliEvent(cmd_group.name, command_name)

try:
return_value = func(*args, **kwargs)
event = get_tracking_event(return_value)
event = get_tracking_event(return_value=return_value)
duration_in_ms = time.time_ns() - start_time
event.duration_in_ms = duration_in_ms / 1e6
track(event)
Expand All @@ -86,6 +99,7 @@ def get_tracking_event(ret):
event.return_code = 2 if isinstance(e, KeyboardInterrupt) else 1
track(event)
raise

return wrapper

def command(self, *args, **kwargs):
Expand Down
74 changes: 44 additions & 30 deletions bentoctl/utils/usage_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import requests
import yaml

# These are internal apis. We will need to make sure update these when BentoML changes.
from bentoml._internal.utils import bentoml_cattr
from bentoml._internal.utils.analytics.schemas import CommonProperties


Expand All @@ -20,46 +22,58 @@ def do_not_track() -> bool:
def get_payload(event_properties: dict) -> dict:
common_properties = CommonProperties()
return {
"common_properties": common_properties,
"common_properties": bentoml_cattr.unstructure(common_properties),
"event_properties": event_properties,
"session_id": uuid.uuid1().hex,
"event_type": "bentoctl-cli"
"event_type": "bentoctl-cli",
}


def track(event_properties: dict):
def track(event_properties):
if do_not_track():
return
payload = get_payload(event_properties=event_properties)
payload = get_payload(event_properties=vars(event_properties))

requests.post(
USAGE_TRACKING_URL, json=payload, timeout=USAGE_REQUEST_TIMEOUT_SECONDS
)


def _cli_bentoctl_build_event(cmd_group, cmd_name, cmd_args, cmd_kwargs):
return {}


cli_events_map = {"cli": {"build": _cli_bentoctl_build_event}}
# {
# 'session_id': 'e0511c1ac5a711ecbc9f1c5c60561cbc',
# 'event_properties': {
# 'cmd_name': 'serve',
# 'error_type': None,
# 'return_code': None
# },
# 'common_properties': {
# 'timestamp': '2022-04-26T21:28:59.408329+00:00',
# 'platform': 'Darwin-21.5.0-x86_64-i386-64bit',
# 'bentoml_version': '1.0.0a7.post9+gea0040f3',
# 'python_version': '3.7.10',
# 'is_interactive': False, 'in_notebook': False, 'memory_usage_percent': 0.6437778472900391, 'total_memory_in_mb': 16384,
# 'client': {
# 'id': '0cf269e4-ecf7-4869-ba7f-f32bc3d39ee5',
# 'creation_timestamp': '2022-04-06T22:14:53.694582+00:00'
# },
# 'yatai_user_email': 'admin@abc.com'
# },
# 'event_type': 'cli'
# }
class CliEvent:
def __init__(
self,
cmd_group,
cmd_name,
duration_in_ms=None,
error_type=None,
return_code=None,
operator=None,
version=None,
):
self.cmd_group = cmd_group
self.cmd_name = cmd_name
self.duration_in_ms = 0
self.error_type = None
self.return_code = None
self.operator = operator
self.version = version


def _bentoctl_event(cmd_group, cmd_name, return_value=None):
if return_value is not None:
deployment_config = return_value
version = (
deployment_config.bento.tag.version if deployment_config.bento else None
)

return CliEvent(
cmd_group,
cmd_name,
operator=deployment_config.operator_name,
version=version,
)
else:
return CliEvent(cmd_group, cmd_name)


cli_events_map = {"bentoctl": _bentoctl_event}

0 comments on commit c49831f

Please sign in to comment.