Compare commits

..

No commits in common. "2978e0c9681ab1f3088cc8f37ce644f5c6d82634" and "0be72827db960201caefde4e103e03faf196c055" have entirely different histories.

3 changed files with 33 additions and 36 deletions

View File

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

View File

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

View File

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