From edbe4d8573cb20338f826b6beab642d1547211be Mon Sep 17 00:00:00 2001 From: nickpetrovic <4001122+nickpetrovic@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:58:32 -0400 Subject: [PATCH] Increase --lines default, add --show-timestamp (#26) - Increase the default value of --lines to 250 logs - Add a --show-timestamp option that will prefix the logs timestamp to the log - Update ruff package --- poetry.lock | 54 +++++++++++++++++----------------- pyproject.toml | 4 +-- src/beam/__init__.py | 2 ++ src/beam/cli/logs.py | 69 +++++++++++++++++++++++++++----------------- 4 files changed, 73 insertions(+), 56 deletions(-) diff --git a/poetry.lock b/poetry.lock index 56b6e99..7f98b43 100644 --- a/poetry.lock +++ b/poetry.lock @@ -55,13 +55,13 @@ tests = ["mypy (>=0.800)", "pytest", "pytest-asyncio"] [[package]] name = "beta9" -version = "0.1.68" +version = "0.1.71" description = "" optional = false python-versions = "<4.0,>=3.8" files = [ - {file = "beta9-0.1.68-py3-none-any.whl", hash = "sha256:2e95e6f6a3fe3a2142a99e180228856b95244ab3a39ddd2eb0377209411c4d66"}, - {file = "beta9-0.1.68.tar.gz", hash = "sha256:67040b456ef566a263b32e308a78c527ac3f1ee6bf756e577d40af5c105e17cb"}, + {file = "beta9-0.1.71-py3-none-any.whl", hash = "sha256:b9722c6109b36a9c0d207d1f5800676171b34c1135d7c5a2f868a2018b24f96a"}, + {file = "beta9-0.1.71.tar.gz", hash = "sha256:4b23924ef5fb2d3886c0debde6861848780ad4421e7c8f1010a749c414338b2c"}, ] [package.dependencies] @@ -69,7 +69,7 @@ asgiref = ">=3.8.1,<4.0.0" betterproto-beta9 = {version = "2.0.0b7", extras = ["compiler"]} click = ">=8.1.7,<9.0.0" cloudpickle = ">=3.0.0,<4.0.0" -croniter = ">=2.0.3,<3.0.0" +croniter = ">=3.0.3,<4.0.0" fastapi = ">=0.110.2,<0.111.0" grpcio = ">=1.60.0,<2.0.0" grpclib = ">=0.4.7,<0.5.0" @@ -298,13 +298,13 @@ files = [ [[package]] name = "croniter" -version = "2.0.7" +version = "3.0.3" description = "croniter provides iteration for datetime object with cron like format" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.6" files = [ - {file = "croniter-2.0.7-py2.py3-none-any.whl", hash = "sha256:f15e80828d23920c4bb7f4d9340b932c9dcabecafc7775703c8b36d1253ed526"}, - {file = "croniter-2.0.7.tar.gz", hash = "sha256:1041b912b4b1e03751a0993531becf77851ae6e8b334c9c76ffeffb8f055f53f"}, + {file = "croniter-3.0.3-py2.py3-none-any.whl", hash = "sha256:b3bd11f270dc54ccd1f2397b813436015a86d30ffc5a7a9438eec1ed916f2101"}, + {file = "croniter-3.0.3.tar.gz", hash = "sha256:34117ec1741f10a7bd0ec3ad7d8f0eb8fa457a2feb9be32e6a2250e158957668"}, ] [package.dependencies] @@ -1051,29 +1051,29 @@ jupyter = ["ipywidgets (>=7.5.1,<9)"] [[package]] name = "ruff" -version = "0.5.6" +version = "0.6.2" description = "An extremely fast Python linter and code formatter, written in Rust." optional = false python-versions = ">=3.7" files = [ - {file = "ruff-0.5.6-py3-none-linux_armv6l.whl", hash = "sha256:a0ef5930799a05522985b9cec8290b185952f3fcd86c1772c3bdbd732667fdcd"}, - {file = "ruff-0.5.6-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:b652dc14f6ef5d1552821e006f747802cc32d98d5509349e168f6bf0ee9f8f42"}, - {file = "ruff-0.5.6-py3-none-macosx_11_0_arm64.whl", hash = "sha256:80521b88d26a45e871f31e4b88938fd87db7011bb961d8afd2664982dfc3641a"}, - {file = "ruff-0.5.6-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d9bc8f328a9f1309ae80e4d392836e7dbc77303b38ed4a7112699e63d3b066ab"}, - {file = "ruff-0.5.6-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:4d394940f61f7720ad371ddedf14722ee1d6250fd8d020f5ea5a86e7be217daf"}, - {file = "ruff-0.5.6-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:111a99cdb02f69ddb2571e2756e017a1496c2c3a2aeefe7b988ddab38b416d36"}, - {file = "ruff-0.5.6-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:e395daba77a79f6dc0d07311f94cc0560375ca20c06f354c7c99af3bf4560c5d"}, - {file = "ruff-0.5.6-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c476acb43c3c51e3c614a2e878ee1589655fa02dab19fe2db0423a06d6a5b1b6"}, - {file = "ruff-0.5.6-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e2ff8003f5252fd68425fd53d27c1f08b201d7ed714bb31a55c9ac1d4c13e2eb"}, - {file = "ruff-0.5.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c94e084ba3eaa80c2172918c2ca2eb2230c3f15925f4ed8b6297260c6ef179ad"}, - {file = "ruff-0.5.6-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:1f77c1c3aa0669fb230b06fb24ffa3e879391a3ba3f15e3d633a752da5a3e670"}, - {file = "ruff-0.5.6-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:f908148c93c02873210a52cad75a6eda856b2cbb72250370ce3afef6fb99b1ed"}, - {file = "ruff-0.5.6-py3-none-musllinux_1_2_i686.whl", hash = "sha256:563a7ae61ad284187d3071d9041c08019975693ff655438d8d4be26e492760bd"}, - {file = "ruff-0.5.6-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:94fe60869bfbf0521e04fd62b74cbca21cbc5beb67cbb75ab33fe8c174f54414"}, - {file = "ruff-0.5.6-py3-none-win32.whl", hash = "sha256:e6a584c1de6f8591c2570e171cc7ce482bb983d49c70ddf014393cd39e9dfaed"}, - {file = "ruff-0.5.6-py3-none-win_amd64.whl", hash = "sha256:d7fe7dccb1a89dc66785d7aa0ac283b2269712d8ed19c63af908fdccca5ccc1a"}, - {file = "ruff-0.5.6-py3-none-win_arm64.whl", hash = "sha256:57c6c0dd997b31b536bff49b9eee5ed3194d60605a4427f735eeb1f9c1b8d264"}, - {file = "ruff-0.5.6.tar.gz", hash = "sha256:07c9e3c2a8e1fe377dd460371c3462671a728c981c3205a5217291422209f642"}, + {file = "ruff-0.6.2-py3-none-linux_armv6l.whl", hash = "sha256:5c8cbc6252deb3ea840ad6a20b0f8583caab0c5ef4f9cca21adc5a92b8f79f3c"}, + {file = "ruff-0.6.2-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:17002fe241e76544448a8e1e6118abecbe8cd10cf68fde635dad480dba594570"}, + {file = "ruff-0.6.2-py3-none-macosx_11_0_arm64.whl", hash = "sha256:3dbeac76ed13456f8158b8f4fe087bf87882e645c8e8b606dd17b0b66c2c1158"}, + {file = "ruff-0.6.2-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:094600ee88cda325988d3f54e3588c46de5c18dae09d683ace278b11f9d4d534"}, + {file = "ruff-0.6.2-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:316d418fe258c036ba05fbf7dfc1f7d3d4096db63431546163b472285668132b"}, + {file = "ruff-0.6.2-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:d72b8b3abf8a2d51b7b9944a41307d2f442558ccb3859bbd87e6ae9be1694a5d"}, + {file = "ruff-0.6.2-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:2aed7e243be68487aa8982e91c6e260982d00da3f38955873aecd5a9204b1d66"}, + {file = "ruff-0.6.2-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:d371f7fc9cec83497fe7cf5eaf5b76e22a8efce463de5f775a1826197feb9df8"}, + {file = "ruff-0.6.2-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:a8f310d63af08f583363dfb844ba8f9417b558199c58a5999215082036d795a1"}, + {file = "ruff-0.6.2-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7db6880c53c56addb8638fe444818183385ec85eeada1d48fc5abe045301b2f1"}, + {file = "ruff-0.6.2-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:1175d39faadd9a50718f478d23bfc1d4da5743f1ab56af81a2b6caf0a2394f23"}, + {file = "ruff-0.6.2-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:5b939f9c86d51635fe486585389f54582f0d65b8238e08c327c1534844b3bb9a"}, + {file = "ruff-0.6.2-py3-none-musllinux_1_2_i686.whl", hash = "sha256:d0d62ca91219f906caf9b187dea50d17353f15ec9bb15aae4a606cd697b49b4c"}, + {file = "ruff-0.6.2-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:7438a7288f9d67ed3c8ce4d059e67f7ed65e9fe3aa2ab6f5b4b3610e57e3cb56"}, + {file = "ruff-0.6.2-py3-none-win32.whl", hash = "sha256:279d5f7d86696df5f9549b56b9b6a7f6c72961b619022b5b7999b15db392a4da"}, + {file = "ruff-0.6.2-py3-none-win_amd64.whl", hash = "sha256:d9f3469c7dd43cd22eb1c3fc16926fb8258d50cb1b216658a07be95dd117b0f2"}, + {file = "ruff-0.6.2-py3-none-win_arm64.whl", hash = "sha256:f28fcd2cd0e02bdf739297516d5643a945cc7caf09bd9bcb4d932540a5ea4fa9"}, + {file = "ruff-0.6.2.tar.gz", hash = "sha256:239ee6beb9e91feb8e0ec384204a763f36cb53fb895a1a364618c6abb076b3be"}, ] [[package]] @@ -1317,4 +1317,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "1ef4d2f355035ba388747b03c164cd4574de9bf923f1210e767d0de0f2e55f23" +content-hash = "631819be8df423ee455333eeef701500c30037bd15cb0e5060f23bc4b9d6f3e9" diff --git a/pyproject.toml b/pyproject.toml index 5ef960b..531ce26 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "beam-client" -version = "0.2.66" +version = "0.2.68" description = "" authors = ["beam.cloud "] packages = [ @@ -10,7 +10,7 @@ packages = [ [tool.poetry.dependencies] python = "^3.8" -beta9 = "0.1.68" +beta9 = "0.1.71" requests = "^2.31.0" websockets = "^12.0" diff --git a/src/beam/__init__.py b/src/beam/__init__.py index 92da487..7d72d4b 100644 --- a/src/beam/__init__.py +++ b/src/beam/__init__.py @@ -4,6 +4,7 @@ from beta9.abstractions.endpoint import ASGI as asgi from beta9.abstractions.endpoint import Endpoint as endpoint from beta9.abstractions.function import Function as function +from beta9.abstractions.function import Schedule as schedule from beta9.abstractions.image import Image from beta9.abstractions.map import Map from beta9.abstractions.output import Output @@ -28,4 +29,5 @@ "Output", "QueueDepthAutoscaler", "experimental", + "schedule", ] diff --git a/src/beam/cli/logs.py b/src/beam/cli/logs.py index 0f59a53..58f50c9 100644 --- a/src/beam/cli/logs.py +++ b/src/beam/cli/logs.py @@ -58,9 +58,16 @@ def common(**_): "--lines", "-n", type=click.INT, - required=True, - default=10, - help="Number of lines back to start.", + required=False, + default=250, + help="Display the last N lines.", +) +@click.option( + "--show-timestamp", + type=click.BOOL, + is_flag=True, + required=False, + help="Include the log's timestamp.", ) @click.option( "--host", @@ -83,6 +90,7 @@ def logs( deployment_id: Optional[str], container_id: Optional[str], lines: int, + show_timestamp: bool, realtime_host: str, config_path: str, ): @@ -108,26 +116,30 @@ def logs( }.get(object_id, "") now = datetime.datetime.now(datetime.timezone.utc) - logs_before = json.dumps({ - "token": context.token, - "streamType": "LOGS_STREAM", - "action": "LOGS_QUERY", - "stream": False, - "objectType": object_type, - "objectId": object_id, - "size": lines, - "endingTimestamp": now.isoformat(), - }) - - logs_current = json.dumps({ - "token": context.token, - "streamType": "LOGS_STREAM", - "action": "LOGS_ADD_STREAM", - "stream": True, - "objectType": object_type, - "objectId": object_id, - "startingTimestamp": now.isoformat(), - }) + logs_before = json.dumps( + { + "token": context.token, + "streamType": "LOGS_STREAM", + "action": "LOGS_QUERY", + "stream": False, + "objectType": object_type, + "objectId": object_id, + "size": lines, + "endingTimestamp": now.isoformat(), + } + ) + + logs_current = json.dumps( + { + "token": context.token, + "streamType": "LOGS_STREAM", + "action": "LOGS_ADD_STREAM", + "stream": True, + "objectType": object_type, + "objectId": object_id, + "startingTimestamp": now.isoformat(), + } + ) with connect(**websocket_params) as w, terminal.progress("Streaming...") as p: keep_alive = Thread(target=websocket_keep_alive, args=(w,)) @@ -135,7 +147,7 @@ def logs( try: w.send(logs_before) - print_message(w.recv()) + print_message(w.recv(), show_timestamp) except Exception as e: p.stop() exit_keep_alive_thread() @@ -144,7 +156,7 @@ def logs( try: w.send(logs_current) while True: - print_message(w.recv()) + print_message(w.recv(), show_timestamp) except KeyboardInterrupt: p.stop() exit_keep_alive_thread() @@ -155,7 +167,7 @@ def logs( terminal.error(str(e)) -def print_message(msg: Union[str, bytes]) -> None: +def print_message(msg: Union[str, bytes], show_timestamp: bool = False) -> None: data = json.loads(msg) if "logs" in data: hits = data["logs"]["hits"]["hits"] @@ -168,7 +180,10 @@ def print_message(msg: Union[str, bytes]) -> None: hits = sorted(hits, key=lambda k: k["_source"]["@timestamp"]) for hit in hits: - terminal.print(hit["_source"]["msg"], highlight=True, end="") + log = hit["_source"]["msg"] + if show_timestamp: + log = f"[{hit['_source']['@timestamp']}] {log}" + terminal.print(log, highlight=True, end="") def websocket_keep_alive(conn: ClientConnection, interval: int = 60):