From 5520780700d7eb6b969a5ac8bae01503c93d9f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E7=BA=B8=E5=BF=98=E5=BF=A7?= Date: Tue, 13 Aug 2024 23:26:49 +0800 Subject: [PATCH] refactor: update database column name --- service/index.ts | 39 ++++++++++++++++++++------------------- service/schema.ts | 14 +++++++------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/service/index.ts b/service/index.ts index 2d0ef9e1..559a680d 100644 --- a/service/index.ts +++ b/service/index.ts @@ -16,34 +16,34 @@ const connection = await mysql.createConnection( const db = drizzle(connection, { schema, mode: 'default' }) export type RunVlmcsType = { - domain: string + host: string port?: number - protocol?: number app?: number + protocol?: number } const runVlmcs = ({ - domain, + host, port = 1688, - protocol = 6, app = 26, + protocol = 6, }: RunVlmcsType) => { return new Promise<{ content: string delay: number - domain: string + host: string status: boolean }>(resolve => { const before = Date.now() execFile( `./service/binaries/vlmcs-${platform()}-${arch()}`, - [`${domain}:${port}`, `-${protocol}`, `-l ${app}`], + [`${host}:${port}`, `-${protocol}`, `-l ${app}`], { timeout: 5 * 1000 }, (err, stdout) => { resolve({ - content: stdout.trim(), + host, delay: Date.now() - before, - domain, + content: stdout.trim(), status: err ? false : true, }) }, @@ -64,19 +64,20 @@ app.use(cors()) app.use( cron({ - name: 'heartbeat', - pattern: Patterns.everySenconds(1), + name: 'check', + pattern: Patterns.everySenconds(10), async run() { - const vlmcsdService = await db.query.serviceTable.findMany() - if (Array.isArray(vlmcsdService) && vlmcsdService?.length > 0) { - for (const item of vlmcsdService) { - const { domain, port } = item - const data = await runVlmcs({ domain, port }) - const result = await db.insert(schema.logsTable).values({ - ...data, + const servers = await db.query.server.findMany() + if (Array.isArray(servers) && servers?.length > 0) { + for (const item of servers) { + const result = await runVlmcs({ + host: item.host, + port: item.port, + }) + await db.insert(schema.logs).values({ + ...result, createdAt: new Date(), }) - console.log(result) } } }, @@ -86,7 +87,7 @@ app.use( app.get('/*', () => Bun.file('dist/index.html')) app.get('/api/logs', async () => { - return await db.query.logsTable.findMany() + return await db.query.logs.findMany() }) app.post('/api/check', async request => { diff --git a/service/schema.ts b/service/schema.ts index c377e9fc..023b3090 100644 --- a/service/schema.ts +++ b/service/schema.ts @@ -7,23 +7,23 @@ import { timestamp, } from 'drizzle-orm/mysql-core' -export const serviceTable = mysqlTable('service', { +export const server = mysqlTable('server', { id: int('id').autoincrement().primaryKey(), - domain: text('domain').notNull(), + host: text('host').notNull(), port: int('port').notNull().default(1688), total: int('total').notNull().default(0), success: int('success').notNull().default(0), fail: int('fail').notNull().default(0), - successRate: float('success_rate').notNull().default(0), - recentSuccessRate: float('recent_success_rate').notNull().default(0), - averageDelay: float('average_delay').notNull().default(0), + delay: float('delay').notNull().default(0), + rate: float('rate').notNull().default(0), + rateAll: float('rate_all').notNull().default(0), updatedAt: timestamp('updated_at'), createdAt: timestamp('created_at'), }) -export const logsTable = mysqlTable('logs', { +export const logs = mysqlTable('logs', { id: int('id').autoincrement().primaryKey(), - domain: text('domain').notNull(), + host: text('host').notNull(), delay: int('delay').notNull().default(0), content: text('content'), status: boolean('status').notNull().default(false),