2019-04-07 07:50:36 -05:00
|
|
|
|
/**
|
|
|
|
|
* チャートエンジン
|
|
|
|
|
*
|
|
|
|
|
* Tests located in test/chart
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as nestedProperty from 'nested-property';
|
|
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
|
import Logger from '../logger';
|
2019-06-27 04:04:09 -05:00
|
|
|
|
import { Schema } from '../../misc/schema';
|
2019-04-07 07:50:36 -05:00
|
|
|
|
import { EntitySchema, getRepository, Repository, LessThan, MoreThanOrEqual } from 'typeorm';
|
|
|
|
|
import { isDuplicateKeyValueError } from '../../misc/is-duplicate-key-value-error';
|
2019-07-24 11:36:48 -05:00
|
|
|
|
import { DateUTC, isTimeSame, isTimeBefore, subtractTimespan } from '../../prelude/time';
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
const logger = new Logger('chart', 'white', process.env.NODE_ENV !== 'test');
|
|
|
|
|
|
|
|
|
|
export type Obj = { [key: string]: any };
|
|
|
|
|
|
|
|
|
|
export type DeepPartial<T> = {
|
|
|
|
|
[P in keyof T]?: DeepPartial<T[P]>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ArrayValue<T> = {
|
|
|
|
|
[P in keyof T]: T[P] extends number ? T[P][] : ArrayValue<T[P]>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Span = 'day' | 'hour';
|
|
|
|
|
|
|
|
|
|
type Log = {
|
|
|
|
|
id: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 集計のグループ
|
|
|
|
|
*/
|
|
|
|
|
group: string | null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 集計日時のUnixタイムスタンプ(秒)
|
|
|
|
|
*/
|
|
|
|
|
date: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 集計期間
|
|
|
|
|
*/
|
|
|
|
|
span: Span;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ユニークインクリメント用
|
|
|
|
|
*/
|
|
|
|
|
unique?: Record<string, any>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const camelToSnake = (str: string) => {
|
|
|
|
|
return str.replace(/([A-Z])/g, s => '_' + s.charAt(0).toLowerCase());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 様々なチャートの管理を司るクラス
|
|
|
|
|
*/
|
|
|
|
|
export default abstract class Chart<T extends Record<string, any>> {
|
|
|
|
|
private static readonly columnPrefix = '___';
|
|
|
|
|
private static readonly columnDot = '_';
|
|
|
|
|
|
|
|
|
|
private name: string;
|
|
|
|
|
public schema: Schema;
|
|
|
|
|
protected repository: Repository<Log>;
|
|
|
|
|
protected abstract genNewLog(latest: T): DeepPartial<T>;
|
2019-09-01 14:41:26 -05:00
|
|
|
|
protected abstract async fetchActual(group: string | null): Promise<DeepPartial<T>>;
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private static convertSchemaToFlatColumnDefinitions(schema: Schema) {
|
|
|
|
|
const columns = {} as any;
|
|
|
|
|
const flatColumns = (x: Obj, path?: string) => {
|
|
|
|
|
for (const [k, v] of Object.entries(x)) {
|
|
|
|
|
const p = path ? `${path}${this.columnDot}${k}` : k;
|
|
|
|
|
if (v.type === 'object') {
|
|
|
|
|
flatColumns(v.properties, p);
|
|
|
|
|
} else {
|
|
|
|
|
columns[this.columnPrefix + p] = {
|
2019-04-16 03:52:09 -05:00
|
|
|
|
type: 'bigint',
|
2019-04-07 07:50:36 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-04-12 11:43:22 -05:00
|
|
|
|
flatColumns(schema.properties!);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
return columns;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private static convertFlattenColumnsToObject(x: Record<string, number>) {
|
|
|
|
|
const obj = {} as any;
|
|
|
|
|
for (const k of Object.keys(x).filter(k => k.startsWith(Chart.columnPrefix))) {
|
|
|
|
|
// now k is ___x_y_z
|
|
|
|
|
const path = k.substr(Chart.columnPrefix.length).split(Chart.columnDot).join('.');
|
|
|
|
|
nestedProperty.set(obj, path, x[k]);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private static convertObjectToFlattenColumns(x: Record<string, any>) {
|
|
|
|
|
const columns = {} as Record<string, number>;
|
|
|
|
|
const flatten = (x: Obj, path?: string) => {
|
|
|
|
|
for (const [k, v] of Object.entries(x)) {
|
|
|
|
|
const p = path ? `${path}${this.columnDot}${k}` : k;
|
|
|
|
|
if (typeof v === 'object') {
|
|
|
|
|
flatten(v, p);
|
|
|
|
|
} else {
|
|
|
|
|
columns[this.columnPrefix + p] = v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
flatten(x);
|
|
|
|
|
return columns;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private static convertQuery(x: Record<string, any>) {
|
|
|
|
|
const query: Record<string, Function> = {};
|
|
|
|
|
|
|
|
|
|
const columns = Chart.convertObjectToFlattenColumns(x);
|
|
|
|
|
|
|
|
|
|
for (const [k, v] of Object.entries(columns)) {
|
|
|
|
|
if (v > 0) query[k] = () => `"${k}" + ${v}`;
|
2019-10-31 01:51:33 -05:00
|
|
|
|
if (v < 0) query[k] = () => `"${k}" - ${Math.abs(v)}`;
|
2019-04-07 07:50:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
2019-07-24 11:36:48 -05:00
|
|
|
|
private static dateToTimestamp(x: Date): Log['date'] {
|
|
|
|
|
return Math.floor(x.getTime() / 1000);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
public static schemaToEntity(name: string, schema: Schema): EntitySchema {
|
|
|
|
|
return new EntitySchema({
|
|
|
|
|
name: `__chart__${camelToSnake(name)}`,
|
|
|
|
|
columns: {
|
|
|
|
|
id: {
|
|
|
|
|
type: 'integer',
|
|
|
|
|
primary: true,
|
|
|
|
|
generated: true
|
|
|
|
|
},
|
|
|
|
|
date: {
|
|
|
|
|
type: 'integer',
|
|
|
|
|
},
|
|
|
|
|
group: {
|
|
|
|
|
type: 'varchar',
|
|
|
|
|
length: 128,
|
|
|
|
|
nullable: true
|
|
|
|
|
},
|
|
|
|
|
span: {
|
|
|
|
|
type: 'enum',
|
|
|
|
|
enum: ['hour', 'day']
|
|
|
|
|
},
|
|
|
|
|
unique: {
|
|
|
|
|
type: 'jsonb',
|
|
|
|
|
default: {}
|
|
|
|
|
},
|
|
|
|
|
...Chart.convertSchemaToFlatColumnDefinitions(schema)
|
|
|
|
|
},
|
2019-06-18 03:10:28 -05:00
|
|
|
|
indices: [{
|
|
|
|
|
columns: ['date']
|
|
|
|
|
}, {
|
|
|
|
|
columns: ['span']
|
|
|
|
|
}, {
|
|
|
|
|
columns: ['group']
|
|
|
|
|
}, {
|
|
|
|
|
columns: ['span', 'date']
|
2019-06-18 03:11:28 -05:00
|
|
|
|
}, {
|
|
|
|
|
columns: ['date', 'group']
|
2019-06-18 03:10:28 -05:00
|
|
|
|
}, {
|
|
|
|
|
columns: ['span', 'date', 'group']
|
|
|
|
|
}]
|
2019-04-07 07:50:36 -05:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(name: string, schema: Schema, grouped = false) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.schema = schema;
|
|
|
|
|
const entity = Chart.schemaToEntity(name, schema);
|
|
|
|
|
|
|
|
|
|
const keys = ['span', 'date'];
|
|
|
|
|
if (grouped) keys.push('group');
|
|
|
|
|
|
|
|
|
|
entity.options.uniques = [{
|
|
|
|
|
columns: keys
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
this.repository = getRepository<Log>(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
2019-04-12 11:43:22 -05:00
|
|
|
|
private getNewLog(latest: T | null): T {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
const log = latest ? this.genNewLog(latest) : {};
|
|
|
|
|
const flatColumns = (x: Obj, path?: string) => {
|
|
|
|
|
for (const [k, v] of Object.entries(x)) {
|
|
|
|
|
const p = path ? `${path}.${k}` : k;
|
|
|
|
|
if (v.type === 'object') {
|
|
|
|
|
flatColumns(v.properties, p);
|
|
|
|
|
} else {
|
|
|
|
|
if (nestedProperty.get(log, p) == null) {
|
|
|
|
|
nestedProperty.set(log, p, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-04-12 11:43:22 -05:00
|
|
|
|
flatColumns(this.schema.properties!);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
return log as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
|
private getCurrentDate(): [number, number, number, number] {
|
2019-07-24 11:36:48 -05:00
|
|
|
|
const now = new Date();
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
2019-07-24 11:36:48 -05:00
|
|
|
|
const y = now.getUTCFullYear();
|
|
|
|
|
const m = now.getUTCMonth();
|
|
|
|
|
const d = now.getUTCDate();
|
|
|
|
|
const h = now.getUTCHours();
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
return [y, m, d, h];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
2019-04-12 11:43:22 -05:00
|
|
|
|
private getLatestLog(span: Span, group: string | null = null): Promise<Log | null> {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
return this.repository.findOne({
|
|
|
|
|
group: group,
|
|
|
|
|
span: span
|
|
|
|
|
}, {
|
|
|
|
|
order: {
|
|
|
|
|
date: -1
|
|
|
|
|
}
|
2019-04-12 11:43:22 -05:00
|
|
|
|
}).then(x => x || null);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
2019-04-12 11:43:22 -05:00
|
|
|
|
private async getCurrentLog(span: Span, group: string | null = null): Promise<Log> {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
const [y, m, d, h] = this.getCurrentDate();
|
|
|
|
|
|
|
|
|
|
const current =
|
2019-07-24 11:36:48 -05:00
|
|
|
|
span == 'day' ? DateUTC([y, m, d]) :
|
|
|
|
|
span == 'hour' ? DateUTC([y, m, d, h]) :
|
2019-04-12 11:43:22 -05:00
|
|
|
|
null as never;
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
// 現在(今日または今のHour)のログ
|
|
|
|
|
const currentLog = await this.repository.findOne({
|
|
|
|
|
span: span,
|
2019-07-24 11:36:48 -05:00
|
|
|
|
date: Chart.dateToTimestamp(current),
|
2019-04-07 07:50:36 -05:00
|
|
|
|
...(group ? { group: group } : {})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ログがあればそれを返して終了
|
|
|
|
|
if (currentLog != null) {
|
|
|
|
|
return currentLog;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let log: Log;
|
|
|
|
|
let data: T;
|
|
|
|
|
|
|
|
|
|
// 集計期間が変わってから、初めてのチャート更新なら
|
|
|
|
|
// 最も最近のログを持ってくる
|
|
|
|
|
// * 例えば集計期間が「日」である場合で考えると、
|
|
|
|
|
// * 昨日何もチャートを更新するような出来事がなかった場合は、
|
|
|
|
|
// * ログがそもそも作られずドキュメントが存在しないということがあり得るため、
|
|
|
|
|
// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
|
|
|
|
|
const latest = await this.getLatestLog(span, group);
|
|
|
|
|
|
|
|
|
|
if (latest != null) {
|
|
|
|
|
const obj = Chart.convertFlattenColumnsToObject(
|
|
|
|
|
latest as Record<string, any>);
|
|
|
|
|
|
|
|
|
|
// 空ログデータを作成
|
2020-01-10 01:04:25 -06:00
|
|
|
|
data = this.getNewLog(obj);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
} else {
|
|
|
|
|
// ログが存在しなかったら
|
2020-02-17 11:27:18 -06:00
|
|
|
|
// (Misskeyインスタンスを建てて初めてのチャート更新時など)
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
// 初期ログデータを作成
|
2020-01-10 01:04:25 -06:00
|
|
|
|
data = this.getNewLog(null);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
logger.info(`${this.name}: Initial commit created`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 新規ログ挿入
|
|
|
|
|
log = await this.repository.save({
|
|
|
|
|
group: group,
|
|
|
|
|
span: span,
|
2019-07-24 11:36:48 -05:00
|
|
|
|
date: Chart.dateToTimestamp(current),
|
2019-04-07 07:50:36 -05:00
|
|
|
|
...Chart.convertObjectToFlattenColumns(data)
|
|
|
|
|
});
|
2020-01-10 00:55:40 -06:00
|
|
|
|
|
|
|
|
|
logger.info(`${this.name}: New commit created`);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
} catch (e) {
|
|
|
|
|
// duplicate key error
|
|
|
|
|
// 並列動作している他のチャートエンジンプロセスと処理が重なる場合がある
|
|
|
|
|
// その場合は再度最も新しいログを持ってくる
|
|
|
|
|
if (isDuplicateKeyValueError(e)) {
|
2019-04-12 11:43:22 -05:00
|
|
|
|
log = await this.getLatestLog(span, group) as Log;
|
2020-01-10 00:55:40 -06:00
|
|
|
|
logger.info(`${this.name}: Commit duplicated`);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
} else {
|
|
|
|
|
logger.error(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
2019-04-12 11:43:22 -05:00
|
|
|
|
protected commit(query: Record<string, Function>, group: string | null = null, uniqueKey?: string, uniqueValue?: string): Promise<any> {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
const update = async (log: Log) => {
|
|
|
|
|
// ユニークインクリメントの場合、指定のキーに指定の値が既に存在していたら弾く
|
|
|
|
|
if (
|
2019-04-12 11:43:22 -05:00
|
|
|
|
uniqueKey && log.unique &&
|
2019-04-07 07:50:36 -05:00
|
|
|
|
log.unique[uniqueKey] &&
|
|
|
|
|
log.unique[uniqueKey].includes(uniqueValue)
|
|
|
|
|
) return;
|
|
|
|
|
|
|
|
|
|
// ユニークインクリメントの指定のキーに値を追加
|
2019-04-12 11:43:22 -05:00
|
|
|
|
if (uniqueKey && log.unique) {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
if (log.unique[uniqueKey]) {
|
|
|
|
|
const sql = `jsonb_set("unique", '{${uniqueKey}}', ("unique"->>'${uniqueKey}')::jsonb || '["${uniqueValue}"]'::jsonb)`;
|
|
|
|
|
query['unique'] = () => sql;
|
|
|
|
|
} else {
|
|
|
|
|
const sql = `jsonb_set("unique", '{${uniqueKey}}', '["${uniqueValue}"]')`;
|
|
|
|
|
query['unique'] = () => sql;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ログ更新
|
|
|
|
|
await this.repository.createQueryBuilder()
|
|
|
|
|
.update()
|
|
|
|
|
.set(query)
|
|
|
|
|
.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)),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 14:41:26 -05:00
|
|
|
|
@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)),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
|
@autobind
|
2019-04-12 11:43:22 -05:00
|
|
|
|
protected async inc(inc: DeepPartial<T>, group: string | null = null): Promise<void> {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
await this.commit(Chart.convertQuery(inc as any), group);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
2019-04-12 11:43:22 -05:00
|
|
|
|
protected async incIfUnique(inc: DeepPartial<T>, key: string, value: string, group: string | null = null): Promise<void> {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
await this.commit(Chart.convertQuery(inc as any), group, key, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@autobind
|
2019-04-12 11:43:22 -05:00
|
|
|
|
public async getChart(span: Span, range: number, group: string | null = null): Promise<ArrayValue<T>> {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
const [y, m, d, h] = this.getCurrentDate();
|
|
|
|
|
|
|
|
|
|
const gt =
|
2019-07-24 11:36:48 -05:00
|
|
|
|
span == 'day' ? subtractTimespan(DateUTC([y, m, d]), range, 'days') :
|
|
|
|
|
span == 'hour' ? subtractTimespan(DateUTC([y, m, d, h]), range, 'hours') :
|
2019-04-12 11:43:22 -05:00
|
|
|
|
null as never;
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
// ログ取得
|
|
|
|
|
let logs = await this.repository.find({
|
|
|
|
|
where: {
|
|
|
|
|
group: group,
|
|
|
|
|
span: span,
|
2019-07-24 11:36:48 -05:00
|
|
|
|
date: MoreThanOrEqual(Chart.dateToTimestamp(gt))
|
2019-04-07 07:50:36 -05:00
|
|
|
|
},
|
|
|
|
|
order: {
|
|
|
|
|
date: -1
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 要求された範囲にログがひとつもなかったら
|
|
|
|
|
if (logs.length === 0) {
|
|
|
|
|
// もっとも新しいログを持ってくる
|
|
|
|
|
// (すくなくともひとつログが無いと隙間埋めできないため)
|
|
|
|
|
const recentLog = await this.repository.findOne({
|
|
|
|
|
group: group,
|
|
|
|
|
span: span
|
|
|
|
|
}, {
|
|
|
|
|
order: {
|
|
|
|
|
date: -1
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (recentLog) {
|
|
|
|
|
logs = [recentLog];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 要求された範囲の最も古い箇所に位置するログが存在しなかったら
|
2019-07-24 11:36:48 -05:00
|
|
|
|
} else if (!isTimeSame(new Date(logs[logs.length - 1].date * 1000), gt)) {
|
2019-04-07 07:50:36 -05:00
|
|
|
|
// 要求された範囲の最も古い箇所時点での最も新しいログを持ってきて末尾に追加する
|
|
|
|
|
// (隙間埋めできないため)
|
|
|
|
|
const outdatedLog = await this.repository.findOne({
|
|
|
|
|
group: group,
|
|
|
|
|
span: span,
|
2019-07-24 11:36:48 -05:00
|
|
|
|
date: LessThan(Chart.dateToTimestamp(gt))
|
2019-04-07 07:50:36 -05:00
|
|
|
|
}, {
|
|
|
|
|
order: {
|
|
|
|
|
date: -1
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (outdatedLog) {
|
|
|
|
|
logs.push(outdatedLog);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const chart: T[] = [];
|
|
|
|
|
|
|
|
|
|
// 整形
|
|
|
|
|
for (let i = (range - 1); i >= 0; i--) {
|
|
|
|
|
const current =
|
2019-07-24 11:36:48 -05:00
|
|
|
|
span == 'day' ? subtractTimespan(DateUTC([y, m, d]), i, 'days') :
|
|
|
|
|
span == 'hour' ? subtractTimespan(DateUTC([y, m, d, h]), i, 'hours') :
|
2019-04-12 11:43:22 -05:00
|
|
|
|
null as never;
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
2019-07-24 11:36:48 -05:00
|
|
|
|
const log = logs.find(l => isTimeSame(new Date(l.date * 1000), current));
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
|
|
if (log) {
|
|
|
|
|
const data = Chart.convertFlattenColumnsToObject(log as Record<string, any>);
|
|
|
|
|
chart.unshift(data);
|
|
|
|
|
} else {
|
|
|
|
|
// 隙間埋め
|
2019-07-24 11:36:48 -05:00
|
|
|
|
const latest = logs.find(l => isTimeBefore(new Date(l.date * 1000), current));
|
2019-04-07 07:50:36 -05:00
|
|
|
|
const data = latest ? Chart.convertFlattenColumnsToObject(latest as Record<string, any>) : null;
|
|
|
|
|
chart.unshift(this.getNewLog(data));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res: ArrayValue<T> = {} as any;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* [{ foo: 1, bar: 5 }, { foo: 2, bar: 6 }, { foo: 3, bar: 7 }]
|
|
|
|
|
* を
|
|
|
|
|
* { foo: [1, 2, 3], bar: [5, 6, 7] }
|
|
|
|
|
* にする
|
|
|
|
|
*/
|
|
|
|
|
const dive = (x: Obj, path?: string) => {
|
|
|
|
|
for (const [k, v] of Object.entries(x)) {
|
|
|
|
|
const p = path ? `${path}.${k}` : k;
|
|
|
|
|
if (typeof v == 'object') {
|
|
|
|
|
dive(v, p);
|
|
|
|
|
} else {
|
2019-07-05 08:32:15 -05:00
|
|
|
|
const values = chart.map(s => nestedProperty.get(s, p))
|
|
|
|
|
.map(v => parseInt(v, 10)); // TypeORMのバグ(?)で何故か数値カラムの値が文字列型になっているので数値に戻す
|
|
|
|
|
nestedProperty.set(res, p, values);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dive(chart[0]);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function convertLog(logSchema: Schema): Schema {
|
|
|
|
|
const v: Schema = JSON.parse(JSON.stringify(logSchema)); // copy
|
|
|
|
|
if (v.type === 'number') {
|
|
|
|
|
v.type = 'array';
|
|
|
|
|
v.items = {
|
2019-06-27 04:04:09 -05:00
|
|
|
|
type: 'number' as const,
|
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-07 07:50:36 -05:00
|
|
|
|
};
|
|
|
|
|
} else if (v.type === 'object') {
|
2019-04-12 11:43:22 -05:00
|
|
|
|
for (const k of Object.keys(v.properties!)) {
|
|
|
|
|
v.properties![k] = convertLog(v.properties![k]);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|