Skip to content

Commit

Permalink
fixing dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
SelfhostedPro committed May 27, 2021
1 parent 5421d72 commit baf2440
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 779 deletions.
14 changes: 14 additions & 0 deletions backend/api/actions/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import stat
from fastapi import HTTPException
from fastapi.responses import StreamingResponse

Expand Down Expand Up @@ -30,6 +31,7 @@
import docker
import aiodocker
import asyncio
import aiostream


"""
Expand Down Expand Up @@ -514,6 +516,18 @@ async def stat_generator(request, app_name):
# so there's no point in checking more often than that
await asyncio.sleep(1)

async def all_stat_generator(request):
async with aiodocker.Docker() as docker:
containers = []
_containers = await docker.containers.list()
for _app in _containers:
if _app._container["State"] == "running":
containers.append(_app)
loops = [stat_generator(request, app._container["Names"][0][1:]) for app in containers]
async with aiostream.stream.merge(*loops).stream() as merged:
async for event in merged:
yield event


async def process_app_stats(line, app_name):
cpu_total = 0.0
Expand Down
8 changes: 6 additions & 2 deletions backend/api/routers/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def update_container(app_name, Authorize: AuthJWT = Depends()):
auth_check(Authorize)
return actions.app_update(app_name)

@router.get("/stats")
async def all_sse_stats(request: Request, Authorize: AuthJWT = Depends()):
auth_check(Authorize)
stat_generator = actions.all_stat_generator(request)
return EventSourceResponse(stat_generator)

@router.get("/{app_name}")
def get_container_details(app_name, Authorize: AuthJWT = Depends()):
Expand Down Expand Up @@ -65,7 +70,6 @@ def deploy_app(template: schemas.DeployForm, Authorize: AuthJWT = Depends()):
auth_check(Authorize)
return actions.deploy_app(template=template)


@router.get("/{app_name}/logs")
async def logs(app_name: str, request: Request, Authorize: AuthJWT = Depends()):
auth_check(Authorize)
Expand All @@ -77,4 +81,4 @@ async def logs(app_name: str, request: Request, Authorize: AuthJWT = Depends()):
async def sse_stats(app_name: str, request: Request, Authorize: AuthJWT = Depends()):
auth_check(Authorize)
stat_generator = actions.stat_generator(request, app_name)
return EventSourceResponse(stat_generator)
return EventSourceResponse(stat_generator)
14 changes: 5 additions & 9 deletions frontend/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ export default {
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
},
readAppStats(appName) {
if (!(appName in this.statConnection)) {
this.statConnection[appName] = new EventSource(
`/api/apps/${appName}/stats`
readAppStats() {
this.statConnection = new EventSource(
`/api/apps/stats`
);
this.statConnection[appName].addEventListener("update", event => {
this.statConnection.addEventListener("update", event => {
let statsGroup = JSON.parse(event.data);
if (!(statsGroup.name in this.stats)) {
this.stats[statsGroup.name] = {};
Expand All @@ -114,7 +113,6 @@ export default {
);
this.$forceUpdate();
});
}
},
refresh() {
this.closeStats();
Expand All @@ -139,9 +137,7 @@ export default {
this.$router.push({ path: `/apps/${appName}/info` });
},
closeStats() {
for (let connection in this.statConnection) {
this.statConnection[connection].close();
}
this.statConnection.close();
this.stats = {};
},
fillStats(app) {
Expand Down
Loading

0 comments on commit baf2440

Please sign in to comment.