Compare commits

...

2 Commits

3 changed files with 36 additions and 33 deletions

View File

@ -107,8 +107,8 @@ async def watch(req, ws):
with watching.state.lock: with watching.state.lock:
q = watching.pubsub[uuid] = asyncio.Queue() q = watching.pubsub[uuid] = asyncio.Queue()
# Init with disk usage and full tree # Init with disk usage and full tree
await ws.send(watching.format_du(watching.state.space)) await ws.send(watching.format_space(watching.state.space))
await ws.send(watching.format_tree(watching.state.root)) await ws.send(watching.format_root(watching.state.root))
# Send updates # Send updates
while True: while True:
await ws.send(await q.get()) await ws.send(await q.get())

View File

@ -86,7 +86,6 @@ class State:
def __getitem__(self, index: PurePosixPath | tuple[PurePosixPath, int]): def __getitem__(self, index: PurePosixPath | tuple[PurePosixPath, int]):
with self.lock: with self.lock:
print(self._slice(index))
return self._listing[self._slice(index)] return self._listing[self._slice(index)]
def __setitem__( def __setitem__(
@ -153,8 +152,7 @@ def watcher_thread(loop):
if old != new: if old != new:
with state.lock: with state.lock:
state.root = new state.root = new
msg = format_tree(new) broadcast(format_root(new), loop)
asyncio.run_coroutine_threadsafe(broadcast(msg), loop).result()
# The watching is not entirely reliable, so do a full refresh every minute # The watching is not entirely reliable, so do a full refresh every minute
refreshdl = time.monotonic() + 60.0 refreshdl = time.monotonic() + 60.0
@ -167,9 +165,7 @@ def watcher_thread(loop):
space = Space(*du, storage=state.root[0].size) space = Space(*du, storage=state.root[0].size)
if space != state.space: if space != state.space:
state.space = space state.space = space
asyncio.run_coroutine_threadsafe( broadcast(format_space(space), loop)
broadcast(format_du(space)), loop
).result()
break break
# Do a full refresh? # Do a full refresh?
if time.monotonic() > refreshdl: if time.monotonic() > refreshdl:
@ -197,27 +193,20 @@ def watcher_thread_poll(loop):
old = state.root old = state.root
new = walk() new = walk()
if old != new: if old != new:
with state.lock:
state.root = new state.root = new
asyncio.run_coroutine_threadsafe(broadcast(format_tree(new)), loop).result() broadcast(format_update(old, new), loop)
# Disk usage update # Disk usage update
du = shutil.disk_usage(rootpath) du = shutil.disk_usage(rootpath)
space = Space(*du, storage=state.root[0].size) space = Space(*du, storage=state.root[0].size)
if space != state.space: if space != state.space:
state.space = space state.space = space
asyncio.run_coroutine_threadsafe(broadcast(format_du(space)), loop).result() broadcast(format_space(space), loop)
time.sleep(2.0) time.sleep(2.0)
def format_du(usage):
return msgspec.json.encode({"space": usage}).decode()
def format_tree(root):
return msgspec.json.encode({"root": root}).decode()
def walk(rel=PurePosixPath()) -> list[FileEntry]: # noqa: B008 def walk(rel=PurePosixPath()) -> list[FileEntry]: # noqa: B008
path = rootpath / rel path = rootpath / rel
try: try:
@ -265,6 +254,7 @@ def update(relpath: PurePosixPath, loop):
if rootpath is None or relpath is None: if rootpath is None or relpath is None:
print("ERROR", rootpath, relpath) print("ERROR", rootpath, relpath)
new = walk(relpath) new = walk(relpath)
with state.lock:
old = state[relpath] old = state[relpath]
if old == new: if old == new:
return return
@ -273,9 +263,7 @@ def update(relpath: PurePosixPath, loop):
state[relpath, new[0].isfile] = new state[relpath, new[0].isfile] = new
else: else:
del state[relpath] del state[relpath]
# FIXME: broadcast format_update() broadcast(format_update(old, state.root), loop)
msg = format_update(old, state.root)
asyncio.run_coroutine_threadsafe(broadcast(msg), loop).result()
def format_update(old, new): def format_update(old, new):
@ -322,7 +310,19 @@ def format_update(old, new):
return msgspec.json.encode({"update": update}).decode() return msgspec.json.encode({"update": update}).decode()
async def broadcast(msg): def format_space(usage):
return msgspec.json.encode({"space": usage}).decode()
def format_root(root):
return msgspec.json.encode({"root": root}).decode()
def broadcast(msg, loop):
return asyncio.run_coroutine_threadsafe(abroadcast(msg), loop).result()
async def abroadcast(msg):
try: try:
for queue in pubsub.values(): for queue in pubsub.values():
queue.put_nowait(msg) queue.put_nowait(msg)

View File

@ -4,6 +4,7 @@ import { defineStore } from 'pinia'
import { collator } from '@/utils' import { collator } from '@/utils'
import { logoutUser } from '@/repositories/User' import { logoutUser } from '@/repositories/User'
import { watchConnect } from '@/repositories/WS' import { watchConnect } from '@/repositories/WS'
import { format } from 'path'
type FileData = { id: string; mtime: number; size: number; dir: DirectoryData } type FileData = { id: string; mtime: number; size: number; dir: DirectoryData }
type DirectoryData = { type DirectoryData = {
@ -39,11 +40,10 @@ export const useDocumentStore = defineStore({
const docs = [] const docs = []
let loc = [] as string[] let loc = [] as string[]
for (const [level, name, key, mtime, size, isfile] of root) { for (const [level, name, key, mtime, size, isfile] of root) {
if (level === 0) continue
loc = loc.slice(0, level - 1) loc = loc.slice(0, level - 1)
docs.push({ docs.push({
name, name,
loc: loc.join('/'), loc: level ? loc.join('/') : '/',
key, key,
size, size,
sizedisp: formatSize(size), sizedisp: formatSize(size),
@ -57,6 +57,9 @@ export const useDocumentStore = defineStore({
console.log("Documents", docs) console.log("Documents", docs)
this.document = docs as Document[] this.document = docs as Document[]
}, },
updateModified() {
for (const doc of this.document) doc.modified = formatUnixDate(doc.mtime)
},
login(username: string, privileged: boolean) { login(username: string, privileged: boolean) {
this.user.username = username this.user.username = username
this.user.privileged = privileged this.user.privileged = privileged