2019-04-07 12:50:36 +00:00
|
|
|
/**
|
|
|
|
* チャートエンジン
|
|
|
|
*
|
|
|
|
* Tests located in test/chart
|
|
|
|
*/
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
import * as nestedProperty from "nested-property";
|
|
|
|
import Logger from "../logger.js";
|
|
|
|
import type { Repository } from "typeorm";
|
|
|
|
import { EntitySchema, LessThan, Between } from "typeorm";
|
|
|
|
import {
|
|
|
|
dateUTC,
|
|
|
|
isTimeSame,
|
|
|
|
isTimeBefore,
|
|
|
|
subtractTime,
|
|
|
|
addTime,
|
|
|
|
} from "@/prelude/time.js";
|
|
|
|
import { getChartInsertLock } from "@/misc/app-lock.js";
|
|
|
|
import { db } from "@/db/postgre.js";
|
|
|
|
|
|
|
|
const logger = new Logger("chart", "white", process.env.NODE_ENV !== "test");
|
|
|
|
|
|
|
|
const columnPrefix = "___" as const;
|
|
|
|
const uniqueTempColumnPrefix = "unique_temp___" as const;
|
|
|
|
const columnDot = "_" as const;
|
|
|
|
|
|
|
|
type Schema = Record<
|
|
|
|
string,
|
|
|
|
{
|
|
|
|
uniqueIncrement?: boolean;
|
2022-02-12 16:39:58 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
intersection?: string[] | ReadonlyArray<string>;
|
2022-02-12 16:39:58 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
range?: "big" | "small" | "medium";
|
2022-02-12 16:39:58 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
// previousな値を引き継ぐかどうか
|
|
|
|
accumulate?: boolean;
|
|
|
|
}
|
|
|
|
>;
|
2022-02-12 16:39:58 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
type KeyToColumnName<T extends string> = T extends `${infer R1}.${infer R2}`
|
|
|
|
? `${R1}${typeof columnDot}${KeyToColumnName<R2>}`
|
|
|
|
: T;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-02-12 16:39:58 +00:00
|
|
|
type Columns<S extends Schema> = {
|
2023-01-13 04:40:33 +00:00
|
|
|
[K in
|
|
|
|
keyof S as `${typeof columnPrefix}${KeyToColumnName<string & K>}`]: number;
|
2022-02-12 16:39:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type TempColumnsForUnique<S extends Schema> = {
|
2023-01-13 04:40:33 +00:00
|
|
|
[K in
|
|
|
|
keyof S as `${typeof uniqueTempColumnPrefix}${KeyToColumnName<
|
|
|
|
string & K
|
|
|
|
>}`]: S[K]["uniqueIncrement"] extends true ? string[] : never;
|
2022-02-12 16:39:58 +00:00
|
|
|
};
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
type RawRecord<S extends Schema> = {
|
2019-04-07 12:50:36 +00:00
|
|
|
id: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 集計のグループ
|
|
|
|
*/
|
2021-12-14 09:12:37 +00:00
|
|
|
group?: string | null;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 集計日時のUnixタイムスタンプ(秒)
|
|
|
|
*/
|
|
|
|
date: number;
|
2023-01-13 04:40:33 +00:00
|
|
|
} & TempColumnsForUnique<S> &
|
|
|
|
Columns<S>;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2021-12-14 09:12:37 +00:00
|
|
|
const camelToSnake = (str: string): string => {
|
2023-01-13 04:40:33 +00:00
|
|
|
return str.replace(/([A-Z])/g, (s) => `_${s.charAt(0).toLowerCase()}`);
|
2019-04-07 12:50:36 +00:00
|
|
|
};
|
|
|
|
|
2021-03-18 02:17:05 +00:00
|
|
|
const removeDuplicates = (array: any[]) => Array.from(new Set(array));
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
type Commit<S extends Schema> = {
|
2023-01-13 04:40:33 +00:00
|
|
|
[K in keyof S]?: S[K]["uniqueIncrement"] extends true ? string[] : number;
|
2022-02-05 15:13:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type KVs<S extends Schema> = {
|
|
|
|
[K in keyof S]: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ChartResult<T extends Schema> = {
|
|
|
|
[P in keyof T]: number[];
|
|
|
|
};
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (
|
|
|
|
x: infer R,
|
|
|
|
) => any
|
|
|
|
? R
|
|
|
|
: never;
|
2022-02-18 13:29:23 +00:00
|
|
|
|
|
|
|
type UnflattenSingleton<K extends string, V> = K extends `${infer A}.${infer B}`
|
2023-01-13 04:40:33 +00:00
|
|
|
? {
|
|
|
|
[_ in A]: UnflattenSingleton<B, V>;
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
[_ in K]: V;
|
|
|
|
};
|
2022-02-18 13:29:23 +00:00
|
|
|
|
|
|
|
type Unflatten<T extends Record<string, any>> = UnionToIntersection<
|
|
|
|
{
|
|
|
|
[K in Extract<keyof T, string>]: UnflattenSingleton<K, T[K]>;
|
|
|
|
}[Extract<keyof T, string>]
|
|
|
|
>;
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
type ToJsonSchema<S> = {
|
2023-01-13 04:40:33 +00:00
|
|
|
type: "object";
|
2022-02-19 05:05:32 +00:00
|
|
|
properties: {
|
2023-01-13 04:40:33 +00:00
|
|
|
[K in keyof S]: S[K] extends number[]
|
|
|
|
? { type: "array"; items: { type: "number" } }
|
|
|
|
: ToJsonSchema<S[K]>;
|
|
|
|
};
|
2022-02-19 05:05:32 +00:00
|
|
|
required: (keyof S)[];
|
|
|
|
};
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
export function getJsonSchema<S extends Schema>(
|
|
|
|
schema: S,
|
|
|
|
): ToJsonSchema<Unflatten<ChartResult<S>>> {
|
2022-05-29 06:15:52 +00:00
|
|
|
const jsonSchema = {
|
2023-01-13 04:40:33 +00:00
|
|
|
type: "object",
|
2022-05-29 06:15:52 +00:00
|
|
|
properties: {} as Record<string, unknown>,
|
|
|
|
required: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const k in schema) {
|
|
|
|
jsonSchema.properties[k] = {
|
2023-01-13 04:40:33 +00:00
|
|
|
type: "array",
|
|
|
|
items: { type: "number" },
|
2022-02-19 05:05:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-29 06:15:52 +00:00
|
|
|
return jsonSchema as ToJsonSchema<Unflatten<ChartResult<S>>>;
|
2022-02-19 05:05:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
/**
|
|
|
|
* 様々なチャートの管理を司るクラス
|
|
|
|
*/
|
2021-12-14 09:12:37 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-05 15:13:52 +00:00
|
|
|
export default abstract class Chart<T extends Schema> {
|
|
|
|
public schema: T;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
private name: string;
|
2021-06-30 15:50:19 +00:00
|
|
|
private buffer: {
|
2022-02-05 15:13:52 +00:00
|
|
|
diff: Commit<T>;
|
2021-03-18 02:17:05 +00:00
|
|
|
group: string | null;
|
|
|
|
}[] = [];
|
2022-02-12 16:39:58 +00:00
|
|
|
// ↓にしたいけどfindOneとかで型エラーになる
|
|
|
|
//private repositoryForHour: Repository<RawRecord<T>>;
|
|
|
|
//private repositoryForDay: Repository<RawRecord<T>>;
|
2023-01-13 04:40:33 +00:00
|
|
|
private repositoryForHour: Repository<{
|
|
|
|
id: number;
|
|
|
|
group?: string | null;
|
|
|
|
date: number;
|
|
|
|
}>;
|
|
|
|
private repositoryForDay: Repository<{
|
|
|
|
id: number;
|
|
|
|
group?: string | null;
|
|
|
|
date: number;
|
|
|
|
}>;
|
2021-03-18 02:17:05 +00:00
|
|
|
|
2022-02-10 08:45:12 +00:00
|
|
|
/**
|
|
|
|
* 1日に一回程度実行されれば良いような計算処理を入れる(主にCASCADE削除などアプリケーション側で感知できない変動によるズレの修正用)
|
|
|
|
*/
|
|
|
|
protected abstract tickMajor(group: string | null): Promise<Partial<KVs<T>>>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 少なくとも最小スパン内に1回は実行されて欲しい計算処理を入れる
|
|
|
|
*/
|
|
|
|
protected abstract tickMinor(group: string | null): Promise<Partial<KVs<T>>>;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
private static convertSchemaToColumnDefinitions(
|
|
|
|
schema: Schema,
|
|
|
|
): Record<string, { type: string; array?: boolean; default?: any }> {
|
|
|
|
const columns = {} as Record<
|
|
|
|
string,
|
|
|
|
{ type: string; array?: boolean; default?: any }
|
|
|
|
>;
|
2022-02-05 15:13:52 +00:00
|
|
|
for (const [k, v] of Object.entries(schema)) {
|
2023-01-13 04:40:33 +00:00
|
|
|
const name = k.replaceAll(".", columnDot);
|
|
|
|
const type =
|
|
|
|
v.range === "big"
|
|
|
|
? "bigint"
|
|
|
|
: v.range === "small"
|
|
|
|
? "smallint"
|
|
|
|
: "integer";
|
2022-02-05 15:13:52 +00:00
|
|
|
if (v.uniqueIncrement) {
|
|
|
|
columns[uniqueTempColumnPrefix + name] = {
|
2023-01-13 04:40:33 +00:00
|
|
|
type: "varchar",
|
2022-02-05 15:13:52 +00:00
|
|
|
array: true,
|
2023-01-13 04:40:33 +00:00
|
|
|
default: "{}",
|
2022-02-05 15:13:52 +00:00
|
|
|
};
|
|
|
|
columns[columnPrefix + name] = {
|
|
|
|
type,
|
|
|
|
default: 0,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
columns[columnPrefix + name] = {
|
|
|
|
type,
|
|
|
|
default: 0,
|
|
|
|
};
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
private static dateToTimestamp(x: Date): number {
|
2019-07-24 16:36:48 +00:00
|
|
|
return Math.floor(x.getTime() / 1000);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
private static parseDate(
|
|
|
|
date: Date,
|
|
|
|
): [number, number, number, number, number, number, number] {
|
2020-03-06 16:04:36 +00:00
|
|
|
const y = date.getUTCFullYear();
|
|
|
|
const m = date.getUTCMonth();
|
|
|
|
const d = date.getUTCDate();
|
|
|
|
const h = date.getUTCHours();
|
2020-03-07 02:23:31 +00:00
|
|
|
const _m = date.getUTCMinutes();
|
|
|
|
const _s = date.getUTCSeconds();
|
|
|
|
const _ms = date.getUTCMilliseconds();
|
2020-03-06 16:04:36 +00:00
|
|
|
|
2020-03-07 02:23:31 +00:00
|
|
|
return [y, m, d, h, _m, _s, _ms];
|
2020-03-06 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 02:23:31 +00:00
|
|
|
private static getCurrentDate() {
|
|
|
|
return Chart.parseDate(new Date());
|
2020-03-06 16:04:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
public static schemaToEntity(
|
|
|
|
name: string,
|
|
|
|
schema: Schema,
|
|
|
|
grouped = false,
|
|
|
|
): {
|
|
|
|
hour: EntitySchema;
|
|
|
|
day: EntitySchema;
|
2021-12-14 09:12:37 +00:00
|
|
|
} {
|
2023-01-13 04:40:33 +00:00
|
|
|
const createEntity = (span: "hour" | "day"): EntitySchema =>
|
|
|
|
new EntitySchema({
|
|
|
|
name:
|
|
|
|
span === "hour"
|
|
|
|
? `__chart__${camelToSnake(name)}`
|
|
|
|
: span === "day"
|
|
|
|
? `__chart_day__${camelToSnake(name)}`
|
|
|
|
: (new Error("not happen") as never),
|
|
|
|
columns: {
|
|
|
|
id: {
|
|
|
|
type: "integer",
|
|
|
|
primary: true,
|
|
|
|
generated: true,
|
|
|
|
},
|
|
|
|
date: {
|
|
|
|
type: "integer",
|
|
|
|
},
|
|
|
|
...(grouped
|
|
|
|
? {
|
|
|
|
group: {
|
|
|
|
type: "varchar",
|
|
|
|
length: 128,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...Chart.convertSchemaToColumnDefinitions(schema),
|
2019-04-07 12:50:36 +00:00
|
|
|
},
|
2023-01-13 04:40:33 +00:00
|
|
|
indices: [
|
|
|
|
{
|
|
|
|
columns: grouped ? ["date", "group"] : ["date"],
|
|
|
|
unique: true,
|
2021-12-14 09:12:37 +00:00
|
|
|
},
|
2023-01-13 04:40:33 +00:00
|
|
|
],
|
|
|
|
uniques: [
|
|
|
|
{
|
|
|
|
columns: grouped ? ["date", "group"] : ["date"],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
relations: {
|
|
|
|
/* TODO
|
2021-12-14 09:12:37 +00:00
|
|
|
group: {
|
|
|
|
target: () => Foo,
|
|
|
|
type: 'many-to-one',
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
},
|
|
|
|
*/
|
2023-01-13 04:40:33 +00:00
|
|
|
},
|
|
|
|
});
|
2021-12-14 09:12:37 +00:00
|
|
|
|
|
|
|
return {
|
2023-01-13 04:40:33 +00:00
|
|
|
hour: createEntity("hour"),
|
|
|
|
day: createEntity("day"),
|
2021-12-14 09:12:37 +00:00
|
|
|
};
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
constructor(name: string, schema: T, grouped = false) {
|
2019-04-07 12:50:36 +00:00
|
|
|
this.name = name;
|
|
|
|
this.schema = schema;
|
|
|
|
|
2021-12-14 09:12:37 +00:00
|
|
|
const { hour, day } = Chart.schemaToEntity(name, schema, grouped);
|
2023-01-13 04:40:33 +00:00
|
|
|
this.repositoryForHour = db.getRepository<{
|
|
|
|
id: number;
|
|
|
|
group?: string | null;
|
|
|
|
date: number;
|
|
|
|
}>(hour);
|
|
|
|
this.repositoryForDay = db.getRepository<{
|
|
|
|
id: number;
|
|
|
|
group?: string | null;
|
|
|
|
date: number;
|
|
|
|
}>(day);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
private convertRawRecord(x: RawRecord<T>): KVs<T> {
|
2022-02-12 16:39:58 +00:00
|
|
|
const kvs = {} as Record<string, number>;
|
2023-01-13 04:40:33 +00:00
|
|
|
for (const k of Object.keys(x).filter((k) =>
|
|
|
|
k.startsWith(columnPrefix),
|
|
|
|
) as (keyof Columns<T>)[]) {
|
|
|
|
kvs[
|
|
|
|
(k as string).substr(columnPrefix.length).split(columnDot).join(".")
|
|
|
|
] = x[k];
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
2022-02-12 16:39:58 +00:00
|
|
|
return kvs as KVs<T>;
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getNewLog(latest: KVs<T> | null): KVs<T> {
|
|
|
|
const log = {} as Record<keyof T, number>;
|
2023-01-13 04:40:33 +00:00
|
|
|
for (const [k, v] of Object.entries(this.schema) as [
|
|
|
|
keyof typeof this["schema"],
|
|
|
|
this["schema"][string],
|
|
|
|
][]) {
|
2022-02-05 15:13:52 +00:00
|
|
|
if (v.accumulate && latest) {
|
|
|
|
log[k] = latest[k];
|
|
|
|
} else {
|
|
|
|
log[k] = 0;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
|
|
|
return log as KVs<T>;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
private getLatestLog(
|
|
|
|
group: string | null,
|
|
|
|
span: "hour" | "day",
|
|
|
|
): Promise<RawRecord<T> | null> {
|
2021-12-14 09:12:37 +00:00
|
|
|
const repository =
|
2023-01-13 04:40:33 +00:00
|
|
|
span === "hour"
|
|
|
|
? this.repositoryForHour
|
|
|
|
: span === "day"
|
|
|
|
? this.repositoryForDay
|
|
|
|
: (new Error("not happen") as never);
|
|
|
|
|
|
|
|
return repository
|
|
|
|
.findOne({
|
|
|
|
where: group
|
|
|
|
? {
|
|
|
|
group: group,
|
|
|
|
}
|
|
|
|
: {},
|
|
|
|
order: {
|
|
|
|
date: -1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((x) => x ?? null) as Promise<RawRecord<T> | null>;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 09:12:37 +00:00
|
|
|
/**
|
|
|
|
* 現在(=今のHour or Day)のログをデータベースから探して、あればそれを返し、なければ作成して返します。
|
|
|
|
*/
|
2023-01-13 04:40:33 +00:00
|
|
|
private async claimCurrentLog(
|
|
|
|
group: string | null,
|
|
|
|
span: "hour" | "day",
|
|
|
|
): Promise<RawRecord<T>> {
|
2020-03-06 16:04:36 +00:00
|
|
|
const [y, m, d, h] = Chart.getCurrentDate();
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2021-12-14 09:12:37 +00:00
|
|
|
const current = dateUTC(
|
2023-01-13 04:40:33 +00:00
|
|
|
span === "hour"
|
|
|
|
? [y, m, d, h]
|
|
|
|
: span === "day"
|
|
|
|
? [y, m, d]
|
|
|
|
: (new Error("not happen") as never),
|
|
|
|
);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2021-12-14 09:12:37 +00:00
|
|
|
const repository =
|
2023-01-13 04:40:33 +00:00
|
|
|
span === "hour"
|
|
|
|
? this.repositoryForHour
|
|
|
|
: span === "day"
|
|
|
|
? this.repositoryForDay
|
|
|
|
: (new Error("not happen") as never);
|
2021-12-14 09:12:37 +00:00
|
|
|
|
|
|
|
// 現在(=今のHour or Day)のログ
|
2023-01-13 04:40:33 +00:00
|
|
|
const currentLog = (await repository.findOneBy({
|
2019-07-24 16:36:48 +00:00
|
|
|
date: Chart.dateToTimestamp(current),
|
2021-12-09 14:58:30 +00:00
|
|
|
...(group ? { group: group } : {}),
|
2023-01-13 04:40:33 +00:00
|
|
|
})) as RawRecord<T> | undefined;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
// ログがあればそれを返して終了
|
|
|
|
if (currentLog != null) {
|
|
|
|
return currentLog;
|
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
let log: RawRecord<T>;
|
|
|
|
let data: KVs<T>;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
// 集計期間が変わってから、初めてのチャート更新なら
|
|
|
|
// 最も最近のログを持ってくる
|
|
|
|
// * 例えば集計期間が「日」である場合で考えると、
|
|
|
|
// * 昨日何もチャートを更新するような出来事がなかった場合は、
|
|
|
|
// * ログがそもそも作られずドキュメントが存在しないということがあり得るため、
|
|
|
|
// * 「昨日の」と決め打ちせずに「もっとも最近の」とします
|
2021-12-14 09:12:37 +00:00
|
|
|
const latest = await this.getLatestLog(group, span);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
if (latest != null) {
|
|
|
|
// 空ログデータを作成
|
2022-02-05 15:13:52 +00:00
|
|
|
data = this.getNewLog(this.convertRawRecord(latest));
|
2019-04-07 12:50:36 +00:00
|
|
|
} else {
|
|
|
|
// ログが存在しなかったら
|
2020-02-17 17:27:18 +00:00
|
|
|
// (Misskeyインスタンスを建てて初めてのチャート更新時など)
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
// 初期ログデータを作成
|
2020-01-10 07:04:25 +00:00
|
|
|
data = this.getNewLog(null);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
logger.info(
|
|
|
|
`${
|
|
|
|
this.name + (group ? `:${group}` : "")
|
|
|
|
}(${span}): Initial commit created`,
|
|
|
|
);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 13:33:54 +00:00
|
|
|
const date = Chart.dateToTimestamp(current);
|
2023-01-13 04:40:33 +00:00
|
|
|
const lockKey = group
|
|
|
|
? `${this.name}:${date}:${span}:${group}`
|
|
|
|
: `${this.name}:${date}:${span}`;
|
2020-03-06 13:33:54 +00:00
|
|
|
|
|
|
|
const unlock = await getChartInsertLock(lockKey);
|
2019-04-07 12:50:36 +00:00
|
|
|
try {
|
2020-03-06 13:33:54 +00:00
|
|
|
// ロック内でもう1回チェックする
|
2023-01-13 04:40:33 +00:00
|
|
|
const currentLog = (await repository.findOneBy({
|
2020-03-06 13:33:54 +00:00
|
|
|
date: date,
|
2021-12-09 14:58:30 +00:00
|
|
|
...(group ? { group: group } : {}),
|
2023-01-13 04:40:33 +00:00
|
|
|
})) as RawRecord<T> | undefined;
|
2020-03-06 13:33:54 +00:00
|
|
|
|
|
|
|
// ログがあればそれを返して終了
|
|
|
|
if (currentLog != null) return currentLog;
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
const columns = {} as Record<string, number | unknown[]>;
|
|
|
|
for (const [k, v] of Object.entries(data)) {
|
2023-01-13 04:40:33 +00:00
|
|
|
const name = k.replaceAll(".", columnDot);
|
2022-02-05 15:13:52 +00:00
|
|
|
columns[columnPrefix + name] = v;
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
// 新規ログ挿入
|
2023-01-13 04:40:33 +00:00
|
|
|
log = (await repository
|
|
|
|
.insert({
|
|
|
|
date: date,
|
|
|
|
...(group ? { group: group } : {}),
|
|
|
|
...columns,
|
|
|
|
})
|
|
|
|
.then((x) =>
|
|
|
|
repository.findOneByOrFail(x.identifiers[0]),
|
|
|
|
)) as RawRecord<T>;
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
`${
|
|
|
|
this.name + (group ? `:${group}` : "")
|
|
|
|
}(${span}): New commit created`,
|
|
|
|
);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2020-03-06 13:33:54 +00:00
|
|
|
return log;
|
|
|
|
} finally {
|
|
|
|
unlock();
|
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
protected commit(diff: Commit<T>, group: string | null = null): void {
|
|
|
|
for (const [k, v] of Object.entries(diff)) {
|
2023-01-13 04:40:33 +00:00
|
|
|
if (v == null || v === 0 || (Array.isArray(v) && v.length === 0))
|
|
|
|
diff[k] = undefined;
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
2021-06-30 15:50:19 +00:00
|
|
|
this.buffer.push({
|
2023-01-13 04:40:33 +00:00
|
|
|
diff,
|
|
|
|
group,
|
2021-03-18 02:17:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-12 01:52:10 +00:00
|
|
|
public async save(): Promise<void> {
|
2021-06-30 15:50:19 +00:00
|
|
|
if (this.buffer.length === 0) {
|
2021-03-18 02:17:05 +00:00
|
|
|
logger.info(`${this.name}: Write skipped`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:50:19 +00:00
|
|
|
// TODO: 前の時間のログがbufferにあった場合のハンドリング
|
2021-03-18 02:17:05 +00:00
|
|
|
// 例えば、save が20分ごとに行われるとして、前回行われたのは 01:50 だったとする。
|
2021-06-30 15:50:19 +00:00
|
|
|
// 次に save が行われるのは 02:10 ということになるが、もし 01:55 に新規ログが buffer に追加されたとすると、
|
2021-03-18 02:17:05 +00:00
|
|
|
// そのログは本来は 01:00~ のログとしてDBに保存されて欲しいのに、02:00~ のログ扱いになってしまう。
|
|
|
|
// これを回避するための実装は複雑になりそうなため、一旦保留。
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const update = async (
|
|
|
|
logHour: RawRecord<T>,
|
|
|
|
logDay: RawRecord<T>,
|
|
|
|
): Promise<void> => {
|
2022-02-12 16:39:58 +00:00
|
|
|
const finalDiffs = {} as Record<string, number | string[]>;
|
2021-03-18 02:17:05 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
for (const diff of this.buffer
|
|
|
|
.filter((q) => q.group == null || q.group === logHour.group)
|
|
|
|
.map((q) => q.diff)) {
|
2022-02-05 15:13:52 +00:00
|
|
|
for (const [k, v] of Object.entries(diff)) {
|
2021-03-18 02:17:05 +00:00
|
|
|
if (finalDiffs[k] == null) {
|
|
|
|
finalDiffs[k] = v;
|
|
|
|
} else {
|
2023-01-13 04:40:33 +00:00
|
|
|
if (typeof finalDiffs[k] === "number") {
|
2021-03-18 02:17:05 +00:00
|
|
|
(finalDiffs[k] as number) += v as number;
|
|
|
|
} else {
|
2023-01-13 04:40:33 +00:00
|
|
|
(finalDiffs[k] as string[]) = (finalDiffs[k] as string[]).concat(
|
|
|
|
v,
|
|
|
|
);
|
2021-03-18 02:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const queryForHour: Record<keyof RawRecord<T>, number | (() => string)> =
|
|
|
|
{} as any;
|
|
|
|
const queryForDay: Record<keyof RawRecord<T>, number | (() => string)> =
|
|
|
|
{} as any;
|
2022-02-05 15:13:52 +00:00
|
|
|
for (const [k, v] of Object.entries(finalDiffs)) {
|
2023-01-13 04:40:33 +00:00
|
|
|
if (typeof v === "number") {
|
|
|
|
const name = (columnPrefix +
|
|
|
|
k.replaceAll(".", columnDot)) as keyof Columns<T>;
|
2022-02-05 15:13:52 +00:00
|
|
|
if (v > 0) queryForHour[name] = () => `"${name}" + ${v}`;
|
|
|
|
if (v < 0) queryForHour[name] = () => `"${name}" - ${Math.abs(v)}`;
|
|
|
|
if (v > 0) queryForDay[name] = () => `"${name}" + ${v}`;
|
|
|
|
if (v < 0) queryForDay[name] = () => `"${name}" - ${Math.abs(v)}`;
|
2023-01-13 04:40:33 +00:00
|
|
|
} else if (Array.isArray(v) && v.length > 0) {
|
|
|
|
// ユニークインクリメント
|
|
|
|
const tempColumnName = (uniqueTempColumnPrefix +
|
|
|
|
k.replaceAll(".", columnDot)) as keyof TempColumnsForUnique<T>;
|
2022-02-05 15:13:52 +00:00
|
|
|
// TODO: item をSQLエスケープ
|
2023-01-13 04:40:33 +00:00
|
|
|
const itemsForHour = v
|
|
|
|
.filter((item) => !logHour[tempColumnName].includes(item))
|
|
|
|
.map((item) => `"${item}"`);
|
|
|
|
const itemsForDay = v
|
|
|
|
.filter((item) => !logDay[tempColumnName].includes(item))
|
|
|
|
.map((item) => `"${item}"`);
|
|
|
|
if (itemsForHour.length > 0)
|
|
|
|
queryForHour[tempColumnName] = () =>
|
|
|
|
`array_cat("${tempColumnName}", '{${itemsForHour.join(
|
|
|
|
",",
|
|
|
|
)}}'::varchar[])`;
|
|
|
|
if (itemsForDay.length > 0)
|
|
|
|
queryForDay[tempColumnName] = () =>
|
|
|
|
`array_cat("${tempColumnName}", '{${itemsForDay.join(
|
|
|
|
",",
|
|
|
|
)}}'::varchar[])`;
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:39:23 +00:00
|
|
|
// bake unique count
|
|
|
|
for (const [k, v] of Object.entries(finalDiffs)) {
|
|
|
|
if (this.schema[k].uniqueIncrement) {
|
2023-01-13 04:40:33 +00:00
|
|
|
const name = (columnPrefix +
|
|
|
|
k.replaceAll(".", columnDot)) as keyof Columns<T>;
|
|
|
|
const tempColumnName = (uniqueTempColumnPrefix +
|
|
|
|
k.replaceAll(".", columnDot)) as keyof TempColumnsForUnique<T>;
|
|
|
|
queryForHour[name] = new Set([
|
|
|
|
...(v as string[]),
|
|
|
|
...logHour[tempColumnName],
|
|
|
|
]).size;
|
|
|
|
queryForDay[name] = new Set([
|
|
|
|
...(v as string[]),
|
|
|
|
...logDay[tempColumnName],
|
|
|
|
]).size;
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 02:17:05 +00:00
|
|
|
|
2022-02-08 18:46:58 +00:00
|
|
|
// compute intersection
|
|
|
|
// TODO: intersectionに指定されたカラムがintersectionだった場合の対応
|
|
|
|
for (const [k, v] of Object.entries(this.schema)) {
|
|
|
|
const intersection = v.intersection;
|
|
|
|
if (intersection) {
|
2023-01-13 04:40:33 +00:00
|
|
|
const name = (columnPrefix +
|
|
|
|
k.replaceAll(".", columnDot)) as keyof Columns<T>;
|
2022-02-08 18:46:58 +00:00
|
|
|
const firstKey = intersection[0];
|
2023-01-13 04:40:33 +00:00
|
|
|
const firstTempColumnName = (uniqueTempColumnPrefix +
|
|
|
|
firstKey.replaceAll(
|
|
|
|
".",
|
|
|
|
columnDot,
|
|
|
|
)) as keyof TempColumnsForUnique<T>;
|
2022-02-12 16:39:58 +00:00
|
|
|
const firstValues = finalDiffs[firstKey] as string[] | undefined;
|
2023-01-13 04:40:33 +00:00
|
|
|
const currentValuesForHour = new Set([
|
|
|
|
...(firstValues ?? []),
|
|
|
|
...logHour[firstTempColumnName],
|
|
|
|
]);
|
|
|
|
const currentValuesForDay = new Set([
|
|
|
|
...(firstValues ?? []),
|
|
|
|
...logDay[firstTempColumnName],
|
|
|
|
]);
|
2022-02-08 18:46:58 +00:00
|
|
|
for (let i = 1; i < intersection.length; i++) {
|
|
|
|
const targetKey = intersection[i];
|
2023-01-13 04:40:33 +00:00
|
|
|
const targetTempColumnName = (uniqueTempColumnPrefix +
|
|
|
|
targetKey.replaceAll(
|
|
|
|
".",
|
|
|
|
columnDot,
|
|
|
|
)) as keyof TempColumnsForUnique<T>;
|
2022-02-12 16:39:58 +00:00
|
|
|
const targetValues = finalDiffs[targetKey] as string[] | undefined;
|
2023-01-13 04:40:33 +00:00
|
|
|
const targetValuesForHour = new Set([
|
|
|
|
...(targetValues ?? []),
|
|
|
|
...logHour[targetTempColumnName],
|
|
|
|
]);
|
|
|
|
const targetValuesForDay = new Set([
|
|
|
|
...(targetValues ?? []),
|
|
|
|
...logDay[targetTempColumnName],
|
|
|
|
]);
|
|
|
|
currentValuesForHour.forEach((v) => {
|
2022-02-08 18:46:58 +00:00
|
|
|
if (!targetValuesForHour.has(v)) currentValuesForHour.delete(v);
|
|
|
|
});
|
2023-01-13 04:40:33 +00:00
|
|
|
currentValuesForDay.forEach((v) => {
|
2022-02-08 18:46:58 +00:00
|
|
|
if (!targetValuesForDay.has(v)) currentValuesForDay.delete(v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
queryForHour[name] = currentValuesForHour.size;
|
|
|
|
queryForDay[name] = currentValuesForDay.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
// ログ更新
|
2021-12-14 09:12:37 +00:00
|
|
|
await Promise.all([
|
2023-01-13 04:40:33 +00:00
|
|
|
this.repositoryForHour
|
|
|
|
.createQueryBuilder()
|
2021-12-14 09:12:37 +00:00
|
|
|
.update()
|
2022-02-12 16:39:58 +00:00
|
|
|
.set(queryForHour as any)
|
2023-01-13 04:40:33 +00:00
|
|
|
.where("id = :id", { id: logHour.id })
|
2021-12-14 09:12:37 +00:00
|
|
|
.execute(),
|
2023-01-13 04:40:33 +00:00
|
|
|
this.repositoryForDay
|
|
|
|
.createQueryBuilder()
|
2021-12-14 09:12:37 +00:00
|
|
|
.update()
|
2022-02-12 16:39:58 +00:00
|
|
|
.set(queryForDay as any)
|
2023-01-13 04:40:33 +00:00
|
|
|
.where("id = :id", { id: logDay.id })
|
2021-12-14 09:12:37 +00:00
|
|
|
.execute(),
|
|
|
|
]);
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
logger.info(
|
|
|
|
`${this.name + (logHour.group ? `:${logHour.group}` : "")}: Updated`,
|
|
|
|
);
|
2021-03-18 02:17:05 +00:00
|
|
|
|
2021-06-30 15:50:19 +00:00
|
|
|
// TODO: この一連の処理が始まった後に新たにbufferに入ったものは消さないようにする
|
2023-01-13 04:40:33 +00:00
|
|
|
this.buffer = this.buffer.filter(
|
|
|
|
(q) => q.group != null && q.group !== logHour.group,
|
|
|
|
);
|
2019-04-07 12:50:36 +00:00
|
|
|
};
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const groups = removeDuplicates(this.buffer.map((log) => log.group));
|
2021-03-18 02:17:05 +00:00
|
|
|
|
2021-12-14 09:12:37 +00:00
|
|
|
await Promise.all(
|
2023-01-13 04:40:33 +00:00
|
|
|
groups.map((group) =>
|
2021-12-14 09:12:37 +00:00
|
|
|
Promise.all([
|
2023-01-13 04:40:33 +00:00
|
|
|
this.claimCurrentLog(group, "hour"),
|
|
|
|
this.claimCurrentLog(group, "day"),
|
|
|
|
]).then(([logHour, logDay]) => update(logHour, logDay)),
|
|
|
|
),
|
|
|
|
);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
public async tick(
|
|
|
|
major: boolean,
|
|
|
|
group: string | null = null,
|
|
|
|
): Promise<void> {
|
|
|
|
const data = major
|
|
|
|
? await this.tickMajor(group)
|
|
|
|
: await this.tickMinor(group);
|
2022-02-05 15:13:52 +00:00
|
|
|
|
2022-02-12 16:39:58 +00:00
|
|
|
const columns = {} as Record<keyof Columns<T>, number>;
|
2023-01-13 04:40:33 +00:00
|
|
|
for (const [k, v] of Object.entries(data) as [
|
|
|
|
keyof typeof data,
|
|
|
|
number,
|
|
|
|
][]) {
|
|
|
|
const name = (columnPrefix +
|
|
|
|
(k as string).replaceAll(".", columnDot)) as keyof Columns<T>;
|
2022-02-12 16:39:58 +00:00
|
|
|
columns[name] = v;
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
2019-09-01 19:41:26 +00:00
|
|
|
|
2022-02-08 18:51:43 +00:00
|
|
|
if (Object.keys(columns).length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const update = async (
|
|
|
|
logHour: RawRecord<T>,
|
|
|
|
logDay: RawRecord<T>,
|
|
|
|
): Promise<void> => {
|
2021-12-14 09:12:37 +00:00
|
|
|
await Promise.all([
|
2023-01-13 04:40:33 +00:00
|
|
|
this.repositoryForHour
|
|
|
|
.createQueryBuilder()
|
2021-12-14 09:12:37 +00:00
|
|
|
.update()
|
2022-02-12 16:39:58 +00:00
|
|
|
.set(columns)
|
2023-01-13 04:40:33 +00:00
|
|
|
.where("id = :id", { id: logHour.id })
|
2021-12-14 09:12:37 +00:00
|
|
|
.execute(),
|
2023-01-13 04:40:33 +00:00
|
|
|
this.repositoryForDay
|
|
|
|
.createQueryBuilder()
|
2021-12-14 09:12:37 +00:00
|
|
|
.update()
|
2022-02-12 16:39:58 +00:00
|
|
|
.set(columns)
|
2023-01-13 04:40:33 +00:00
|
|
|
.where("id = :id", { id: logDay.id })
|
2021-12-14 09:12:37 +00:00
|
|
|
.execute(),
|
|
|
|
]);
|
2019-09-01 19:41:26 +00:00
|
|
|
};
|
|
|
|
|
2021-12-14 09:12:37 +00:00
|
|
|
return Promise.all([
|
2023-01-13 04:40:33 +00:00
|
|
|
this.claimCurrentLog(group, "hour"),
|
|
|
|
this.claimCurrentLog(group, "day"),
|
|
|
|
]).then(([logHour, logDay]) => update(logHour, logDay));
|
2019-09-01 19:41:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 08:45:12 +00:00
|
|
|
public resync(group: string | null = null): Promise<void> {
|
|
|
|
return this.tick(true, group);
|
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
public async clean(): Promise<void> {
|
|
|
|
const current = dateUTC(Chart.getCurrentDate());
|
|
|
|
|
|
|
|
// 一日以上前かつ三日以内
|
2023-01-13 04:40:33 +00:00
|
|
|
const gt = Chart.dateToTimestamp(current) - 60 * 60 * 24 * 3;
|
|
|
|
const lt = Chart.dateToTimestamp(current) - 60 * 60 * 24;
|
2022-02-05 15:13:52 +00:00
|
|
|
|
2022-02-12 16:39:58 +00:00
|
|
|
const columns = {} as Record<keyof TempColumnsForUnique<T>, []>;
|
2022-02-05 15:13:52 +00:00
|
|
|
for (const [k, v] of Object.entries(this.schema)) {
|
|
|
|
if (v.uniqueIncrement) {
|
2023-01-13 04:40:33 +00:00
|
|
|
const name = (uniqueTempColumnPrefix +
|
|
|
|
k.replaceAll(".", columnDot)) as keyof TempColumnsForUnique<T>;
|
2022-02-12 16:39:58 +00:00
|
|
|
columns[name] = [];
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:55:49 +00:00
|
|
|
if (Object.keys(columns).length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
await Promise.all([
|
2023-01-13 04:40:33 +00:00
|
|
|
this.repositoryForHour
|
|
|
|
.createQueryBuilder()
|
2022-02-05 15:13:52 +00:00
|
|
|
.update()
|
2022-02-12 16:39:58 +00:00
|
|
|
.set(columns)
|
2023-01-13 04:40:33 +00:00
|
|
|
.where("date > :gt", { gt })
|
|
|
|
.andWhere("date < :lt", { lt })
|
2022-02-05 15:13:52 +00:00
|
|
|
.execute(),
|
2023-01-13 04:40:33 +00:00
|
|
|
this.repositoryForDay
|
|
|
|
.createQueryBuilder()
|
2022-02-05 15:13:52 +00:00
|
|
|
.update()
|
2022-02-12 16:39:58 +00:00
|
|
|
.set(columns)
|
2023-01-13 04:40:33 +00:00
|
|
|
.where("date > :gt", { gt })
|
|
|
|
.andWhere("date < :lt", { lt })
|
2022-02-05 15:13:52 +00:00
|
|
|
.execute(),
|
|
|
|
]);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
public async getChartRaw(
|
|
|
|
span: "hour" | "day",
|
|
|
|
amount: number,
|
|
|
|
cursor: Date | null,
|
|
|
|
group: string | null = null,
|
|
|
|
): Promise<ChartResult<T>> {
|
|
|
|
const [y, m, d, h, _m, _s, _ms] = cursor
|
|
|
|
? Chart.parseDate(subtractTime(addTime(cursor, 1, span), 1))
|
|
|
|
: Chart.getCurrentDate();
|
|
|
|
const [y2, m2, d2, h2] = cursor
|
|
|
|
? Chart.parseDate(addTime(cursor, 1, span))
|
|
|
|
: ([] as never);
|
2020-03-06 16:04:36 +00:00
|
|
|
|
2020-03-07 02:23:31 +00:00
|
|
|
const lt = dateUTC([y, m, d, h, _m, _s, _ms]);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
const gt =
|
2023-01-13 04:40:33 +00:00
|
|
|
span === "day"
|
|
|
|
? subtractTime(
|
|
|
|
cursor ? dateUTC([y2, m2, d2, 0]) : dateUTC([y, m, d, 0]),
|
|
|
|
amount - 1,
|
|
|
|
"day",
|
|
|
|
)
|
|
|
|
: span === "hour"
|
|
|
|
? subtractTime(
|
|
|
|
cursor ? dateUTC([y2, m2, d2, h2]) : dateUTC([y, m, d, h]),
|
|
|
|
amount - 1,
|
|
|
|
"hour",
|
|
|
|
)
|
|
|
|
: (new Error("not happen") as never);
|
2021-12-14 09:12:37 +00:00
|
|
|
|
|
|
|
const repository =
|
2023-01-13 04:40:33 +00:00
|
|
|
span === "hour"
|
|
|
|
? this.repositoryForHour
|
|
|
|
: span === "day"
|
|
|
|
? this.repositoryForDay
|
|
|
|
: (new Error("not happen") as never);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
// ログ取得
|
2023-01-13 04:40:33 +00:00
|
|
|
let logs = (await repository.find({
|
2019-04-07 12:50:36 +00:00
|
|
|
where: {
|
2021-12-09 14:58:30 +00:00
|
|
|
date: Between(Chart.dateToTimestamp(gt), Chart.dateToTimestamp(lt)),
|
2021-12-14 09:12:37 +00:00
|
|
|
...(group ? { group: group } : {}),
|
2019-04-07 12:50:36 +00:00
|
|
|
},
|
|
|
|
order: {
|
2021-12-09 14:58:30 +00:00
|
|
|
date: -1,
|
2019-04-07 12:50:36 +00:00
|
|
|
},
|
2023-01-13 04:40:33 +00:00
|
|
|
})) as RawRecord<T>[];
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
// 要求された範囲にログがひとつもなかったら
|
|
|
|
if (logs.length === 0) {
|
|
|
|
// もっとも新しいログを持ってくる
|
|
|
|
// (すくなくともひとつログが無いと隙間埋めできないため)
|
2023-01-13 04:40:33 +00:00
|
|
|
const recentLog = (await repository.findOne({
|
|
|
|
where: group
|
|
|
|
? {
|
|
|
|
group: group,
|
|
|
|
}
|
|
|
|
: {},
|
2019-04-07 12:50:36 +00:00
|
|
|
order: {
|
2021-12-09 14:58:30 +00:00
|
|
|
date: -1,
|
2019-04-07 12:50:36 +00:00
|
|
|
},
|
2023-01-13 04:40:33 +00:00
|
|
|
})) as RawRecord<T> | undefined;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
if (recentLog) {
|
|
|
|
logs = [recentLog];
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
// 要求された範囲の最も古い箇所に位置するログが存在しなかったら
|
2019-07-24 16:36:48 +00:00
|
|
|
} else if (!isTimeSame(new Date(logs[logs.length - 1].date * 1000), gt)) {
|
2019-04-07 12:50:36 +00:00
|
|
|
// 要求された範囲の最も古い箇所時点での最も新しいログを持ってきて末尾に追加する
|
|
|
|
// (隙間埋めできないため)
|
2023-01-13 04:40:33 +00:00
|
|
|
const outdatedLog = (await repository.findOne({
|
2022-03-26 06:34:00 +00:00
|
|
|
where: {
|
|
|
|
date: LessThan(Chart.dateToTimestamp(gt)),
|
|
|
|
...(group ? { group: group } : {}),
|
|
|
|
},
|
2019-04-07 12:50:36 +00:00
|
|
|
order: {
|
2021-12-09 14:58:30 +00:00
|
|
|
date: -1,
|
2019-04-07 12:50:36 +00:00
|
|
|
},
|
2023-01-13 04:40:33 +00:00
|
|
|
})) as RawRecord<T> | undefined;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
if (outdatedLog) {
|
|
|
|
logs.push(outdatedLog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
const chart: KVs<T>[] = [];
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
for (let i = amount - 1; i >= 0; i--) {
|
2021-12-14 09:12:37 +00:00
|
|
|
const current =
|
2023-01-13 04:40:33 +00:00
|
|
|
span === "hour"
|
|
|
|
? subtractTime(dateUTC([y, m, d, h]), i, "hour")
|
|
|
|
: span === "day"
|
|
|
|
? subtractTime(dateUTC([y, m, d]), i, "day")
|
|
|
|
: (new Error("not happen") as never);
|
2021-12-14 09:12:37 +00:00
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
const log = logs.find((l) =>
|
|
|
|
isTimeSame(new Date(l.date * 1000), current),
|
|
|
|
);
|
2021-12-14 09:12:37 +00:00
|
|
|
|
|
|
|
if (log) {
|
2022-02-05 15:13:52 +00:00
|
|
|
chart.unshift(this.convertRawRecord(log));
|
2021-12-14 09:12:37 +00:00
|
|
|
} else {
|
|
|
|
// 隙間埋め
|
2023-01-13 04:40:33 +00:00
|
|
|
const latest = logs.find((l) =>
|
|
|
|
isTimeBefore(new Date(l.date * 1000), current),
|
|
|
|
);
|
2022-02-05 15:13:52 +00:00
|
|
|
const data = latest ? this.convertRawRecord(latest) : null;
|
|
|
|
chart.unshift(this.getNewLog(data));
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
const res = {} as ChartResult<T>;
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [{ foo: 1, bar: 5 }, { foo: 2, bar: 6 }, { foo: 3, bar: 7 }]
|
|
|
|
* を
|
|
|
|
* { foo: [1, 2, 3], bar: [5, 6, 7] }
|
|
|
|
* にする
|
|
|
|
*/
|
2022-02-05 15:13:52 +00:00
|
|
|
for (const record of chart) {
|
2023-01-13 04:40:33 +00:00
|
|
|
for (const [k, v] of Object.entries(record) as [
|
|
|
|
keyof typeof record,
|
|
|
|
number,
|
|
|
|
][]) {
|
2022-02-05 15:13:52 +00:00
|
|
|
if (res[k]) {
|
|
|
|
res[k].push(v);
|
2019-04-07 12:50:36 +00:00
|
|
|
} else {
|
2022-02-05 15:13:52 +00:00
|
|
|
res[k] = [v];
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-05 15:13:52 +00:00
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-02-05 15:13:52 +00:00
|
|
|
return res;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 04:40:33 +00:00
|
|
|
public async getChart(
|
|
|
|
span: "hour" | "day",
|
|
|
|
amount: number,
|
|
|
|
cursor: Date | null,
|
|
|
|
group: string | null = null,
|
|
|
|
): Promise<Unflatten<ChartResult<T>>> {
|
2022-02-05 15:13:52 +00:00
|
|
|
const result = await this.getChartRaw(span, amount, cursor, group);
|
|
|
|
const object = {};
|
|
|
|
for (const [k, v] of Object.entries(result)) {
|
|
|
|
nestedProperty.set(object, k, v);
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2022-02-18 13:29:23 +00:00
|
|
|
return object as Unflatten<ChartResult<T>>;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
|
|
|
}
|