Skip to content

Commit

Permalink
Merge pull request #34 from yorublaireau/add_version_link
Browse files Browse the repository at this point in the history
show a new version link when it is available
  • Loading branch information
rwnx committed Oct 8, 2022
2 parents 49210b0 + 30867ce commit f63c10d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
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())


@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

0 comments on commit f63c10d

Please sign in to comment.