Skip to content

Commit

Permalink
db-tabulator: use web db for querying transclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Jun 12, 2024
1 parent 836c683 commit d80d677
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions db-tabulator/web-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<string, string>;
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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'))
})()

0 comments on commit d80d677

Please sign in to comment.