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 @@
{{ favo.item_id }} | {{ favo.favs }} |