Added JSDoc comments.

This commit is contained in:
BurnyLlama 2021-10-06 20:36:20 +02:00
parent 07dc061c3c
commit 0058b6eed2
13 changed files with 71 additions and 4 deletions

View File

@ -15,4 +15,8 @@ api.get(
api.use('/players', playerApi) api.use('/players', playerApi)
api.use('/maps', mapApi) api.use('/maps', mapApi)
/**
* This module is the entrypoint for the API.
* @module api/api
*/
export default api export default api

View File

@ -66,4 +66,8 @@ mapApi.get(
} }
) )
/**
* This module handles all API actions related to maps.
* @module api/maps
*/
export default mapApi export default mapApi

View File

@ -67,4 +67,8 @@ playerApi.get(
} }
) )
/**
* This module handles all API actions related to players.
* @module api/players
*/
export default playerApi export default playerApi

View File

@ -1,3 +1,9 @@
/**
* This is the entrypoint for 'DDStats'.
* DDStats is a custom API that servers statistics from the DDRace Network-
* @author BurnyLlama <xmpp:burnyllama@qwik.space>
*/
import express from 'express' import express from 'express'
import dotenv from 'dotenv' import dotenv from 'dotenv'
import sqlite2mongo from './libs/database/sqlite2mongo.js' import sqlite2mongo from './libs/database/sqlite2mongo.js'

View File

@ -7,6 +7,10 @@ const log = initLog("database")
export let sqlite = undefined export let sqlite = undefined
/**
* This initializes both the sqlite db and mongodb.
* @module libs/database/init
*/
async function init() { async function init() {
log("Starting up databases...") log("Starting up databases...")

View File

@ -4,6 +4,11 @@ import postTasks from './sqlite2mongo/postTasks.js'
const log = initLog("sqlite2mongo") const log = initLog("sqlite2mongo")
/**
* This function handles the sqlite database provided by DDNet and
* migrates the data to mongodb.
* @module libs/database/sqlite2mongo
*/
async function sqlite2mongo() { async function sqlite2mongo() {
log("Checking for additions to 'ddnet.sqlite'...") log("Checking for additions to 'ddnet.sqlite'...")

View File

@ -7,7 +7,10 @@ import initLog from '../../utills/log.js'
const log = initLog("sqlite2mongo") const log = initLog("sqlite2mongo")
/**
* This for new maps from the sqlite db. If any are found it adds them to mongodb.
* @module libs/database/sqlite2mongo/checks
*/
export async function checkForMaps() { export async function checkForMaps() {
log("Checking for new maps...") log("Checking for new maps...")
@ -39,7 +42,10 @@ export async function checkForMaps() {
} }
/**
* This checks for new players in the sqlite db. If any are found it adds them to mongodb.
* @module libs/database/sqlite2mongo/checks
*/
export async function checkForPlayers() { export async function checkForPlayers() {
log("Checking for new players...") log("Checking for new players...")
@ -47,7 +53,8 @@ export async function checkForPlayers() {
const date = latestPlayer ? latestPlayer.firstFinish.toISOString().replace(/[TZ]/g, " ").replace(/\.[0-9\s]+/, "") : undefined const date = latestPlayer ? latestPlayer.firstFinish.toISOString().replace(/[TZ]/g, " ").replace(/\.[0-9\s]+/, "") : undefined
const newPlayerAmount = (await sqlite.get(`select count(Name) FROM (SELECT Name, Timestamp FROM (SELECT Name, Timestamp FROM (SELECT * FROM race ORDER BY Timestamp ASC, Name ASC) GROUP BY Name) ${date ? `WHERE Timestamp > datetime('${date}')` : ''} ORDER BY Timestamp ASC)`))['count(Name)'] const newPlayerAmount = (await sqlite.get(`select count(Name) FROM (SELECT Name, Timestamp FROM (SELECT Name, Timestamp FROM (SELECT * FROM race ORDER BY Timestamp ASC, Name ASC) GROUP BY Name) ${date ? `WHERE Timestamp > datetime('${date}')` : ''} ORDER BY Timestamp ASC)`))['count(Name)']
if (newPlayerAmount === 0) return log("No new players found...") if (newPlayerAmount === 0) return log("No new players found...")
// Create a temporary table to prevent re-running CPU-intensive queries.
log(`Found ${newPlayerAmount} new players!`) log(`Found ${newPlayerAmount} new players!`)
log("Importing new players...") log("Importing new players...")
await sqlite.exec("DROP TABLE IF EXISTS temp") await sqlite.exec("DROP TABLE IF EXISTS temp")
@ -78,7 +85,10 @@ export async function checkForPlayers() {
} }
} }
/**
* This checks for new finishes in the sqlite db. If any are found it adds them to mongodb.
* @module libs/database/sqlite2mongo/checks
*/
export async function checkForFinishes() { export async function checkForFinishes() {
log("Checking for new finishes...") log("Checking for new finishes...")

View File

@ -6,6 +6,10 @@ import { sqlite } from '../init.js'
const log = initLog("sqlite2mongo") const log = initLog("sqlite2mongo")
/**
* This handles some post-tasks that run after the initial checks.
* @module libs/database/sqlite2mongo/postTasks
*/
export default async function postTasks() { export default async function postTasks() {
log("Post tasks...") log("Post tasks...")

View File

@ -1,3 +1,13 @@
/**
* This function creates a custom logging method that adds a prefix evrytime used.
* This is so that you can see what component has done what.
* Example:
* The database-component would log with the prefix 'database'
*
* @param {string} prefix The prefix for the logging-function.
* @returns {function} The created log function.
*/
export default function initLog(prefix) { export default function initLog(prefix) {
return string => console.log(`${prefix} >>> ${string}`) return string => console.log(`${prefix} >>> ${string}`)
} }

View File

@ -8,4 +8,8 @@ const Finish = new mongoose.Schema({
player: String player: String
}) })
/**
* This cotains the mongoose 'Finish' model.
* @module schemas/Finish
*/
export default mongoose.model("Finish", Finish) export default mongoose.model("Finish", Finish)

View File

@ -9,4 +9,8 @@ const Level = new mongoose.Schema({
awardPoints: Number awardPoints: Number
}) })
/**
* This cotains the mongoose 'Level' (maps) model.
* @module schemas/Level
*/
export default mongoose.model("Level", Level) export default mongoose.model("Level", Level)

View File

@ -8,4 +8,8 @@ const Player = new mongoose.Schema({
firstFinish: Date firstFinish: Date
}) })
/**
* This cotains the mongoose 'Player' model.
* @module schemas/Player
*/
export default mongoose.model("Player", Player) export default mongoose.model("Player", Player)

View File

@ -8,4 +8,8 @@ const TeamFinish = new mongoose.Schema({
players: [ String ] players: [ String ]
}) })
/**
* This cotains the mongoose 'TeamFinish' model.
* @module schemas/TeamFinish
*/
export default mongoose.model("TeamFinish", TeamFinish) export default mongoose.model("TeamFinish", TeamFinish)