Skip to content

Commit 444646c

Browse files
committed
add json api for disk usage
1 parent bb39023 commit 444646c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

cephfs_disk_usage/server.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pathlib import Path
1111
import stat
1212
import time
13-
from typing import Self
13+
from typing import Self, Any
1414

1515
from tornado.web import RequestHandler, HTTPError
1616
from rest_tools.server import RestServer
@@ -70,6 +70,17 @@ async def get(self, path):
7070
self.render('details.html', path=path, data=data)
7171

7272

73+
class API(BaseHandler):
74+
async def get(self, path):
75+
for fs in self.filesystems:
76+
if path.startswith(fs):
77+
data = await self.filesystems[fs].dir_entry(path[len(fs):])
78+
break
79+
else:
80+
raise HTTPError(400, reason='bad path')
81+
self.write(data.to_dict())
82+
83+
7384
async def call(*args, shell=False):
7485
if shell:
7586
ret = await asyncio.create_subprocess_shell(' '.join(args), stdout=asyncio.subprocess.PIPE)
@@ -108,6 +119,11 @@ def from_entry(cls, e: Entry) -> Self:
108119
raise Exception('is not a directory!')
109120
return cls(e.name, e.path, e.size, e.nfiles, [])
110121

122+
def to_dict(self) -> dict[str, Any]:
123+
ret = vars(self)
124+
ret['children'] = [vars(c) for c in self.children]
125+
return ret
126+
111127
def child_summary(self, threshold: float = .5) -> list[Entry]:
112128
"""
113129
Summarize children
@@ -256,6 +272,7 @@ def __init__(self, s3_override=None):
256272
server.add_route(cwd, Main, kwargs)
257273
else:
258274
server.add_route('/data', Main, kwargs)
275+
server.add_route(r'/api(.*)', API, kwargs)
259276
server.add_route('/healthz', Health, kwargs)
260277
server.add_route(r'(.*)', Details, kwargs)
261278

0 commit comments

Comments
 (0)