Работа над проектом

This commit is contained in:
Book Pauk
2022-08-21 21:10:56 +07:00
parent c7073635e3
commit 705fce73f7
7 changed files with 172 additions and 28 deletions

View File

@@ -4,7 +4,7 @@ const _ = require('lodash');
const WorkerState = require('../core/WorkerState');//singleton
const WebWorker = require('../core/WebWorker');//singleton
const log = new (require('../core/AppLogger'))().log;//singleton
//const utils = require('../core/utils');
const utils = require('../core/utils');
const cleanPeriod = 1*60*1000;//1 минута
const closeSocketOnIdle = 5*60*1000;//5 минут
@@ -68,6 +68,8 @@ class WebSocketController {
await this.getWorkerState(req, ws); break;
case 'search':
await this.search(req, ws); break;
case 'get-book-list':
await this.getBookList(req, ws); break;
default:
throw new Error(`Action not found: ${req.action}`);
@@ -122,6 +124,15 @@ class WebSocketController {
this.send(result, req, ws);
}
async getBookList(req, ws) {
if (!utils.hasProp(req, 'authorId'))
throw new Error(`authorId is empty`);
const result = await this.webWorker.getBookList(req.authorId);
this.send(result, req, ws);
}
}
module.exports = WebSocketController;

View File

@@ -193,7 +193,7 @@ class DbSearcher {
//сначала попробуем найти в кеше
const q = query;
const keyArr = [q.author, q.series, q.title, q.genre, q.lang];
const keyStr = keyArr.join('');
const keyStr = `query-${keyArr.join('')}`;
if (!keyStr) {//пустой запрос
if (db.searchCache.authorIdsAll)
@@ -262,6 +262,42 @@ class DbSearcher {
}
}
async getBookList(authorId) {
if (this.closed)
throw new Error('DbSearcher closed');
this.searchFlag++;
try {
const db = this.db;
//выборка автора по authorId
const rows = await db.select({
table: 'author',
map: `(r) => ({author: r.author, bookId: r.bookId})`,
where: `@@id(${db.esc(authorId)})`
});
let author = '';
let result = [];
if (rows.length) {
author = rows[0].author;
//выборка книг по bookId
result = await db.select({
table: 'book',
//map: `(r) => ({})`,
where: `@@id(${db.esc(rows[0].bookId)})`,
});
}
return {author, books: result};
} finally {
this.searchFlag--;
}
}
async periodicCleanCache() {
this.timer = null;
const cleanInterval = 5*1000;//this.config.cacheCleanInterval*60*1000;

View File

@@ -195,7 +195,13 @@ class WebWorker {
totalFound: result.totalFound,
inpxHash: (config.inpxHash ? config.inpxHash : ''),
};
}
}
async getBookList(authorId) {
this.checkMyState();
return await this.dbSearcher.getBookList(authorId);
}
async logServerStats() {
while (1) {// eslint-disable-line