From e2f15ce86231f82ea34df6bf0d84508e86026b40 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 31 May 2023 01:08:23 +0200 Subject: [PATCH] ranking schmanking --- src/inc/routes/index.mjs | 41 --------------------------- src/inc/routes/ranking.mjs | 57 ++++++++++++++++++++++++++++++++++++++ views/ranking.html | 7 +++++ 3 files changed, 64 insertions(+), 41 deletions(-) create mode 100644 src/inc/routes/ranking.mjs diff --git a/src/inc/routes/index.mjs b/src/inc/routes/index.mjs index 5e5a6cc..39e00d9 100644 --- a/src/inc/routes/index.mjs +++ b/src/inc/routes/index.mjs @@ -118,46 +118,5 @@ export default (router, tpl) => { res.redirect(`/${referer}`); }); - router.get(/^\/ranking$/, async (req, res) => { - try { - const list = await db` - select - "user".user, - coalesce("user_options".avatar, ${await lib.getDefaultAvatar()}) as avatar, - count(distinct(tag_id, item_id)) as count - from "tags_assign" - left join "user" on "user".id = "tags_assign".user_id - left join "user_options" on "user_options".user_id = "user".id - group by "user".user, "user_options".avatar - order by count desc - `; - const stats = await lib.countf0cks(); - - const hoster = await db` - with t as ( - select - split_part(substring(src, position('//' in src)+2), '/', 1) part - from items - ) - select t.part, count(t.part) as c - from t - group by t.part - order by c desc - limit 20 - `; - - res.reply({ - body: tpl.render('ranking', { - list, - stats, - hoster, - tmp: null - }, req) - }); - } catch(err) { - res.end(JSON.stringify(err.message)); - } - }); - return router; }; diff --git a/src/inc/routes/ranking.mjs b/src/inc/routes/ranking.mjs new file mode 100644 index 0000000..f741bae --- /dev/null +++ b/src/inc/routes/ranking.mjs @@ -0,0 +1,57 @@ +import db from "../../inc/sql.mjs"; +import lib from "../lib.mjs"; + +export default (router, tpl) => { + router.get(/^\/ranking$/, async (req, res) => { + try { + const list = await db` + select + "user".user, + coalesce("user_options".avatar, ${await lib.getDefaultAvatar()}) as avatar, + count(distinct(tag_id, item_id)) as count + from "tags_assign" + left join "user" on "user".id = "tags_assign".user_id + left join "user_options" on "user_options".user_id = "user".id + group by "user".user, "user_options".avatar + order by count desc + `; + const stats = await lib.countf0cks(); + + const hoster = await db` + with t as ( + select + split_part(substring(src, position('//' in src)+2), '/', 1) part + from items + ) + select t.part, count(t.part) as c + from t + group by t.part + order by c desc + limit 20 + `; + + const favotop = await db` + select item_id, count(*) favs + from favorites + group by item_id + having count(*) > 1 + order by favs desc + limit 10 + `; + + res.reply({ + body: tpl.render('ranking', { + list, + stats, + hoster, + favotop, + tmp: null + }, req) + }); + } catch(err) { + res.end(JSON.stringify(err.message)); + } + }); + + return router; +}; diff --git a/views/ranking.html b/views/ranking.html index 098180b..c8ac9f9 100644 --- a/views/ranking.html +++ b/views/ranking.html @@ -38,6 +38,13 @@ deleted{{ stats.deleted }} missing ids{{ stats.untracked }} + +

Top f0cks

+ + @each(favotop as favo) + + @endeach +
{{ favo.item_id }}{{ favo.favs }}