From aba85b977dfc868c1a65ce06ed58ea59d0371f7f Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 8 Feb 2019 04:31:33 +0900 Subject: Refactoring: Move chart dir into services dir --- src/services/chart/federation.ts | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/services/chart/federation.ts (limited to 'src/services/chart/federation.ts') diff --git a/src/services/chart/federation.ts b/src/services/chart/federation.ts new file mode 100644 index 0000000000..20da7a7421 --- /dev/null +++ b/src/services/chart/federation.ts @@ -0,0 +1,66 @@ +import autobind from 'autobind-decorator'; +import Chart, { Obj } from '.'; +import Instance from '../../models/instance'; + +/** + * フェデレーションに関するチャート + */ +type FederationLog = { + instance: { + /** + * インスタンス数の合計 + */ + total: number; + + /** + * 増加インスタンス数 + */ + inc: number; + + /** + * 減少インスタンス数 + */ + dec: number; + }; +}; + +class FederationChart extends Chart { + constructor() { + super('federation'); + } + + @autobind + protected async getTemplate(init: boolean, latest?: FederationLog): Promise { + const [total] = init ? await Promise.all([ + Instance.count({}) + ]) : [ + latest ? latest.instance.total : 0 + ]; + + return { + instance: { + total: total, + inc: 0, + dec: 0 + } + }; + } + + @autobind + public async update(isAdditional: boolean) { + const update: Obj = {}; + + update.total = isAdditional ? 1 : -1; + if (isAdditional) { + update.inc = 1; + } else { + update.dec = 1; + } + + await this.inc({ + instance: update + }); + } +} + +export default new FederationChart(); -- cgit v1.2.3-freya