Skip to content

Commit

Permalink
Merge pull request #21 from jonnyjohnson1/jonny/menu-button
Browse files Browse the repository at this point in the history
fix: icon is orange when a new release is available
  • Loading branch information
jonnyjohnson1 authored Nov 15, 2024
2 parents 3778283 + 88a0ab6 commit f1a4c4d
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 41 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Tech: nixOS, ollama, postgres, FastAPI, huggingface-transformers
---

## (MacOS) Easy Install With .dmg
[![Download](https://img.shields.io/badge/Download-File-blue?style=for-the-badge&logo=google-drive)](https://drive.google.com/file/d/1UpI8YOqqXnSQQbCo3oB28-w8nNlbhPHp/view?usp=sharing)

*(Experimental)*: This is new, and should work on most MacOS machines!
Simply double click the topos.dmg file, and drag the app into your Applications directory.
You should be able to launch the Topos service anywhere from your machine.
Expand All @@ -39,7 +41,7 @@ If nix is not installed:
windows: `sh <(curl -L https://nixos.org/nix/install) --daemon`
2. Run Topos and all its dependencies:
```
nix run github:jonnyjohnson1/topos-cli/v0.2.7
nix run github:jonnyjohnson1/topos-cli/v0.2.8
```
This will start all services including Topos, Postgres, Kafka, and Ollama.

Expand Down Expand Up @@ -146,4 +148,4 @@ Insert this url into the field under settings-> "Api Endpoints" -> "Custom API"

## Future Setup
[ ] Theme the TUI [docs](https://f1bonacc1.github.io/process-compose/tui/)
[ ] Remotely connect to TUI [docs](https://f1bonacc1.github.io/process-compose/client/)
[ ] Remotely connect to TUI [docs](https://f1bonacc1.github.io/process-compose/client/)
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ stoppg:
export PGDATA=$(pwd)/pgdata
echo "Stopping any existing PostgreSQL server..."
pg_ctl -D "$PGDATA" stop || echo "No existing server to stop."



# TESTS
test_update:
poetry run python -m topos.test.topos_updater.test_check_update
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "topos"
version = "0.2.7"
version = "0.2.8"
description = "The official Python client for Topos."
authors = ["Dialogues <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ tell application "Terminal"
echo 'Nix installation complete.'; \
else \
echo 'Nix is already installed.'; \
fi && nix run github:jonnyjohnson1/topos-cli/v0.2.7 --extra-experimental-features nix-command --extra-experimental-features flakes --show-trace"
fi && nix run github:jonnyjohnson1/topos-cli/v0.2.8 --extra-experimental-features nix-command --extra-experimental-features flakes --show-trace"
end tell
EOF
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = topos
version = 0.2.7
version = 0.2.8
author = Jonny Johnson
author_email = [email protected]
description = For interacting with Topos tooling
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='topos',
version='0.2.7',
version='0.2.8',
packages=find_packages(),
entry_points={
'console_scripts': [
Expand Down
Binary file modified topos.dmg
Binary file not shown.
102 changes: 71 additions & 31 deletions topos/app/menu_bar_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,98 @@ def start_web_app():
api_thread.start()
# Create and start the tray icon on the main thread
create_tray_icon()



status_checks = {
"health_check": False,
"update_check": False,
}

def check_health(icon):
"""Periodically check the service health."""
certs = get_ssl_certificates()
if not os.path.exists(certs['cert_path']):
print(f"Certificate file not found: {certs['cert_path']}")
if not os.path.exists(certs['key_path']):
print(f"Key file not found: {certs['key_path']}")

while icon.visible:
try:
with warnings.catch_warnings(): # cert=(certs['cert_path'], certs['key_path']) #for verification, but wasn't working
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
response = requests.get(API_URL, verify=False)
if response.status_code == 200:
update_status(icon, "Service is running", (170, 255, 0, 255))
else:
update_status(icon, "Service is not running", "red")
# Update health check status based on response
status_checks["health_check"] = response.status_code == 200
except requests.exceptions.RequestException as e:
update_status(icon, f"Error: {str(e)}", "red")
print(f"Health check error: {str(e)}")
status_checks["health_check"] = False
finally:
evaluate_icon_status(icon)
time.sleep(5)

def check_for_update():
# Check the latest release status from the GitHub API
# Returns True if an update is available, else False
update_is_available = check_for_update("jonnyjohnson1", "topos-cli")
return update_is_available

def check_update():
"""Check if an update is available."""
return check_for_update("jonnyjohnson1", "topos-cli")


def evaluate_icon_status(icon):
"""Evaluate and update the icon's status and menu based on checks."""
if not status_checks["health_check"]:
# If the service is not running
update_status(icon, "Service is not running", "red")
elif status_checks["update_check"]:
# If an update is available
update_status(icon, "Update available", (255, 165, 0, 255)) # Orange
else:
# If all checks pass
update_status(icon, "Service is running", (170, 255, 0, 255)) # Green

# Update the menu based on the current status
update_menu(icon)

def check_update_available(icon):
"""Periodically check for updates."""
while icon.visible:
if check_for_update():
update_status(icon, "Update available", (255, 165, 0, 255)) # Orange indicator
# Add "Check for Update" option if update is available
icon.menu = pystray.Menu(
pystray.MenuItem("Open API Docs", open_docs),
pystray.MenuItem("Update your Topos", pull_latest_release),
pystray.MenuItem("Exit", on_exit)
)
else:
# Set the menu back to its default state without "Check for Update"
icon.menu = pystray.Menu(
pystray.MenuItem("Open API Docs", open_docs),
pystray.MenuItem("Exit", on_exit)
)
update_status(icon, "Service is running", (170, 255, 0, 255)) # Normal green status
time.sleep(60) # Check every minute
# Update the update check status
status_checks["update_check"] = check_update()
evaluate_icon_status(icon)
time.sleep(60)

def pull_latest_release():
print("Pulling latest release...")
update_topos()

def update_icon(icon):
# Start a separate thread for checking updates
update_thread = threading.Thread(target=check_update_available, args=(icon,), daemon=True)
update_thread.start()

# Start a separate thread for checking health
health_thread = threading.Thread(target=check_health, args=(icon,), daemon=True)
health_thread.start()

def update_status(icon, text, color):
icon.icon = create_image(color)
# icon.notify(text)

def update_menu(icon):
"""Dynamically update the icon menu based on update status."""
if status_checks["update_check"]:
icon.menu = pystray.Menu(
pystray.MenuItem("Open API Docs", open_docs),
pystray.MenuItem("Update Topos", pull_latest_release),
pystray.MenuItem("Exit", on_exit)
)
else:
icon.menu = pystray.Menu(
pystray.MenuItem("Open API Docs", open_docs),
pystray.MenuItem("Exit", on_exit)
)

# def update_status(icon, text, color):
# icon.icon = create_image(color)
# icon.notify(text)


def open_docs():
webbrowser.open_new(DOCS_URL)
Expand Down Expand Up @@ -115,7 +155,7 @@ def create_tray_icon():
def on_setup(icon):
icon.visible = True
# Start health check in a separate thread
health_thread = threading.Thread(target=check_health, args=(icon,))
health_thread = threading.Thread(target=update_icon, args=(icon,))
health_thread.daemon = True
health_thread.start()

Expand Down
7 changes: 7 additions & 0 deletions topos/test/topos_updater/test_check_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ...utils.check_for_update import check_for_update, update_topos

import logging
logging.basicConfig(level=logging.DEBUG)

update_is_available = check_for_update("jonnyjohnson1", "topos-cli")
print(update_is_available)
5 changes: 2 additions & 3 deletions topos/utils/check_for_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


repo_owner = "jonnyjohnson1"
repo_name = "topos-cli"

Expand All @@ -16,7 +15,7 @@ def get_local_version_with_poetry():
"""
result = subprocess.run(["poetry", "version", "--short"], capture_output=True, text=True, check=True)
local_version = "v" + result.stdout.strip()
# logger.debug(f"Local version from Poetry: {local_version}")
logger.debug(f"Local version from Poetry: {local_version}")
return local_version

def get_latest_release_tag(repo_owner, repo_name):
Expand All @@ -28,7 +27,7 @@ def get_latest_release_tag(repo_owner, repo_name):
response = requests.get(url)
response.raise_for_status()
latest_release = response.json()["tag_name"]
# logger.debug(f"Latest release from GitHub: {latest_release}")
logger.debug(f"Latest release from GitHub: {latest_release}")
return latest_release
except requests.ConnectionError:
logger.debug("No internet connection. Cannot check for the latest release.")
Expand Down

0 comments on commit f1a4c4d

Please sign in to comment.