From f4667c08929cff77313423f1584d07d71dedb97b Mon Sep 17 00:00:00 2001 From: nymkappa Date: Sat, 9 Jul 2022 12:35:17 +0200 Subject: [PATCH] Use raw diff adjustment for diff adj table widget --- .../DifficultyAdjustmentsRepository.ts | 33 +++++++++++++++++-- backend/src/routes.ts | 2 +- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/backend/src/repositories/DifficultyAdjustmentsRepository.ts b/backend/src/repositories/DifficultyAdjustmentsRepository.ts index 6952b3be9..910c65c10 100644 --- a/backend/src/repositories/DifficultyAdjustmentsRepository.ts +++ b/backend/src/repositories/DifficultyAdjustmentsRepository.ts @@ -46,9 +46,38 @@ class DifficultyAdjustmentsRepository { query += ` GROUP BY UNIX_TIMESTAMP(time) DIV ${86400}`; if (descOrder === true) { - query += ` ORDER BY time DESC`; + query += ` ORDER BY height DESC`; } else { - query += ` ORDER BY time`; + query += ` ORDER BY height`; + } + + try { + const [rows] = await DB.query(query); + return rows as IndexedDifficultyAdjustment[]; + } catch (e) { + logger.err(`Cannot get difficulty adjustments from the database. Reason: ` + (e instanceof Error ? e.message : e)); + throw e; + } + } + + public async $getRawAdjustments(interval: string | null, descOrder: boolean = false): Promise { + interval = Common.getSqlInterval(interval); + + let query = `SELECT + UNIX_TIMESTAMP(time) as time, + height as height, + difficulty as difficulty, + adjustment as adjustment + FROM difficulty_adjustments`; + + if (interval) { + query += ` WHERE time BETWEEN DATE_SUB(NOW(), INTERVAL ${interval}) AND NOW()`; + } + + if (descOrder === true) { + query += ` ORDER BY height DESC`; + } else { + query += ` ORDER BY height`; } try { diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 57c64ddf5..90a4536c2 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -734,7 +734,7 @@ class Routes { public async $getDifficultyAdjustments(req: Request, res: Response) { try { - const difficulty = await DifficultyAdjustmentsRepository.$getAdjustments(req.params.interval, true); + const difficulty = await DifficultyAdjustmentsRepository.$getRawAdjustments(req.params.interval, true); res.header('Pragma', 'public'); res.header('Cache-control', 'public'); res.setHeader('Expires', new Date(Date.now() + 1000 * 300).toUTCString());