This commit is contained in:
tamaina 2023-05-29 16:26:16 +00:00
parent 78558eb666
commit 07bbadbbdc
5 changed files with 117 additions and 91 deletions

View file

@ -6,98 +6,10 @@ import * as Redis from 'ioredis';
import { Endpoint } from '@/server/api/endpoint-base.js'; import { Endpoint } from '@/server/api/endpoint-base.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
export const meta = {
requireCredential: true,
requireModerator: true,
tags: ['admin', 'meta'],
res: {
type: 'object',
optional: false, nullable: false,
properties: {
machine: {
type: 'string',
optional: false, nullable: false,
},
os: {
type: 'string',
optional: false, nullable: false,
example: 'linux',
},
node: {
type: 'string',
optional: false, nullable: false,
},
psql: {
type: 'string',
optional: false, nullable: false,
},
cpu: {
type: 'object',
optional: false, nullable: false,
properties: {
model: {
type: 'string',
optional: false, nullable: false,
},
cores: {
type: 'number',
optional: false, nullable: false,
},
},
},
mem: {
type: 'object',
optional: false, nullable: false,
properties: {
total: {
type: 'number',
optional: false, nullable: false,
format: 'bytes',
},
},
},
fs: {
type: 'object',
optional: false, nullable: false,
properties: {
total: {
type: 'number',
optional: false, nullable: false,
format: 'bytes',
},
used: {
type: 'number',
optional: false, nullable: false,
format: 'bytes',
},
},
},
net: {
type: 'object',
optional: false, nullable: false,
properties: {
interface: {
type: 'string',
optional: false, nullable: false,
example: 'eth0',
},
},
},
},
},
} as const;
export const paramDef = {
type: 'object',
properties: {},
required: [],
} as const;
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
@Injectable() @Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { export default class extends Endpoint<'admin/server-info'> {
name = 'admin/server-info' as const;
constructor( constructor(
@Inject(DI.db) @Inject(DI.db)
private db: DataSource, private db: DataSource,
@ -106,7 +18,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private redisClient: Redis.Redis, private redisClient: Redis.Redis,
) { ) {
super(meta, paramDef, async () => { super(async () => {
const memStats = await si.mem(); const memStats = await si.mem();
const fsStats = await si.fsSize(); const fsStats = await si.fsSize();
const netInterface = await si.networkInterfaceDefault(); const netInterface = await si.networkInterfaceDefault();

View file

@ -1613,6 +1613,19 @@ export const endpoints = {
res: undefined, res: undefined,
}], }],
}, },
'admin/server-info': {
requireCredential: true,
requireModerator: true,
tags: ['admin', 'meta'],
defines: [{
req: undefined,
res: {
$ref: 'https://misskey-hub.net/api/schemas/ServerInfoAdmin',
}
}],
},
} as const satisfies { [x: string]: IEndpointMeta; }; } as const satisfies { [x: string]: IEndpointMeta; };
/** /**

View file

@ -47,6 +47,10 @@ import {
InstanceMetaSharedSchema, InstanceMetaSharedSchema,
InstanceMetaAdminSchema, InstanceMetaAdminSchema,
} from './schemas/instance-meta.js'; } from './schemas/instance-meta.js';
import {
ServerInfoSchema,
ServerInfoAdminSchema,
} from './schemas/server-info.js';
import { Error, ApiError } from './schemas/error.js'; import { Error, ApiError } from './schemas/error.js';
import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray } from 'schema-type'; import type { JSONSchema7, JSONSchema7Definition, GetDef, GetRefs, GetKeys, UnionToArray } from 'schema-type';
@ -95,6 +99,8 @@ export const refs = {
AbuseUserReport: packedAbuseUserReportSchema, AbuseUserReport: packedAbuseUserReportSchema,
InstanceMetaShared: InstanceMetaSharedSchema, InstanceMetaShared: InstanceMetaSharedSchema,
InstanceMetaAdmin: InstanceMetaAdminSchema, InstanceMetaAdmin: InstanceMetaAdminSchema,
ServerInfo: ServerInfoSchema,
ServerInfoAdmin: ServerInfoAdminSchema,
Error: Error, Error: Error,
ApiError: ApiError, ApiError: ApiError,

View file

@ -0,0 +1,86 @@
import type { JSONSchema7Definition } from 'schema-type';
export const ServerInfoSchema = {
$id: 'https://misskey-hub.net/api/schemas/ServerInfo',
type: 'object',
properties: {
machine: {
type: 'string',
},
cpu: {
type: 'object',
properties: {
model: { type: 'string' },
cores: { type: 'number' },
},
required: ['model', 'cores'],
},
mem: {
type: 'object',
properties: {
total: { type: 'number', format: 'bytes' },
},
required: ['total'],
},
fs: {
type: 'object',
properties: {
total: { type: 'number', format: 'bytes' },
used: { type: 'number', format: 'bytes' },
},
},
},
required: [
'machine',
'cpu',
'mem',
'fs',
],
} as const satisfies JSONSchema7Definition;
export const ServerInfoAdminSchema = {
$id: 'https://misskey-hub.net/api/schemas/ServerInfoAdmin',
allOf: [
{ $ref: 'https://misskey-hub.net/api/schemas/ServerInfo' },
{
type: 'object',
properties: {
os: {
type: 'string',
enum: [
// NodeJS.Platform
'aix',
'android',
'darwin',
'freebsd',
'haiku',
'linux',
'openbsd',
'sunos',
'win32',
'cygwin',
'netbsd',
]
},
node: { type: 'string', examples: ['v14.17.0'] },
psql: { type: 'string' },
redis: { type: 'string' },
net: {
type: 'object',
properties: {
interface: { type: 'string', examples: ['eth0'] },
},
required: ['interface'],
},
},
required: [
'os',
'node',
'psql',
'net',
],
},
],
} as const satisfies JSONSchema7Definition;

View file

@ -110,6 +110,15 @@ describe('schemas', () => {
test('abuse user report', () => { test('abuse user report', () => {
type AbuseUserReport = Packed<'AbuseUserReport'>; type AbuseUserReport = Packed<'AbuseUserReport'>;
}); });
test('meta', () => {
type MetaShared = Packed<'InstanceMetaShared'>;
//type Meta = Packed<'InstanceMeta'>;
type MetaAdmin = Packed<'InstanceMetaAdmin'>;
});
test('server info', () => {
type ServerInfo = Packed<'ServerInfo'>;
type ServerInfoAdmin = Packed<'ServerInfoAdmin'>;
});
test('error', () => { test('error', () => {
type Error = Packed<'Error'>; type Error = Packed<'Error'>;
type ApiError = Packed<'ApiError'>; type ApiError = Packed<'ApiError'>;