<commit_before>const router = require('express').Router();
const Guild = require('./models/guild-model.js');
const User = require('./models/user-model.js');

router.get('/', (req, res) => {

	Promise.all([Guild.count({isActive: true}), User.count({})])
	.then(stats => {	
		res.json({guilds: stats[0], users: stats[1]});
	})
	.catch(err => res.status(503).send('Database error'));
});

module.exports = router;<commit_msg>Improve API stats endpoint<commit_after>const router = require('express').Router();
const Guild = require('./models/guild-model.js');
const User = require('./models/user-model.js');

router.get('/', async (req, res) => {

	try{

		//Total Guild Stickers
		let guild_sticker_count = await Guild.aggregate([
			{$unwind: "$customStickers"},
			{$group: {_id: null, stickerNames: {$push: "$customStickers.name"}}},
			{$project: {_id: false, amount: {$size: "$stickerNames"}}}
		]);

		//Total User Stickers
		let user_sticker_count = await User.aggregate([
			{$unwind: "$customStickers"},
			{$group: {_id: null, stickerNames: {$push: "$customStickers.name"}}},
			{$project: {_id: false, amount: {$size: "$stickerNames"}}}
		]);

		//Convert queries to numbers
		guild_sticker_count = (guild_sticker_count.length > 0) ? guild_sticker_count[0].amount : 0;
		user_sticker_count = (user_sticker_count.length > 0) ? user_sticker_count[0].amount : 0;	

		return res.json({
			active_guilds: await Guild.count({isActive: true}),
			inactive_guilds: await Guild.count({isActive: false}),
			users: await User.count({}),
			stickers: guild_sticker_count + user_sticker_count
		});

	}catch(err){
		console.error("Error fetching stats: " + err.message);
		res.status(500).send('Internal server error');
	}

});

module.exports = router;
{'fraction_non_alphanumeric': 0.19210977701543738, 'fraction_numerical': 0.008004574042309892, 'mean_word_length': 4.932203389830509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5726786', 'n_tokens_mistral': 659, 'n_tokens_neox': 588, 'n_words': 123}