server: optimize res data size

This commit is contained in:
fantasticit 2022-05-24 19:54:57 +08:00
parent a18918f0f9
commit f17d6bfbe0
3 changed files with 44 additions and 46 deletions

View File

@ -524,10 +524,6 @@ export class DocumentService {
} }
); );
documents.forEach((doc) => {
delete doc.state;
});
const docs = documents const docs = documents
.filter((doc) => !doc.isWikiHome) .filter((doc) => !doc.isWikiHome)
.map((doc) => { .map((doc) => {
@ -535,6 +531,9 @@ export class DocumentService {
res.key = res.id; res.key = res.id;
res.label = res.title; res.label = res.title;
return res; return res;
})
.map((item) => {
return lodash.omit(item, ['content', 'state']);
}); });
const docsWithCreateUser = await Promise.all( const docsWithCreateUser = await Promise.all(
@ -579,10 +578,6 @@ export class DocumentService {
} }
); );
documents.forEach((doc) => {
delete doc.state;
});
const docs = documents const docs = documents
.filter((doc) => !doc.isWikiHome) .filter((doc) => !doc.isWikiHome)
.map((doc) => { .map((doc) => {
@ -590,6 +585,9 @@ export class DocumentService {
res.key = res.id; res.key = res.id;
res.label = res.title; res.label = res.title;
return res; return res;
})
.map((item) => {
return lodash.omit(item, ['content', 'state']);
}); });
const docsWithCreateUser = await Promise.all( const docsWithCreateUser = await Promise.all(

View File

@ -70,38 +70,41 @@ export class ViewService {
const now = Date.now(); const now = Date.now();
const queryBuilder = this.viewRepo.createQueryBuilder('view'); const queryBuilder = this.viewRepo.createQueryBuilder('view');
queryBuilder.where('view.userId=:userId', { userId }).andWhere('view.createdAt BETWEEN :start AND :end', { queryBuilder
.where('view.userId=:userId', { userId })
.andWhere('view.createdAt BETWEEN :start AND :end', {
start: new Date(now - 3 * ONE_DAY), start: new Date(now - 3 * ONE_DAY),
end: new Date(now), end: new Date(now),
}); })
.orderBy('view.createdAt', 'DESC');
const ret = await queryBuilder.getMany(); const ret = await queryBuilder.getMany();
const map = {}; // const map = {};
ret.forEach((item) => { // ret.forEach((item) => {
const key = item.documentId; // const key = item.documentId;
if (!map[key]) { // if (!map[key]) {
map[key] = item; // map[key] = item;
} // }
const mapItem = map[key]; // const mapItem = map[key];
const isGreaterThan = new Date(mapItem.createdAt).valueOf() < new Date(item.createdAt).valueOf(); // const isGreaterThan = new Date(mapItem.createdAt).valueOf() < new Date(item.createdAt).valueOf();
if (isGreaterThan) { // if (isGreaterThan) {
map[key] = item; // map[key] = item;
} // }
}); // });
const res = Object.keys(map).map((documentId) => { const res = ret.slice(0, 20).map((item) => {
return { return {
documentId, documentId: item.documentId,
visitedAt: map[documentId].createdAt, visitedAt: item.createdAt,
}; };
}); });
res.sort((a, b) => { // res.sort((a, b) => {
return -new Date(a.visitedAt).valueOf() + new Date(b.visitedAt).valueOf(); // return -new Date(a.visitedAt).valueOf() + new Date(b.visitedAt).valueOf();
}); // });
return res.slice(0, 20); return res;
} }
} }

View File

@ -15,6 +15,7 @@ import { OutUser } from '@services/user.service';
import { ViewService } from '@services/view.service'; import { ViewService } from '@services/view.service';
import { DocumentStatus, IPagination, WikiStatus, WikiUserRole } from '@think/domains'; import { DocumentStatus, IPagination, WikiStatus, WikiUserRole } from '@think/domains';
import { instanceToPlain } from 'class-transformer'; import { instanceToPlain } from 'class-transformer';
import * as lodash from 'lodash';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
@Injectable() @Injectable()
@ -424,7 +425,7 @@ export class WikiService {
*/ */
async getWikiHomeDocument(user: OutUser, wikiId) { async getWikiHomeDocument(user: OutUser, wikiId) {
const res = await this.documentService.documentRepo.findOne({ wikiId, isWikiHome: true }); const res = await this.documentService.documentRepo.findOne({ wikiId, isWikiHome: true });
return instanceToPlain(res); return lodash.omit(instanceToPlain(res), ['state']);
} }
/** /**
@ -539,11 +540,6 @@ export class WikiService {
}); });
documents.sort((a, b) => a.index - b.index); documents.sort((a, b) => a.index - b.index);
documents.forEach((doc) => {
delete doc.content;
delete doc.state;
});
const docs = documents const docs = documents
.filter((doc) => !doc.isWikiHome) .filter((doc) => !doc.isWikiHome)
.map((doc) => { .map((doc) => {
@ -551,6 +547,9 @@ export class WikiService {
res.key = res.id; res.key = res.id;
res.label = res.title; res.label = res.title;
return res; return res;
})
.map((item) => {
return lodash.omit(item, ['content', 'state']);
}); });
const docsWithCreateUser = await Promise.all( const docsWithCreateUser = await Promise.all(
@ -602,9 +601,6 @@ export class WikiService {
const ids = records.map((record) => record.documentId); const ids = records.map((record) => record.documentId);
const documents = await this.documentService.documentRepo.findByIds(ids); const documents = await this.documentService.documentRepo.findByIds(ids);
documents.forEach((doc) => {
delete doc.state;
});
const docs = documents const docs = documents
.filter((doc) => !doc.isWikiHome) .filter((doc) => !doc.isWikiHome)
@ -612,6 +608,9 @@ export class WikiService {
const res = instanceToPlain(doc); const res = instanceToPlain(doc);
res.key = res.id; res.key = res.id;
return res; return res;
})
.map((item) => {
return lodash.omit(item, ['content', 'state']);
}); });
const docsWithCreateUser = await Promise.all( const docsWithCreateUser = await Promise.all(
@ -651,11 +650,9 @@ export class WikiService {
res.key = res.id; res.key = res.id;
res.label = res.title; res.label = res.title;
return res; return res;
}); })
.map((item) => {
docs.forEach((doc) => { return lodash.omit(item, ['content', 'state']);
delete doc.state;
delete doc.content;
}); });
return array2tree(docs); return array2tree(docs);
@ -676,7 +673,7 @@ export class WikiService {
userAgent, userAgent,
}); });
const views = await this.viewService.getDocumentTotalViews(res.id); const views = await this.viewService.getDocumentTotalViews(res.id);
return { ...instanceToPlain(res), views }; return { ...lodash.omit(instanceToPlain(res), ['state']), views };
} }
/** /**