Skip to content

Commit

Permalink
Rework status api and drop cache stats since they were per process
Browse files Browse the repository at this point in the history
The issue with cache htis/misses was that they were collected per
process which made them useless when using gunicorn.

Signed-off-by: Alexis Jeandet <[email protected]>
  • Loading branch information
jeandet committed Jan 16, 2024
1 parent c34f20f commit 75773a7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion speasy_proxy/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
from .get_inventory import *
from .get_speasy_version import *
from .get_version import *
from .get_statistics import *
from .get_server_status import *
from .routes import router as api_router
13 changes: 13 additions & 0 deletions speasy_proxy/api/v1/get_server_status.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 status

log = logging.getLogger(__name__)


@router.get('/get_server_status', description='Get server status', response_class=JSONResponse)
async def get_server_status():
log.debug(f'Client asking for server status')
return JSONResponse(content=jsonable_encoder(status()))
13 changes: 0 additions & 13 deletions speasy_proxy/api/v1/get_statistics.py

This file was deleted.

5 changes: 1 addition & 4 deletions speasy_proxy/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
import speasy as spz


def statistics():
def status():
"""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),
Expand Down
5 changes: 2 additions & 3 deletions speasy_proxy/static/status.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
async function update_status() {
const current_url = window.location.href;
$.ajax({
url: current_url+'get_statistics',
url: current_url + 'get_server_status',
type: 'GET',
dataType: 'json',
success: function (data) {
console.log(data);
var status_container = $('body').find("ul#status-container.container");
status_container.find("li.version").html('version: ' + data.version);
status_container.find("li.speasy-version").html('Speasy version: ' + data.speasy_version);
status_container.find("li.up-since").html('up since: ' + data.up_since);
status_container.find("li.cache-entries").html('Entries in cache: ' + numeral(data.entries).format('0 a'));
status_container.find("li.uptime").html('up for: ' + numeral(data.up_duration).format('0,0') + ' seconds');
status_container.find("li.cache-hits").html('cache hits: ' + data.cache_hits);
status_container.find("li.cache-misses").html('cache misses: ' + data.cache_misses);
status_container.find("li.cache-size").html('cache size: ' + numeral(data.cache_disk_size).format('0.00b'));
}
});
Expand Down
3 changes: 1 addition & 2 deletions speasy_proxy/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ <h2>Cache Status</h2>
<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="speasy-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>
Expand Down

0 comments on commit 75773a7

Please sign in to comment.