diff --git a/api/finishes.js b/api/finishes.js index 98e9063..f56655e 100644 --- a/api/finishes.js +++ b/api/finishes.js @@ -1,5 +1,5 @@ import { Router } from 'express' -import { sqlite } from '../db/init.js' +import { sqlite } from '../libs/db/init.js' const finishApi = Router() diff --git a/api/graph.js b/api/graph.js index e19307a..0a00067 100644 --- a/api/graph.js +++ b/api/graph.js @@ -1,5 +1,5 @@ import { Router } from 'express' -import { sqlite } from '../db/init.js' +import { sqlite } from '../libs/db/init.js' const graphApi = Router() diff --git a/api/maps.js b/api/maps.js index 1d38999..5ec29c3 100644 --- a/api/maps.js +++ b/api/maps.js @@ -1,5 +1,5 @@ import { Router } from 'express' -import { sqlite } from '../db/init.js' +import { sqlite } from '../libs/db/init.js' const mapApi = Router() diff --git a/api/players.js b/api/players.js index 942f656..75131cc 100644 --- a/api/players.js +++ b/api/players.js @@ -1,5 +1,5 @@ import { Router } from 'express' -import { sqlite } from '../db/init.js' +import { sqlite } from '../libs/db/init.js' const playerApi = Router() diff --git a/index.js b/index.js index a5e3ed1..59388e6 100644 --- a/index.js +++ b/index.js @@ -1,25 +1,20 @@ import express from 'express' import dotenv from 'dotenv' import api from './api/api.js' -import { generateDB } from "./db/generate.js" -import { sqlite, dbInit } from "./db/init.js" -import { ddnssStart, scrapeServer } from './ddnss/handler.js' +import { generateDB } from "./libs/db/generate.js" +import { sqlite, dbInit } from "./libs/db/init.js" +import { ddnssStart, scrapeServer } from './libs/ddnss/handler.js' //import tasks from './db/tasks.js' -/* Read the .env file */ dotenv.config() -/* Init db */ dbInit() -/* check if the table points exists */ const exists = sqlite.prepare(`SELECT count(*) as a FROM sqlite_master WHERE type='table' AND name='points'`).get() -/* Generate the DB if false */ if(!exists.a) generateDB() -/* Init express */ const Server = express() Server.use('/api', api) diff --git a/db/generate.js b/libs/db/generate.js similarity index 71% rename from db/generate.js rename to libs/db/generate.js index c462124..d147867 100644 --- a/db/generate.js +++ b/libs/db/generate.js @@ -1,5 +1,9 @@ -import { sqlite, skinDB } from "./init.js" -import tasks from "./tasks.js" +import { sqlite, skinDB } from './init.js' +import tasks from './tasks.js' +import { execMany } from './helper.js' +import initLog from '../utils/log.js' + +const log = initLog("DB Generation") /** * This constructs the DB with indexes and rankings... @@ -7,18 +11,20 @@ import tasks from "./tasks.js" */ export function generateDB() { /* TODO: Clean this up as it is a mess */ - console.log("Generating race index") + log("Generating race index...") /* Generate race index TODO: Remove useless ones */ - sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Map_2" ON "race" ("Map","Name")`) - sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Name" ON "race" ("Name","Timestamp")`) - sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Server" ON "race" ("Server")`) - sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_MapTimestamp" ON "race" ("Map","Timestamp")`) - sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Timestamp" ON "race" ("Timestamp")`) - sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_MapNameTime" ON "race" ("Map", "Name", "Time")`) + execMany([ + `CREATE INDEX IF NOT EXISTS "idx_race_Map_2" ON "race" ("Map","Name")`, + `CREATE INDEX IF NOT EXISTS "idx_race_Name" ON "race" ("Name","Timestamp")`, + `CREATE INDEX IF NOT EXISTS "idx_race_Server" ON "race" ("Server")`, + `CREATE INDEX IF NOT EXISTS "idx_race_MapTimestamp" ON "race" ("Map","Timestamp")`, + `CREATE INDEX IF NOT EXISTS "idx_race_Timestamp" ON "race" ("Timestamp")`, + `CREATE INDEX IF NOT EXISTS "idx_race_MapNameTime" ON "race" ("Map", "Name", "Time")` + ]) /* Create rankings table */ - console.log("Creating rankings table") + log("Creating rankings table...") sqlite.exec(` CREATE TABLE IF NOT EXISTS "rankings" ( "Map" varchar(128) NOT NULL, @@ -29,22 +35,22 @@ export function generateDB() { "rank" INTEGER NOT NULL); `) - console.log("Calculating rankings for each map") + log("Calculating rankings for each map...") tasks.processRankings() /* Generate rankings index */ - console.log("Generating rankings index") + log("Generating rankings index...") sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_map" ON "rankings" ("Map")`) sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_rank" ON "rankings" ("rank")`) sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_player" ON "rankings" ("Name")`) /* Generate teamrace index */ - console.log("Generating teamrace index") + log("Generating teamrace index...") sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_Map" ON "teamrace" ("Map")`); sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_ID" ON "teamrace" ("ID")`); sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_MapID" ON "teamrace" ("Map", "ID")`); - console.log("Creating teamrankings table") + log("Creating teamrankings table...") sqlite.exec(` CREATE TABLE IF NOT EXISTS "teamrankings" ( "Map" varchar(128) NOT NULL, @@ -56,10 +62,10 @@ export function generateDB() { "teamrank" INTEGER NOT NULL); `) - console.log("Calculating teamrankings for each map") + log("Calculating teamrankings for each map...") tasks.processTeamRankings() - console.log("Generating teamrankings index") + log("Generating teamrankings index...") sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrankings_map" ON "teamrankings" ("Map")`) sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrankings_rank" ON "teamrankings" ("teamrank")`) sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrankings_player" ON "teamrankings" ("name")`) @@ -72,7 +78,7 @@ export function generateDB() { `) /* Process all types of points */ - console.log("Inserting points to DB") + log("Inserting points to DB...") tasks.processAllPoints() skinDB.exec(` diff --git a/libs/db/helper.js b/libs/db/helper.js new file mode 100644 index 0000000..8382380 --- /dev/null +++ b/libs/db/helper.js @@ -0,0 +1,12 @@ +import { sqlite } from './init.js' + +/** + * This function takes an array of strings to be ran on the DB. + * + * @param {array} instructions Array of instructions to be ran. + * @author BurnyLlama + */ +export function execMany(instructions) { + for (const instruction of instructions) + sqlite.exec(instruction) +} \ No newline at end of file diff --git a/db/init.js b/libs/db/init.js similarity index 100% rename from db/init.js rename to libs/db/init.js diff --git a/db/tasks.js b/libs/db/tasks.js similarity index 100% rename from db/tasks.js rename to libs/db/tasks.js diff --git a/ddnss/handler.js b/libs/ddnss/handler.js similarity index 100% rename from ddnss/handler.js rename to libs/ddnss/handler.js diff --git a/libs/utils/log.js b/libs/utils/log.js new file mode 100644 index 0000000..88647a8 --- /dev/null +++ b/libs/utils/log.js @@ -0,0 +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) { + return string => console.log(`${prefix} >>> ${string}`) +}