mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
core: use common plugin-api interface
This commit is contained in:
1 parent
c61c875bee
commit
70a96aecac
54 files changed
+387
-566
No files matched your search
+49
-48
@@ -1,56 +1,57 @@
|
||||
import { Schema } from 'hydrooj';
|
||||
import { Logger } from 'hydrooj/src/logger';
|
||||
import DomainModel from 'hydrooj/src/model/domain';
|
||||
import * as system from 'hydrooj/src/model/system';
|
||||
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/src/pipelineUtils';
|
||||
import sonic from './service';
|
||||
import {
|
||||
Context, DomainModel, iterateAllProblem, iterateAllProblemInDomain,
|
||||
Logger, Schema, SystemModel,
|
||||
} from 'hydrooj';
|
||||
import * as SonicService from './service';
|
||||
|
||||
const logger = new Logger('sonic');
|
||||
|
||||
global.Hydro.lib.problemSearch = async (domainId, query, opts) => {
|
||||
const limit = opts?.limit || system.get('pagination.problem');
|
||||
const ids = await sonic.query('problem', `${domainId}@title`, query, { limit });
|
||||
if (limit - ids.length > 0) ids.push(...await sonic.query('problem', `${domainId}@content`, query, { limit: limit - ids.length }));
|
||||
return {
|
||||
countRelation: ids.length >= limit ? 'gte' : 'eq',
|
||||
total: ids.length,
|
||||
hits: ids,
|
||||
};
|
||||
};
|
||||
export function apply(ctx: Context) {
|
||||
ctx.plugin(SonicService);
|
||||
|
||||
async function run({ domainId }, report) {
|
||||
if (domainId) await sonic.flushb('problem', domainId);
|
||||
else await sonic.flushc('problem');
|
||||
let i = 0;
|
||||
const cb = async (pdoc) => {
|
||||
i++;
|
||||
if (!(i % 1000)) report({ message: `${i} problems indexed` });
|
||||
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
|
||||
const tasks = [];
|
||||
for (const did of [pdoc.domainId, ...union.map((j) => j._id)]) {
|
||||
tasks.push(
|
||||
pdoc.title && sonic.push(
|
||||
'problem', `${did}@title`, `${pdoc.domainId}/${pdoc.docId}`,
|
||||
`${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag.join(' ')}`,
|
||||
),
|
||||
pdoc.content.toString() && sonic.push('problem', `${did}@content`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.content.toString()),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
|
||||
global.Hydro.lib.problemSearch = async (domainId, query, opts) => {
|
||||
const limit = opts?.limit || SystemModel.get('pagination.problem');
|
||||
const ids = await ctx.sonic.query('problem', `${domainId}@title`, query, { limit });
|
||||
if (limit - ids.length > 0) ids.push(...await ctx.sonic.query('problem', `${domainId}@content`, query, { limit: limit - ids.length }));
|
||||
return {
|
||||
countRelation: ids.length >= limit ? 'gte' : 'eq',
|
||||
total: ids.length,
|
||||
hits: ids,
|
||||
};
|
||||
};
|
||||
if (domainId) await iterateAllProblemInDomain(domainId, ['title', 'content'], cb);
|
||||
else await iterateAllProblem(['title', 'content', 'tag'], cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function apply(ctx) {
|
||||
async function run({ domainId }, report) {
|
||||
if (domainId) await ctx.sonic.flushb('problem', domainId);
|
||||
else await ctx.sonic.flushc('problem');
|
||||
let i = 0;
|
||||
const cb = async (pdoc) => {
|
||||
i++;
|
||||
if (!(i % 1000)) report({ message: `${i} problems indexed` });
|
||||
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
|
||||
const tasks = [];
|
||||
for (const did of [pdoc.domainId, ...union.map((j) => j._id)]) {
|
||||
tasks.push(
|
||||
pdoc.title && ctx.sonic.push(
|
||||
'problem', `${did}@title`, `${pdoc.domainId}/${pdoc.docId}`,
|
||||
`${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag.join(' ')}`,
|
||||
),
|
||||
pdoc.content.toString() && ctx.sonic.push('problem', `${did}@content`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.content.toString()),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
|
||||
};
|
||||
if (domainId) await iterateAllProblemInDomain(domainId, ['title', 'content'], cb);
|
||||
else await iterateAllProblem(['title', 'content', 'tag'], cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
ctx.on('problem/add', async (doc, docId) => {
|
||||
const union = await DomainModel.searchUnion({ union: doc.domainId, problem: true });
|
||||
const tasks = [];
|
||||
for (const domainId of [doc.domainId, ...union.map((i) => i._id)]) {
|
||||
tasks.push(
|
||||
sonic.push('problem', `${domainId}@title`, `${doc.domainId}/${docId}`, `${doc.pid || ''} ${doc.title} ${doc.tag?.join(' ')}`),
|
||||
sonic.push('problem', `${domainId}@content`, `${doc.domainId}/${docId}`, doc.content.toString()),
|
||||
ctx.sonic.push('problem', `${domainId}@title`, `${doc.domainId}/${docId}`, `${doc.pid || ''} ${doc.title} ${doc.tag?.join(' ')}`),
|
||||
ctx.sonic.push('problem', `${domainId}@content`, `${doc.domainId}/${docId}`, doc.content.toString()),
|
||||
);
|
||||
}
|
||||
Promise.all(tasks).catch((e) => logger.error(e));
|
||||
@@ -62,10 +63,10 @@ export function apply(ctx) {
|
||||
const id = `${pdoc.domainId}/${pdoc.docId}`;
|
||||
for (const domainId of [pdoc.domainId, ...union.map((i) => i._id)]) {
|
||||
tasks.push(
|
||||
sonic.flusho('problem', `${domainId}@title`, id)
|
||||
.then(() => sonic.push('problem', `${domainId}@title`, id, `${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag?.join(' ')}`)),
|
||||
sonic.flusho('problem', `${domainId}@content`, id)
|
||||
.then(() => sonic.push('problem', `${domainId}@content`, id, pdoc.content.toString())),
|
||||
ctx.sonic.flusho('problem', `${domainId}@title`, id)
|
||||
.then(() => ctx.sonic.push('problem', `${domainId}@title`, id, `${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag?.join(' ')}`)),
|
||||
ctx.sonic.flusho('problem', `${domainId}@content`, id)
|
||||
.then(() => ctx.sonic.push('problem', `${domainId}@content`, id, pdoc.content.toString())),
|
||||
);
|
||||
}
|
||||
Promise.all(tasks).catch((e) => logger.error(e));
|
||||
@@ -77,8 +78,8 @@ export function apply(ctx) {
|
||||
const id = `${domainId}/${docId}`;
|
||||
for (const domain of [domainId, ...union.map((i) => i._id)]) {
|
||||
tasks.push(
|
||||
sonic.flusho('problem', `${domain}@title`, id),
|
||||
sonic.flusho('problem', `${domain}@content`, id),
|
||||
ctx.sonic.flusho('problem', `${domain}@title`, id),
|
||||
ctx.sonic.flusho('problem', `${domain}@content`, id),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import { Logger } from 'hydrooj/src/logger';
|
||||
import DomainModel from 'hydrooj/src/model/domain';
|
||||
import * as system from 'hydrooj/src/model/system';
|
||||
import * as bus from 'hydrooj/src/service/bus';
|
||||
import sonic from './service';
|
||||
|
||||
const logger = new Logger('sonic');
|
||||
|
||||
bus.on('problem/add', async (doc, docId) => {
|
||||
const union = await DomainModel.searchUnion({ union: doc.domainId, problem: true });
|
||||
const tasks = [];
|
||||
for (const domainId of [doc.domainId, ...union.map((i) => i._id)]) {
|
||||
tasks.push(
|
||||
sonic.push('problem', `${domainId}@title`, `${doc.domainId}/${docId}`, `${doc.pid || ''} ${doc.title} ${doc.tag?.join(' ')}`),
|
||||
sonic.push('problem', `${domainId}@content`, `${doc.domainId}/${docId}`, doc.content.toString()),
|
||||
);
|
||||
}
|
||||
Promise.all(tasks).catch((e) => logger.error(e));
|
||||
});
|
||||
|
||||
bus.on('problem/edit', async (pdoc) => {
|
||||
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
|
||||
const tasks = [];
|
||||
const id = `${pdoc.domainId}/${pdoc.docId}`;
|
||||
for (const domainId of [pdoc.domainId, ...union.map((i) => i._id)]) {
|
||||
tasks.push(
|
||||
sonic.flusho('problem', `${domainId}@title`, id)
|
||||
.then(() => sonic.push('problem', `${domainId}@title`, id, `${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag?.join(' ')}`)),
|
||||
sonic.flusho('problem', `${domainId}@content`, id)
|
||||
.then(() => sonic.push('problem', `${domainId}@content`, id, pdoc.content.toString())),
|
||||
);
|
||||
}
|
||||
Promise.all(tasks).catch((e) => logger.error(e));
|
||||
});
|
||||
|
||||
bus.on('problem/del', async (domainId, docId) => {
|
||||
const union = await DomainModel.searchUnion({ union: domainId, problem: true });
|
||||
const tasks = [];
|
||||
const id = `${domainId}/${docId}`;
|
||||
for (const domain of [domainId, ...union.map((i) => i._id)]) {
|
||||
tasks.push(
|
||||
sonic.flusho('problem', `${domain}@title`, id),
|
||||
sonic.flusho('problem', `${domain}@content`, id),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
});
|
||||
|
||||
global.Hydro.lib.problemSearch = async (domainId, query, opts) => {
|
||||
const limit = opts?.limit || system.get('pagination.problem');
|
||||
const ids = await sonic.query('problem', `${domainId}@title`, query, { limit });
|
||||
if (limit - ids.length > 0) ids.push(...await sonic.query('problem', `${domainId}@content`, query, { limit: limit - ids.length }));
|
||||
return {
|
||||
countRelation: ids.length >= limit ? 'gte' : 'eq',
|
||||
total: ids.length,
|
||||
hits: ids,
|
||||
};
|
||||
};
|
||||
@@ -1,37 +0,0 @@
|
||||
import { Schema } from 'hydrooj';
|
||||
import DomainModel from 'hydrooj/src/model/domain';
|
||||
import { iterateAllProblem, iterateAllProblemInDomain } from 'hydrooj/src/pipelineUtils';
|
||||
import sonic from './service';
|
||||
|
||||
async function run({ domainId }, report) {
|
||||
if (domainId) await sonic.flushb('problem', domainId);
|
||||
else await sonic.flushc('problem');
|
||||
let i = 0;
|
||||
const cb = async (pdoc) => {
|
||||
i++;
|
||||
if (!(i % 1000)) report({ message: `${i} problems indexed` });
|
||||
const union = await DomainModel.searchUnion({ union: pdoc.domainId, problem: true });
|
||||
const tasks = [];
|
||||
for (const did of [pdoc.domainId, ...union.map((j) => j._id)]) {
|
||||
tasks.push(
|
||||
pdoc.title && sonic.push(
|
||||
'problem', `${did}@title`, `${pdoc.domainId}/${pdoc.docId}`,
|
||||
`${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag.join(' ')}`,
|
||||
),
|
||||
pdoc.content.toString() && sonic.push('problem', `${did}@content`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.content.toString()),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
|
||||
};
|
||||
if (domainId) await iterateAllProblemInDomain(domainId, ['title', 'content'], cb);
|
||||
else await iterateAllProblem(['title', 'content', 'tag'], cb);
|
||||
return true;
|
||||
}
|
||||
|
||||
export const apply = (ctx) => ctx.addScript(
|
||||
'ensureSonicSearch', 'Sonic problem search re-index',
|
||||
Schema.object({
|
||||
domainId: Schema.string(),
|
||||
}),
|
||||
run,
|
||||
);
|
||||
+13
-18
@@ -2,28 +2,29 @@ import {
|
||||
Ingest, PushOptions, QueryOptions,
|
||||
Search, SuggestOptions,
|
||||
} from 'sonic-channel';
|
||||
import { BaseService } from 'hydrooj';
|
||||
import { Logger } from 'hydrooj/src/logger';
|
||||
import * as system from 'hydrooj/src/model/system';
|
||||
import {
|
||||
Context, Logger, Service, SystemModel,
|
||||
} from 'hydrooj';
|
||||
|
||||
const logger = new Logger('sonic');
|
||||
|
||||
declare module 'hydrooj/src/interface' {
|
||||
declare module 'hydrooj' {
|
||||
interface SystemKeys {
|
||||
'sonic.host': string;
|
||||
'sonic.port': number;
|
||||
'sonic.auth': string;
|
||||
}
|
||||
interface Context {
|
||||
sonic: SonicService;
|
||||
}
|
||||
}
|
||||
|
||||
const logger = new Logger('sonic');
|
||||
|
||||
function getHandler(type: string, that: any) {
|
||||
return {
|
||||
connected() {
|
||||
that.started = true;
|
||||
logger.info(`Sonic Channel succeeded to connect to host (${type}).`);
|
||||
},
|
||||
disconnected() {
|
||||
that.started = false;
|
||||
logger.error(`Sonic Channel is now disconnected (${type}).`);
|
||||
},
|
||||
timeout() {
|
||||
@@ -39,14 +40,13 @@ function getHandler(type: string, that: any) {
|
||||
};
|
||||
}
|
||||
|
||||
class SonicService implements BaseService {
|
||||
class SonicService extends Service {
|
||||
public search: Search;
|
||||
public ingest: Ingest;
|
||||
public started = false;
|
||||
public error = '';
|
||||
|
||||
async start() {
|
||||
const [host, port, auth] = system.getMany(['sonic.host', 'sonic.port', 'sonic.auth']);
|
||||
const [host, port, auth] = SystemModel.getMany(['sonic.host', 'sonic.port', 'sonic.auth']);
|
||||
const cfg = {
|
||||
host: host || '::1',
|
||||
port: port || 1491,
|
||||
@@ -108,11 +108,6 @@ class SonicService implements BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
const service = new SonicService();
|
||||
global.Hydro.service.sonic = service;
|
||||
export = service;
|
||||
declare module 'hydrooj/src/interface' {
|
||||
interface Service {
|
||||
sonic: typeof service
|
||||
}
|
||||
export function apply() {
|
||||
Context.service('sonic', SonicService);
|
||||
}
|
||||
Reference in new issue
Block a user