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

View File

@ -70,38 +70,41 @@ export class ViewService {
const now = Date.now();
const queryBuilder = this.viewRepo.createQueryBuilder('view');
queryBuilder.where('view.userId=:userId', { userId }).andWhere('view.createdAt BETWEEN :start AND :end', {
start: new Date(now - 3 * ONE_DAY),
end: new Date(now),
});
queryBuilder
.where('view.userId=:userId', { userId })
.andWhere('view.createdAt BETWEEN :start AND :end', {
start: new Date(now - 3 * ONE_DAY),
end: new Date(now),
})
.orderBy('view.createdAt', 'DESC');
const ret = await queryBuilder.getMany();
const map = {};
// const map = {};
ret.forEach((item) => {
const key = item.documentId;
if (!map[key]) {
map[key] = item;
}
const mapItem = map[key];
const isGreaterThan = new Date(mapItem.createdAt).valueOf() < new Date(item.createdAt).valueOf();
if (isGreaterThan) {
map[key] = item;
}
});
// ret.forEach((item) => {
// const key = item.documentId;
// if (!map[key]) {
// map[key] = item;
// }
// const mapItem = map[key];
// const isGreaterThan = new Date(mapItem.createdAt).valueOf() < new Date(item.createdAt).valueOf();
// if (isGreaterThan) {
// map[key] = item;
// }
// });
const res = Object.keys(map).map((documentId) => {
const res = ret.slice(0, 20).map((item) => {
return {
documentId,
visitedAt: map[documentId].createdAt,
documentId: item.documentId,
visitedAt: item.createdAt,
};
});
res.sort((a, b) => {
return -new Date(a.visitedAt).valueOf() + new Date(b.visitedAt).valueOf();
});
// res.sort((a, b) => {
// 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 { DocumentStatus, IPagination, WikiStatus, WikiUserRole } from '@think/domains';
import { instanceToPlain } from 'class-transformer';
import * as lodash from 'lodash';
import { Repository } from 'typeorm';
@Injectable()
@ -424,7 +425,7 @@ export class WikiService {
*/
async getWikiHomeDocument(user: OutUser, wikiId) {
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.forEach((doc) => {
delete doc.content;
delete doc.state;
});
const docs = documents
.filter((doc) => !doc.isWikiHome)
.map((doc) => {
@ -551,6 +547,9 @@ export class WikiService {
res.key = res.id;
res.label = res.title;
return res;
})
.map((item) => {
return lodash.omit(item, ['content', 'state']);
});
const docsWithCreateUser = await Promise.all(
@ -602,9 +601,6 @@ export class WikiService {
const ids = records.map((record) => record.documentId);
const documents = await this.documentService.documentRepo.findByIds(ids);
documents.forEach((doc) => {
delete doc.state;
});
const docs = documents
.filter((doc) => !doc.isWikiHome)
@ -612,6 +608,9 @@ export class WikiService {
const res = instanceToPlain(doc);
res.key = res.id;
return res;
})
.map((item) => {
return lodash.omit(item, ['content', 'state']);
});
const docsWithCreateUser = await Promise.all(
@ -651,13 +650,11 @@ export class WikiService {
res.key = res.id;
res.label = res.title;
return res;
})
.map((item) => {
return lodash.omit(item, ['content', 'state']);
});
docs.forEach((doc) => {
delete doc.state;
delete doc.content;
});
return array2tree(docs);
}
@ -676,7 +673,7 @@ export class WikiService {
userAgent,
});
const views = await this.viewService.getDocumentTotalViews(res.id);
return { ...instanceToPlain(res), views };
return { ...lodash.omit(instanceToPlain(res), ['state']), views };
}
/**