From 4350c7b7a9cdc209f272d0ffc61512a97da6e5c4 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 26 Nov 2024 15:59:02 -0500 Subject: [PATCH] storage: Add a little app to show something about feed sizes. --- apps/storage.json | 5 ++++ apps/storage/app.js | 72 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 apps/storage.json create mode 100644 apps/storage/app.js diff --git a/apps/storage.json b/apps/storage.json new file mode 100644 index 00000000..fda4f638 --- /dev/null +++ b/apps/storage.json @@ -0,0 +1,5 @@ +{ + "type": "tildefriends-app", + "emoji": "💾", + "previous": "&vC6kbwiCRkFMMrapQEQ+SprSbItL/+Tm9AC4eigk48I=.sha256" +} diff --git a/apps/storage/app.js b/apps/storage/app.js new file mode 100644 index 00000000..f5b47c89 --- /dev/null +++ b/apps/storage/app.js @@ -0,0 +1,72 @@ + +async function query(sql, args) { + let rows = []; + await ssb.sqlAsync(sql, args ?? [], function(row) { + rows.push(row); + }); + return rows;; +} + +async function get_biggest() { + return query(` + select author, sum(length(content)) as size from messages group by author order by size desc limit 10; + `); +} + +async function get_names(identities) { + return query(` + SELECT author, name FROM ( + SELECT + messages.author, + RANK() OVER (PARTITION BY messages.author ORDER BY messages.sequence DESC) AS author_rank, + messages.content ->> 'name' AS name + FROM messages + JOIN json_each(?) AS identities ON identities.value = messages.author + WHERE + json_extract(messages.content, '$.type') = 'about' AND + content ->> 'about' = messages.author AND name IS NOT NULL) + WHERE author_rank = 1 + `, [JSON.stringify(identities)]); +} + +function nice_size(bytes) { + let value = bytes; + let index = 0; + let units = ['B', 'kB', 'MB', 'GB']; + while (value > 1024 && index < units.length - 1) { + value /= 1024; + index++; + } + return `${Math.round(value * 10) / 10} ${units[index]}`; +} + +async function main() { + await app.setDocument('

Finding the top 10 largest feeds...

'); + let identities = await ssb.getAllIdentities(); + let following1 = await ssb.following(identities, 1); + let following2 = await ssb.following(identities, 2); + let biggest = await get_biggest(); + let names = await get_names(biggest.map(x => x.author)); + names = Object.fromEntries(names.map(x => [x.author, x.name])); + for (let item of biggest) { + item.name = names[item.author]; + item.following = + identities.indexOf(item.author) != -1 ? 0 : + following1[item.author] !== undefined ? 1 : + following2[item.author] !== undefined ? 2 : + undefined; + } + let html = `\n +

Top 10 Accounts by Size

+
    `; + for (let item of biggest) { + html += `
  1. + ${nice_size(item.size)} + ${item.name ?? item.author} +
  2. \n`; + } + html += '
\n'; + await app.setDocument(html); +} + +main().catch(function(e) { print(e); }); \ No newline at end of file