Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updates bot logs command to support new interface #149

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import shlex
import subprocess
from datetime import timedelta
from datetime import datetime, timedelta, timezone
from pathlib import Path

import click
Expand Down Expand Up @@ -31,7 +31,7 @@
token_amount_callback,
)
from silverback.cluster.client import ClusterClient, PlatformClient
from silverback.cluster.types import ClusterTier, ResourceStatus
from silverback.cluster.types import ClusterTier, LogLevel, ResourceStatus
from silverback.runner import PollingRunner, WebsocketRunner
from silverback.worker import run_worker

Expand Down Expand Up @@ -120,7 +120,8 @@ def run(cli_ctx, account, runner_class, recorder_class, max_exceptions, bot):
runner_class = PollingRunner
else:
raise click.BadOptionUsage(
option_name="network", message="Network choice cannot support running bot"
option_name="network",
message="Network choice cannot support running bot",
)

runner = runner_class(
Expand Down Expand Up @@ -177,7 +178,11 @@ def build(generate, path):
f"-t {file.name.split('.')[1]}:latest ."
)
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
click.echo(result.stdout)
except subprocess.CalledProcessError as e:
Expand Down Expand Up @@ -1086,14 +1091,34 @@ def stop_bot(cluster: ClusterClient, name: str):

@bots.command(name="logs", section="Bot Operation Commands")
@click.argument("name", metavar="BOT")
@click.option(
"-l",
"--log-level",
"log_level",
help="Minimum log level to display.",
default="INFO",
)
@click.option(
"-s",
"--since",
"since",
help="Return logs since N ago.",
callback=timedelta_callback,
)
@cluster_client
def show_bot_logs(cluster: ClusterClient, name: str):
def show_bot_logs(cluster: ClusterClient, name: str, log_level: str, since: timedelta | None):
"""Show runtime logs for BOT in CLUSTER"""

start_time = None
if since:
start_time = datetime.now(tz=timezone.utc) - since

if not (bot := cluster.bots.get(name)):
raise click.UsageError(f"Unknown bot '{name}'.")

for log in bot.logs:
for log in bot.filter_logs(
log_level=LogLevel.by_name(log_level, LogLevel.INFO), start_time=start_time
):
click.echo(log)


Expand Down
30 changes: 26 additions & 4 deletions silverback/cluster/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from functools import cache
from typing import ClassVar, Literal

Expand All @@ -12,9 +13,11 @@
from .types import (
BotHealth,
BotInfo,
BotLogEntry,
ClusterHealth,
ClusterInfo,
ClusterState,
LogLevel,
RegistryCredentialsInfo,
StreamInfo,
VariableGroupInfo,
Expand Down Expand Up @@ -189,11 +192,30 @@ def errors(self) -> list[str]:
handle_error_with_response(response)
return response.json()

@property
def logs(self) -> list[str]:
response = self.cluster.get(f"/bots/{self.id}/logs")
def filter_logs(
self,
log_level: LogLevel = LogLevel.INFO,
start_time: datetime | None = None,
end_time: datetime | None = None,
) -> list[BotLogEntry]:
print("filter_logs:", log_level)
query = {"log_level": log_level.name}

if start_time:
query["start_time"] = start_time.isoformat()

if end_time:
query["end_time"] = end_time.isoformat()

response = self.cluster.get(f"/bots/{self.id}/logs", params=query, timeout=120)
print("response:", response)
breakpoint()
mikeshultz marked this conversation as resolved.
Show resolved Hide resolved
handle_error_with_response(response)
return response.json()
return [BotLogEntry.model_validate(log) for log in response.json()]

@property
def logs(self) -> list[BotLogEntry]:
return self.filter_logs()

def remove(self):
response = self.cluster.delete(f"/bots/{self.id}")
Expand Down
30 changes: 30 additions & 0 deletions silverback/cluster/types.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations
johnson2427 marked this conversation as resolved.
Show resolved Hide resolved

import enum
import math
import uuid
from datetime import datetime
from enum import IntEnum
from typing import Annotated, Any

from ape.types import AddressType, HexBytes
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.hmac import HMAC, hashes
from eth_utils import to_bytes, to_int
from pydantic import BaseModel, Field, computed_field, field_validator
from typing_extensions import Self


def normalize_bytes(val: bytes, length: int = 16) -> bytes:
Expand Down Expand Up @@ -374,3 +378,29 @@ class BotInfo(BaseModel):
registry_credentials_id: str | None

environment: list[EnvironmentVariable] = []


class LogLevel(IntEnum):
mikeshultz marked this conversation as resolved.
Show resolved Hide resolved
DEBUG = 10
INFO = 20
WARNING = 30
ERROR = 40
CRITICAL = 50

@classmethod
def by_name(cls, name: str = "INFO", default: Self | None = None) -> Self:
try:
return cls.__dict__[name.upper()]
except KeyError as err:
if default:
return default
raise err


class BotLogEntry(BaseModel):
message: str
timestamp: datetime | None
level: LogLevel

def __str__(self) -> str:
return f"{self.timestamp}: {self.message}"
Loading