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

show a new version link when it is available #34

Merged
merged 2 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 33 additions & 3 deletions piku_dashboard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import sys
import os
from flask import Flask, request, render_template, flash, redirect, url_for
from functools import wraps
from functools import wraps, lru_cache
from datetime import datetime, timedelta
import requests

from piku_dashboard.host_client import info
from piku_dashboard import piku_client
Expand All @@ -21,6 +23,35 @@

logger.info(f"Detected self id as: {self_app}")

def timed_lru_cache(seconds: int, maxsize: int = 16):
def wrapper_cache(func):
func = lru_cache(maxsize=maxsize)(func)
func.lifetime = timedelta(seconds=seconds)
func.expiration = datetime.utcnow() + func.lifetime

@wraps(func)
def wrapped_func(*args, **kwargs):
if datetime.utcnow() >= func.expiration:
func.cache_clear()
func.expiration = datetime.utcnow() + func.lifetime

return func(*args, **kwargs)

return wrapped_func

return wrapper_cache

@timed_lru_cache(60*60*24, 1)
def get_github_sha():
try:
response = requests.get("https://api.github.com/repos/rwnx/piku-dashboard/commits")
history = response.json()

sha = history[0]['sha'][:6]
return sha
except:
return "invalid_sha"

def is_self(appid):
return appid == self_app

Expand All @@ -40,7 +71,6 @@ def wrapped_view(**kwargs):

return wrapped_view


@app.route("/")
@login_required
def home():
Expand All @@ -51,7 +81,7 @@ def home():
flash(f"Could Not fetch app list: {e}", "error")
apps = []

return render_template("home.html", apps=apps, host_info=host_info, self_app=self_app)
return render_template("home.html", apps=apps, host_info=host_info, self_app=self_app, github_sha=get_github_sha())
Corvimia marked this conversation as resolved.
Show resolved Hide resolved


@app.route("/apps/<appid>/config", methods=["GET"])
Expand Down
4 changes: 2 additions & 2 deletions piku_dashboard/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3>Uptime</h3>
<aside>
<h2>{{app.id}} <sup class="applabel-{{ "active" if app.active else "inactive" }}">{{ 'Active' if app.active else 'Inactive' }}</sup></h2>
<ul class="app-meta">
<li>{{app.ref}}</li>
<li>{{app.ref}}{% if github_sha != app.ref and app.id == self_app %}<a href="https://github.com/rwnx/piku-dashboard" target="_blank">New Version</a>{% endif %}</li>
</ul>
<ul>
<li><a href="{{url_for('app_logs', appid=app.id)}}">📖 View Logs</a></li>
Expand All @@ -37,4 +37,4 @@ <h2>{{app.id}} <sup class="applabel-{{ "active" if app.active else "inactive" }}
</aside>
{% endfor %}
</section>
{% endblock %}
{% endblock %}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
Werkzeug==2.0.1
requests==2.28.1