add basic stats page

This commit is contained in:
Chris W 2023-10-21 10:07:20 -06:00
parent 366ec34956
commit dc5cfcee3b
5 changed files with 132 additions and 0 deletions

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
.env
.svelte-kit
build/

54
src/lib/db/index.ts Normal file
View File

@ -0,0 +1,54 @@
import { MongoClient } from 'mongodb';
import { env } from '$env/dynamic/private';
import type { Db, Collection } from 'mongodb';
import type PasteSchema from "./paste-schema";
import { get } from 'http';
// let client: MongoClient;
// let db: Db;
// let pastes: Collection<PasteSchema>;
// const connect = async (url: string) => {
// client = new MongoClient(url);
// await client.connect();
// const db = client.db("paste69");
// pastes = db.collection<PasteSchema>("pastes");
// }
// export {
// db,
// pastes,
// connect,
// }
interface Collections {
pastes: Collection<PasteSchema>;
}
export class Mongo {
private static instance: Promise<MongoClient> | null = null;
private constructor() {}
public static getClient(): Promise<MongoClient> {
if (!Mongo.instance) {
Mongo.instance = MongoClient.connect(env.DB_URL);
}
return Mongo.instance;
}
public static async getDb(): Promise<Db> {
const client = await Mongo.getClient();
return client.db("paste69");
}
public static async getCollection<T extends PasteSchema>(name: string): Promise<Collection<T>> {
const db = await Mongo.getDb();
return db.collection<T>(name);
}
public static async getNamedCollection<name extends keyof Collections>(name: name): Promise<Collections[name]> {
return Mongo.getCollection(name) as Promise<Collections[name]>;
}
}

View File

@ -0,0 +1,8 @@
export default interface PasteSchema {
id: string;
contents: string;
highlight: string;
encrypted: boolean;
burnAfterReading: boolean;
createdAt: Date;
}

View File

@ -0,0 +1,28 @@
import { Mongo } from "$lib/db/index";
import type { PageServerLoad } from "./$types";
import { env } from '$env/dynamic/private';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async () => {
// Check if the stats page is enabled
if (!env.STATS_ENABLED) {
throw error(404, 'Paste not found');
}
const pastes = await Mongo.getNamedCollection('pastes');
// Total pastes
const totalPastes = await pastes.countDocuments();
// Total encrypted pastes
const totalEncryptedPastes = await pastes.countDocuments({ encrypted: true });
// Total pastes with burnAfterReading enabled (and not yet read)
const totalBurnAfterReadingPastes = await pastes.countDocuments({ burnAfterReading: true });
return {
totalPastes,
totalEncryptedPastes,
totalBurnAfterReadingPastes,
};
}

View File

@ -0,0 +1,39 @@
<script lang="ts">
import type { PageData } from './$types';
import ToolBox from "$lib/components/ToolBox.svelte";
import { ChevronRight } from "svelte-tabler";
export let data: PageData;
const { totalPastes, totalEncryptedPastes, totalBurnAfterReadingPastes } = data;
</script>
<svelte:head>
<title>Paste69 - Server stats</title>
</svelte:head>
<div class="absolute top-[23px] left-[5px]">
<ChevronRight />
</div>
<div class="pl-12 pt-4 pb-24 max-w-[100ch]">
<h1 class="h1 mb-8">Server Stats</h1>
<p class="mb-4">
<span class="font-bold">Total pastes:</span> {totalPastes}
</p>
<p class="mb-4">
<span class="font-bold">Total encrypted pastes:</span> {totalEncryptedPastes}
</p>
<p class="mb-4">
<span class="font-bold">Total burn after reading pastes (unread):</span> {totalBurnAfterReadingPastes}
</p>
</div>
<div class="fixed bottom-0 right-0 w-full md:w-auto">
<ToolBox
disableSave={true}
disableCopy={true}
/>
</div>