From c4df7a735a6a48a3828d1022853dfec4892422de Mon Sep 17 00:00:00 2001 From: furo Date: Mon, 1 Nov 2021 15:09:03 +0100 Subject: [PATCH] Some refactoring and cache generation for graphs --- api/graph.js | 20 +++---------------- api/players.js | 2 +- index.js | 2 +- libs/database/generate.js | 39 ++++++++++++++++++++++++++++-------- libs/database/init.js | 7 +++++-- libs/database/tasks.js | 42 +++++++++++++++++++++++++++++++++++++-- libs/ddnss/handler.js | 2 -- libs/utils/log.js | 2 +- 8 files changed, 82 insertions(+), 34 deletions(-) diff --git a/api/graph.js b/api/graph.js index c934aaf..9f8ea80 100644 --- a/api/graph.js +++ b/api/graph.js @@ -22,15 +22,12 @@ graphApi.get( INNER JOIN maps AS b ON a.map = b.map WHERE a.NAME = ? - AND a.map LIKE '%' GROUP BY a.map ORDER BY a.timestamp; `) - let currentPoints = 0 let array = [] for (const finish of finishes.iterate(player)) { - console.log(finish) currentPoints += finish.Points array.push({ t: new Date(finish.Timestamp), y: currentPoints }) } @@ -52,22 +49,11 @@ graphApi.get( response: "No query ('host/path?q=query') provided!" }) } - let map = req.query.q + const finishes = sqlite.prepare(`SELECT * FROM graphRecordCache WHERE map = ? ORDER BY Timestamp`) - const finishes = sqlite.prepare( - ` - SELECT * FROM race WHERE map = ? ORDER BY Timestamp; - `) - let currentFinish - let currentBest = 0; let array = [] - for (const record of finishes.iterate(map)) { - currentFinish = record.Time - if(currentFinish <= currentBest || currentBest == 0) { - currentBest = currentFinish - array.push({ player: record.Name, Time: record.Time, Date: new Date(record.Timestamp) }) - } - } + for (const record of finishes.iterate(req.query.q)) + array.push({ t: new Date(record.timestamp), y: record.time, player: record.player}) return res.json({ success: true, diff --git a/api/players.js b/api/players.js index b8e82aa..3dd337a 100644 --- a/api/players.js +++ b/api/players.js @@ -9,7 +9,7 @@ playerApi.get( async (req, res) => { let player = req.params.player - /* Misc, may be worth to cache this? */ + /* Misc */ const firstFinish = sqlite.prepare(`SELECT server as server, map as map, time as time, Timestamp as date FROM race WHERE name = ? ORDER BY Timestamp ASC LIMIT 1`).get(player) /* Points */ diff --git a/index.js b/index.js index a6c61fb..38869b3 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ import api from './api/api.js' import { generateDB } from "./libs/database/generate.js" import { sqlite, dbInit } from "./libs/database/init.js" import { ddnssStart } from './libs/ddnss/handler.js' -//import tasks from './db/tasks.js' +import tasks from './libs/database/tasks.js' dotenv.config() diff --git a/libs/database/generate.js b/libs/database/generate.js index 10fa71e..f629367 100644 --- a/libs/database/generate.js +++ b/libs/database/generate.js @@ -12,14 +12,21 @@ const log = initLog("DB Generation") export function generateDB() { /* TODO: Clean this up as it is a mess */ /* TODO: Remove useless ones */ + + log("Generating map index...") + execMany([ + `CREATE INDEX IF NOT EXISTS "idx_maps_map" ON "maps" ("map")`, + `CREATE INDEX IF NOT EXISTS "idx_maps_category" ON "maps" ("server");` + ]) + log("Generating race index...") 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 INDEX IF NOT EXISTS "idx_race_player" ON "race" ("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")` ]) log("Creating rankings table...") @@ -45,9 +52,9 @@ export function generateDB() { log("Generating teamrace index...") execMany([ - `CREATE INDEX IF NOT EXISTS "idx_teamrace_Map" ON "teamrace" ("Map")`, + `CREATE INDEX IF NOT EXISTS "idx_teamrace_map" ON "teamrace" ("Map")`, `CREATE INDEX IF NOT EXISTS "idx_teamrace_ID" ON "teamrace" ("ID")`, - `CREATE INDEX IF NOT EXISTS "idx_teamrace_MapID" ON "teamrace" ("Map", "ID")` + `CREATE INDEX IF NOT EXISTS "idx_teamrace_mapID" ON "teamrace" ("Map", "ID")` ]) log("Creating teamrankings table...") @@ -79,6 +86,22 @@ export function generateDB() { "points" INTEGER NOT NULL); `) + log("Generating graphRecordCache...") + sqlite.exec(` + CREATE TABLE IF NOT EXISTS "graphRecordCache" ( + "map" varchar(128) NOT NULL, + "player" varchar(16) NOT NULL, + "time" float NOT NULL DEFAULT 0, + "timestamp" timestamp NOT NULL DEFAULT current_timestamp, + "Server" char(4) NOT NULL DEFAULT ''); + `) + tasks.processTimeGraph() + + execMany([ + `CREATE INDEX IF NOT EXISTS "idx_graphCache_player" ON "graphRecordCache" ("player")`, + `CREATE INDEX IF NOT EXISTS "idx_graphCache_map" ON "graphRecordCache" ("map");` + ]) + log("Inserting points to DB...") tasks.processAllPoints() diff --git a/libs/database/init.js b/libs/database/init.js index 0496bd2..7aa1573 100644 --- a/libs/database/init.js +++ b/libs/database/init.js @@ -1,9 +1,12 @@ import Database from 'better-sqlite3' +import initLog from '../utils/log.js' /* Export DB for use in other files */ export let sqlite = undefined export let skinDB = undefined +const log = initLog("DB Init") + /** * This initalizes the ddnet.sqlite and skindata.sqlite DB... * @module db/dbInit @@ -21,6 +24,6 @@ export function dbInit() { /* Unsafe mode */ sqlite.unsafeMode() - console.log("Loaded in 'ddnet.sqlite'!") - console.log("Loaded in 'skindata.sqlite'!") + log("Loaded in ddnet.sqlite...") + log("Loaded in skindata.sqlite...") } diff --git a/libs/database/tasks.js b/libs/database/tasks.js index 2d81072..97a47fd 100644 --- a/libs/database/tasks.js +++ b/libs/database/tasks.js @@ -86,6 +86,43 @@ export function processTeamRankings() { .run(map.Map) } +/** + * This generates a cache for all the dates the top record has been improved for each map... + * @module libs/database/processTimeGraph + */ +export function processTimeGraph() { + + let currentFinish + let currentBest = 0; + + const maps = sqlite.prepare(`SELECT map FROM maps`); + const finishes = sqlite.prepare(`SELECT * FROM race WHERE map = ? ORDER BY Timestamp`) + for (const map of maps.iterate()) { + let currentFinish + let currentBest = 0; + + for (const record of finishes.iterate(map.Map)) { + currentFinish = record.Time + if (currentFinish <= currentBest || currentBest == 0) { + currentBest = currentFinish + + sqlite.prepare(` + INSERT INTO "graphRecordCache" + ( + map, player, time, timestamp, server + ) VALUES (?, ?, ?, ?, ?) + `).run( + map.Map, + record.Name, + record.Time, + record.Timestamp, + record.Server + ) + } + } + } +} + /** * This inserts all types of points into a table... * @module db/processAllPoints @@ -113,7 +150,7 @@ export function processAllPoints() { `) /* Insert data */ - for(const type in types) { + for (const type in types) { let rank = 1 for (const entry of types[type]) { @@ -146,5 +183,6 @@ export function processAllPoints() { export default { processAllPoints, processRankings, - processTeamRankings + processTeamRankings, + processTimeGraph } \ No newline at end of file diff --git a/libs/ddnss/handler.js b/libs/ddnss/handler.js index 7f2a6ce..5e215aa 100644 --- a/libs/ddnss/handler.js +++ b/libs/ddnss/handler.js @@ -9,8 +9,6 @@ export async function ddnssStart() { const getServers = await fetch('https://ddnet.tw/status/index.json'); const servers = await getServers.json(); - console.log(servers) - for (const server of servers) { const connection = `${server.ip}:${server.port}` diff --git a/libs/utils/log.js b/libs/utils/log.js index 88647a8..238e009 100644 --- a/libs/utils/log.js +++ b/libs/utils/log.js @@ -1,5 +1,5 @@ /** - * This function creates a custom logging method that adds a prefix evrytime used. + * This function creates a custom logging method that adds a prefix everytime used. * This is so that you can see what component has done what. * Example: * The database-component would log with the prefix 'database'