2018-10-22 23:36:35 +03:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import Chart, { Obj } from './';
|
2019-02-07 21:31:33 +02:00
|
|
|
import User, { IUser, isLocalUser } from '../../models/user';
|
2019-02-24 05:23:31 +02:00
|
|
|
import { SchemaType } from '../../prelude/schema';
|
2018-10-22 23:36:35 +03:00
|
|
|
|
2019-02-24 05:23:31 +02:00
|
|
|
const logSchema = {
|
|
|
|
/**
|
|
|
|
* 集計期間時点での、全ユーザー数
|
|
|
|
*/
|
|
|
|
total: {
|
|
|
|
type: 'number' as 'number',
|
|
|
|
description: '集計期間時点での、全ユーザー数'
|
|
|
|
},
|
2018-10-22 23:36:35 +03:00
|
|
|
|
2019-02-24 05:23:31 +02:00
|
|
|
/**
|
|
|
|
* 増加したユーザー数
|
|
|
|
*/
|
|
|
|
inc: {
|
|
|
|
type: 'number' as 'number',
|
|
|
|
description: '増加したユーザー数'
|
|
|
|
},
|
2018-10-22 23:36:35 +03:00
|
|
|
|
2019-02-24 05:23:31 +02:00
|
|
|
/**
|
|
|
|
* 減少したユーザー数
|
|
|
|
*/
|
|
|
|
dec: {
|
|
|
|
type: 'number' as 'number',
|
|
|
|
description: '減少したユーザー数'
|
|
|
|
},
|
|
|
|
};
|
2018-10-22 23:36:35 +03:00
|
|
|
|
2019-02-24 05:23:31 +02:00
|
|
|
export const usersLogSchema = {
|
|
|
|
type: 'object' as 'object',
|
|
|
|
properties: {
|
|
|
|
local: {
|
|
|
|
type: 'object' as 'object',
|
|
|
|
properties: logSchema
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
type: 'object' as 'object',
|
|
|
|
properties: logSchema
|
|
|
|
},
|
|
|
|
}
|
2018-10-22 23:36:35 +03:00
|
|
|
};
|
|
|
|
|
2019-02-24 05:23:31 +02:00
|
|
|
type UsersLog = SchemaType<typeof usersLogSchema>;
|
|
|
|
|
2018-10-22 23:36:35 +03:00
|
|
|
class UsersChart extends Chart<UsersLog> {
|
|
|
|
constructor() {
|
|
|
|
super('users');
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
protected async getTemplate(init: boolean, latest?: UsersLog): Promise<UsersLog> {
|
|
|
|
const [localCount, remoteCount] = init ? await Promise.all([
|
|
|
|
User.count({ host: null }),
|
|
|
|
User.count({ host: { $ne: null } })
|
|
|
|
]) : [
|
|
|
|
latest ? latest.local.total : 0,
|
|
|
|
latest ? latest.remote.total : 0
|
|
|
|
];
|
|
|
|
|
|
|
|
return {
|
|
|
|
local: {
|
|
|
|
total: localCount,
|
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: remoteCount,
|
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public async update(user: IUser, isAdditional: boolean) {
|
|
|
|
const update: Obj = {};
|
|
|
|
|
|
|
|
update.total = isAdditional ? 1 : -1;
|
|
|
|
if (isAdditional) {
|
|
|
|
update.inc = 1;
|
|
|
|
} else {
|
|
|
|
update.dec = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.inc({
|
|
|
|
[isLocalUser(user) ? 'local' : 'remote']: update
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new UsersChart();
|