better stats view
This commit is contained in:
parent
1cc00aba84
commit
1380bba5ce
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
from datetime import datetime
|
||||
import docker, requests
|
||||
|
||||
with open("config.json", "r") as in_file:
|
||||
|
@ -6,6 +7,7 @@ with open("config.json", "r") as in_file:
|
|||
services = sorted_dict = dict(sorted(services.items()))
|
||||
|
||||
docker_client = docker.from_env()
|
||||
last_updated = None
|
||||
|
||||
def healthcheck(container, status):
|
||||
container_name = container["name"]
|
||||
|
@ -26,6 +28,8 @@ def healthcheck(container, status):
|
|||
return "unhealthy"
|
||||
|
||||
def update_status():
|
||||
global last_updated
|
||||
last_updated = datetime.now()
|
||||
containers = docker_client.containers.list(all=True)
|
||||
status = { container.name: container.status for container in containers }
|
||||
|
||||
|
@ -36,6 +40,26 @@ def update_status():
|
|||
container["status"] = healthcheck(container, status)
|
||||
return services
|
||||
|
||||
def increment_counter(counter_dict, key):
|
||||
try:
|
||||
counter_dict[key] += 1
|
||||
except KeyError:
|
||||
counter_dict[key] = 1
|
||||
|
||||
def get_stats():
|
||||
if last_updated is None:
|
||||
update_status()
|
||||
tracked = 0
|
||||
counts = {}
|
||||
for k, v in services.items():
|
||||
for container in v["containers"]:
|
||||
tracked += 1
|
||||
increment_counter(counts, container["status"])
|
||||
return {
|
||||
"counts": counts,
|
||||
"total": tracked,
|
||||
"last_updated": last_updated.strftime("%Y-%m-%dT%H:%M:%SZ%z"),
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(update_status())
|
||||
print(services)
|
||||
|
|
|
@ -2,9 +2,7 @@ import json
|
|||
from datetime import datetime
|
||||
from flask import request, redirect, render_template
|
||||
from docker_status import app
|
||||
from docker_status.my_docker import update_status
|
||||
|
||||
last_updated = None
|
||||
from docker_status.my_docker import update_status, get_stats
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def route_index():
|
||||
|
@ -17,11 +15,9 @@ def route_index():
|
|||
service["names"] = ", ".join([ x["name"] for x in containers ])
|
||||
if request.method == 'GET':
|
||||
return render_template("index.html", services=services)
|
||||
return json.dumps(services)
|
||||
return services
|
||||
|
||||
@app.route('/stats')
|
||||
def route_stats():
|
||||
return json.dumps({
|
||||
"last_updated": last_updated.strftime("%Y-%m-%dT%H:%M:%SZ%z") if last_updated is not None else None,
|
||||
})
|
||||
return get_stats()
|
||||
|
Loading…
Reference in New Issue