From a56b04f04b990effb89b517b128cce67a6000958 Mon Sep 17 00:00:00 2001 From: HDwayne Date: Sat, 3 Feb 2024 22:06:13 +0100 Subject: [PATCH] Add Temporary statistics endpoint --- src/node/addon/src/lib/repository.js | 29 ++++++++++++++++++++++++++++ src/node/addon/src/serverless.js | 15 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/node/addon/src/lib/repository.js b/src/node/addon/src/lib/repository.js index acce491..1c26864 100644 --- a/src/node/addon/src/lib/repository.js +++ b/src/node/addon/src/lib/repository.js @@ -130,3 +130,32 @@ export function getKitsuIdSeriesEntries(kitsuId, episode) { ] }); } + + +export async function computeTorrentStatistics() { + try { + // Global Stats + const totalTorrents = await Torrent.count(); + + // Stats by Provider + const torrentsByProvider = await Torrent.findAll({ + attributes: ['provider', [Sequelize.fn('COUNT', Sequelize.col('provider')), 'count']], + group: ['provider'] + }); + + // Stats by Content Type (Movie, Series, etc.) + const torrentsByType = await Torrent.findAll({ + attributes: ['type', [Sequelize.fn('COUNT', Sequelize.col('type')), 'count']], + group: ['type'] + }); + + return { + totalTorrents, + torrentsByProvider, + torrentsByType, + }; + } catch (error) { + console.error('Error computing statistics:', error); + throw error; + } +} diff --git a/src/node/addon/src/serverless.js b/src/node/addon/src/serverless.js index 1cf275b..177ac6e 100644 --- a/src/node/addon/src/serverless.js +++ b/src/node/addon/src/serverless.js @@ -9,6 +9,7 @@ import { parseConfiguration } from './lib/configuration.js'; import landingTemplate from './lib/landingTemplate.js'; import { manifest } from './lib/manifest.js'; import * as moch from './moch/moch.js'; +import { computeTorrentStatistics } from './lib/repository.js'; const router = new Router(); const limiter = rateLimit({ @@ -99,6 +100,20 @@ router.get('/:moch/:apiKey/:infoHash/:cachedEntryInfo/:fileIndex/:filename?', (r }); }); +/** + * Temporary solution to provide statistics (NOT FOR PRODUCTION USE) + * easy way to know how many torrents are stored in the PG database + * These data should not be exposed to the public + */ +router.get('/statistics', async (req, res) => { + try { + const stats = await computeTorrentStatistics(); + res.json(stats); + } catch (error) { + res.status(500).json({ error: 'Failed to compute statistics' }); + } +}); + export default function (req, res) { router(req, res, function () { res.statusCode = 404;