Skip to content

Commit

Permalink
Reworked home page and added server statistics api resource
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Jeandet <[email protected]>
  • Loading branch information
jeandet committed Jan 12, 2024
1 parent 97c7233 commit eebe18d
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 194 deletions.
1 change: 1 addition & 0 deletions speasy_proxy/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
from .get_inventory import *
from .get_speasy_version import *
from .get_version import *
from .get_statistics import *
from .routes import router as api_router
13 changes: 13 additions & 0 deletions speasy_proxy/api/v1/get_statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from fastapi.responses import JSONResponse
from .routes import router
from fastapi.encoders import jsonable_encoder
import logging
from speasy_proxy.backend import statistics

log = logging.getLogger(__name__)


@router.get('/get_statistics', description='Get cache statistics', response_class=JSONResponse)
async def get_statistics():
log.debug(f'Client asking for statistics')
return JSONResponse(content=jsonable_encoder(statistics()))
32 changes: 32 additions & 0 deletions speasy_proxy/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from speasy.core.cache import _cache
from speasy_proxy.index import up_since
from speasy_proxy import __version__
from speasy_proxy.backend.inventory_updater import ensure_update_inventory, last_update
from datetime import datetime, UTC
import speasy as spz


def statistics():
"""Return statistics about the backend."""
_up_since = up_since.value()
up_time = datetime.now(UTC) - _up_since

with _cache.transact():
cache_stats = _cache.stats()
cache_len = len(_cache)
cache_disk = _cache.disk_size()
return {
'entries': cache_len,
'cache_disk_size': cache_disk,
'up_since': _up_since.isoformat(),
'up_duration': up_time.total_seconds(),
'cache_hits': cache_stats['hit'],
'cache_misses': cache_stats['misses'],
'inventory_update': last_update.value().isoformat(),
'inventory_size': str(
sum(map(lambda p: len(p.parameters),
set(spz.inventories.flat_inventories.__dict__.values())))),
'docs': 'https://speasyproxy.readthedocs.io/en/latest/',
'speasy_version': spz.__version__,
'version': __version__
}
36 changes: 2 additions & 34 deletions speasy_proxy/frontend/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,10 @@

log = logging.getLogger(__name__)

_inventory_refresh_thread = None


def _refresh_inventory():
global _inventory_refresh_thread
if _inventory_refresh_thread is None or not _inventory_refresh_thread.is_alive():
_inventory_refresh_thread = Thread(target=ensure_update_inventory)
_inventory_refresh_thread.start()


templates = Jinja2Templates(directory=f"{os.path.dirname(os.path.abspath(__file__))}/../templates")
index_html = open(f'{os.path.dirname(os.path.abspath(__file__))}/../static/index.html').read()


@router.get('/', response_class=HTMLResponse)
def home(request: Request, user_agent: Annotated[str | None, Header()] = None):
log.debug(f'Client asking for home page from {user_agent}')
_up_since = up_since.value()
up_time = datetime.now(UTC) - _up_since

with _cache.transact():
cache_stats = _cache.stats()
cache_len = len(_cache)
cache_disk = _cache.disk_size()
_refresh_inventory()
return templates.TemplateResponse("welcome.html",
{"request": request,
'entries': cache_len,
'cache_disk_size': filesize.naturalsize(
cache_disk),
'up_date': time.naturaldate(_up_since),
'up_duration': time.naturaldelta(up_time),
'cache_hits': str(cache_stats['hit']),
'cache_misses': str(cache_stats['misses']),
'inventory_update': str(last_update.value().isoformat()),
'inventory_size': str(
sum(map(lambda p: len(p.parameters),
set(inventories.flat_inventories.__dict__.values())))),
'docs': urljoin(str(request.base_url), "docs"),
})
return HTMLResponse(content=index_html, status_code=200)
65 changes: 65 additions & 0 deletions speasy_proxy/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Speasy Cache Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="static/theme.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
<script src="static/status.js"></script>
<script>
$(document).ready(function () {
update_status();
setInterval(update_status, 10000);
});
</script>
</head>
<body>
<header>
<div class="container" style="display: flex; align-items: center; justify-content: center;">
<div class="logo">
<img src="static/logo_speasy.svg" alt="Speasy logo" height="80px">
</div>
<div class="title">
<h1>Speasy Cache Server</h1>
</div>
</div>
</header>

<div class="container" style="height: 90%; text-align: center; justify-content: center;">
<section class="container" style="height: 100%">
<p> This the shared cache server for the Speasy project. It allows to share Speasy requests results between
users across the world.
</p>
<p>You can access and experiment with the API <a href="/docs">here</a>.</p>
<p>The source code is available on <a href="https://github.com/SciQLop/speasy_proxy">GitHub</a>.
</p>
</section>

<div class="container">
<h2>Cache Status</h2>
<div class="container" style="text-align: center;">
<ul class="container" id="status-container" style="height: 100%; text-align: left; display: inline-block;">
<li class="last-updated">Last updated: 2024-01-11 18:31 CET</li>
<li class="version"></li>
<li class="up-since"></li>
<li class="cache-size"></li>
<li class="cache-entries"></li>
<li class="cache-hits"></li>
<li class="cache-misses"></li>
<li class="uptime"></li>
</ul>
</div>
</div>
</div>

<div class="container" style="position: fixed; bottom: 2.5rem; right:0; width: 40%; height: 2.5rem;">
<img src="static/logo_LPP.png" alt="Speasy logo" height="80px">
<img src="static/logo_CDPP.jpg" alt="Speasy logo" height="80px">
</div>
<footer style="position: absolute; bottom: 0; width: 40%; height: 2.5rem">
Copyright &copy; 2024 Laboratory of Plasma Physics
</footer>
</body>
</html>
Binary file added speasy_proxy/static/logo_CDPP.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit eebe18d

Please sign in to comment.