Add Temporary statistics endpoint

This commit is contained in:
HDwayne
2024-02-03 22:06:13 +01:00
parent 679a658463
commit a56b04f04b
2 changed files with 44 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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;