From 9530cb01b92920ba270dbf6f7a5feaaed352d727 Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 5 Jun 2023 05:46:35 +0000 Subject: [PATCH] wip --- .../chart/charts/entities/active-users.ts | 16 +----- .../core/chart/charts/entities/ap-request.ts | 10 +--- .../src/core/chart/charts/entities/drive.ts | 15 +---- .../core/chart/charts/entities/federation.ts | 15 +---- .../core/chart/charts/entities/instance.ts | 31 +--------- .../src/core/chart/charts/entities/notes.ts | 21 +------ .../chart/charts/entities/per-user-drive.ts | 13 +---- .../charts/entities/per-user-following.ts | 19 +------ .../chart/charts/entities/per-user-notes.ts | 14 +---- .../core/chart/charts/entities/per-user-pv.ts | 11 +--- .../charts/entities/per-user-reactions.ts | 9 +-- .../chart/charts/entities/test-grouped.ts | 10 +--- .../charts/entities/test-intersection.ts | 10 +--- .../core/chart/charts/entities/test-unique.ts | 8 +-- .../src/core/chart/charts/entities/test.ts | 10 +--- .../src/core/chart/charts/entities/users.ts | 13 +---- packages/backend/src/core/chart/core.ts | 53 +----------------- .../server/api/endpoints/charts/user/drive.ts | 27 +-------- packages/misskey-js/src/endpoints.ts | 26 +++++++++ packages/misskey-js/src/schemas.ts | 56 ++++++++++++++++++- .../src/schemas/charts/active-users.ts | 13 +++++ .../src/schemas/charts/ap-request.ts | 7 +++ .../misskey-js/src/schemas/charts/drive.ts | 12 ++++ .../src/schemas/charts/federation.ts | 12 ++++ .../misskey-js/src/schemas/charts/instance.ts | 28 ++++++++++ .../misskey-js/src/schemas/charts/notes.ts | 18 ++++++ .../src/schemas/charts/per-user-drive.ts | 10 ++++ .../src/schemas/charts/per-user-following.ts | 16 ++++++ .../src/schemas/charts/per-user-notes.ts | 11 ++++ .../src/schemas/charts/per-user-pv.ts | 8 +++ .../src/schemas/charts/per-user-reactions.ts | 6 ++ .../src/schemas/charts/test-grouped.ts | 7 +++ .../src/schemas/charts/test-intersection.ts | 7 +++ .../src/schemas/charts/test-unique.ts | 5 ++ .../misskey-js/src/schemas/charts/test.ts | 7 +++ .../misskey-js/src/schemas/charts/users.ts | 10 ++++ 36 files changed, 310 insertions(+), 254 deletions(-) create mode 100644 packages/misskey-js/src/schemas/charts/active-users.ts create mode 100644 packages/misskey-js/src/schemas/charts/ap-request.ts create mode 100644 packages/misskey-js/src/schemas/charts/drive.ts create mode 100644 packages/misskey-js/src/schemas/charts/federation.ts create mode 100644 packages/misskey-js/src/schemas/charts/instance.ts create mode 100644 packages/misskey-js/src/schemas/charts/notes.ts create mode 100644 packages/misskey-js/src/schemas/charts/per-user-drive.ts create mode 100644 packages/misskey-js/src/schemas/charts/per-user-following.ts create mode 100644 packages/misskey-js/src/schemas/charts/per-user-notes.ts create mode 100644 packages/misskey-js/src/schemas/charts/per-user-pv.ts create mode 100644 packages/misskey-js/src/schemas/charts/per-user-reactions.ts create mode 100644 packages/misskey-js/src/schemas/charts/test-grouped.ts create mode 100644 packages/misskey-js/src/schemas/charts/test-intersection.ts create mode 100644 packages/misskey-js/src/schemas/charts/test-unique.ts create mode 100644 packages/misskey-js/src/schemas/charts/test.ts create mode 100644 packages/misskey-js/src/schemas/charts/users.ts diff --git a/packages/backend/src/core/chart/charts/entities/active-users.ts b/packages/backend/src/core/chart/charts/entities/active-users.ts index e291e37c1b..1c630f2095 100644 --- a/packages/backend/src/core/chart/charts/entities/active-users.ts +++ b/packages/backend/src/core/chart/charts/entities/active-users.ts @@ -1,17 +1,7 @@ import Chart from '../../core.js'; -export const name = 'activeUsers'; - -export const schema = { - 'readWrite': { intersection: ['read', 'write'] }, - 'read': { uniqueIncrement: true }, - 'write': { uniqueIncrement: true }, - 'registeredWithinWeek': { uniqueIncrement: true }, - 'registeredWithinMonth': { uniqueIncrement: true }, - 'registeredWithinYear': { uniqueIncrement: true }, - 'registeredOutsideWeek': { uniqueIncrement: true }, - 'registeredOutsideMonth': { uniqueIncrement: true }, - 'registeredOutsideYear': { uniqueIncrement: true }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/active-users.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/ap-request.ts b/packages/backend/src/core/chart/charts/entities/ap-request.ts index 3a9f3dacfd..1e92592358 100644 --- a/packages/backend/src/core/chart/charts/entities/ap-request.ts +++ b/packages/backend/src/core/chart/charts/entities/ap-request.ts @@ -1,11 +1,7 @@ import Chart from '../../core.js'; -export const name = 'apRequest'; - -export const schema = { - 'deliverFailed': { }, - 'deliverSucceeded': { }, - 'inboxReceived': { }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/ap-request.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/drive.ts b/packages/backend/src/core/chart/charts/entities/drive.ts index 4bf5bb729e..2050dba142 100644 --- a/packages/backend/src/core/chart/charts/entities/drive.ts +++ b/packages/backend/src/core/chart/charts/entities/drive.ts @@ -1,16 +1,7 @@ import Chart from '../../core.js'; -export const name = 'drive'; - -export const schema = { - 'local.incCount': {}, - 'local.incSize': {}, // in kilobyte - 'local.decCount': {}, - 'local.decSize': {}, // in kilobyte - 'remote.incCount': {}, - 'remote.incSize': {}, // in kilobyte - 'remote.decCount': {}, - 'remote.decSize': {}, // in kilobyte -} as const; +import * as _ from 'misskey-js/built/schemas/charts/drive.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/federation.ts b/packages/backend/src/core/chart/charts/entities/federation.ts index a8466b0b4c..ec71804825 100644 --- a/packages/backend/src/core/chart/charts/entities/federation.ts +++ b/packages/backend/src/core/chart/charts/entities/federation.ts @@ -1,16 +1,7 @@ import Chart from '../../core.js'; -export const name = 'federation'; - -export const schema = { - 'deliveredInstances': { uniqueIncrement: true, range: 'small' }, - 'inboxInstances': { uniqueIncrement: true, range: 'small' }, - 'stalled': { uniqueIncrement: true, range: 'small' }, - 'sub': { accumulate: true, range: 'small' }, - 'pub': { accumulate: true, range: 'small' }, - 'pubsub': { accumulate: true, range: 'small' }, - 'subActive': { accumulate: true, range: 'small' }, - 'pubActive': { accumulate: true, range: 'small' }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/federation.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/instance.ts b/packages/backend/src/core/chart/charts/entities/instance.ts index 06962120e2..32d47dc67f 100644 --- a/packages/backend/src/core/chart/charts/entities/instance.ts +++ b/packages/backend/src/core/chart/charts/entities/instance.ts @@ -1,32 +1,7 @@ import Chart from '../../core.js'; -export const name = 'instance'; - -export const schema = { - 'requests.failed': { range: 'small' }, - 'requests.succeeded': { range: 'small' }, - 'requests.received': { range: 'small' }, - 'notes.total': { accumulate: true }, - 'notes.inc': {}, - 'notes.dec': {}, - 'notes.diffs.normal': {}, - 'notes.diffs.reply': {}, - 'notes.diffs.renote': {}, - 'notes.diffs.withFile': {}, - 'users.total': { accumulate: true }, - 'users.inc': { range: 'small' }, - 'users.dec': { range: 'small' }, - 'following.total': { accumulate: true }, - 'following.inc': { range: 'small' }, - 'following.dec': { range: 'small' }, - 'followers.total': { accumulate: true }, - 'followers.inc': { range: 'small' }, - 'followers.dec': { range: 'small' }, - 'drive.totalFiles': { accumulate: true }, - 'drive.incFiles': {}, - 'drive.decFiles': {}, - 'drive.incUsage': {}, // in kilobyte - 'drive.decUsage': {}, // in kilobyte -} as const; +import * as _ from 'misskey-js/built/schemas/charts/instance.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/entities/notes.ts b/packages/backend/src/core/chart/charts/entities/notes.ts index 9387dbfb2c..1dc1daeeee 100644 --- a/packages/backend/src/core/chart/charts/entities/notes.ts +++ b/packages/backend/src/core/chart/charts/entities/notes.ts @@ -1,22 +1,7 @@ import Chart from '../../core.js'; -export const name = 'notes'; - -export const schema = { - 'local.total': { accumulate: true }, - 'local.inc': {}, - 'local.dec': {}, - 'local.diffs.normal': {}, - 'local.diffs.reply': {}, - 'local.diffs.renote': {}, - 'local.diffs.withFile': {}, - 'remote.total': { accumulate: true }, - 'remote.inc': {}, - 'remote.dec': {}, - 'remote.diffs.normal': {}, - 'remote.diffs.reply': {}, - 'remote.diffs.renote': {}, - 'remote.diffs.withFile': {}, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/notes.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/per-user-drive.ts b/packages/backend/src/core/chart/charts/entities/per-user-drive.ts index 6111640ea0..735d83291b 100644 --- a/packages/backend/src/core/chart/charts/entities/per-user-drive.ts +++ b/packages/backend/src/core/chart/charts/entities/per-user-drive.ts @@ -1,14 +1,7 @@ import Chart from '../../core.js'; -export const name = 'perUserDrive'; - -export const schema = { - 'totalCount': { accumulate: true }, - 'totalSize': { accumulate: true }, // in kilobyte - 'incCount': { range: 'small' }, - 'incSize': {}, // in kilobyte - 'decCount': { range: 'small' }, - 'decSize': {}, // in kilobyte -} as const; +import * as _ from 'misskey-js/built/schemas/charts/per-user-drive.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/entities/per-user-following.ts b/packages/backend/src/core/chart/charts/entities/per-user-following.ts index 4118daa474..accedb56b9 100644 --- a/packages/backend/src/core/chart/charts/entities/per-user-following.ts +++ b/packages/backend/src/core/chart/charts/entities/per-user-following.ts @@ -1,20 +1,7 @@ import Chart from '../../core.js'; -export const name = 'perUserFollowing'; - -export const schema = { - 'local.followings.total': { accumulate: true }, - 'local.followings.inc': { range: 'small' }, - 'local.followings.dec': { range: 'small' }, - 'local.followers.total': { accumulate: true }, - 'local.followers.inc': { range: 'small' }, - 'local.followers.dec': { range: 'small' }, - 'remote.followings.total': { accumulate: true }, - 'remote.followings.inc': { range: 'small' }, - 'remote.followings.dec': { range: 'small' }, - 'remote.followers.total': { accumulate: true }, - 'remote.followers.inc': { range: 'small' }, - 'remote.followers.dec': { range: 'small' }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/per-user-following.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/entities/per-user-notes.ts b/packages/backend/src/core/chart/charts/entities/per-user-notes.ts index c1fa174452..23057fb924 100644 --- a/packages/backend/src/core/chart/charts/entities/per-user-notes.ts +++ b/packages/backend/src/core/chart/charts/entities/per-user-notes.ts @@ -1,15 +1,7 @@ import Chart from '../../core.js'; -export const name = 'perUserNotes'; - -export const schema = { - 'total': { accumulate: true }, - 'inc': { range: 'small' }, - 'dec': { range: 'small' }, - 'diffs.normal': { range: 'small' }, - 'diffs.reply': { range: 'small' }, - 'diffs.renote': { range: 'small' }, - 'diffs.withFile': { range: 'small' }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/per-user-notes.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/entities/per-user-pv.ts b/packages/backend/src/core/chart/charts/entities/per-user-pv.ts index 64c8ed1fb1..2d8717c815 100644 --- a/packages/backend/src/core/chart/charts/entities/per-user-pv.ts +++ b/packages/backend/src/core/chart/charts/entities/per-user-pv.ts @@ -1,12 +1,7 @@ import Chart from '../../core.js'; -export const name = 'perUserPv'; - -export const schema = { - 'upv.user': { uniqueIncrement: true, range: 'small' }, - 'pv.user': { range: 'small' }, - 'upv.visitor': { uniqueIncrement: true, range: 'small' }, - 'pv.visitor': { range: 'small' }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/per-user-pv.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/entities/per-user-reactions.ts b/packages/backend/src/core/chart/charts/entities/per-user-reactions.ts index 5e1a6c7b30..5f1f50993e 100644 --- a/packages/backend/src/core/chart/charts/entities/per-user-reactions.ts +++ b/packages/backend/src/core/chart/charts/entities/per-user-reactions.ts @@ -1,10 +1,7 @@ import Chart from '../../core.js'; -export const name = 'perUserReaction'; - -export const schema = { - 'local.count': { range: 'small' }, - 'remote.count': { range: 'small' }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/per-user-reactions.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/entities/test-grouped.ts b/packages/backend/src/core/chart/charts/entities/test-grouped.ts index 66b6e8e864..059047d1af 100644 --- a/packages/backend/src/core/chart/charts/entities/test-grouped.ts +++ b/packages/backend/src/core/chart/charts/entities/test-grouped.ts @@ -1,11 +1,7 @@ import Chart from '../../core.js'; -export const name = 'testGrouped'; - -export const schema = { - 'foo.total': { accumulate: true }, - 'foo.inc': {}, - 'foo.dec': {}, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/test-grouped.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema, true); diff --git a/packages/backend/src/core/chart/charts/entities/test-intersection.ts b/packages/backend/src/core/chart/charts/entities/test-intersection.ts index a3bdcb367f..c9b0756c4a 100644 --- a/packages/backend/src/core/chart/charts/entities/test-intersection.ts +++ b/packages/backend/src/core/chart/charts/entities/test-intersection.ts @@ -1,11 +1,7 @@ import Chart from '../../core.js'; -export const name = 'testIntersection'; - -export const schema = { - 'a': { uniqueIncrement: true }, - 'b': { uniqueIncrement: true }, - 'aAndB': { intersection: ['a', 'b'] }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/test-intersection.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/test-unique.ts b/packages/backend/src/core/chart/charts/entities/test-unique.ts index b2cfb71b05..26c838c15f 100644 --- a/packages/backend/src/core/chart/charts/entities/test-unique.ts +++ b/packages/backend/src/core/chart/charts/entities/test-unique.ts @@ -1,9 +1,7 @@ import Chart from '../../core.js'; -export const name = 'testUnique'; - -export const schema = { - 'foo': { uniqueIncrement: true }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/test-unique.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/test.ts b/packages/backend/src/core/chart/charts/entities/test.ts index 7cba21e16a..9731796b3a 100644 --- a/packages/backend/src/core/chart/charts/entities/test.ts +++ b/packages/backend/src/core/chart/charts/entities/test.ts @@ -1,11 +1,7 @@ import Chart from '../../core.js'; -export const name = 'test'; - -export const schema = { - 'foo.total': { accumulate: true }, - 'foo.inc': {}, - 'foo.dec': {}, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/test.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/charts/entities/users.ts b/packages/backend/src/core/chart/charts/entities/users.ts index c0b83094ae..1c8e664825 100644 --- a/packages/backend/src/core/chart/charts/entities/users.ts +++ b/packages/backend/src/core/chart/charts/entities/users.ts @@ -1,14 +1,7 @@ import Chart from '../../core.js'; -export const name = 'users'; - -export const schema = { - 'local.total': { accumulate: true }, - 'local.inc': { range: 'small' }, - 'local.dec': { range: 'small' }, - 'remote.total': { accumulate: true }, - 'remote.inc': { range: 'small' }, - 'remote.dec': { range: 'small' }, -} as const; +import * as _ from 'misskey-js/built/schemas/charts/users.js'; +export const name = _.name; +export const schema = _.schema; export const entity = Chart.schemaToEntity(name, schema); diff --git a/packages/backend/src/core/chart/core.ts b/packages/backend/src/core/chart/core.ts index d352adcc1f..57faa0eb84 100644 --- a/packages/backend/src/core/chart/core.ts +++ b/packages/backend/src/core/chart/core.ts @@ -10,22 +10,12 @@ import { dateUTC, isTimeSame, isTimeBefore, subtractTime, addTime } from '@/misc import type Logger from '@/logger.js'; import { bindThis } from '@/decorators.js'; import type { Repository, DataSource } from 'typeorm'; +import type { ChartSchema as Schema, ChartResult, Unflatten } from 'misskey-js/built/schemas'; const COLUMN_PREFIX = '___' as const; const UNIQUE_TEMP_COLUMN_PREFIX = 'unique_temp___' as const; const COLUMN_DELIMITER = '_' as const; -type Schema = Record; - - range?: 'big' | 'small' | 'medium'; - - // previousな値を引き継ぐかどうか - accumulate?: boolean; -}>; - type KeyToColumnName = T extends `${infer R1}.${infer R2}` ? `${R1}${typeof COLUMN_DELIMITER}${KeyToColumnName}` : T; type Columns = { @@ -64,47 +54,6 @@ export type KVs = { [K in keyof S]: number; }; -type ChartResult = { - [P in keyof T]: number[]; -}; - -type UnionToIntersection = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never; - -type UnflattenSingleton = K extends `${infer A}.${infer B}` - ? { [_ in A]: UnflattenSingleton; } - : { [_ in K]: V; }; - -type Unflatten> = UnionToIntersection< - { - [K in Extract]: UnflattenSingleton; - }[Extract] ->; - -type ToJsonSchema = { - type: 'object'; - properties: { - [K in keyof S]: S[K] extends number[] ? { type: 'array'; items: { type: 'number'; }; } : ToJsonSchema; - }, - required: (keyof S)[]; -}; - -export function getJsonSchema(schema: S): ToJsonSchema>> { - const jsonSchema = { - type: 'object', - properties: {} as Record, - required: [], - }; - - for (const k in schema) { - jsonSchema.properties[k] = { - type: 'array', - items: { type: 'number' }, - }; - } - - return jsonSchema as ToJsonSchema>>; -} - /** * 様々なチャートの管理を司るクラス */ diff --git a/packages/backend/src/server/api/endpoints/charts/user/drive.ts b/packages/backend/src/server/api/endpoints/charts/user/drive.ts index 5e372294b7..66dff34d59 100644 --- a/packages/backend/src/server/api/endpoints/charts/user/drive.ts +++ b/packages/backend/src/server/api/endpoints/charts/user/drive.ts @@ -1,36 +1,15 @@ import { Injectable } from '@nestjs/common'; -import { getJsonSchema } from '@/core/chart/core.js'; import { Endpoint } from '@/server/api/endpoint-base.js'; import PerUserDriveChart from '@/core/chart/charts/per-user-drive.js'; -import { schema } from '@/core/chart/charts/entities/per-user-drive.js'; - -export const meta = { - tags: ['charts', 'drive', 'users'], - - res: getJsonSchema(schema), - - allowGet: true, - cacheSec: 60 * 60, -} as const; - -export const paramDef = { - type: 'object', - properties: { - span: { type: 'string', enum: ['day', 'hour'] }, - limit: { type: 'integer', minimum: 1, maximum: 500, default: 30 }, - offset: { type: 'integer', nullable: true, default: null }, - userId: { type: 'string', format: 'misskey:id' }, - }, - required: ['span', 'userId'], -} as const; // eslint-disable-next-line import/no-default-export @Injectable() -export default class extends Endpoint { +export default class extends Endpoint<'charts/user/drive'> { + name = 'charts/user/drive' as const; constructor( private perUserDriveChart: PerUserDriveChart, ) { - super(meta, paramDef, async (ps, me) => { + super(async (ps, me) => { return await this.perUserDriveChart.getChart(ps.span, ps.limit, ps.offset ? new Date(ps.offset) : null, ps.userId); }); } diff --git a/packages/misskey-js/src/endpoints.ts b/packages/misskey-js/src/endpoints.ts index 1933a00490..219bff64f9 100644 --- a/packages/misskey-js/src/endpoints.ts +++ b/packages/misskey-js/src/endpoints.ts @@ -2,6 +2,9 @@ import type { JSONSchema7 } from 'schema-type'; import { IEndpointMeta } from './endpoints.types'; import { localUsernameSchema, passwordSchema } from './schemas/user'; import ms from 'ms'; +import { getJsonSchema } from './schemas'; +import * as perUserDriveChartSchema from './schemas/charts/per-user-drive'; +import * as driveChartSchema from './schemas/charts/drive'; export const endpoints = { //#region admin @@ -2984,6 +2987,29 @@ export const endpoints = { }], }, //#endregion + + //#region charts + 'charts/user/drive': { + tags: ['charts', 'drive', 'users'], + + allowGet: true, + cacheSec: 60 * 60, + + defines: [{ + req: { + type: 'object', + properties: { + span: { type: 'string', enum: ['day', 'hour'] }, + limit: { type: 'integer', minimum: 1, maximum: 500, default: 30 }, + offset: { type: 'integer', nullable: true, default: null }, + userId: { type: 'string', format: 'misskey:id' }, + }, + required: ['span', 'userId'], + }, + res: getJsonSchema(perUserDriveChartSchema.schema) satisfies JSONSchema7, + }], + }, + //#endregion } as const satisfies { [x: string]: IEndpointMeta; }; /** diff --git a/packages/misskey-js/src/schemas.ts b/packages/misskey-js/src/schemas.ts index 83272bb3de..efd507dbee 100644 --- a/packages/misskey-js/src/schemas.ts +++ b/packages/misskey-js/src/schemas.ts @@ -58,7 +58,7 @@ import { import { packedModerationLogSchema } from './schemas/moderation-log.js'; import { packedAuthSessionSchema } from './schemas/auth-session.js'; import { Error, ApiError } from './schemas/error.js'; -import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray, Serialized } from 'schema-type'; +import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray, Serialized, Projected } from 'schema-type'; export const refs = { Id: IdSchema, @@ -120,3 +120,57 @@ export type References = GetRefs; export type Packed> = Serialized>; export type Def> = GetDef; + +//#reginon Chart +export type ChartSchema = Record; + + range?: 'big' | 'small' | 'medium'; + + // previousな値を引き継ぐかどうか + accumulate?: boolean; +}>; + +export type ChartResult = { + [P in keyof T]: number[]; +}; + +type UnionToIntersection = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never; + +type UnflattenSingleton = K extends `${infer A}.${infer B}` + ? { [_ in A]: UnflattenSingleton; } + : { [_ in K]: V; }; + +export type Unflatten> = Projected]: UnflattenSingleton; + }[Extract] +>>; + +type ToJsonSchema = { + type: 'object'; + properties: { + [K in keyof S]: S[K] extends number[] ? { type: 'array'; items: { type: 'number'; }; } : ToJsonSchema; + }, + required: (keyof S)[]; +}; + +export function getJsonSchema(schema: S): ToJsonSchema>> { + const jsonSchema = { + type: 'object', + properties: {} as Record, + required: [], + }; + + for (const k in schema) { + jsonSchema.properties[k] = { + type: 'array', + items: { type: 'number' }, + }; + } + + return jsonSchema as ToJsonSchema>>; +} +//#endregion diff --git a/packages/misskey-js/src/schemas/charts/active-users.ts b/packages/misskey-js/src/schemas/charts/active-users.ts new file mode 100644 index 0000000000..b4c0df5ce9 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/active-users.ts @@ -0,0 +1,13 @@ +export const name = 'activeUsers'; + +export const schema = { + 'readWrite': { intersection: ['read', 'write'] }, + 'read': { uniqueIncrement: true }, + 'write': { uniqueIncrement: true }, + 'registeredWithinWeek': { uniqueIncrement: true }, + 'registeredWithinMonth': { uniqueIncrement: true }, + 'registeredWithinYear': { uniqueIncrement: true }, + 'registeredOutsideWeek': { uniqueIncrement: true }, + 'registeredOutsideMonth': { uniqueIncrement: true }, + 'registeredOutsideYear': { uniqueIncrement: true }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/ap-request.ts b/packages/misskey-js/src/schemas/charts/ap-request.ts new file mode 100644 index 0000000000..77e81b9b58 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/ap-request.ts @@ -0,0 +1,7 @@ +export const name = 'apRequest'; + +export const schema = { + 'deliverFailed': { }, + 'deliverSucceeded': { }, + 'inboxReceived': { }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/drive.ts b/packages/misskey-js/src/schemas/charts/drive.ts new file mode 100644 index 0000000000..ade3fae7d0 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/drive.ts @@ -0,0 +1,12 @@ +export const name = 'drive'; + +export const schema = { + 'local.incCount': {}, + 'local.incSize': {}, // in kilobyte + 'local.decCount': {}, + 'local.decSize': {}, // in kilobyte + 'remote.incCount': {}, + 'remote.incSize': {}, // in kilobyte + 'remote.decCount': {}, + 'remote.decSize': {}, // in kilobyte +} as const; diff --git a/packages/misskey-js/src/schemas/charts/federation.ts b/packages/misskey-js/src/schemas/charts/federation.ts new file mode 100644 index 0000000000..bd720b41b2 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/federation.ts @@ -0,0 +1,12 @@ +export const name = 'federation'; + +export const schema = { + 'deliveredInstances': { uniqueIncrement: true, range: 'small' }, + 'inboxInstances': { uniqueIncrement: true, range: 'small' }, + 'stalled': { uniqueIncrement: true, range: 'small' }, + 'sub': { accumulate: true, range: 'small' }, + 'pub': { accumulate: true, range: 'small' }, + 'pubsub': { accumulate: true, range: 'small' }, + 'subActive': { accumulate: true, range: 'small' }, + 'pubActive': { accumulate: true, range: 'small' }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/instance.ts b/packages/misskey-js/src/schemas/charts/instance.ts new file mode 100644 index 0000000000..06c3dda0e4 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/instance.ts @@ -0,0 +1,28 @@ +export const name = 'instance'; + +export const schema = { + 'requests.failed': { range: 'small' }, + 'requests.succeeded': { range: 'small' }, + 'requests.received': { range: 'small' }, + 'notes.total': { accumulate: true }, + 'notes.inc': {}, + 'notes.dec': {}, + 'notes.diffs.normal': {}, + 'notes.diffs.reply': {}, + 'notes.diffs.renote': {}, + 'notes.diffs.withFile': {}, + 'users.total': { accumulate: true }, + 'users.inc': { range: 'small' }, + 'users.dec': { range: 'small' }, + 'following.total': { accumulate: true }, + 'following.inc': { range: 'small' }, + 'following.dec': { range: 'small' }, + 'followers.total': { accumulate: true }, + 'followers.inc': { range: 'small' }, + 'followers.dec': { range: 'small' }, + 'drive.totalFiles': { accumulate: true }, + 'drive.incFiles': {}, + 'drive.decFiles': {}, + 'drive.incUsage': {}, // in kilobyte + 'drive.decUsage': {}, // in kilobyte +} as const; diff --git a/packages/misskey-js/src/schemas/charts/notes.ts b/packages/misskey-js/src/schemas/charts/notes.ts new file mode 100644 index 0000000000..3df1d12249 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/notes.ts @@ -0,0 +1,18 @@ +export const name = 'notes'; + +export const schema = { + 'local.total': { accumulate: true }, + 'local.inc': {}, + 'local.dec': {}, + 'local.diffs.normal': {}, + 'local.diffs.reply': {}, + 'local.diffs.renote': {}, + 'local.diffs.withFile': {}, + 'remote.total': { accumulate: true }, + 'remote.inc': {}, + 'remote.dec': {}, + 'remote.diffs.normal': {}, + 'remote.diffs.reply': {}, + 'remote.diffs.renote': {}, + 'remote.diffs.withFile': {}, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/per-user-drive.ts b/packages/misskey-js/src/schemas/charts/per-user-drive.ts new file mode 100644 index 0000000000..ea2c67c1fb --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/per-user-drive.ts @@ -0,0 +1,10 @@ +export const name = 'perUserDrive'; + +export const schema = { + 'totalCount': { accumulate: true }, + 'totalSize': { accumulate: true }, // in kilobyte + 'incCount': { range: 'small' }, + 'incSize': {}, // in kilobyte + 'decCount': { range: 'small' }, + 'decSize': {}, // in kilobyte +} as const; diff --git a/packages/misskey-js/src/schemas/charts/per-user-following.ts b/packages/misskey-js/src/schemas/charts/per-user-following.ts new file mode 100644 index 0000000000..56d80c43f9 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/per-user-following.ts @@ -0,0 +1,16 @@ +export const name = 'perUserFollowing'; + +export const schema = { + 'local.followings.total': { accumulate: true }, + 'local.followings.inc': { range: 'small' }, + 'local.followings.dec': { range: 'small' }, + 'local.followers.total': { accumulate: true }, + 'local.followers.inc': { range: 'small' }, + 'local.followers.dec': { range: 'small' }, + 'remote.followings.total': { accumulate: true }, + 'remote.followings.inc': { range: 'small' }, + 'remote.followings.dec': { range: 'small' }, + 'remote.followers.total': { accumulate: true }, + 'remote.followers.inc': { range: 'small' }, + 'remote.followers.dec': { range: 'small' }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/per-user-notes.ts b/packages/misskey-js/src/schemas/charts/per-user-notes.ts new file mode 100644 index 0000000000..cd51b70812 --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/per-user-notes.ts @@ -0,0 +1,11 @@ +export const name = 'perUserNotes'; + +export const schema = { + 'total': { accumulate: true }, + 'inc': { range: 'small' }, + 'dec': { range: 'small' }, + 'diffs.normal': { range: 'small' }, + 'diffs.reply': { range: 'small' }, + 'diffs.renote': { range: 'small' }, + 'diffs.withFile': { range: 'small' }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/per-user-pv.ts b/packages/misskey-js/src/schemas/charts/per-user-pv.ts new file mode 100644 index 0000000000..929307d4fe --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/per-user-pv.ts @@ -0,0 +1,8 @@ +export const name = 'perUserPv'; + +export const schema = { + 'upv.user': { uniqueIncrement: true, range: 'small' }, + 'pv.user': { range: 'small' }, + 'upv.visitor': { uniqueIncrement: true, range: 'small' }, + 'pv.visitor': { range: 'small' }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/per-user-reactions.ts b/packages/misskey-js/src/schemas/charts/per-user-reactions.ts new file mode 100644 index 0000000000..a8b9c8923f --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/per-user-reactions.ts @@ -0,0 +1,6 @@ +export const name = 'perUserReaction'; + +export const schema = { + 'local.count': { range: 'small' }, + 'remote.count': { range: 'small' }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/test-grouped.ts b/packages/misskey-js/src/schemas/charts/test-grouped.ts new file mode 100644 index 0000000000..659a728afd --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/test-grouped.ts @@ -0,0 +1,7 @@ +export const name = 'testGrouped'; + +export const schema = { + 'foo.total': { accumulate: true }, + 'foo.inc': {}, + 'foo.dec': {}, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/test-intersection.ts b/packages/misskey-js/src/schemas/charts/test-intersection.ts new file mode 100644 index 0000000000..2eb34d0ddf --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/test-intersection.ts @@ -0,0 +1,7 @@ +export const name = 'testIntersection'; + +export const schema = { + 'a': { uniqueIncrement: true }, + 'b': { uniqueIncrement: true }, + 'aAndB': { intersection: ['a', 'b'] }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/test-unique.ts b/packages/misskey-js/src/schemas/charts/test-unique.ts new file mode 100644 index 0000000000..7c90413caf --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/test-unique.ts @@ -0,0 +1,5 @@ +export const name = 'testUnique'; + +export const schema = { + 'foo': { uniqueIncrement: true }, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/test.ts b/packages/misskey-js/src/schemas/charts/test.ts new file mode 100644 index 0000000000..5e196108ef --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/test.ts @@ -0,0 +1,7 @@ +export const name = 'test'; + +export const schema = { + 'foo.total': { accumulate: true }, + 'foo.inc': {}, + 'foo.dec': {}, +} as const; diff --git a/packages/misskey-js/src/schemas/charts/users.ts b/packages/misskey-js/src/schemas/charts/users.ts new file mode 100644 index 0000000000..f96462c35d --- /dev/null +++ b/packages/misskey-js/src/schemas/charts/users.ts @@ -0,0 +1,10 @@ +export const name = 'users'; + +export const schema = { + 'local.total': { accumulate: true }, + 'local.inc': { range: 'small' }, + 'local.dec': { range: 'small' }, + 'remote.total': { accumulate: true }, + 'remote.inc': { range: 'small' }, + 'remote.dec': { range: 'small' }, +} as const;