Skip to content

Commit

Permalink
piping out logs in the cli (#143)
Browse files Browse the repository at this point in the history
* piping out logs in the cli

* add loading animation while fetching logs
  • Loading branch information
kaustubh-cf authored Sep 7, 2023
1 parent c6c7d73 commit 58fa7f3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ functions-framework = "^3.4.0"
yaspin = "^3.0.0"
pydantic = "^2.3.0"
google-generativeai = "^0.1.0"
rich = "^13.5.2"

[build-system]
requires = ["poetry-core"]
Expand Down
44 changes: 43 additions & 1 deletion textbase/textbase_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import importlib.resources
import re
import urllib.parse
from textbase.utils.logs import fetch_and_display_logs


CLOUD_URL = "https://us-east1-chat-agents.cloudfunctions.net/deploy-from-cli"
UPLOAD_URL = "https://us-east1-chat-agents.cloudfunctions.net/upload-file"
Expand Down Expand Up @@ -71,7 +73,8 @@ def validate_bot_name(ctx, param, value):
@click.option("--path", prompt="Path to the zip folder", required=True)
@click.option("--bot_name", prompt="Name of the bot", required=True, callback=validate_bot_name)
@click.option("--api_key", prompt="Textbase API Key", required=True)
def deploy(path, bot_name, api_key):
@click.option("--show_logs", is_flag=True, default=True, help="Fetch show_logs after deployment")
def deploy(path, bot_name, api_key, show_logs):
click.echo(click.style(f"Deploying bot '{bot_name}' with zip folder from path: {path}", fg='yellow'))

headers = {
Expand Down Expand Up @@ -116,6 +119,23 @@ def deploy(path, bot_name, api_key):
else:
click.echo(click.style("Something went wrong! ❌", fg='red'))
click.echo(response.text)

# Piping logs in the cli in real-time
if show_logs:
click.echo(click.style(f"Fetching logs for bot '{bot_name}'...", fg='green'))

cloud_url = f"{CLOUD_URL}/logs"
headers = {
"Authorization": f"Bearer {api_key}"
}
params = {
"botName": bot_name,
"pageToken": None
}

fetch_and_display_logs(cloud_url=cloud_url,
headers=headers,
params=params)
#################################################################################################################

@cli.command()
Expand Down Expand Up @@ -223,6 +243,28 @@ def delete(bot_id, api_key):
else:
click.echo(click.style("Something went wrong!", fg='red'))


@cli.command()
@click.option("--bot_name", prompt="Name of the bot", required=True)
@click.option("--api_key", prompt="Textbase API Key", required=True)
@click.option("--start_time", prompt="Logs for previous ___ minutes", required=False, default=5)
def logs(bot_name, api_key, start_time):
click.echo(click.style(f"Fetching logs for bot '{bot_name}'...", fg='green'))

cloud_url = f"{CLOUD_URL}/logs"
headers = {
"Authorization": f"Bearer {api_key}"
}
params = {
"botName": bot_name,
"startTime": start_time,
"pageToken": None
}

fetch_and_display_logs(cloud_url=cloud_url,
headers=headers,
params=params)

if __name__ == "__main__":
cli()

42 changes: 42 additions & 0 deletions textbase/utils/logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import requests
from rich.console import Console
from rich.table import Table
from rich.text import Text
import time
import click
from yaspin import yaspin

def fetch_and_display_logs(cloud_url, headers, params):
console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Timestamp")
table.add_column("Severity")
table.add_column("Summary")

while True:
with yaspin(text="Logs...", color="yellow") as spinner:
response = requests.get(cloud_url, headers=headers, params=params)

if response.ok:
response_data = response.json()
data = response_data.get('data')
if data is not None:
logs = data.get('logs')
if logs:
for log in logs:
severity_color = 'blue' if log['severity'].lower() in ['notice', 'info', 'debug'] else 'red' if log['severity'].lower() in ['alert', 'critical', 'error'] else 'yellow'

table.add_row(log['timestamp'], Text(log['severity'], style=severity_color), Text(log.get('text', ''), style=severity_color))

console.clear()
console.print(table)
# Update the params for the next request
params['pageToken'] = data.get('nextPageToken')
params['startTime'] = data.get('startTime')
else:
click.echo(click.style("No logs found in the response.", fg='yellow'))
else:
click.echo(click.style("Failed to retrieve logs.", fg='red'))

# Poll the endpoint every 3 seconds
time.sleep(3)

0 comments on commit 58fa7f3

Please sign in to comment.