2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-12-25 01:09:46 +01:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-02-16 15:09:41 +01:00
|
|
|
import { IsNull, MoreThan } from 'typeorm';
|
2022-12-25 01:09:46 +01:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import type Logger from '@/logger.js';
|
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { RetentionAggregationsRepository, UsersRepository } from '@/models/_.js';
|
2022-12-25 01:09:46 +01:00
|
|
|
import { deepClone } from '@/misc/clone.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-03-15 09:43:13 +01:00
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
2022-12-25 01:09:46 +01:00
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
2023-05-29 04:54:49 +02:00
|
|
|
import type * as Bull from 'bullmq';
|
2022-12-25 01:09:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AggregateRetentionProcessorService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.retentionAggregationsRepository)
|
|
|
|
private retentionAggregationsRepository: RetentionAggregationsRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
) {
|
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('aggregate-retention');
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-05-29 04:54:49 +02:00
|
|
|
public async process(): Promise<void> {
|
2022-12-25 01:09:46 +01:00
|
|
|
this.logger.info('Aggregating retention...');
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const dateKey = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
|
|
|
|
|
|
|
|
// 過去(だいたい)30日分のレコードを取得
|
|
|
|
const pastRecords = await this.retentionAggregationsRepository.findBy({
|
|
|
|
createdAt: MoreThan(new Date(Date.now() - (1000 * 60 * 60 * 24 * 31))),
|
|
|
|
});
|
|
|
|
|
|
|
|
// 今日登録したユーザーを全て取得
|
|
|
|
const targetUsers = await this.usersRepository.findBy({
|
|
|
|
host: IsNull(),
|
2023-10-16 03:45:22 +02:00
|
|
|
id: MoreThan(this.idService.gen(Date.now() - (1000 * 60 * 60 * 24))),
|
2022-12-25 01:09:46 +01:00
|
|
|
});
|
|
|
|
const targetUserIds = targetUsers.map(u => u.id);
|
|
|
|
|
2023-03-15 09:43:13 +01:00
|
|
|
try {
|
|
|
|
await this.retentionAggregationsRepository.insert({
|
2023-10-16 03:45:22 +02:00
|
|
|
id: this.idService.gen(),
|
2023-03-15 09:43:13 +01:00
|
|
|
createdAt: now,
|
|
|
|
updatedAt: now,
|
|
|
|
dateKey,
|
|
|
|
userIds: targetUserIds,
|
|
|
|
usersCount: targetUserIds.length,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (isDuplicateKeyValueError(err)) {
|
|
|
|
this.logger.succ('Skip because it has already been processed by another worker.');
|
2023-03-15 09:45:59 +01:00
|
|
|
return;
|
2023-03-15 09:43:13 +01:00
|
|
|
}
|
2023-03-15 09:45:59 +01:00
|
|
|
throw err;
|
2023-03-15 09:43:13 +01:00
|
|
|
}
|
2022-12-25 01:09:46 +01:00
|
|
|
|
2023-01-27 03:10:37 +01:00
|
|
|
// 今日活動したユーザーを全て取得
|
|
|
|
const activeUsers = await this.usersRepository.findBy({
|
|
|
|
host: IsNull(),
|
|
|
|
lastActiveDate: MoreThan(new Date(Date.now() - (1000 * 60 * 60 * 24))),
|
|
|
|
});
|
|
|
|
const activeUsersIds = activeUsers.map(u => u.id);
|
|
|
|
|
2022-12-25 01:09:46 +01:00
|
|
|
for (const record of pastRecords) {
|
2023-01-27 03:10:37 +01:00
|
|
|
const retention = record.userIds.filter(id => activeUsersIds.includes(id)).length;
|
2022-12-25 01:09:46 +01:00
|
|
|
|
|
|
|
const data = deepClone(record.data);
|
|
|
|
data[dateKey] = retention;
|
|
|
|
|
|
|
|
this.retentionAggregationsRepository.update(record.id, {
|
|
|
|
updatedAt: now,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.succ('Retention aggregated.');
|
|
|
|
}
|
|
|
|
}
|