Skip to content

Commit

Permalink
fix: 完善访客记录逻辑&增加一个洗数据脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Aug 17, 2022
1 parent 57f0cab commit ea7f5c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async function bootstrap() {
await metaProvider.updateTotalWords();
// 测试用的
// const articleProvider = app.get(ArticleProvider);
// await articleProvider.washViewerInfoToVisitProvider();
// console.log('done');
// const res = await articleProvider.getAllImageLinks();
// console.log(res);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/server/src/provider/article/article.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ export class ArticleProvider {
}
}

async washViewerInfoToVisitProvider() {
// 用 visitProvider 里面的数据洗一下 article 的。
const articles = await this.getAll('list', true);
for (const a of articles) {
await this.visitProvider.rewriteToday(
`/post/${a.id}`,
a.viewer,
a.visited,
);
}
}

async importArticles(articles: Article[]) {
// 先获取一遍新的 id
// for (let i = 0; i < articles.length; i++) {
Expand Down
27 changes: 23 additions & 4 deletions packages/server/src/provider/visit/visit.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,31 @@ export class VisitProvider {
}
}

async rewriteToday(pathname: string, viewer: number, visited: number) {
const today = dayjs().format('YYYY-MM-DD');
const todayData = await this.findByDateAndPath(today, pathname);
if (todayData) {
await this.visitModel.updateOne(
{ _id: todayData.id },
{ viewer, visited },
);
} else {
await this.visitModel.create({
date: today,
viewer,
visited,
pathname,
});
}
}

async getLastData(pathname: string) {
const lastData = await this.visitModel
.findOne({ pathname })
.sort({ date: -1 });
if (lastData) {
return lastData;
.find({ pathname })
.sort({ date: -1 })
.limit(1);
if (lastData && lastData.length > 0) {
return lastData[0];
}
return null;
}
Expand Down

0 comments on commit ea7f5c1

Please sign in to comment.