summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2019-09-02 04:41:26 +0900
committerGitHub <noreply@github.com>2019-09-02 04:41:26 +0900
commit96b2267cb8ed9744f933e0682449b35922df527a (patch)
tree2c1b957b02ef54e494965c537ea7ebe0b9291728 /src
parentUpdate README.md [AUTOGEN] (#5377) (diff)
downloadsharkey-96b2267cb8ed9744f933e0682449b35922df527a.tar.gz
sharkey-96b2267cb8ed9744f933e0682449b35922df527a.tar.bz2
sharkey-96b2267cb8ed9744f933e0682449b35922df527a.zip
Chart resyncing (#5372)
* wip * Add test * Fix test * Insert moderation log * Add todo
Diffstat (limited to 'src')
-rw-r--r--src/server/api/endpoints/admin/resync-chart.ts21
-rw-r--r--src/services/chart/charts/classes/test.ts2
-rw-r--r--src/services/chart/core.ts20
3 files changed, 41 insertions, 2 deletions
diff --git a/src/server/api/endpoints/admin/resync-chart.ts b/src/server/api/endpoints/admin/resync-chart.ts
new file mode 100644
index 0000000000..7f4c5e03c8
--- /dev/null
+++ b/src/server/api/endpoints/admin/resync-chart.ts
@@ -0,0 +1,21 @@
+import define from '../../define';
+import { driveChart, notesChart, usersChart, instanceChart } from '../../../../services/chart';
+import { insertModerationLog } from '../../../../services/insert-moderation-log';
+
+export const meta = {
+ tags: ['admin'],
+
+ requireCredential: true,
+ requireModerator: true,
+};
+
+export default define(meta, async (ps, me) => {
+ insertModerationLog(me, 'chartResync');
+
+ driveChart.resync();
+ notesChart.resync();
+ usersChart.resync();
+ instanceChart.resync();
+
+ // TODO: ユーザーごとのチャートもキューに入れて更新する
+});
diff --git a/src/services/chart/charts/classes/test.ts b/src/services/chart/charts/classes/test.ts
index 57c22822f2..0ca63d174c 100644
--- a/src/services/chart/charts/classes/test.ts
+++ b/src/services/chart/charts/classes/test.ts
@@ -6,7 +6,7 @@ import { name, schema } from '../schemas/test';
type TestLog = SchemaType<typeof schema>;
export default class TestChart extends Chart<TestLog> {
- private total = 0;
+ public total = 0; // publicにするのはテストのため
constructor() {
super(name, schema);
diff --git a/src/services/chart/core.ts b/src/services/chart/core.ts
index 60ba1ebb46..9dc250f753 100644
--- a/src/services/chart/core.ts
+++ b/src/services/chart/core.ts
@@ -65,7 +65,7 @@ export default abstract class Chart<T extends Record<string, any>> {
public schema: Schema;
protected repository: Repository<Log>;
protected abstract genNewLog(latest: T): DeepPartial<T>;
- protected abstract async fetchActual(group?: string): Promise<DeepPartial<T>>;
+ protected abstract async fetchActual(group: string | null): Promise<DeepPartial<T>>;
@autobind
private static convertSchemaToFlatColumnDefinitions(schema: Schema) {
@@ -342,6 +342,24 @@ export default abstract class Chart<T extends Record<string, any>> {
}
@autobind
+ public async resync(group: string | null = null): Promise<any> {
+ const data = await this.fetchActual(group);
+
+ const update = async (log: Log) => {
+ await this.repository.createQueryBuilder()
+ .update()
+ .set(Chart.convertObjectToFlattenColumns(data))
+ .where('id = :id', { id: log.id })
+ .execute();
+ };
+
+ return Promise.all([
+ this.getCurrentLog('day', group).then(log => update(log)),
+ this.getCurrentLog('hour', group).then(log => update(log)),
+ ]);
+ }
+
+ @autobind
protected async inc(inc: DeepPartial<T>, group: string | null = null): Promise<void> {
await this.commit(Chart.convertQuery(inc as any), group);
}