From c488f5d5e068dd4741157f0962b67e77a855f4cb Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 9 Feb 2023 15:16:22 +0200 Subject: [PATCH] feat: store message size --- crud.py | 7 ++++--- migrations.py | 3 ++- models.py | 5 +++++ static/components/relay-details/relay-details.html | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crud.py b/crud.py index 5f2cc04..f67dd95 100644 --- a/crud.py +++ b/crud.py @@ -82,11 +82,12 @@ async def create_event(relay_id: str, e: NostrEvent): created_at, kind, content, - sig + sig, + size ) - VALUES (?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) """, - (relay_id, e.id, e.pubkey, e.created_at, e.kind, e.content, e.sig), + (relay_id, e.id, e.pubkey, e.created_at, e.kind, e.content, e.sig, e.size_bytes), ) # todo: optimize with bulk insert diff --git a/migrations.py b/migrations.py index bcf9074..e4837e8 100644 --- a/migrations.py +++ b/migrations.py @@ -28,7 +28,8 @@ async def m001_initial(db): created_at {db.big_int} NOT NULL, kind INT NOT NULL, content TEXT NOT NULL, - sig TEXT NOT NULL + sig TEXT NOT NULL, + size {db.big_int} DEFAULT 0 ); """ ) diff --git a/models.py b/models.py index 33f82fe..ce6074f 100644 --- a/models.py +++ b/models.py @@ -116,6 +116,11 @@ class NostrEvent(BaseModel): id = hashlib.sha256(data.encode()).hexdigest() return id + @property + def size_bytes(self) -> int: + s = json.dumps(dict(self), separators=(",", ":"), ensure_ascii=False) + return len(s.encode()) + def is_replaceable_event(self) -> bool: return self.kind in [0, 3] diff --git a/static/components/relay-details/relay-details.html b/static/components/relay-details/relay-details.html index a41107b..56d3efc 100644 --- a/static/components/relay-details/relay-details.html +++ b/static/components/relay-details/relay-details.html @@ -62,7 +62,7 @@
-
Free storage:
+
Free Storage: