diff --git a/db-tabulator/web-endpoint.ts b/db-tabulator/web-endpoint.ts index b208f03..e86ff13 100644 --- a/db-tabulator/web-endpoint.ts +++ b/db-tabulator/web-endpoint.ts @@ -9,10 +9,11 @@ import { processQueries, BOT_NAME } from "./app"; -import { createLogStream, mapPath } from "../utils"; -import {bot, enwikidb} from "../botbase"; +import {createLogStream, mapPath} from "../utils"; +import {bot} from "../botbase"; import {getRedisInstance} from "../redis"; import {EventEmitter} from "events"; +import {EnwikiWebDb} from "../db"; const router = express.Router(); @@ -22,7 +23,7 @@ const redis = getRedisInstance(); /** Store the list of pages currently undergoing update as a redis set */ const redisKey = 'web-db-tabulator-pages'; -const db = new enwikidb(); +const db = new EnwikiWebDb(); router.get('/stream', async (req, res) => { const {page} = req.query as Record; @@ -77,7 +78,6 @@ router.get('/stream', async (req, res) => { stream('looking-up-transclusions'); const title = bot.Title.newFromText(page); try { - // FIXME: use the web replica here as this is a blocking call? const transcludedReportPages = await db.query(` SELECT lt_namespace, lt_title FROM templatelinks JOIN linktarget ON tl_target_id = lt_id diff --git a/db.ts b/db.ts index 597119e..061d658 100644 --- a/db.ts +++ b/db.ts @@ -10,6 +10,7 @@ import type {MwnDate} from "../mwn"; import {onToolforge} from "./utils"; export const ENWIKI_DB_HOST = 'enwiki.analytics.db.svc.wikimedia.cloud'; +export const ENWIKI_WEB_DB_HOST = 'enwiki.web.db.svc.wikimedia.cloud'; export const TOOLS_DB_HOST = 'tools.db.svc.wikimedia.cloud'; export abstract class db { @@ -127,6 +128,7 @@ export class enwikidb extends db { async getReplagHours() { log('[V] Querying database lag'); + // TODO: use heartbeat_p database for querying lag const lastrev = await this.query(`SELECT MAX(rev_timestamp) AS ts FROM revision`); const lastrevtime = new bot.date(lastrev[0].ts); this.replagHours = Math.round((Date.now() - lastrevtime.getTime()) / 1000 / 60 / 60); @@ -143,6 +145,15 @@ export class enwikidb extends db { } } +export class EnwikiWebDb extends enwikidb { + constructor(customOptions: mysql.PoolOptions = {}) { + super({ + host: onToolforge() ? ENWIKI_WEB_DB_HOST : '127.0.0.1', + ...customOptions + }); + } +} + export class toolsdb extends db { /** * @param dbname - DB name, `s54328__` will be prepended to this @@ -166,3 +177,9 @@ export interface SQLError extends Error { sqlState: string; sqlMessage: string; } + + +const enweb = new EnwikiWebDb(); +(async function () { + console.log(await enweb.query('SELECT page_title FROM page limit 10')) +})() \ No newline at end of file